⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 midletsuiteimpl.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
字号:
/** * * @(#)MIDletSuiteImpl.java	1.10 01/06/01 * * Copyright 2001 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. */package com.sun.midp.midletsuite;import java.io.*;import java.util.*;import javax.microedition.io.Connector;import com.sun.midp.security.SecurityDomain;import com.sun.midp.security.Actions;import com.sun.midp.midlet.MIDletSuite;import com.sun.midp.midlet.Scheduler;import com.sun.midp.midlet.MIDletInfo;import com.sun.midp.io.j2me.storage.File;import com.sun.midp.io.j2me.storage.RandomAccessStream;/** * Implements a the required MIDletSuite functionality needed by the * Scheduler. */public class MIDletSuiteImpl implements MIDletSuite {    /** This class has a different security domain than the application */    private static SecurityDomain classSecurityDomain;    /** scheduler for this suite */    private Scheduler scheduler;    /** Buffered properties from the application descriptor */    private JadProperties bufferedJadProps = null;    /** Buffered properties from the JAR manifest */    private ManifestProperties bufferedJarProps = null;    /** Security domain for this suite */    private SecurityDomain securityDomain;    /** the storage path of this suite */    private String storageRoot = null;    /** Initial midlet class name. */    private String initialMIDletClassName = null;    /**     * number of midlets in this suite. less than 0 mean they need to     * counted     */    private int numberOfMidlets = -1;    /**     * Initializes the security domain for this class, so it can     * perform actions that a normal MIDlet Suite cannot.     *     * @param theDomain SecurityDomain for this class.     */    static void initSecurityDomain(SecurityDomain theDomain) {        if (classSecurityDomain != null) {            return;        }        classSecurityDomain = theDomain;    }    /**     * Constructor for development subclass.     */    protected MIDletSuiteImpl() {};    /**     * Constructs MIDletSuiteImpl from an installed MIDlet Suite.     *     * @param securityDomain security domain for the calling class     * @param aScheduler scheduler to use for the midlets in this suite     * @param theStorageRoot root path of any files for this suite     * @param securityDomainName name of the security domain for this     *        suite     * @param midletToRun the name of the intial MIDlet in this suite to run,     *        can be null.     */    MIDletSuiteImpl(SecurityDomain securityDomain, Scheduler aScheduler,                    String theStorageRoot, String securityDomainName,                    String midletToRun) {        securityDomain.checkIfPermitted(Actions.DEVICE_CORE_FUNCTION);        scheduler = aScheduler;        securityDomain = new SecurityDomain(classSecurityDomain,                                            securityDomainName);        storageRoot = theStorageRoot;        if (midletToRun != null) {            initialMIDletClassName = getMIDletClassName(midletToRun);        }    }    /**     * Get a property of the suite. A property is an attribute from     * either the application descriptor or JAR Manifest.     *     * @param key the name of the property     * @return A string with the value of the property.     * 		<code>null</code> is returned if no value is available for     *          the key.     */    public String getProperty(String key) {        String prop;        if (bufferedJadProps == null) {            getPropertiesFromStorage();            if (bufferedJadProps == null) {                return null;            }        }        // check the JAD first        prop = bufferedJadProps.getProperty(key);        if (prop != null) {            return prop;        }        // Only MIDlet-* keys can be retrieved from the JAR manifest        if (bufferedJarProps == null) {            /*             * for the TCK that fails when it cannot get "Manifest-Version"             * the JAR manifest we must go against the spec and pass any             * the attribute from the manifest. put this code back when             * the TCK matches the spec.             *             *   (!key.startsWith("MIDlet-"))) {             */            return null;        }        return bufferedJarProps.getProperty(key);    }    /**     * Provides the number of of MIDlets in this suite.     *     * @return number of MIDlet in the suite     */    public int getNumberOfMIDlets() {        if (numberOfMidlets <= 0) {            numberOfMidlets = countMIDlets();        }        return numberOfMidlets;    }    /**     * Loads the initial midlet to run.     *     * @exception ClassNotFoundException if one of the MIDlet classes cannot     * be found     * @exception InstantiationException if an instance cannot be created     * @exception IllegalAccessException if an instance cannot be accessed     */      public void loadInitialMIDlet() throws            ClassNotFoundException, InstantiationException,            IllegalAccessException {        String className;        className = initialMIDletClassName;        if (className == null &&               getNumberOfMIDlets() == 1) {            className =                new MIDletInfo(getProperty("MIDlet-1")).classname;        }        loadInitialMIDlet(className, true);    }    /**     * Loads the initial midlet to run.     *     * @param initialMIDletClassName initial midlet class name provided     *                               at object creation, can be null.     * @param selectorThatExits set to true if the select that may be     *                          presented to the user should exit after     *                          selection or cancellation.     *     * @exception ClassNotFoundException if one of the MIDlet classes cannot     * be found     * @exception InstantiationException if an instance cannot be created     * @exception IllegalAccessException if an instance cannot be accessed     */      protected void loadInitialMIDlet(String initialMIDletClassName,                                     boolean selectorThatExits) throws            ClassNotFoundException, InstantiationException,            IllegalAccessException {        if (initialMIDletClassName == null) {            new com.sun.midp.midlet.Selector(selectorThatExits);            return;        }        Class.forName(initialMIDletClassName).newInstance();    }    /**     * Gets the name of the security domain for the suite.     *     * @return name of security domain     */    public String getSecurityDomainName() {        return securityDomain.getName();    }    /**     * Check to see the domain permitted to perform a specific action.     *     * @param action an action code from com.sun.midp.security.Action     * @exception SecurityException if the domain is not     *            permitted to perform the specified action.     */    public void checkIfPermitted(int action) {        securityDomain.checkIfPermitted(action);    }    /**     * Schedules this MIDlet suite to run.     * Returns after the last midlet has been destroyed.     * @exception ClassNotFoundException if one of the MIDlet classes cannot     * be found.     * @exception InstantiationException if an instance can not be created.     */    public void schedule() throws            ClassNotFoundException, InstantiationException,            IllegalAccessException {        scheduler.schedule(this);    }    /**     * Gets the path root of any file this suite.     * Has any needed file separators appended.     *     * @return storage path root     */    public String getStorageRoot() {        return storageRoot;    }    /**     * Get a named resource out of the JAR of this MIDlet suite.     *     * @param name name of the resource     * @return raw bytes of the resource or null if not available     */    public byte[] getResource(String name) {        if (name.charAt(0) == '/') {            // the jar reader does not remove the leading '/'            name = name.substring(1, name.length());        }        try {            return JarReader.readJarEntry(classSecurityDomain,                                          getStorageRoot() +                                          Installer.JAR_FILENAME,                                          name);        } catch (IOException e) {            return null;        }    }    /**     * Get the amount of storage on the device that this suite is using.     * This includes the JAD, JAR, management data, and RMS.     *     * @return number of bytes of storage the suite is using.     */    public int getStorageUsed() {        File file = new File(classSecurityDomain);        RandomAccessStream stream =            new RandomAccessStream(classSecurityDomain);        Vector files;        int storageUsed = 0;        files = file.filenamesThatStartWith(getStorageRoot());        for (int i = 0; i < files.size(); i++) {            try {                stream.connect((String)files.elementAt(i), Connector.READ);                storageUsed += stream.getSizeOf();                stream.disconnect();            } catch (IOException ioe) {                // just move on to the next file            }        }        return storageUsed;    }    /**     * The URL that the JAD of the suite was downloaded from.     *     * @return URL of the JAD, never null, even in development environments     */    public String getJadUrl() {        RandomAccessStream storage =            new RandomAccessStream(classSecurityDomain);        DataInputStream storageStream;        try {            storage.connect(getStorageRoot() + Installer.JAD_URL_FILENAME,                            Connector.READ);            // convert the JAD URL to UTF8 and write it to storage            storageStream = storage.openDataInputStream();            return storageStream.readUTF();        } catch (Throwable t) {            // old installations did not have URL's            return "Unknown";        } finally {            try {                storage.disconnect();            } catch (IOException e) {                // ignore            }        }    }    /**     * Count the number of MIDlets from its properties.     *     * @return number of midlet in the suite     */    protected int countMIDlets() {        int i;        for (i = 1; getProperty("MIDlet-" + i) != null; i++);        return i - 1;    }    /**     * Retrieve the classname for a given MIDlet name.     * <p>     *     * @param midletName the name of the MIDlet to find.     * @return the classname of the MIDlet. <code>null</code> if the     *         MIDlet cannot be found.     */    protected String getMIDletClassName(String midletName) {        String midlet;        MIDletInfo midletInfo;        for (int i = 1; ; i++) {            midlet = getProperty("MIDlet-" + i);            if (midlet == null) {                return null; // We went past the last MIDlet            }            midletInfo = new MIDletInfo(midlet);            if (midletInfo.name.equals(midletName)) {                return midletInfo.classname;            }        }    }    /**     * Gets properites from a symbolically named installed package.     * The properties are the attributes in the application descriptor     * and JAR Manifest.     */    private void getPropertiesFromStorage() {        RandomAccessStream myStorage;        DataInputStream is;        String jadEncoding = null;        myStorage = new RandomAccessStream(classSecurityDomain);        // Get the JAD encoding, if the server provided one        try {            myStorage.connect(storageRoot +                              Installer.JAD_ENCODING_FILENAME,                              Connector.READ);            // convert the JAD encoding to UTF8 and write it to storage            is = myStorage.openDataInputStream();            try {                jadEncoding = is.readUTF();            } finally {                is.close();                myStorage.disconnect();            }        } catch (IOException e) {            // servers can choose the default encoding by not providing one        } finally {        }        // Load .jad file        bufferedJadProps = new JadProperties();        try {            myStorage.connect(storageRoot + Installer.JAD_FILENAME,                              Connector.READ);            is = myStorage.openDataInputStream();            try {                /*                 * the installer may be an secure installer so let installer                 * parse the JAD                 */                Installer.getInstaller(classSecurityDomain).                    parseJad(is, jadEncoding, bufferedJadProps);            } finally {                is.close();                myStorage.disconnect();            }                    // Get Manifest file so we can buffer it            myStorage.connect(storageRoot + Installer.MANIFEST_FILENAME,                              Connector.READ);            is = myStorage.openDataInputStream();            try {                bufferedJarProps = new ManifestProperties();                bufferedJarProps.load(is);            } finally {                is.close();                myStorage.disconnect();            }        } catch (Exception e) {            // ignore        }    }}

⌨️ 快捷键说明

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