midletproxylist.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,361 行 · 第 1/3 页

JAVA
1,361
字号
/* * * * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package com.sun.midp.main;import java.util.*;import com.sun.j2me.security.AccessController;import com.sun.midp.events.EventQueue;import com.sun.midp.midlet.MIDletSuite;import com.sun.midp.security.Permissions;import com.sun.midp.security.SecurityToken;import com.sun.midp.security.ImplicitlyTrustedClass;import com.sun.midp.security.SecurityInitializer;import com.sun.midp.suspend.SuspendSystem;import com.sun.midp.suspend.SuspendSystemListener;import com.sun.midp.configurator.Constants;/** * Manages a list of MIDlet proxies, each proxy representing a running MIDlet * and tracks which MIDlet has the foreground display, the list only available * to objects running in the AMS isolate. * <p> * The list is updated upon receiving events.  See the process method for event * processing. * <p> * Objects can listen (see MIDletProxyListListener) to the list for additions, * updates to proxies, and removals as will as set foreground events and * select foreground events. * <p> * This class also provides a shutdown method and processes a shutdown event * to enable the method be used by native code. * */public class MIDletProxyList        implements MIDletControllerEventConsumer, SuspendSystemListener {    /** MIDletProxy added constant. */    static final int PROXY_ADDED = 0;    /** MIDletProxy removed constant. */    static final int PROXY_REMOVED = 1;    /** The one and only MIDlet proxy list. */    private static MIDletProxyList midletProxyList;    /** True when the system is shutting down. */    private static boolean shutdownFlag;    /** What objects should get list changes. */    private Vector listeners = new Vector(2, 2);    /** Vector to hold MIDlet proxies. */    private Vector midletProxies = new Vector(5, 5);    /** The foreground MIDlet. */    private MIDletProxy foregroundMidlet;    /** The one and only displayController. */    private DisplayController displayController;    /** True if all midlets are paused, false - otherwise */    private boolean allPaused; // = false    /**     * MIDlet proxy whose peer application runs in the AMS isolate. Should     * be accessed through findAmsProxy(), may be invalid otherwise.     */    private MIDletProxy amsProxy;    /**     * Class registered in SecurityInitializer that identifies this     * implementation permissions for accessing restricted API's     */    private static class SecurityTrusted implements ImplicitlyTrustedClass {}    /** Security token for provileged access to restricted API's. */    private static SecurityToken classSecurityToken =        SecurityInitializer.requestToken(new SecurityTrusted());    /**     * Called by the MIDlet suite loader in AMS Isolate to intialize the     * midletProxy list. Shall be called only by MIDletSuiteLoader's main().     *     * @param theMIDletProxyList proxy list instance to be used     *                           as MIDlet controller container     *     * Should only be called in the AMS Isolate.     */    static void initClass(MIDletProxyList theMIDletProxyList) {        /*        TBD: the code below is commented until        the issue with non-static Logging.assertTrue        will be fixed !        Logging.assertTrue(theMIDletProxyList != null,                           "theMIDletProxyList must be non-null");        Logging.assertTrue(midletProxyList == null,                           "midletProxyList must be initialized only once");        */        midletProxyList = theMIDletProxyList;    }    /**     * Get a reference to the MIDlet proxy list in a secure way.     * The calling suite must have the com.sun.midp.ams permission "allowed".     *     * Should only be called in the AMS Isolate.     *     * @return MIDP MIDlet proxy list reference     */    public static MIDletProxyList getMIDletProxyList() {        return getMIDletProxyList(null);    }    /**     * Get a reference to the MIDlet proxy list in a secure way.     * The calling suite must have the com.sun.midp.ams permission "allowed".     * <p>     * Should only be called in the AMS Isolate.     * <p>     * Method requires com.sun.midp.ams permission.     *     * @param token SecurityToken with the AMS permission allowed or     *              null to use the midletSuite permission     *     * @return MIDP MIDlet proxy list reference     */    public static MIDletProxyList getMIDletProxyList(SecurityToken token) {        if (token != null) {            token.checkIfPermissionAllowed(Permissions.AMS);        } else {            AccessController.checkPermission(Permissions.AMS_PERMISSION_NAME);        }        return midletProxyList;    }    /**     * Returns shutdown status     *     * @return true if shutdown is in progress, else false     */    static boolean shutdownInProgress() {        return shutdownFlag;    }    /**     * Package private constructor.     * Shall be called from MIDletSuiteLoader's main()     *     * @param  eventQueue reference to the event queue     */    MIDletProxyList(EventQueue eventQueue) {        displayController = new DisplayController(this);        /* register event listener for events processed by MIDletProxyList */        new MIDletControllerEventListener(eventQueue, this);    }    /**     * Enables the display controller to be replaced by an application     * manager.     *     * @param newController new display controller     */    public void setDisplayController(DisplayController newController) {        displayController = newController;    }    /**     * Add a listener for MIDlet proxy list changes.     *     * @param listener MIDlet proxy list listener     */    public void addListener(MIDletProxyListListener listener) {        listeners.addElement(listener);    }    /**     * Remove a listener for MIDlet proxy list changes.     *     * @param listener MIDlet proxy list listener     */    public void removeListener(MIDletProxyListListener listener) {        listeners.removeElement(listener);    }    /**     * Get an enumeration of MIDlet proxies.     *     * @return enumeration of midletProxys     */    public Enumeration getMIDlets() {        Vector v = new Vector();        synchronized (midletProxies) {            int size = midletProxies.size();            for (int i = 0; i < size; i++) {                v.addElement(midletProxies.elementAt(i));            }       }        return v.elements();    }    /**     * Shutdown the system by asynchronously destroying all MIDlets.     *     */    public void shutdown() {        synchronized (midletProxies) {            if (shutdownFlag) {                return;            }            shutdownFlag = true;            if (midletProxies.size() == 0) {                /*                 * We are done if no proxyies are in the list.                 * Notify any objects waiting for shutdown.                 */                midletProxies.notifyAll();                return;            }            for (int i = midletProxies.size() - 1; i >= 0; i--) {                MIDletProxy current = (MIDletProxy)midletProxies.elementAt(i);                current.destroyMidlet();            }        }    }    /** Wait for the system to asynchronously destroy all MIDlets. */    public void waitForShutdownToComplete() {        synchronized (midletProxies) {            // Wait for shutdown to be called.            while (!shutdownFlag) {                try {                    midletProxies.wait();                } catch (InterruptedException ie) {                    return;                }            }            // Wait for all MIDlets to be destroyed.            while (midletProxies.size() > 0) {                try {                    midletProxies.wait();                } catch (InterruptedException ie) {                    return;                }            }        }    }    /**     * Find the MIDletProxy that has matching Isolate ID and Display ID.     *     * @param isolateId Isolate ID     * @param displayId Display ID     *     * @return a reference to the matching MIDletProxy or null if no match     */    private MIDletProxy findMIDletProxy(int isolateId, int displayId) {        synchronized (midletProxies) {            for (int i = midletProxies.size() - 1; i >= 0; i--) {                MIDletProxy current = (MIDletProxy)midletProxies.elementAt(i);                if (current.getIsolateId() == isolateId &&		    current.containsDisplay(displayId)) {                    return current;                }            }        }        return null;    }    /**     * Find the MIDletProxy that has matching suiteId and classname.     *     * @param suiteId the suiteId of the target application     * @param classname classname of the MIDlet     *     * @return a reference to the matching MIDletProxy or null if no match     */    public MIDletProxy findMIDletProxy(int suiteId, String classname) {        synchronized (midletProxies) {            for (int i = midletProxies.size() - 1; i >= 0; i--) {                MIDletProxy current = (MIDletProxy)midletProxies.elementAt(i);                if (current.getSuiteId() == suiteId &&                        current.getClassName().equals(classname)) {                    return current;                }            }        }        return null;    }    /**     * Find the MIDletProxy that has matching external app ID.     *     * @param externalAppId ID assigned by the external application manager     *     * @return a reference to the matching MIDletProxy or null if no match     */    public MIDletProxy findMIDletProxy(int externalAppId) {        synchronized (midletProxies) {            for (int i = midletProxies.size() - 1; i >= 0; i--) {                MIDletProxy current = (MIDletProxy)midletProxies.elementAt(i);                if (current.getExternalAppId() == externalAppId) {                    return current;                }            }        }        return null;    }    /**     * Retireves proxy whose peer application runs in the AMS isolate.     * The proxy is identified by isolate ID since only one application     * can run in the AMS solate.     * @return the proxy or null if one cannot be found     */    public MIDletProxy findAmsProxy() {        if (null == amsProxy) {            for (int i = midletProxies.size() - 1; i >= 0; i--) {                MIDletProxy current = (MIDletProxy)midletProxies.elementAt(i);                if (current.getIsolateId() == MIDletSuiteUtils.getAmsIsolateId()) {                    amsProxy = current;                    break;                }            }        }        return amsProxy;    }    /**     * Process a MIDlet created notification.     * MIDletControllerEventConsumer I/F method.     *     * @param midletSuiteId ID of the MIDlet suite     * @param midletClassName Class name of the MIDlet     * @param midletIsolateId isolate ID of the sending MIDlet     * @param midletExternalAppId ID of given by an external application     *                            manager     * @param midletDisplayName name to show the user     */    public void handleMIDletCreateNotifyEvent(        int midletSuiteId,        String midletClassName,        int midletIsolateId,        int midletExternalAppId,        String midletDisplayName) {        MIDletProxy midletProxy = findMIDletProxy(midletSuiteId,                                                  midletClassName);        if (midletProxy != null) {            /**             * The isolate this MIDlet was last run in, died because the             * event proccessing encountered a fatal error, which ends             * the isolate without notifing the proxy list.             * So just remove the MIDlet's proxy now.             * So we can add the new midlet proxy.             */            removeMidletProxy(midletProxy);        }        /* MIDlet's are constructed in PAUSED state. */        midletProxy = new MIDletProxy(this,            midletExternalAppId,            midletIsolateId,            midletSuiteId,            midletClassName,            midletDisplayName,            MIDletProxy.MIDLET_PAUSED);        int suspendResumeState =                SuspendSystem.getInstance(classSecurityToken).getState();        if ((suspendResumeState == SuspendSystem.SUSPENDED) ||                (suspendResumeState == SuspendSystem.SUSPENDING)) {            MIDletProxyUtils.terminateMIDletIsolate(midletProxy, this);            midletProxy = null;            return;        }        // Load and cache run-time extended MIDlet attributes        MIDletProxyUtils.setupExtendedAttributes(midletProxy);        midletProxies.addElement(midletProxy);        notifyListenersOfProxyListChange(midletProxy, PROXY_ADDED);        displayController.midletCreated(midletProxy);    }    /**     * Process a MIDlet active notification     * MIDletControllerEventConsumer I/F method.     *     * TBD: param midletProxy proxy with information about MIDlet     *     * @param midletSuiteId ID of the MIDlet suite     * @param midletClassName Class name of the MIDlet     */    public void handleMIDletActiveNotifyEvent(        int midletSuiteId,        String midletClassName) {        MIDletProxy midletProxy = findMIDletProxy(midletSuiteId,                                                  midletClassName);        if (midletProxy == null) {            /*             * There is nothing we can do for the other events             * if a proxy was not found.             *             * Sometimes an display can send an event after a

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?