📄 midletsuiteimpl.java
字号:
/* * @(#)MIDletSuiteImpl.java 1.52 02/10/14 @(#) * * Copyright (c) 2001-2002 Sun Microsystems, Inc. All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. */package com.sun.midp.midletsuite;import java.io.*;import java.util.*;import javax.microedition.io.Connector;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import com.sun.midp.io.*;import com.sun.midp.lcdui.*;import com.sun.midp.security.SecurityToken;import com.sun.midp.security.Permissions;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 { /** Interrupt dialog title for push. */ static final String PUSH_INTERRUPT_DIALOG_TITLE = "Can &1 Interrupt?"; /** Interrupt question for push. */ protected static final String PUSH_INTERRUPT_QUESTION = "Information is arriving for %1. " + "Is it OK to exit %2 and launch %1?"; /** Interrupt question for alarms. */ protected static final String ALARM_INTERRUPT_QUESTION = "%1 needs to start itself to check to see if it has received " + "information. Is it OK to exit %2 and launch %1?"; /** This class has a different security domain than the application. */ private static SecurityToken classSecurityToken; /** Buffered properties from the application descriptor. */ private JadProperties bufferedJadProps; /** Buffered properties from the JAR manifest. */ private ManifestProperties bufferedJarProps; /** Security token for this suite. */ private SecurityToken securityToken; /** Permissions for this suite. */ private byte[][] permissions; /** Can this MIDlet suite interrupt other suites. */ private int pushInterruptSetting; /** The storage path of this suite. */ private String storageRoot; /** The storage name of this suite. */ private String storageName; /** The CA that authorized this suite. */ private String ca; /** Indicates if this suite is trusted. */ private boolean trusted; /** Initial midlet class name. */ private String initialMIDletClassName; /** * Number of midlets in this suite. less than 0 mean they need to * counted. */ private int numberOfMidlets = -1; /** * Initializes the security token for this class, so it can * perform actions that a normal MIDlet Suite cannot. * * @param token security token for this class */ static void initSecurityToken(SecurityToken token) { if (classSecurityToken != null) { return; } classSecurityToken = token; } /** * Constructor for development subclass. * * @param callerSecurityToken security token for the calling class * @param suitePermissions security token of the suite * @param pushSetting can this MIDlet suite interrupt other suites * @param trustedFlag true if the suite is to considered trusted * (not to be confused with a domain named "trusted", * this only shown to the user and not used for permissions) * @param theStorageName name to separate this suite's storage from others */ protected MIDletSuiteImpl(SecurityToken callerSecurityToken, byte[][] suitePermissions, int pushSetting, boolean trustedFlag, String theStorageName) { callerSecurityToken.checkIfPermissionAllowed(Permissions.MIDP); permissions = suitePermissions; securityToken = new SecurityToken(classSecurityToken, permissions); pushInterruptSetting = pushSetting; trusted = trustedFlag; storageName = theStorageName; } /** * Constructs MIDletSuiteImpl from an installed MIDlet Suite. * * @param callerSecurityToken security token for the calling class * @param theStorageRoot root path of any files for this suite * @param theStorageName unique vendor and suite name identifying this * suite * @param theCA name of CA that authorized this suite * @param midletToRun the name of the initial MIDlet in this suite to run, * can be null */ MIDletSuiteImpl(SecurityToken callerSecurityToken, String theStorageRoot, String theStorageName, String theCA, String midletToRun) { callerSecurityToken.checkIfPermissionAllowed(Permissions.MIDP); storageRoot = theStorageRoot; storageName = theStorageName; ca = theCA; readSettings(); securityToken = new SecurityToken(classSecurityToken, permissions); if (midletToRun != null) { initialMIDletClassName = getMIDletClassName(midletToRun); } } /** * Constructs MIDletSuiteImpl from an installed MIDlet Suite. * * @param callerSecurityToken security token for the calling class * @param theStorageRoot root path of any files for this suite * @param theStorageName unique vendor and suite name identifying this * suite * @param theCA name of CA that authorized this suite * @param midletToRun the number of the initial MIDlet in this suite */ MIDletSuiteImpl(SecurityToken callerSecurityToken, String theStorageRoot, String theStorageName, String theCA, int midletToRun) { this(callerSecurityToken, theStorageRoot, theStorageName, theCA, null); String temp; temp = getProperty("MIDlet-" + midletToRun); if (temp == null) { return; } initialMIDletClassName = new MIDletInfo(temp).classname; } /** * Gets 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; } if (bufferedJarProps == null) { return null; } return bufferedJarProps.getProperty(key); } /** * Adds a property to the suite. * * @param key the name of the property * @param value the value of the property * * @exception SecurityException if the calling suite does not have * internal API permission */ public void addProperty(String key, String value) { MIDletSuite current = Scheduler.getScheduler().getMIDletSuite(); if (current != null) { current.checkIfPermissionAllowed(Permissions.MIDP); } if (bufferedJadProps != null) { bufferedJadProps.addProperty(key, value); return; } bufferedJarProps.addProperty(key, value); } /** * 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; } /** * Gets the classname of the initial MIDlet to run. * * @return classname of a MIDlet */ public String getInitialMIDletClassname() { if (initialMIDletClassName != null) { return initialMIDletClassName; } if (getNumberOfMIDlets() == 1) { return new MIDletInfo(getProperty("MIDlet-1")).classname; } // Have the user select a MIDlet. return "com.sun.midp.midlet.Selector"; } /** * Checks to see the suite has the ALLOW level for specific permission. * This is used for by internal APIs that only provide access to * trusted system applications. * * @param permission permission ID from com.sun.midp.security.Permissions * * @exception SecurityException if the suite is not ALLOWed the permission */ public void checkIfPermissionAllowed(int permission) { securityToken.checkIfPermissionAllowed(permission); } /** * Checks for permission and throw an exception if not allowed. * May block to ask the user a question. * * @param permission ID of the permission to check for, * the ID must be from * {@link com.sun.midp.security.Permissions} * @param resource string to insert into the permission question, * can be null * * @exception SecurityException if the permission is not * allowed by this token * @exception InterruptedException if another thread interrupts the * calling thread while this method is waiting to preempt the * display. */ public void checkForPermission(int permission, String resource) throws InterruptedException { checkForPermission(permission, getProperty(Installer.SUITE_NAME_PROP), resource); } /** * Checks for permission and throw an exception if not allowed. * May block to ask the user a question. * * @param permission ID of the permission to check for, * the ID must be from * {@link com.sun.midp.security.Permissions} * @param name name of the suite * @param resource string to insert into the question, can be null * * @exception SecurityException if the permission is not * allowed by this token * @exception InterruptedException if another thread interrupts the * calling thread while this method is waiting to preempt the * display. */ protected void checkForPermission(int permission, String name, String resource) throws InterruptedException { String protocolName = null; try { int colon = resource.indexOf(':'); if (colon != -1) { protocolName = resource.substring(0, colon); } } catch (Exception e) { // ignore } securityToken.checkForPermission(permission, Permissions.getTitle(permission), Permissions.getQuestion(permission), name, resource, protocolName); } /** * Gets the status of the specified permission. * If no API on the device defines the specific permission * requested then it must be reported as denied. * If the status of the permission is not known because it might * require a user interaction then it should be reported as unknown. * * @param permission to check if denied, allowed, or unknown * @return 0 if the permission is denied; 1 if the permission is allowed; * -1 if the status is unknown */ public int checkPermission(String permission) { return securityToken.checkPermission(permission); } /** * Gets the path root of any file this suite. * Has any needed file separators appended. * * @return storage path root */ public String getStorageRoot() { return storageRoot; } /** * Gets the unique name of vendor and suite. * * @return storage name */ public String getStorageName() { return storageName; } /** * 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(classSecurityToken, getStorageRoot() + Installer.JAR_FILENAME, name); } catch (IOException e) { return null; } } /** * Gets 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(classSecurityToken); RandomAccessStream stream = new RandomAccessStream(classSecurityToken); 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); try { storageUsed += stream.getSizeOf(); } finally { stream.disconnect(); } } catch (IOException ioe) { // just move on to the next file } } return storageUsed; } /** * Gets the URL that the suite was downloaded from. * * @return URL of the JAD, or JAR for a JAR only suite, never null, * even in development environments */ public String getDownloadUrl() { String url = getJadUrl(); if (url != null) { return url; } return getJarUrl(); } /** * Gets the name of CA that authorized this suite. * * @return name of a CA or null if the suite was not signed */ public String getCA() { return ca; } /** * Counts the number of MIDlets from its properties. * * @return number of midlet in the suite */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -