📄 launcher.java
字号:
sealed = attr.getValue(Name.SEALED); } if (sealed == null) { if ((attr = man.getMainAttributes()) != null) { sealed = attr.getValue(Name.SEALED); } } return "true".equalsIgnoreCase(sealed); } private byte[] getBytesPrivate(InputStream in, int len) throws IOException { byte[] b; try { if (len != -1) { // Read exactly len bytes from the input stream b = new byte[len]; while (len > 0) { int n = in.read(b, b.length - len, len); if (n == -1) { throw new IOException("unexpected EOF"); } len -= n; } } else { // Read until end of stream is reached b = new byte[1024]; int total = 0; while ((len = in.read(b, total, b.length - total)) != -1) { total += len; if (total >= b.length) { byte[] tmp = new byte[total * 2]; System.arraycopy(b, 0, tmp, 0, total); b = tmp; } } // Trim array to correct size, if necessary if (total != b.length) { byte[] tmp = new byte[total]; System.arraycopy(b, 0, tmp, 0, total); b = tmp; } } } finally { in.close(); } return b; } private void handlePackage(String name, Manifest man, URL url) throws IOException { int i = name.lastIndexOf('.'); if (i == -1) { return; } String pkgname = name.substring(0, i); if (name.startsWith("java.")) { throw new SecurityException("Prohibited package name: " + pkgname); } // Check if package already loaded. Package pkg = getPackage(pkgname); if (pkg != null) { // Package found, so check package sealing. boolean ok; if (pkg.isSealed()) { // Verify that code source URL is the same. ok = pkg.isSealed(url); } else { // Make sure we are not attempting to seal the package // at this code source URL. ok = (man == null) || !isSealedPrivate(pkgname, man); } if (!ok) { throw new SecurityException("sealing violation"); } } else { if (man != null) { definePackage(pkgname, man, url); } else { definePackage(pkgname, null, null, null, null, null, null, null); } } } private Class defineClassPrivate(String name, ClassContainer cc) throws IOException { URL url = cc.url; JarFile jfile = cc.jfile; Manifest man; if (jfile != null) { // Must be a ZIP/JAR man = jfile.getManifest(); } else { man = null; } // See if there are any package operations to do handlePackage(name, man, url); if (cc.clazz != null) { /* load superclasses in a way that avoid C recursion */ sun.misc.CVM.executeLoadSuperClasses(cc.clazz); return cc.clazz; } else { String path = cc.entryname; JarEntry entry = jfile.getJarEntry(path); InputStream in = jfile.getInputStream(entry); int len = (int)entry.getSize(); // Now get the class bytes and define the class byte[] b = getBytesPrivate(in, len); java.security.cert.Certificate[] certs = entry.getCertificates(); CodeSource cs = new CodeSource(url, certs); return defineClass(name, b, 0, b.length, cs); } } private Class doClassFind(final String name) throws ClassNotFoundException, java.security.PrivilegedActionException { return (Class) AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws ClassNotFoundException { ClassContainer cc = findContainer(name); if (cc != null) { try { return defineClassPrivate(name, cc); } catch (IOException e) { throw new ClassNotFoundException(name, e); } } else { throw new ClassNotFoundException(name); } } }); } /* * Check syntax of name passed to defineClass() or findClass methods * array syntax clasnames with /'s are not allowed */ private boolean checkName(String name) { if(name == null || name.length() == 0) return true; if (name.indexOf('/') != -1) return false; if (name.charAt(0) == '[') return false; return true; } /** * Override findClass */ protected Class findClass(final String name) throws ClassNotFoundException { if (!checkName(name)) throw new ClassNotFoundException("Illegal name: " + name); // If Extension Mechanism is supported // we use URLClassLoader's implementation. // Otherwise we use our own. if (hasExtension) { return super.findClass(name); } try { return doClassFind(name); } catch (java.security.PrivilegedActionException pae) { throw (ClassNotFoundException) pae.getException(); } } private native ClassContainer findContainer(String name) throws ClassNotFoundException; /** * allow any classes loaded from classpath to exit the VM. */ protected PermissionCollection getPermissions(CodeSource codesource) { PermissionCollection perms = super.getPermissions(codesource); perms.add(new RuntimePermission("exitVM")); return perms; } } // // Cache the bootstrap class path so it does not have to be recreated // from the sun.boot.class.path every time. // private static URLClassPath bootstrapClassPath = null; // Returns the URLClassPath that is used for finding system resources. public static URLClassPath getBootstrapClassPath() { if (bootstrapClassPath == null) { bootstrapClassPath = getBootstrapClassPath0(); } return bootstrapClassPath; } // Compute a URLClassPath representing the boot classpath private static URLClassPath getBootstrapClassPath0() { String prop = (String)AccessController.doPrivileged( new GetPropertyAction("sun.boot.class.path")); URL[] urls; if (prop != null) { final String path = prop; urls = (URL[])AccessController.doPrivileged( new PrivilegedAction() { public Object run() { return pathToURLs(getClassPath(path)); } } ); } else { urls = new URL[0]; } return new URLClassPath(urls, factory); } private static URL[] pathToURLs(File[] path) { URL[] urls = new URL[path.length]; for (int i = 0; i < path.length; i++) { urls[i] = getFileURL(path[i]); } // DEBUG //for (int i = 0; i < urls.length; i++) { // System.out.println("urls[" + i + "] = " + '"' + urls[i] + '"'); //} return urls; } private static File[] getClassPath(String cp) { File[] path; if (cp != null) { int count = 0, maxCount = 1; int pos = 0, lastPos = 0; // Count the number of separators first while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) { maxCount++; lastPos = pos + 1; } path = new File[maxCount]; lastPos = pos = 0; // Now scan for each path component while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) { if (pos - lastPos > 0) { path[count++] = new File(cp.substring(lastPos, pos)); } else { // empty path component translates to "." path[count++] = new File("."); } lastPos = pos + 1; } // Make sure we include the last path component if (lastPos < cp.length()) { path[count++] = new File(cp.substring(lastPos)); } else { path[count++] = new File("."); } // Trim array to correct size if (count != maxCount) { File[] tmp = new File[count]; System.arraycopy(path, 0, tmp, 0, count); path = tmp; } } else { path = new File[0]; } // DEBUG //for (int i = 0; i < path.length; i++) { // System.out.println("path[" + i + "] = " + '"' + path[i] + '"'); //} return path; } private static URLStreamHandler fileHandler; static URL getFileURL(File file) { try { file = file.getCanonicalFile(); } catch (IOException e) { } String path = file.getAbsolutePath(); path = ParseUtil.encodePath(path); if (File.separatorChar != '/') { path = path.replace(File.separatorChar, '/'); } if (!path.startsWith("/")) { path = "/" + path; } if (!path.endsWith("/") && file.isDirectory()) { path = path + "/"; } if (fileHandler == null) { fileHandler = factory.createURLStreamHandler("file"); } try { return new URL("file", "", -1, path, fileHandler); } catch (MalformedURLException e) { // Should never happen since we specify the protocol... throw new InternalError(); } } /* * The stream handler factory for loading system protocol handlers. */ private static class Factory implements URLStreamHandlerFactory { private static String PREFIX = "sun.net.www.protocol"; public URLStreamHandler createURLStreamHandler(String protocol) { String name = PREFIX + "." + protocol + ".Handler"; try { Class c = Class.forName(name); return (URLStreamHandler)c.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } throw new InternalError("could not load " + protocol + "system protocol handler"); } }}class PathPermissions extends PermissionCollection { // use serialVersionUID from JDK 1.2.2 for interoperability private static final long serialVersionUID = 8133287259134945693L; private File path[]; private Permissions perms; URL codeBase; PathPermissions(File path[]) { this.path = path; this.perms = null; this.codeBase = null; } URL getCodeBase() { return codeBase; } public void add(java.security.Permission permission) { throw new SecurityException("attempt to add a permission"); } private synchronized void init() { if (perms != null) return; perms = new Permissions(); // this is needed to be able to create the classloader itself! perms.add(new RuntimePermission("createClassLoader")); // add permission to read any "java.*" property perms.add(new java.util.PropertyPermission("java.*","read")); AccessController.doPrivileged(new PrivilegedAction() { public Object run() { for (int i=0; i < path.length; i++) { File f = path[i]; String path; try { path = f.getCanonicalPath(); } catch (IOException ioe) { path = f.getAbsolutePath(); } if (i == 0) { codeBase = Launcher.getFileURL(new File(path)); } if (f.isDirectory()) { if (path.endsWith(File.separator)) { perms.add(new FilePermission(path+"-", "read")); } else { perms.add(new FilePermission(path + File.separator+"-", "read")); } } else { int endIndex = path.lastIndexOf(File.separatorChar); if (endIndex != -1) { path = path.substring(0, endIndex+1) + "-"; perms.add(new FilePermission(path, "read")); } else { // ? } } } return null; } }); } public boolean implies(java.security.Permission permission) { if (perms == null) init(); return perms.implies(permission); } public java.util.Enumeration elements() { if (perms == null) init(); return perms.elements(); } public String toString() { if (perms == null) init(); return perms.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -