midpconfig.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 479 行 · 第 1/2 页
JAVA
479 行
} /* * Set up the permissions that will be granted to MIDlet code proper. * Currently this is very little: only the ability to modify the properties * of a Thread. And this is very limited by API hiding so is not very dangerous. * We absolutely do not give them vmExit, which is explicitly prohibited. * This set of permissions is read-only and shared by all MIDletClassLoaders. * * Property access cannot be dealt with using Java permissions, as we * want to make properties disappear, and permissions will throw an Exception * to prohibit seeing the property. * * This depends on being run in the following environment: * security enabled, but all permissions granted to main program. * This is achieved but putting -Djava.security.manager on * the command line, and having my own .java.policy file * that looks like this: * grant codeBase "file:*" { * permission java.security.AllPermission; * }; */ static PermissionCollection newMidletPermissions(){ PermissionCollection mp = new Permissions(); mp.add(new java.lang.RuntimePermission("modifyThread")); mp.add(new java.util.PropertyPermission("*", "read")); mp.setReadOnly(); return mp; } static PermissionCollection midletPermissions = newMidletPermissions(); /* * Set up a new MIDletClassLoader * There should probably be one of these per MIDlet suite. * This would allow sharing between suite members, including data. */ static String[] split(String path, char separator){ int nComponents = 1; //char separator = ' '; String components[]; int length = path.length(); int start; int componentIndex; for (int i=0; i<length; i++){ if (path.charAt(i) == separator) nComponents += 1; } components = new String[nComponents]; start = 0; componentIndex = 0; /* could optimize here for the common case of nComponents == 1 */ for (int i=0; i<length; i++){ if (path.charAt(i) == separator){ components[componentIndex] = path.substring(start, i); componentIndex += 1; start = i+1; } } /* and the last components is delimited by end of String */ components[componentIndex] = path.substring(start, length); return components; } /* * This version allows the caller to specify a set of permissions. * This is less useful than the usual version, which grants the * permissions we plan on granting to MIDlets. * * The 'enableFilter' argument specifies that if the API hiding * filter is being enabled. If the filter is enabled, midlet * can only access CLDC/MIDP classes. If the filter is disabled, * midlet can access all classes on the bootclasspath, including * all the CDC classes. * * The 'auxClassLoader' is a helper classloader used when the * MIDletClassLoader and its parents fail to load the requested * class. */ private static MIDletClassLoader newMIDletClassLoader( String midpPath[], MemberFilter mf, PermissionCollection perms, MIDPImplementationClassLoader implClassLdr, boolean enableFilter, ClassLoader auxClassLoader) { if (midpImplCL == null) { throw new InternalError( "Need to create the parent MIDPImplementationClassLoader first"); } URL midJarURL[]; int nComponents = midpPath.length; midJarURL = new URL[nComponents]; try { for (int i=0; i<nComponents; i++){ midJarURL[i] = new File(midpPath[i]).toURI().toURL(); } }catch(Exception e){ System.err.println("URL Creation:"); e.printStackTrace(); return null; } //DEBUG System.out.println( // "Constructing MIDletClassLoader with permissions "+perms); MIDletClassLoader midletCL = new MIDletClassLoader( midJarURL, systemPackages, perms, mf, implClassLdr, enableFilter, auxClassLoader); return midletCL; } /* * This version allows the caller to specify a set of permissions. * The parent classloader is the MIDPImplementationClassLoader. * The API hiding filter is enabled. */ public static MIDletClassLoader newMIDletClassLoader( String midpPath[], PermissionCollection perms) { return newMIDletClassLoader(midpPath, memberFilter, perms, midpImplCL, true, null); } /* * Use the default midlet permission collection. The parent classloader * is the MIDPImplementationClassLoader. The API hiding filter is * enabled. */ public static MIDletClassLoader newMIDletClassLoader(String midpPath[]) { return newMIDletClassLoader(midpPath, memberFilter, midletPermissions, midpImplCL, true, null); } /* * The 'enableFilter' argument specifies that if the API hiding * filter is being enabled. If the filter is enabled, midlet * can only access CLDC/MIDP classes. If the filter is disabled, * midlet can access all classes on the bootclasspath, including * all the CDC classes. * * The 'auxClassLoader' is a helper classloader used when the * MIDletClassLoader and its parents fail to load the requested * class. */ public static MIDletClassLoader newMIDletClassLoader(String midpPath[], boolean enableFilter, ClassLoader auxClassLoader) { return newMIDletClassLoader(midpPath, memberFilter, midletPermissions, midpImplCL, enableFilter, auxClassLoader); } /* * Get the MIDletClassLoader instance that loads the caller * midlet class. */ public static MIDletClassLoader getMIDletClassLoader() { int i = 1; /* skip the direct caller, who must be system code */ ClassLoader loader = null; Class cl = CVM.getCallerClass(i); while (cl != null) { loader = cl.getClassLoader(); if (loader instanceof MIDletClassLoader) { return (MIDletClassLoader)loader; } cl = CVM.getCallerClass(++i); } return null; } /* * A utility method used to load resources using the caller's * classloaders. This is useful when application and system * code are loaded by different classloaders. */ public static InputStream getResourceAsStream(String name){ int i = 1; /* skip tha caller, which must be system code */ InputStream is = null; ClassLoader lastFailedLoader = null; /* This is a bit slow since we need to walk up the stack. * Because we don't know which classloader to load the resource, * so we have to do it the hard way. */ while (is == null) { Class cl = sun.misc.CVM.getCallerClass(i); if (cl == null) { /* reach the top of the stack */ break; } ClassLoader loader = cl.getClassLoader(); if (i == 1 || loader != lastFailedLoader) { is = cl.getResourceAsStream(name); if (is != null) { break; } else { lastFailedLoader = loader; } } i++; /* the next caller */ } return is; } /* Check if the class is allowed for midlet. */ public static boolean isClassAllowed(String classname) { return allowedClasses.contains(classname); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?