Ignore:
Timestamp:
05/30/08 10:14:43 (16 years ago)
Author:
ra33
Message:

Added some more unit tests
Did a bunch of refactoring
AND added a few new features... @b @v were the most significant

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/Actions.java

    r72 r80  
    77import java.net.URL;
    88import java.util.ArrayList;
     9import java.util.Collection;
    910import java.util.Enumeration;
    1011import java.util.HashMap;
     
    2627
    2728/**
    28  * The Action class is used to launch KMS Actions as well as launching JAGs.
     29 * The Action class is used to launch Actions and Agents.
    2930 *
    3031 * This class checks all class files in the same directory, and reads in and
     
    3233 * the lowercase method names can be mapped to the correctly capatilized method
    3334 * names (to provide case-insensitivity)
     35 *
     36 * When adding an action to a class in the actions folder the following must be
     37 * considered:
     38 * <li> If the first parameter is of type Frame or Item they will be called with
     39 * the current frame and the item used to execute the action.</li>
     40 * <li> If the second item is also of type Frame or Item it will be set to the
     41 * current frame or item.</li>
     42 * <li> If there are multiple overloads for the same method they should be
     43 * declared in order of the methods with the most parameteres to least
     44 * parameters.</li>
    3445 */
    3546public class Actions {
     
    5970                // Get a File object for the package
    6071                File directory = null;
    61                 String path = pckgname.replace('.', '/');
     72                String path = pckgname.replace('.', File.separatorChar);
    6273                try {
    6374                        ClassLoader cld = Thread.currentThread().getContextClassLoader();
     
    6576                                throw new ClassNotFoundException("Can't get class loader.");
    6677                        }
    67                         URL resource = cld.getResource(path);
     78                        URL resource = null;
     79                        try {
     80                                Enumeration<URL> resources = cld.getResources(path);
     81                                while (resources.hasMoreElements()) {
     82                                        URL url = resources.nextElement();
     83                                        // Ingore the classes in the test folder when we are running
     84                                        // the program from Eclipse
     85                                        // This doesnt apply when running directly from the jar
     86                                        // because the test classes are not compiled into the jar.
     87                                        if (!url.toString().toLowerCase().contains("test")) {
     88                                                resource = url;
     89                                                break;
     90                                        }
     91                                }
     92                        } catch (Exception e) {
     93
     94                        }
    6895                        if (resource == null) {
    6996                                throw new ClassNotFoundException("No resource for " + path);
     
    137164         * Clears out the Action and JAG Hashtables and refills them. Normally this
    138165         * is only called once when the system starts.
    139          */
    140         public static void Init() {
     166         * @return a warning message if there were any problems loading agents or actions.
     167         */
     168        public static Collection<String> Init() {
     169                Collection<String> warnings = new LinkedList<String>();
    141170                Class[] classes;
     171               
    142172                try {
    143173                        classes = getClasses(AGENTS_PACKAGE);
     
    149179                        }
    150180                } catch (Exception e) {
    151                         System.out
    152                                         .println("You must have Java 1.5 or higher to run Expeditee");
    153                         System.out.println(e.getMessage());
     181                        warnings.add("You must have Java 1.5 or higher to run Expeditee");
     182                        warnings.add(e.getMessage());
    154183                }
    155184                try {
     
    158187                        for (int i = 0; i < classes.length; i++) {
    159188                                String name = classes[i].getSimpleName();
     189                                // Ignore the test classes
     190                                if (name.toLowerCase().contains("test"))
     191                                        continue;
    160192                                // read in all the methods from the class
    161193                                try {
    162                                         // System.out.println(name);
     194                                        // System.out.println(name)
    163195                                        LoadMethods(Class.forName(ACTIONS_PACKAGE + name));
    164196                                } catch (ClassNotFoundException e) {
     
    168200                        }
    169201                } catch (Exception e) {
    170                         System.out.println(e.getMessage());
    171                 }
     202                        warnings.add(e.getMessage());
     203                }
     204                return warnings;
    172205        }
    173206
     
    186219                        // only allow methods with the right modifiers
    187220                        if (MethodCheck(m)) {
    188                                 if (!(_Actions.containsKey(m.getName().toLowerCase())))
    189                                         _Actions.put(m.getName().toLowerCase(), m);
     221                                String lowercaseName = m.getName().toLowerCase();
     222                                if (!(_Actions.containsKey(lowercaseName)))
     223                                        _Actions.put(lowercaseName, m);
    190224                                else {
    191225                                        int i = 0;
    192                                         while (_Actions.containsKey(m.getName().toLowerCase() + i))
     226                                        while (_Actions.containsKey(lowercaseName + i))
    193227                                                i++;
    194228
    195                                         _Actions.put(m.getName().toLowerCase() + i, m);
     229                                        _Actions.put(lowercaseName + i, m);
    196230                                }
    197231
     
    259293                        mname = mname.substring(1);
    260294
    261                 mname = mname.trim().toLowerCase();
    262 
     295                mname = mname.trim();
     296                String lowercaseName = mname.toLowerCase();
    263297                // check for protection on frame
    264298                if (ItemUtils.ContainsTag(source.getItems(), "@No" + mname)) {
     
    269303
    270304                // retrieve methods that match the name
    271                 Method toRun = _Actions.get(mname);
     305                Method toRun = _Actions.get(lowercaseName);
    272306
    273307                // if this is not the name of a method, it may be the name of an agent
     
    287321                possibles.add(toRun);
    288322                int i = 0;
    289                 while (_Actions.containsKey(mname + i)) {
    290                         possibles.add(_Actions.get(mname + i));
     323                while (_Actions.containsKey(lowercaseName + i)) {
     324                        possibles.add(_Actions.get(lowercaseName + i));
    291325                        i++;
    292326                }
     
    301335                                Object[] parameters = CreateObjects(possible, source, launcher,
    302336                                                command);
     337                                // Check that there are the same amount of params
     338                                if (parameters == null) {
     339                                        continue;
     340                                }
    303341
    304342                                possible.invoke(null, parameters);
     
    322360         */
    323361        private static void LaunchAgent(String name, String parameters, Frame source) {
     362                // Use the correct case version for printing error messages
     363                String nameWithCorrectCase = name;
     364                name = name.toLowerCase();
    324365                // save the current frame (if necesssary)
    325366                FrameUtils.LeavingFrame(source);
     
    362403                        // if there is no constructor, return
    363404                        if (con == null) {
    364                                 FrameGraphics.DisplayMessage("Invalid parametres for agent.");
     405                                FrameGraphics.DisplayMessage("Invalid parametres for agent: "
     406                                                + nameWithCorrectCase);
    365407                                // System.out.println("Constructor not found...");
    366408                                return;
     
    375417                        // check for errors during initialisation
    376418                        if (!_Agent.initialise(source)) {
    377                                 FrameGraphics.ErrorMessage("Error initialising agent: " + name);
     419                                FrameGraphics.ErrorMessage("Error initialising agent: "
     420                                                + nameWithCorrectCase);
    378421                                return;
    379422                        }
     
    390433
    391434                } catch (ClassNotFoundException cnf) {
    392                         FrameGraphics.ErrorMessage("Error: '" + name
    393                                         + "' is not an action statement or Agent.");
     435                        FrameGraphics.ErrorMessage("Error: '" + nameWithCorrectCase
     436                                        + "' is not an action or agent.");
    394437                } catch (Exception e) {
    395                         FrameGraphics.ErrorMessage("Error creating Agent: '" + name + "'");
     438                        FrameGraphics.ErrorMessage("Error creating Agent: '"
     439                                        + nameWithCorrectCase + "'");
    396440                        System.out.println("Agent set to Null.");
    397441                        _Agent = null;
     
    421465         */
    422466        public static void stopAgent() {
    423                 if (_Agent != null) {
     467                if (_Agent != null && _Agent.isRunning()) {
    424468                        FrameGraphics.DisplayMessage("Stopping Agent...");
    425469                        _Agent.stop();
    426 
    427                         // while (_Agent.isRunning())
    428                         // ;
    429                         // FrameGraphics.DisplayMessage("Agent Stopped.");
    430470                }
    431471        }
     
    489529                                param = values.trim();
    490530                                // check if its a string
    491                                 if (param.length() > 0 && param.charAt(0) == '"'){
     531                                if (param.length() > 0 && param.charAt(0) == '"') {
    492532                                        int endOfString = param.indexOf('"', 1);
    493                                         if (endOfString > 0){
    494                                                 param = param.substring(0,endOfString);
     533                                        if (endOfString > 0) {
     534                                                param = param.substring(0, endOfString);
    495535                                        }
    496536                                }
     
    501541                        // convert the value to an object
    502542                        Object o = Conversion.Convert(paramTypes[ind], param);
     543                        if (o == null)
     544                                return null;
    503545                        objects[ind] = o;
    504546                }
     
    530572                if (params.charAt(0) == '"') {
    531573                        int endOfString = params.indexOf('"', 1);
    532                         if (endOfString > 0){
     574                        if (endOfString > 0) {
    533575                                if (endOfString > params.length())
    534576                                        return "";
Note: See TracChangeset for help on using the changeset viewer.