FileDocCategorySizeDatePackage
CdcMIDletSuiteLoader.javaAPI DocphoneME MR2 API (J2ME)15068Wed May 02 18:00:08 BST 2007com.sun.midp.main

CdcMIDletSuiteLoader

public class CdcMIDletSuiteLoader extends AbstractMIDletSuiteLoader implements MIDletStateListener, PlatformRequest, ForegroundController
The first class loaded in VM by midp_run_midlet_with_args to initialize internal security the internal AMS classes and start a MIDlet suite.

Fields Summary
protected int
disableAlerts
Disable startup error alerts, uninitialized by default
protected int
currentDisplayId
Holds the ID of the current display, for preempting purposes.
Constructors Summary
Methods Summary
protected voidcloseSuite()
Overrides suite close logic for the AMS task

        super.closeSuite();

        // shutdown any preempting
        if (displayEventHandler != null) {
            displayEventHandler.donePreempting(null);
        }
    
protected MIDletSuitecreateMIDletSuite()
Creates MIDlet suite instance by suite ID

return
MIDlet suite to load
throws
Exception in the case MIDlet suite can not be created because of a security reasons or some problems related to suite storage

        MIDletSuiteStorage storage;
        MIDletSuite suite = null;

        if (suiteId == MIDletSuite.INTERNAL_SUITE_ID) {
            // assume a class name of a MIDlet in the classpath
            suite = InternalMIDletSuiteImpl.create(midletDisplayName, suiteId);
        } else {
            storage = MIDletSuiteStorage.
                getMIDletSuiteStorage(internalSecurityToken);

            suite = storage.getMIDletSuite(suiteId, false);
            Logging.initLogSettings(suiteId);
        }

        return suite;
    
protected voidcreateSuiteEnvironment()
Creates environment objects needed to AMS task

        foregroundController = this;

        // creates display container, needs foregroundController
        super.createSuiteEnvironment();

        midletStateHandler =
            MIDletStateHandler.getMidletStateHandler();

        MIDletSuiteStorage mss =
            MIDletSuiteStorage.getMIDletSuiteStorage(internalSecurityToken);

        midletStateHandler.initMIDletStateHandler(
            internalSecurityToken,
            this,
            new CdcMIDletLoader(mss),
            this);
    
public booleandispatch(java.lang.String URL)

        throw new ConnectionNotFoundException("not implemented");
    
protected voiddisplayException(java.lang.String exceptionMsg)
Displays an exception message to user

param
exceptionMsg the message text

        System.err.println(exceptionMsg);
    
protected voidexitLoader()
Gracefully terminates VM with proper return code

    
protected static intgetErrorCode(java.lang.Throwable t)
Gets error code by exception type

param
t exception instance
return
error code

        if (t instanceof ClassNotFoundException) {
            return Constants.MIDLET_CLASS_NOT_FOUND;
        } else if (t instanceof InstantiationException) {
            return Constants.MIDLET_INSTANTIATION_EXCEPTION;
        } else if (t instanceof IllegalAccessException) {
            return Constants.MIDLET_ILLEGAL_ACCESS_EXCEPTION;
        } else if (t instanceof OutOfMemoryError) {
            return Constants.MIDLET_OUT_OF_MEM_ERROR;
        } else if (t instanceof MIDletSuiteLockedException) {
            return Constants.MIDLET_INSTALLER_RUNNING;
        } else {
            return Constants.MIDLET_CONSTRUCTOR_FAILED;
        }
    
protected static java.lang.StringgetErrorMessage(int errorCode)
Gets AMS error message by generic error code.

param
errorCode generic error code
return
error message

        switch (errorCode) {
            case Constants.MIDLET_SUITE_DISABLED:
                return "MIDlet suite dispabled.";

            case Constants.MIDLET_SUITE_NOT_FOUND:
                return "MIDlet suite not found.";

            case Constants.MIDLET_CLASS_NOT_FOUND:
                return "MIDlet class not found.";

            case Constants.MIDLET_INSTANTIATION_EXCEPTION:
                return "Cannot launch MIDlet due to illegal operation.";

            case Constants.MIDLET_ILLEGAL_ACCESS_EXCEPTION:
                return "Cannot launch MIDlet due to illegal operation.";

            case Constants.MIDLET_OUT_OF_MEM_ERROR:
                return "Out of memory";
        }

        return "MIDlet suite has unexpectedly quit.";
    
public voidhandleException(java.lang.Throwable t)
Handles exceptions happened during MIDlet suite execution.

param
t exception instance

        t.printStackTrace();
        int errorCode = getErrorCode(t);

        if (Logging.TRACE_ENABLED) {
            Logging.trace(t,
                "Exception caught in CdcMIDletSuiteLoader");
        }

        reportError(errorCode, t.getMessage());
    
protected voidinit()
Extends base class initialization with initializatons specific for the AMS task

        super.init();
    
protected voidinitSuiteEnvironment()
Inits created MIDlet suite environment objects and global subsystems needed for all suites.

The method also loads MIDlet suite paramaters and arguments from the natively saved CommandState instance.

        super.initSuiteEnvironment();

        // Init internal state from the restored command state
        externalAppId = 0;
        midletDisplayName = null;
    
public static voidmain(java.lang.String[] args)
Called at the initial start of the VM. Initializes internal security and any other AMS classes related classes before starting the MIDlet.

param
args arg[0] the suite ID (-1=rommized MIDlet), arg[1] the class name of the MIDlet, arg[2-n] optional MIDlet args, the first labled arg-0, then second arg-1, etc


                                                                                              
         
        try {
            CDCInit.init();

            CdcMIDletSuiteLoader loader = new CdcMIDletSuiteLoader();

            if (args.length < 1) {
                System.err.println("The suite ID for the " +
                                   "MIDlet suite was not given");
                throw new IllegalArgumentException("Suite ID absent");
            }

            try {
                loader.suiteId = Integer.parseInt(args[0]);
            } catch (Throwable t) {
                throw new
                    IllegalArgumentException("Suite ID: " + t.getMessage());
            }            

            if (args.length < 2) {
                System.err.println("The class name for the " +
                                   "MIDlet was not given");
                throw new
                    IllegalArgumentException("MIDlet class name absent.");
            }

            loader.midletClassName = args[1];

            int numberOfSuiteArgs = args.length - 2;

            if (numberOfSuiteArgs > 0) {
                loader.args = new String[numberOfSuiteArgs];
                for (int i = 0; i < numberOfSuiteArgs; i++) {
                    loader.args[i] = args[i + 2];
                }
            }

            loader.runMIDletSuite();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    
public voidmidletActivated(MIDletSuite suite, javax.microedition.midlet.MIDlet midlet)
Called after a MIDlet is successfully activated. This is after the startApp method is called. This implementation does nothing.

param
suite reference to the loaded suite
param
midlet reference to the MIDlet

    
public voidmidletCreated(MIDletSuite suite, java.lang.String className, int externalAppId)
Called after a MIDlet is successfully created. This implementation does nothing.

param
suite reference to the loaded suite
param
className Class name of the MIDlet
param
externalAppId ID of given by an external application manager

    
public voidmidletDestroyed(MIDletSuite suite, java.lang.String className)
Called after a MIDlet is successfully destroyed. This implementation does nothing.

param
suite reference to the loaded suite
param
className class name of the MIDlet

        displayContainer.removeDisplay(className);
    
public voidmidletPaused(MIDletSuite suite, java.lang.String className)
Called after a MIDlet is successfully paused. This implementation does nothing.

param
suite reference to the loaded suite
param
className class name of the MIDlet

    
public voidmidletPausedItself(MIDletSuite suite, java.lang.String className)
Called after a MIDlet pauses itself. In this case pauseApp has not been called.

param
suite reference to the loaded suite
param
className class name of the MIDlet

    
public voidmidletPreStart(MIDletSuite suite, java.lang.String className)
Called before a MIDlet is created. This implementation does nothing.

param
suite reference to the loaded suite
param
className class name of the MIDlet to be created

    
public voidpreActivated(MIDletSuite suite, java.lang.String className)
Called before a MIDlet is activated. This implementation does nothing.

param
suite reference to the loaded suite
param
className class name of the MIDlet

    
public javax.microedition.lcdui.DisplayableregisterDisplay(int displayId, java.lang.String ownerClassName)
Called to register a newly create Display. Must method must be called before the other methods can be called. This implementation does nothing.

param
displayId ID of the Display
param
ownerClassName Class name of the that owns the display
return
a place holder displayable to used when "getCurrent()==null", if null is returned an empty form is used

        currentDisplayId = displayId;
        return null;
    
protected voidreportError(int errorCode, java.lang.String details)
Updates CommandState status and displays proper exception message to user

param
errorCode generic error code
param
details text with error details

        String errorMsg = getErrorMessage(errorCode);
        if (details != null) {
            errorMsg += "\n\n" + details;
        }

        displayException(errorMsg);

        // Error message is always obtained for logging
        if (Logging.REPORT_LEVEL <= Logging.ERROR) {
            Logging.report(Logging.ERROR, LogChannels.LC_CORE, errorMsg);
        }
    
public voidrequestBackground(int displayId)
Called to request the background. This implementation does nothing.

param
displayId ID of the Display

        ForegroundEventConsumer fc =
            displayContainer.findForegroundEventConsumer(displayId);

        if (fc == null) {
            return;
        }

        fc.handleDisplayBackgroundNotifyEvent();
    
public voidrequestForeground(int displayId, boolean isAlert)
Called to request the foreground. This implementation does nothing.

param
displayId ID of the Display
param
isAlert true if the current displayable is an Alert

        ForegroundEventConsumer fc =
            displayContainer.findForegroundEventConsumer(displayId);

        if (fc == null) {
            return;
        }

        setForegroundInNativeState(displayId);

        fc.handleDisplayForegroundNotifyEvent();
    
public voidresumeRequest(MIDletSuite suite, java.lang.String className)
Called when a MIDlet calls MIDlet resume request.

param
suite reference to the loaded suite
param
className class name of the MIDlet

        MIDletEventConsumer mec =
            midletStateHandler.getMIDletEventConsumer(internalSecurityToken,
                                                      className);

        if (mec == null) {
            return;
        }

        mec.handleMIDletActivateEvent();
    
private native voidsetForegroundInNativeState(int displayId)
Set foreground display native state, so the native code will know which display can draw.

param
displayId Display ID

public voidstartPreempting(int displayId)
Called to start preempting. The given display will preempt all other displays for this isolate.

param
displayId ID of the Display

        requestBackground(currentDisplayId);
        requestForeground(displayId, true);
    
public voidstopPreempting(int displayId)
Called to end preempting.

param
displayId ID of the Display

        requestBackground(displayId);
        requestForeground(currentDisplayId, false);