connectionregistry.java

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

JAVA
843
字号
/* * * * 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.io.j2me.push;import java.io.IOException;import java.util.Enumeration;import javax.microedition.io.ConnectionNotFoundException;import com.sun.midp.io.Util;import com.sun.midp.main.*;import com.sun.midp.midletsuite.MIDletSuiteStorage;import com.sun.midp.midlet.MIDletStateHandler;import com.sun.midp.midlet.MIDletSuite;import com.sun.midp.security.SecurityToken;import com.sun.midp.log.Logging;/** * CLDC implementation of ConnectionRegistry. */final class ConnectionRegistry    implements Runnable, MIDletProxyListListener {    /**     * Push option to only launch this suite when not other applications     * are running.     */    static final int PUSH_OPT_WHEN_ONLY_APP = 1;    /** This class has a different security domain than the MIDlet suite. */    private static SecurityToken classSecurityToken;    /**     * Flag to control when push launching is permitted.     * This flag is set to false by the AMS when installing or removing     * MIDlets, when an interruption could compromise the integrity of     * the operation.     */    static boolean pushEnabled = true;    /**     * This flag is set to true by the AMS when running in MVM singal MIDlet     * mode. In this mode the current MIDlet that is not the application     * manager should be destroyed before the next MIDlet is started.     */    static boolean mvmSingleMidletMode;    /** MIDlet proxy list reference. */    private MIDletProxyList midletProxyList;    /** Cached reference to the midlet suite storage instance. */    private static MIDletSuiteStorage storage;    /**     * Start listening for push notifications. Will throw a security     * exception if called by any thing other than the MIDletSuiteLoader.     */    static void startListening() {        (new Thread(new ConnectionRegistry())).start();    }    /**     * Keeps an object of this class from being created out of     * the startListening method.     * Will throw a security exception if called by any thing other     * than the MIDletSuiteLoader.     */    private ConnectionRegistry() {        /*         * Will throw a security exception if called by any thing other         * than the MIDletSuiteLoader.         */        midletProxyList =            MIDletProxyList.getMIDletProxyList(classSecurityToken);        midletProxyList.addListener(this);        if (storage == null) {            storage = MIDletSuiteStorage.getMIDletSuiteStorage(                classSecurityToken);        }    }    /**     * Run the polling loop to check for inbound connections.     */    public void run() {        int fd = -1;        int ret = 0;        while (true) {            try {                fd = poll0(System.currentTimeMillis());                if (fd != -1) {                    if (pushEnabled) {                        byte[] registryEntry = new byte[512];                        if ((ret = getMIDlet0(fd, registryEntry, 512)) == 0) {                            String name = Util.toJavaString(registryEntry);                            launchEntry(name);                        }                    } else {                        checkInByHandle0(fd);                    }                }            } catch (Exception e) {                if (Logging.TRACE_ENABLED) {                    Logging.trace(e, null);                }            }        }    }    /**     * Parse the registration entry and launch the associated     * <code>MIDlet</code>.     * @param name registration string for connection and     * <code>MIDlet</code> to be launched     */    private void launchEntry(String name) {        String conn;        String midlet;        String filter;        String strSuiteId;        int id;        MIDletSuite next = null;        /*         * Parse the comma separated values  -         *  " connection, midlet,  filter, id"         *  "  midlet,  wakeup, midlet suite ID"         */        int comma1 = name.indexOf(',', 0);        int comma2 = name.indexOf(',', comma1 + 1);        int comma3 = name.indexOf(',', comma2 + 1);        if (comma3 == -1) {            /* Alarm was triggered */            conn = null;            midlet = name.substring(0, comma1).trim();            strSuiteId = name.substring(comma2+1).trim();        } else {            conn = name.substring(0, comma1).trim();            midlet = name.substring(comma1+1, comma2).trim();            filter = name.substring(comma2+1, comma3).trim();            strSuiteId = name.substring(comma3+1).trim();        }        try {            /*             * IMPL_NOTE: here it's assumed that when a suiteId is converted             * to string the padding zeroes are placed _before_ the value,             * for ex., suiteId 3 is converted into "00000003".             * MIDletSuiteStorage.stringToSuiteId() API should be added later.             */            id = Integer.parseInt(strSuiteId);        } catch (NumberFormatException nfe) {            id = MIDletSuite.UNUSED_SUITE_ID;        }        try {            /*             * Check to see if the MIDlet is already started.             */            if (midletProxyList.isMidletInList(id, midlet)) {                if (conn != null) {                    checkInConnectionInternal(conn);                }                return;            }            next = storage.getMIDletSuite(id, false);            if (next == null) {                if (conn != null) {                    checkInConnectionInternal(conn);                }                return;            }            if ((next.getPushOptions() & PUSH_OPT_WHEN_ONLY_APP) != 0 &&                    !onlyAppManagerRunning()) {                if (conn != null) {                    checkInConnectionInternal(conn);                }                return;            }            if (!next.permissionToInterrupt(conn)) {                // user does not want the interruption                if (conn != null) {                    checkInConnectionInternal(conn);                }                return;            }            if (MIDletSuiteUtils.execute(classSecurityToken, id, midlet,                                          null)) {                /* We are in SVM mode, destroy all running MIDlets. */                MIDletStateHandler.getMidletStateHandler().destroySuite();            } else if (mvmSingleMidletMode) {                destroyAppMidlets();            }        } catch (Throwable e) {            // Could not launch requested push entry            if (conn != null) {                checkInConnectionInternal(conn);            }            if (Logging.TRACE_ENABLED) {                Logging.trace(e, null);            }        } finally {            if (next != null) {                next.close();            }        }    }    /**     * Check to see if only the application manager MIDlet is running.     *     * @return true if only the application manager is running     */    private boolean onlyAppManagerRunning() {        Enumeration midlets = midletProxyList.getMIDlets();        while (midlets.hasMoreElements()) {            MIDletProxy midlet = (MIDletProxy)midlets.nextElement();            if (midlet.getSuiteId() != MIDletSuite.INTERNAL_SUITE_ID ||                    midlet.getClassName().indexOf("Manager") == -1) {                return false;            }        }        return true;    }    /**     * Destroy every MIDlet except the application manager midlet.     * This should only be used in MVM Signal MIDlet Mode.     */    private void destroyAppMidlets() {        Enumeration midlets = midletProxyList.getMIDlets();        while (midlets.hasMoreElements()) {            MIDletProxy midlet = (MIDletProxy)midlets.nextElement();            if (midlet.getSuiteId() == MIDletSuite.INTERNAL_SUITE_ID &&                    midlet.getClassName().indexOf("Manager") != -1) {

⌨️ 快捷键说明

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