xletmanager.java

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

JAVA
489
字号
/* * @(#)XletManager.java	1.21 06/10/10 * * Copyright  1990-2008 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.xlet;import javax.microedition.xlet.*;import javax.microedition.xlet.ixc.*;import java.awt.BorderLayout;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import java.io.File;import java.net.URL;import java.net.MalformedURLException;import java.lang.reflect.Constructor;import java.rmi.AccessException;import java.security.AccessController;import java.security.PrivilegedAction;import java.security.PrivilegedExceptionAction;import java.security.PrivilegedActionException;import java.util.Hashtable;import java.util.Vector;import sun.awt.SunToolkit;import sun.awt.AppContext;public class XletManager implements XletLifecycleHandler {    // the root frame shared by all xlets    static ToplevelFrame theFrame;     // list of active <XletContext,XletManager> pairs     static Hashtable activeContexts = new Hashtable();     // the xlet class this XletManager instance handles    private Class xletClass;        // this XletManager's state queue    private XletStateQueue xletQueue;     // The instance of the xletClass above.      // Both Class and the instance are kept    // as xlet instantiation calls into the user code,    // thus should be handled in the XletStateQueue thread,    // and this thread only knows about local data right now.    private Xlet xlet;      // this Xlet's XletContext    private XletContextImpl context;     // Xlet's current state    private XletState currentState = XletState.UNLOADED;    // this Xlet's threadGroup and AppContext    ThreadGroup threadGroup;      AppContext appContext;    private Object stateGuard = new Object();    protected XletManager(XletClassLoader xletClassLoader,                 String mainClass,                 String[] args) throws ClassNotFoundException {       xletClass = xletClassLoader.loadClass(mainClass);                if (!(Xlet.class).isAssignableFrom(xletClass)) {          throw new ClassCastException(             "Attempt to run a non-Xlet class: " + mainClass);       }       context = new XletContextImpl(mainClass, args, this);       activeContexts.put(context, this);       final ThreadGroup currentTG = Thread.currentThread().getThreadGroup();       final int numOfXlets = activeContexts.size();       final String className = mainClass;       final ClassLoader finalLoader = xletClassLoader;       threadGroup = (ThreadGroup) AccessController.doPrivileged(          new PrivilegedAction() {             public Object run() {                return  new ThreadGroup(currentTG,                                        "Xlet Thread Group " + numOfXlets);             }             }             );             // Create an Xlet state change dispatcher queue       final XletManager thisXletManager = this;       xletQueue = (XletStateQueue) AccessController.doPrivileged(          new PrivilegedAction() {             public Object run() {                Thread t = new Thread(threadGroup,                           new Runnable() {                              public void run() {                                 manageLifecycle();                              }                           }, "Xlet lifecycle for " + className);                t.setContextClassLoader(finalLoader);                t.start();                return new XletStateQueue(thisXletManager);             }             }             );       // Let the ClassLoader have the reference to this XletManager       xletClassLoader.setXletManager(this);       // Register an AppContext with this ThreadGroup.       // Note: Create AppContext after registering XletManager to XletClassLoader,       //       as AppContext creation leads to EventQueue dispatcher thread       //       creation.  ThreadGroup for the new Thread is provided by       //       the SecurityManager if installed, and XletSecurity needs       //       XletClassLoader to have a reference of xletManager to get TG.       createAppContext(threadGroup);       if (theFrame == null) { // Grab the Frame before xlet gets it's chance.          theFrame = new ToplevelFrame("Xlet Frame", this);        }       // Install a SecurityManager on this xlet if nothing is set.       // Note: If this is launching a second xlet, all code above is already       //       executing with XletSecurity installed.       installSecurityManager();       // Finally, request the xlet to be instantiated.       xletQueue.push(XletState.LOADED);    }    /**     * Call SunToolkit to create a dedicated EventQueue     * for the caller Xlet.  Guarantees that AppContext      * is created by the the the method returns.     *     * @param ThreadGroup which the xlet's lifecycle thread     *        is running within     */    final Object syncObject = new Object();    private void createAppContext(ThreadGroup tg) {       final ThreadGroup finalTG = tg;       final ClassLoader finalLoader = getClassLoader();       AccessController.doPrivileged(           new PrivilegedAction() {             public Object run() {                Thread t2 = new Thread(finalTG,                   new Runnable() {                       public void run() {                          synchronized (syncObject) {                              appContext = SunToolkit.createNewAppContext();                              syncObject.notifyAll();                          }                       }                   }, "AppContext creation thread");                // Need to set ContextClassLoader, as this thread                 // is later used as the EventQueue's dispatching thread                t2.setContextClassLoader(finalLoader);                synchronized (syncObject) {                   t2.start();                   try {                      syncObject.wait();                   } catch (InterruptedException e) {}                }                return null;             }          }       );       return;    }    /*     * Install XletSecurity if no SecurityManager is set.     */    private void installSecurityManager() {        if (System.getSecurityManager() == null) {           System.setSecurityManager(new XletSecurity(appContext));        }    }    public Container getContainer() {        return theFrame.getXletContainer();    }    public void postInitXlet() {        xletQueue.push(DesiredXletState.INITIALIZE);    }    public void postStartXlet() {        xletQueue.push(XletState.ACTIVE);    }    public void postPauseXlet() {        xletQueue.push(XletState.PAUSED);    }     public void postDestroyXlet(boolean unconditional) {        if (!unconditional) {            xletQueue.push(DesiredXletState.CONDITIONAL_DESTROY);        } else {            xletQueue.push(XletState.DESTROYED);        }    }    public void setState(XletState state) {                 if (currentState == XletState.DESTROYED ||             state == currentState)              return;        synchronized (stateGuard) {            currentState = state;            stateGuard.notifyAll();        }    }    public int getState() {         XletState state = getXletState();        if (state == XletState.LOADED) {            return LOADED;        } else if (state == XletState.PAUSED) {            return PAUSED;        } else if (state == XletState.ACTIVE) {            return ACTIVE;

⌨️ 快捷键说明

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