FileDocCategorySizeDatePackage
CommandProcessor.javaAPI DocJ2ME MIDP 2.013282Thu Nov 07 12:02:26 GMT 2002com.sun.midp.main

CommandProcessor

public class CommandProcessor extends Object
Performs basic MIDlet Suite management commands.

The Main class performs four basic operations:

  • Application installation
  • Application removal
  • Application listing
  • Application execution

Together, these operations are refered to as the Java Application Manager or JAM.

Fields Summary
static final int
MAIN_EXIT
return value from Main, so we know that Main exited normally
static final int
OK
status for success
static final int
ERROR
status when an error occurs
static final int
MIDLET_SUITE_NOT_FOUND
status when the MIDlet suite is not found
static final int
EXIT
command code for exit
static final int
MANAGE
command code for Graphical MIDlet Suite Manager in a dev environment
static final int
INSTALL
command code for installing a suite
static final int
INSTALL_RUN
command code for installing and running a suite
static final int
RUN
command code for running a suite
static final int
REMOVE
command code for removing a suite
static final int
LIST
command code for listing all installed suites in dev environment
static final int
RUN_CLASS
command code for running a class directly
static final int
STORAGE_NAMES
command code for listing the only the storage names of suites
private static boolean
performing
whether-or-not we are peforming a command
private static com.sun.midp.midletsuite.Installer
installer
Application Installer
Constructors Summary
private CommandProcessor()
This class is not meant to be instatiated

    
Methods Summary
private static voiddispatch(CommandState state)
Dispatches the command for processing.

param
state command state containing the command to perform and any arguments that the command may require.

        int currentCommand;

        currentCommand = state.nextCommand;
        state.nextCommand = EXIT;

        if (currentCommand == INSTALL || currentCommand == INSTALL_RUN) {
            install(state);
        } else if (currentCommand == RUN) {
            // keep the Manager around if that was initial command
            if (state.initialCommand == MANAGE) {
                state.nextCommand = MANAGE;
            }

            run(state);
        } else if (currentCommand == REMOVE) {
            remove(state);
        }
    
private static voidinstall(CommandState state)
Installs a MIDlet Suite.

param
state command state containing the URL of the suite to install
exception
IllegalArgumentException if the JAD URL in the state is not valid

        try {
            state.status = ERROR;

            if (state.securityDomain != null) {
                installer.setUnsignedSecurityDomain(state.securityDomain);
            }

            // save the suite name for "run" command
            state.suiteStorageName = installer.installJad(state.suiteURL,
                                            state.forceOverwrite,
                                            state.removeRMS, null);
            state.status = OK;

            // this may be a one of a set of commands to execute
            if (state.initialCommand == INSTALL_RUN) {
                state.nextCommand = RUN;
            }

            return;
        } catch (InvalidJadException ije) {
            if (ije.getReason() == InvalidJadException.INVALID_JAD_URL) {
                state.status = MIDLET_SUITE_NOT_FOUND;
                throw new IllegalArgumentException("The JAD URL is not valid");
            }

            if (ije.getReason() == InvalidJadException.JAD_SERVER_NOT_FOUND ||
                ije.getReason() == InvalidJadException.JAD_NOT_FOUND) {

                state.status = MIDLET_SUITE_NOT_FOUND;

                if (state.autotest && state.suiteStorageName != null) {
                    // This end of the tests, remove the last test installed
                    state.nextCommand = REMOVE;
                }

                return;
            }

            if (ije.getReason() == InvalidJadException.INVALID_JAD_TYPE) {
                // media type of JAD was wrong, it could be a JAR
                String mediaType = (String)ije.getExtraData();

                if (Installer.JAR_MT_1.equals(mediaType) ||
                        Installer.JAR_MT_2.equals(mediaType)) {
                    // re-run as a JAR only install
                    state.suiteStorageName =
                        installer.installJar(state.suiteURL,
                                             state.forceOverwrite,
                                             state.removeRMS, null);
                    state.status = OK;

                    // this may be a one of a set of commands to execute
                    if (state.initialCommand == INSTALL_RUN) {
                        state.nextCommand = RUN;
                    }

                    return;
                }
            }

            throw ije;
        } catch (IllegalArgumentException iae) {
            // the suite was not found be case of an illegal argument
            state.status = MIDLET_SUITE_NOT_FOUND;
            throw iae;
        } finally {
            if (state.status == ERROR) {
                // during autotest mode, only exit if we cannot get the suite
                if (state.autotest) {
                    state.nextCommand = INSTALL;
                }
            }
        }
    
