FileDocCategorySizeDatePackage
IsolateMonitor.javaAPI DocphoneME MR2 API (J2ME)7320Wed May 02 18:00:04 BST 2007com.sun.midp.main

IsolateMonitor

public class IsolateMonitor extends Object implements MIDletProxyListListener
Implements the mechanism to monitor MIDlet suites isolate. The StartMIDletMonitor provides the isolate references at it gets a MIDlet create notification from the MIDletProxyList. When an isolate terminates it notifies the native application manager. The notification is needed for native auto testers because the notification that the test MIDlet is destoryed does not indicate that the VM has closed it JAR.

Fields Summary
private static MIDletProxyList
midletProxyList
Reference to the ProxyList.
private static IsolateMonitor
monitor
Reference to the Isolate Monitor.
private Hashtable
isolates
Table of isolates indexed by midlet proxies.
private Vector
listeners
What objects should get list changes.
Constructors Summary
private IsolateMonitor()
Construct a new StartMIDletMonitor instance to track the process of starting a MIDlet in a new Isolate. The new instance is appended to the startPending vector and is registered with the MIDletProxyList to receive notifications if/when the MIDlet starts/fails to start.

        isolates = new Hashtable();
        listeners = new Vector();
        midletProxyList.addListener(this);
    
Methods Summary
static voidaddIsolate(MIDletProxy proxy, com.sun.cldc.isolate.Isolate isolate)
Adds the Isolate associated with a started MIDlet.

param
proxy proxy of a started MIDlet
param
isolate the Isolate of the started MIDlet, for systems that run multiple Isolates from a suite concurrently this may be a duplicate isolate

        synchronized (monitor.isolates) {
            monitor.isolates.put(proxy, isolate);
        }
    
public static voidaddListener(IsolateMonitorListener listener)
Add a listener for MIDlet suite terminations.

param
listener Isolate monitor listener

        monitor.listeners.addElement(listener);
    
static voidinitClass(MIDletProxyList theMIDletProxyList)
Initializes StartMIDletMonitor class. Shall only be called from AmsUtil. No need in security checks since it is package private method.

param
theMIDletProxyList MIDletController's container


        midletProxyList = theMIDletProxyList;

        monitor = new IsolateMonitor();
    
private booleanlastInIsolate(MIDletProxy proxy)
Determine if a MIDlet is the last MIDlet in an isolate.

param
proxy proxy of MIDlet
return
true if the MIDlet is the last MIDlet in the isolate

        Isolate isolate = (Isolate)isolates.get(proxy);
        int midletCount = 0;

        if (isolate != null) {
            Enumeration enum = isolates.elements();

            while (enum.hasMoreElements()) {
                Isolate current = (Isolate)enum.nextElement();

                if (current == isolate) {
                    midletCount++;
                }
            }
        }

        return midletCount == 1;
    
public voidmidletAdded(MIDletProxy midlet)
Called when a MIDlet is added to the list.

param
midlet The proxy of the MIDlet being added

    
public voidmidletRemoved(MIDletProxy proxy)
Called when a MIDlet is removed from the list. If this is the last MIDlet in its isolate then start a termination notification thread to wait on its isolate.

param
proxy The proxy of the removed MIDlet

        synchronized (isolates) {
            if (lastInIsolate(proxy)) {
                startNotificationThread(proxy);
            }

            isolates.remove(proxy);
        }
    
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

public voidmidletUpdated(MIDletProxy midlet, int fieldId)
Called when the state of a MIDlet in the list is updated.

param
midlet The proxy of the MIDlet that was updated
param
fieldId code for which field of the proxy was updated

    
voidnotifyListeners(int suiteId)
Notifies the listeners that a suite has terminated.

param
suiteId ID of the MIDlet suite

        synchronized (listeners) {
            for (int i = 0; i < listeners.size(); i++) {
                IsolateMonitorListener listener =
                    (IsolateMonitorListener)listeners.elementAt(i);

                listener.suiteTerminated(suiteId);
            }
        }
    
private voidstartNotificationThread(MIDletProxy proxy)
Start a thread that will wait the MIDlet's isolate to terminate and then notify the native application manager. This method will also remove the MIDlet from the monitor's MIDlet list.

param
proxy proxy of MIDlet

        Isolate isolate = (Isolate)isolates.get(proxy);
        TerminationNotifier notifier = new TerminationNotifier();

        notifier.midlet = proxy;
        notifier.isolate = isolate;
        notifier.parent = this;

        new Thread(notifier).start();