FileDocCategorySizeDatePackage
AutoMIDletStateController.javaAPI DocphoneME MR2 API (J2ME)12239Wed May 02 18:00:08 BST 2007com.sun.midp.automation

AutoMIDletStateController

public final class AutoMIDletStateController extends Object implements MIDletProxyListListener
Class controlling states of the MIDlets

Fields Summary
private MIDletProxyList
midletProxyList
MIDlet proxy list reference.
private AutoMIDletInfoList
midletsInfo
List of AutoMIDletInfo for MIDlet's we are interested in
private static AutoMIDletStateController
stateController
The one and only AutoMIDletStateController instance
Constructors Summary
private AutoMIDletStateController()
Private constructor to prevent direct creation of instances.


                 
      
        midletProxyList = MIDletProxyList.getMIDletProxyList();
        midletProxyList.addListener(this);

        midletsInfo = AutoMIDletInfoList.getMIDletInfoList();
    
Methods Summary
static synchronized com.sun.midp.automation.AutoMIDletStateControllergetMIDletStateController()
Gets AutoMIDletStateController instance.

return
AutoMIDletStateController instance

        if (stateController == null) {
            stateController = new AutoMIDletStateController();
        }

        return stateController;
    
voidisolateExited(MIDletProxy midletProxy)
Called when a MIDlet Isolate has exited.

param
midletProxy The proxy of the MIDlet being added

        synchronized (midletsInfo) {
            AutoMIDletInfo info = midletsInfo.findMIDletInfo(midletProxy);
            if (info != null) {
                AutoMIDletImpl midlet = info.midlet;
                if (midlet != null) {
                    midlet.stateChanged(AutoMIDletLifeCycleState.DESTROYED);
                }

                // remove MIDlet info from our list as well
                midletsInfo.removeFromList(info);
            }
        }
    
public voidmidletAdded(MIDletProxy midletProxy)
Called when a MIDlet is added to the list.

param
midletProxy The proxy of the MIDlet being added

        synchronized (midletsInfo) {
            AutoMIDletInfo info = midletsInfo.findMIDletInfo(midletProxy);
            if (info != null) {
                info.midletProxy = midletProxy;

                // notify waiter that MIDlet has been created
                midletsInfo.notify();
            }
        }
    
public voidmidletRemoved(MIDletProxy midletProxy)
Called when a MIDlet is removed from the list.

param
midletProxy The proxy of the removed MIDlet

        synchronized (midletsInfo) {
            AutoMIDletInfo info = midletsInfo.findMIDletInfo(midletProxy);
            if (info != null) {
                startNotificationThread(info);
            }
        }
    
public voidmidletStartError(int externalAppID, int suiteID, java.lang.String className, int errorCode, java.lang.String errorDetails)
Called when error occurred while starting a MIDlet object.

param
externalAppID ID assigned by the external application manager
param
suiteID Suite ID of the MIDlet
param
className Class name of the MIDlet
param
errorCode start error code
param
errorDetails start error details


        synchronized (midletsInfo) {
            AutoMIDletInfo info =
                midletsInfo.findMIDletInfo(suiteID, className);

            if (info != null) {
                // set error flag and notify waiter
                info.startError = true;
                info.startErrorCode = errorCode;
                info.startErrorDetails = errorDetails;
                midletsInfo.notify();
            }
        }
    
public voidmidletUpdated(MIDletProxy midletProxy, int fieldID)
Called when the state of a MIDlet in the list is updated.

param
midletProxy The proxy of the MIDlet that was updated
param
fieldID code for which field of the proxy was updated

        AutoMIDletImpl midlet = midletsInfo.findMIDlet(midletProxy);

        // notify AutoMIDletImpl about state change
        if (midlet != null &&
            fieldID == MIDletProxyListListener.MIDLET_STATE) {

            switch (midletProxy.getMidletState()) {
                case MIDletProxy.MIDLET_ACTIVE:
                    midlet.stateChanged(AutoMIDletLifeCycleState.ACTIVE);
                    break;

                case MIDletProxy.MIDLET_PAUSED:
                    midlet.stateChanged(AutoMIDletLifeCycleState.PAUSED);
                    break;

                default:
                    break;
            }
        }
    
AutoMIDletImplstartMIDlet(AutoMIDletDescriptorImpl midletDescriptor, java.lang.String[] args)
Starts MIDlet.