public static voidperform(CommandState state)
Perform the specified command.

The operation is specified in the initialCommand field of the command state. The nextCommand state will be set to next command or EXIT. The status will also be set.

The command state also contains the arguments needed a command.

Command Argument(s)
----------- -----------
install suiteURL
remove suiteStorageName
run suiteStorageName and optionally (midletName and runOnce)
install-run suiteURL and optionally (midletName, autotest, and runOnce)

The list command displays a list of the currently installed applications.

The install command will install the given application. The URL used for installation must point to a valid application descriptor file. Only HTTP URLs are accepted. If any errors were encountered during installtion, the status will be ERROR or MIDLET_SUITE_NOT_FOUND. if forceOverwrite in the command state is set MIDlet suite will be forcibly installed. See Installer.installJad() for a description.

The remove command will remove the given application suite or storage used when running single class of local descriptor. If you specify the special keywork all as the suite name, all application suites will be removed.

The run command will execute the given MIDlet from a suite, or if not MIDlet is given, let the user select one. If runOnce in the command state is the next command will be remove.

The install-run command performs the install and then the run command. If autotest and runOnce are set in the command state, then after removing the suite the next command will be install. The autotest cycle is broken when the suite at the URL is not found.

param
state command state containing the command to perform and any arguments that the command may require.
exception
IOException is thrown, if an error occurs fetching the descriptor or jar file
exception
InvalidJadException is thrown, if an error is detected in the descriptor file
exception
ClassNotFoundException is thrown, if the desginated MIDlet is not found in the downloaded bundle
exception
InstantiationException is thrown, installer can not be constructed
exception
IllegalAccessException is thrown,if the caller does not have sufficient authority to perorm the requested operation


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
          
             
              

        String nextMidletSuiteToRun;

        // don't run twice, a sneaky untrusted MIDlet may try this
        if (performing) {
            return;
        }

        performing = true;

        // we need to get the installer now, before the security level drops
        if (installer == null) {
            installer = Installer.getInstaller();
        }

        dispatch(state);

        nextMidletSuiteToRun = installer.getNextMIDletSuiteToRun();
        if (nextMidletSuiteToRun != null) {
            state.nextCommand = RUN;
            state.suiteStorageName = nextMidletSuiteToRun;

            // the run method should clear this
            state.midletName = installer.getNextMIDletToRun();
        }

        performing = false;
    
private static voidremove(CommandState state)
Removes a MIDlet Suite.

param
state command state containing the name of the suite to remove
exception
IllegalArgumentException if storage name in the state is null

        state.status = ERROR;

        if (state.suiteStorageName == null) {
            throw new IllegalArgumentException("No suite specified");
        }
        
        if (state.suiteStorageName.equals("all")) {
            String suite[] = installer.list();
            for (int i = 0; i < suite.length; i++) {
                installer.remove(suite[i]);
            }
        } else {
            installer.remove(state.suiteStorageName);
        }

        state.status = OK;

        return;
    
private static voidrun(CommandState state)
Runs a MIDlet suite.

param
state command state containing the name of the suite to run


        MIDletSuite midletSuite;

        // Note: the install command creates the suite storage names

        state.status = ERROR;

        if (state.suiteStorageName == null) {
            throw new IllegalArgumentException("The storage name for the " +
                "MIDlet suite was not given");
        }

        if (state.midletName != null || state.midletNumber == null) {
            String temp = state.midletName;

            /*
             * When autotesting push, both the number and name may both be set
             * remove the launched suite name for the next test.
             */
            state.midletName = null;

            midletSuite = installer.getMIDletSuite(state.suiteStorageName,
                                                   temp);

        } else {
            int midletNumber;

            try {
                midletNumber = Integer.parseInt(state.midletNumber);
            } catch (NumberFormatException nfe) {
                throw new IllegalArgumentException("The value for " +
                    "MIDlet number was not formatted properly");
            }

            midletSuite = installer.getMIDletSuite(state.suiteStorageName,
                                                   midletNumber);
        }

        if (midletSuite == null) {
            state.status = MIDLET_SUITE_NOT_FOUND;
            return;
        }

        // remove any run once suites
        if (state.runOnce) {
            state.nextCommand = REMOVE;
        } else if (state.autotest) {
            state.nextCommand = INSTALL;
        }

        // run MIDlets til there are none
        if (!Scheduler.getScheduler().schedule(midletSuite)) {
            // The system is shutting down
            state.nextCommand = EXIT;
        }

        state.status = OK;