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

StartMIDletMonitor

public class StartMIDletMonitor extends Object implements MIDletProxyListListener
Implements the mechanism to monitor the startup of a MIDlet. It keeps track of which MIDlets are being started and are not yet in the MIDletProxyList. The information is used to avoid starting the same MIDlet twice.

Fields Summary
private static MIDletProxyList
midletProxyList
Reference to the ProxyList.
private static Vector
startPending
Vector of pending start requests.
private int
suiteId
The id of the MIDlet suite being started.
private String
midlet
The midlet of the MIDlet being started.
private com.sun.cldc.isolate.Isolate
isolate
The IsolateID of the MIDlet being started.
Constructors Summary
private StartMIDletMonitor(int id, String midletClassName)
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.

param
id ID of an installed suite
param
midletClassName class name of MIDlet to invoke

        suiteId = id;
        midlet = midletClassName;
        startPending.addElement(this);
        midletProxyList.addListener(this);
    
Methods Summary
private voidcleanupPending(int id, java.lang.String midlet)
Cleanup the matching entry in the startPending list. Once removed; the MIDlet will be eligible to be started again.

param
id ID of an installed suite of the notifying MIDlet
param
midlet class name of MIDlet of the notifying MIDlet

	synchronized (startPending) {
	    // If the notification is for this monitor
	    if (id == suiteId &&
		(midlet == null || midlet.equals(midlet))) {
		// Remove from the startPending list
		startPending.removeElement(this);

		// Remove the instance as a listener of the MIDletProxyList
		midletProxyList.removeListener(this);
	    }
	}
    
private static com.sun.midp.main.StartMIDletMonitorfindMonitor(int id, java.lang.String midlet)
Scan the startPending list for a matching id and MIDlet. The caller must synchronize using {@link #startPending}. If midlet is null then it only checks to see if the suite is started and returns any monitor for the suite. To prevent using stale Isolate state; check that the Isolate (if any) has not terminated.

param
id ID of an installed suite
param
midlet class name of MIDlet to invoke
return
a StartMIDletMonitor entry with id and midlet; otherwise null

	for (int i = 0; i < startPending.size(); i++) {
	    StartMIDletMonitor pending =
		(StartMIDletMonitor)startPending.elementAt(i);
	    // If there is a terminated Isolate in the list, clean it up
	    if (pending.isolate != null &&
		pending.isolate.isTerminated()) {
		// Isolate is not alive, clean the pending entry
		startPending.removeElementAt(i);
		midletProxyList.removeListener(pending);
		// Recheck the element at the same index
		i--;
		continue; // keep looking
	    }

	    if (id == pending.suiteId &&
		(midlet == null || midlet.equals(pending.midlet))) {
		return pending;
	    }
	}
	return null;
    
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;
    
public voidmidletAdded(MIDletProxy midlet)
Called when a MIDlet is added to the list. If there's a match in the startPending list clean it up.

param
midlet The proxy of the MIDlet being added

        IsolateMonitor.addIsolate(midlet, isolate);
        cleanupPending(midlet.getSuiteId(), midlet.getClassName());
    
public voidmidletRemoved(MIDletProxy midlet)
Called when a MIDlet is removed from the list. If there's a match in the startPending list clean it up.

param
midlet The proxy of the removed MIDlet

        cleanupPending(midlet.getSuiteId(), midlet.getClassName());
    
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. If there's a match in the startPending list clean it up.

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

        cleanupPending(suiteId, className);
    
public voidmidletUpdated(MIDletProxy midlet, int fieldId)
Called when the state of a MIDlet in the list is updated. If there's a match in the startPending list clean it up.

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

    
static com.sun.midp.main.StartMIDletMonitorokToStart(int id, java.lang.String midlet)
Check if the MIDlet is already in the ProxyList or is already being started. If so, return. If not, start it. Register with the proxy list and cleanup when the start of the MIDlet succeeds (and is now in the ProxyList) or fails (and is eligible to be started again).

param
id ID of an installed suite
param
midlet class name of MIDlet to invoke; may be null
return
the new StartMIDletMonitor to allow the MIDlet to be started; null if the MIDlet is already active or being started

	synchronized (startPending) {

	    // Verify that the requested MIDlet is not already running
	    // (is not in the MIDletProxyList)
	    if (midletProxyList.isMidletInList(id, midlet)) {
		if (Logging.REPORT_LEVEL <= Logging.WARNING) {
		    Logging.report(Logging.WARNING, LogChannels.LC_CORE,
				   "MIDlet already running; execute ignored");
		}
		return null;
	    }

	    /*
	     * Find the StartMIDletMonitor instance
	     * to track the startup, (if any)
	     */
	    StartMIDletMonitor start = findMonitor(id, midlet);
	    if (start == null) {
		// Not already starting; register new start
		start = new StartMIDletMonitor(id, midlet);
	    } else {
		// MIDlet is already started; return null
		start = null;
		if (Logging.REPORT_LEVEL <= Logging.WARNING) {
		    Logging.report(Logging.WARNING, LogChannels.LC_CORE,
				   "MIDlet already started; execute ignored");
		}
	    }
	    return start;
	}
    
voidsetIsolate(com.sun.cldc.isolate.Isolate newIsolate)
Sets the Isolate associated with this starting MIDlet. It is used to cleanup the Isolate if the start does not start correctly.

param
newIsolate the Isolate used to start the MIDlet

        isolate = newIsolate;