param
midletDescriptor descriptor of MIDlet to start
param
args MIDlet's arguments
throws
throws RuntimeException if MIDlet couldn't be started
return
AutoMIDletImpl instance representing started MIDlet or null if there was a problem starting MIDlet


        AutoMIDletImpl midlet = null;

        synchronized (midletsInfo) {
            AutoSuiteDescriptorImpl suite;
            suite = (AutoSuiteDescriptorImpl)
                midletDescriptor.getSuiteDescriptor();

            // what is needed to start MIDlet
            int suiteID = suite.getSuiteID();
            String midletClassName = midletDescriptor.getMIDletClassName();
            String midletName = midletDescriptor.getMIDletName();

            // MIDlet's arguments: only 3 are supported by now
            String arg1 = null;
            String arg2 = null;
            String arg3 = null;
            if (args != null) {
                if (args.length > 0) {
                    arg1 = args[0];
                }

                if (args.length > 1) {
                    arg2 = args[1];
                }

                if (args.length > 2) {
                    arg3 = args[2];
                }
            }


            // create info for MIDlet to be started
            AutoMIDletInfo info = midletsInfo.addToList(
                    suiteID, midletClassName);

            // initiate MIDlet starting
            // IMPL_NOTE: add passing of memory quotas and VM profile name
            Isolate isolate = AmsUtil.startMidletInNewIsolate(
                    suiteID, midletClassName, midletName,
                    arg1, arg2, arg3);

            // wait for MIDlet to start
            while (info.midletProxy == null &&
                   info.startError == false) {
                try {
                    midletsInfo.wait();
                } catch (InterruptedException e) {
                    // just ignore
                }
            }

            // By now, either MIDlet is running,
            // either there was a problem starting it
            if (info.startError) {
                midletsInfo.removeFromList(info);

                String errorMsg = "";
                switch (info.startErrorCode) {
                    case Constants.MIDLET_ISOLATE_CONSTRUCTOR_FAILED:
                        errorMsg = "MIDlet Isolate constructor failed";
                        break;

                    case Constants.MIDLET_RESOURCE_LIMIT:
                        errorMsg = "Not enough resources to start the MIDlet";
                        break;

                    case Constants.MIDLET_ISOLATE_RESOURCE_LIMIT:
                        errorMsg = "Maximum Isolate count is exceeded";
                        break;

                    case Constants.MIDLET_OUT_OF_MEM_ERROR:
                        errorMsg = "Not enough memory to start the MIDlet";
                        break;

                    case Constants.MIDLET_SUITE_NOT_FOUND:
                        errorMsg = "MIDlet suite is not found";
                        break;

                    case Constants.MIDLET_SUITE_DISABLED:
                        errorMsg = "MIDlet suite is disabled";
                        break;

                    case Constants.MIDLET_CLASS_NOT_FOUND:
                        errorMsg = "MIDlet class is not found";
                        break;

                    case Constants.MIDLET_INSTANTIATION_EXCEPTION:
                        errorMsg = "MIDlet instantiation exception";
                        break;

                    case Constants.MIDLET_ILLEGAL_ACCESS_EXCEPTION:
                        errorMsg = "MIDlet illegal access exception";
                        break;

                    case Constants.MIDLET_CONSTRUCTOR_FAILED:
                        errorMsg = "MIDlet constructor failed";
                        break;

                    default:
                        errorMsg = "Error starting MIDlet: " +
                            info.startErrorCode;
                        break;

                }

                String details = info.startErrorDetails;
                if (details != null) {
                    errorMsg += " (" + details + ")";
                }

                throw new RuntimeException(errorMsg);
            } else {
                midlet = new AutoMIDletImpl(midletDescriptor);
                info.midlet = midlet;
                info.midletIsolate = isolate;
            }
        }

        return midlet;
    
private voidstartNotificationThread(AutoMIDletInfo info)
Start notifier thread that will wait for the MIDlet's isolate to exit.

param
proxy proxy of MIDlet

        IsolateExitNotifier notifier = new IsolateExitNotifier();

        notifier.midletProxy = info.midletProxy;
        notifier.midletIsolate = info.midletIsolate;
        notifier.parent = this;

        new Thread(notifier).start();
    
voidswitchTo(AutoMIDletImpl midlet, AutoMIDletLifeCycleState state)
Initiates switching MIDlet to specified state.

param
midlet AutoMIDletImpl instance representing MIDlet to switch
param
state state to switch to

        MIDletProxy midletProxy = midletsInfo.findMIDletProxy(midlet);

        if (midletProxy != null) {
            if (state == AutoMIDletLifeCycleState.ACTIVE) {
                midletProxy.activateMidlet();
            } else if (state == AutoMIDletLifeCycleState.PAUSED) {
                 midletProxy.pauseMidlet();
            } else if (state == AutoMIDletLifeCycleState.DESTROYED) {
                midletProxy.destroyMidlet();
            }
        }