xletmanager.java

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

JAVA
489
字号
        } else if (state == XletState.DESTROYED) {            return DESTROYED;        } else {            return UNKNOWN;        }    }    public XletState getXletState() {         synchronized (stateGuard) {            return currentState;        }    }    // typically called from XletStateQueue    public void handleRequest(XletState desiredState) {        XletState targetState = currentState;        synchronized (stateGuard) {           try {                 if (desiredState == XletState.LOADED) {                    if (currentState != XletState.UNLOADED)                        return;                    targetState = XletState.LOADED;                    Constructor m = xletClass.getConstructor(new Class[0]);                    xlet = (Xlet) m.newInstance(new Object[0]);                } else if (desiredState == DesiredXletState.INITIALIZE) {                    if (currentState != XletState.LOADED)                         return;                    targetState = XletState.PAUSED;                    try {                        xlet.initXlet(context);                    } catch (XletStateChangeException xsce) {                        targetState = XletState.DESTROYED;                        xlet.destroyXlet(true);                    }                } else if (desiredState == XletState.ACTIVE) {                    if (currentState != XletState.PAUSED)                         return;                    targetState = XletState.ACTIVE;                    try {                        xlet.startXlet();                    } catch (XletStateChangeException xsce) {                        targetState = currentState;                    }                 } else if (desiredState == XletState.PAUSED) {                    if (currentState != XletState.ACTIVE)                         return;                    targetState = XletState.PAUSED;                    xlet.pauseXlet();                } else if (desiredState == DesiredXletState.CONDITIONAL_DESTROY) {                    if (currentState == XletState.DESTROYED)                         return;                    targetState = XletState.DESTROYED;                    try {                        xlet.destroyXlet(false);                    } catch (XletStateChangeException xsce) {                        targetState = currentState;                    }                 } else if (desiredState == XletState.DESTROYED) {                    targetState = XletState.DESTROYED;                    if (currentState == XletState.DESTROYED)                         return;                    try {                        xlet.destroyXlet(true);                    } catch (XletStateChangeException xsce) {}                }            } catch (Exception e) {               // Some unknown exception from ths user code.               // Destroy it...               //System.err.println("Exception during execution: " + e);               e.printStackTrace();               if (targetState != XletState.DESTROYED) {                   handleRequest(XletState.DESTROYED);               }           }           // State change done - update this xlet's State.           setState(targetState);        }    }    // really private, but invoked from inner class.    void manageLifecycle() {        try {            while (currentState != XletState.DESTROYED) {                synchronized (stateGuard) {                    stateGuard.wait();                  }            }            // xlet has destroyed, do appropreate cleanup             cleanUp();        } catch (Throwable t) {            System.out.println(                "Xlet had unexpected exception in lifecycle thread.");            t.printStackTrace();            setState(XletState.DESTROYED);        }        // 4739427. If no other xlet running, then shut down.        synchronized (activeContexts) {           if (activeContexts.isEmpty()) {              exit();           }        }    }    private void cleanUp() {        // Clean up IxcRegistry        IxcRegistry.getRegistry(context).unbindAll();        // remove the Root Container from the parent frame        if (context.container != null) {            theFrame.remove(context.container);            context.container = null;            theFrame.repaint();        }        // clear the State Queue        xletQueue.destroy();        // remove the xlet context from the hash        synchronized (activeContexts) {            activeContexts.remove(context);            activeContexts.notifyAll();        }    }    static void exit() {        synchronized (activeContexts) {            XletManager[] managers =                 (XletManager[]) activeContexts.values().toArray(                    new XletManager[activeContexts.size()]);             for (int i = 0; i < managers.length; i++) {                try {                    // request for a DESTROY state to be picked up at the XletStateQueue                    managers[i].postDestroyXlet(false);                    activeContexts.wait(1000);  // wait for 1 sec for each..                    if (activeContexts.containsValue(managers[i])) {                        // try to interrupt anything that the XletStateQueue is doing                        managers[i].xletQueue.queueThread.interrupt();                         // force the state to be DESTROYED                        managers[i].setState(XletState.DESTROYED);                        activeContexts.wait(500);  // wait for .5 sec more...                    }                } catch (InterruptedException ie) {                    managers[i].setState(XletState.DESTROYED);                }            }        }        System.exit(0);     }     /**       *  Look up the XletContext-Xlet hashtable (activeContexts) and      *  return the classloader for the xlet based on the XletContext passed in.     *  For XletContext.getClassLoader() impl.     **/    static ClassLoader getXletClassLoader(XletContext context) {       XletManager thisManager = (XletManager)activeContexts.get(context);       if (thisManager != null) {           return thisManager.getClassLoader();       }       return null;  // no matching xlet found.    }    // Returns the ClassLoader for this xlet     public ClassLoader getClassLoader() {       return xletClass.getClassLoader();    }    /**      * Entry point for XletManager.     * Instanciates the xlet class in the current dir.     * Returns the handler to control this xlet's lifecycle.     * @param mainClass the xlet class name.    **/    public static XletLifecycleHandler createXlet(String mainClass)        throws IOException {        return createXlet(mainClass, new String[] {"."}, new String[]{});    }    /**      * Entry point for XletManager.     * Returns the handler to control this xlet's lifecycle.     * @param mainClass the xlet class name.     * @param path array of url-formed strings or file paths to find     *             the xlet class.     * @param args the runtime arguments given to the xlet     *             (accessed by XletContext.getXletProperty(ARGS))    **/    public static XletLifecycleHandler createXlet(String mainClass,        String[] paths,        String[] args)        throws IOException {        Vector v = new Vector();        int index = 0;        for (int i = 0; i < paths.length; i++) {           try {              v.add(new URL(paths[i]));           } catch (MalformedURLException mue) {              final File file = new File(paths[i]);              try {                 URL path = (URL) AccessController.doPrivileged(                    new PrivilegedExceptionAction() {                       public Object run() throws IOException {                          if (!file.exists()) {                             System.out.println("Warning: \"" + file.getPath() + "\" not found");                             return null;                          }                          // CR 6252530.                          return file.toURI().toURL();                       }                       }                    );                               if (path != null)                      v.add(path);              } catch (PrivilegedActionException e) { e.getException().printStackTrace(); }           }        }        final URL[] array = (URL[])v.toArray(new URL[v.size()]);        XletClassLoader cl = (XletClassLoader) AccessController.doPrivileged(           new PrivilegedAction() {              public Object run() {                 return new XletClassLoader(array);              }              }        );                                                                                        try {            return (XletLifecycleHandler) new XletManager(cl, mainClass, args);        } catch (ClassNotFoundException e) {            e.printStackTrace();            throw new IOException("Cannot find class " + mainClass);        }    }     public static int getState(XletContext context) {       XletManager thisManager = (XletManager)activeContexts.get(context);       if (thisManager != null)          return thisManager.getState();       return 0;    }}

⌨️ 快捷键说明

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