0N/A/*
3909N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.misc;
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport java.io.FilePermission;
0N/Aimport java.net.URL;
0N/Aimport java.net.URLClassLoader;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.net.URLStreamHandler;
0N/Aimport java.net.URLStreamHandlerFactory;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.StringTokenizer;
0N/Aimport java.util.Set;
0N/Aimport java.util.Vector;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.PrivilegedExceptionAction;
0N/Aimport java.security.AccessControlContext;
0N/Aimport java.security.PermissionCollection;
0N/Aimport java.security.Permissions;
0N/Aimport java.security.Permission;
0N/Aimport java.security.ProtectionDomain;
0N/Aimport java.security.CodeSource;
0N/Aimport sun.security.util.SecurityConstants;
0N/Aimport sun.net.www.ParseUtil;
0N/A
0N/A/**
0N/A * This class is used by the system to launch the main application.
0N/ALauncher */
0N/Apublic class Launcher {
0N/A private static URLStreamHandlerFactory factory = new Factory();
0N/A private static Launcher launcher = new Launcher();
2916N/A private static String bootClassPath =
2916N/A System.getProperty("sun.boot.class.path");
0N/A
0N/A public static Launcher getLauncher() {
0N/A return launcher;
0N/A }
0N/A
0N/A private ClassLoader loader;
0N/A
0N/A public Launcher() {
0N/A // Create the extension class loader
0N/A ClassLoader extcl;
0N/A try {
0N/A extcl = ExtClassLoader.getExtClassLoader();
0N/A } catch (IOException e) {
0N/A throw new InternalError(
0N/A "Could not create extension class loader");
0N/A }
0N/A
0N/A // Now create the class loader to use to launch the application
0N/A try {
0N/A loader = AppClassLoader.getAppClassLoader(extcl);
0N/A } catch (IOException e) {
0N/A throw new InternalError(
0N/A "Could not create application class loader");
0N/A }
0N/A
0N/A // Also set the context class loader for the primordial thread.
0N/A Thread.currentThread().setContextClassLoader(loader);
0N/A
0N/A // Finally, install a security manager if requested
0N/A String s = System.getProperty("java.security.manager");
0N/A if (s != null) {
0N/A SecurityManager sm = null;
0N/A if ("".equals(s) || "default".equals(s)) {
0N/A sm = new java.lang.SecurityManager();
0N/A } else {
0N/A try {
0N/A sm = (SecurityManager)loader.loadClass(s).newInstance();
0N/A } catch (IllegalAccessException e) {
0N/A } catch (InstantiationException e) {
0N/A } catch (ClassNotFoundException e) {
0N/A } catch (ClassCastException e) {
0N/A }
0N/A }
0N/A if (sm != null) {
0N/A System.setSecurityManager(sm);
0N/A } else {
0N/A throw new InternalError(
0N/A "Could not create SecurityManager: " + s);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Returns the class loader used to launch the main application.
0N/A */
0N/A public ClassLoader getClassLoader() {
0N/A return loader;
0N/A }
0N/A
0N/A /*
0N/A * The class loader used for loading installed extensions.
0N/A */
0N/A static class ExtClassLoader extends URLClassLoader {
1042N/A
1042N/A static {
1042N/A ClassLoader.registerAsParallelCapable();
1042N/A }
0N/A
0N/A /**
0N/A * create an ExtClassLoader. The ExtClassLoader is created
0N/A * within a context that limits which files it can read
0N/A */
0N/A public static ExtClassLoader getExtClassLoader() throws IOException
0N/A {
0N/A final File[] dirs = getExtDirs();
0N/A
0N/A try {
0N/A // Prior implementations of this doPrivileged() block supplied
0N/A // aa synthesized ACC via a call to the private method
0N/A // ExtClassLoader.getContext().
0N/A
28N/A return AccessController.doPrivileged(
28N/A new PrivilegedExceptionAction<ExtClassLoader>() {
28N/A public ExtClassLoader run() throws IOException {
0N/A int len = dirs.length;
0N/A for (int i = 0; i < len; i++) {
0N/A MetaIndex.registerDirectory(dirs[i]);
0N/A }
0N/A return new ExtClassLoader(dirs);
0N/A }
0N/A });
0N/A } catch (java.security.PrivilegedActionException e) {
1042N/A throw (IOException) e.getException();
0N/A }
0N/A }
0N/A
0N/A void addExtURL(URL url) {
1042N/A super.addURL(url);
0N/A }
0N/A
0N/A /*
0N/A * Creates a new ExtClassLoader for the specified directories.
0N/A */
0N/A public ExtClassLoader(File[] dirs) throws IOException {
0N/A super(getExtURLs(dirs), null, factory);
0N/A }
0N/A
0N/A private static File[] getExtDirs() {
0N/A String s = System.getProperty("java.ext.dirs");
0N/A File[] dirs;
0N/A if (s != null) {
0N/A StringTokenizer st =
0N/A new StringTokenizer(s, File.pathSeparator);
0N/A int count = st.countTokens();
0N/A dirs = new File[count];
0N/A for (int i = 0; i < count; i++) {
0N/A dirs[i] = new File(st.nextToken());
0N/A }
0N/A } else {
0N/A dirs = new File[0];
0N/A }
0N/A return dirs;
0N/A }
0N/A
0N/A private static URL[] getExtURLs(File[] dirs) throws IOException {
28N/A Vector<URL> urls = new Vector<URL>();
0N/A for (int i = 0; i < dirs.length; i++) {
0N/A String[] files = dirs[i].list();
0N/A if (files != null) {
0N/A for (int j = 0; j < files.length; j++) {
0N/A if (!files[j].equals("meta-index")) {
0N/A File f = new File(dirs[i], files[j]);
0N/A urls.add(getFileURL(f));
0N/A }
0N/A }
0N/A }
0N/A }
0N/A URL[] ua = new URL[urls.size()];
0N/A urls.copyInto(ua);
0N/A return ua;
0N/A }
0N/A
0N/A /*
0N/A * Searches the installed extension directories for the specified
0N/A * library name. For each extension directory, we first look for
0N/A * the native library in the subdirectory whose name is the value
0N/A * of the system property <code>os.arch</code>. Failing that, we
0N/A * look in the extension directory itself.
0N/A */
0N/A public String findLibrary(String name) {
0N/A name = System.mapLibraryName(name);
1042N/A URL[] urls = super.getURLs();
1042N/A File prevDir = null;
1042N/A for (int i = 0; i < urls.length; i++) {
1042N/A // Get the ext directory from the URL
1042N/A File dir = new File(urls[i].getPath()).getParentFile();
1042N/A if (dir != null && !dir.equals(prevDir)) {
1042N/A // Look in architecture-specific subdirectory first
2916N/A // Read from the saved system properties to avoid deadlock
2916N/A String arch = VM.getSavedProperty("os.arch");
1042N/A if (arch != null) {
1042N/A File file = new File(new File(dir, arch), name);
1042N/A if (file.exists()) {
1042N/A return file.getAbsolutePath();
1042N/A }
1042N/A }
1042N/A // Then check the extension directory
1042N/A File file = new File(dir, name);
0N/A if (file.exists()) {
0N/A return file.getAbsolutePath();
0N/A }
0N/A }
1042N/A prevDir = dir;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A private static AccessControlContext getContext(File[] dirs)
0N/A throws IOException
0N/A {
0N/A PathPermissions perms =
0N/A new PathPermissions(dirs);
0N/A
0N/A ProtectionDomain domain = new ProtectionDomain(
0N/A new CodeSource(perms.getCodeBase(),
0N/A (java.security.cert.Certificate[]) null),
0N/A perms);
0N/A
0N/A AccessControlContext acc =
0N/A new AccessControlContext(new ProtectionDomain[] { domain });
0N/A
0N/A return acc;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The class loader used for loading from java.class.path.
0N/A * runs in a restricted security context.
0N/A */
0N/A static class AppClassLoader extends URLClassLoader {
0N/A
1042N/A static {
1042N/A ClassLoader.registerAsParallelCapable();
1042N/A }
1042N/A
0N/A public static ClassLoader getAppClassLoader(final ClassLoader extcl)
0N/A throws IOException
0N/A {
0N/A final String s = System.getProperty("java.class.path");
0N/A final File[] path = (s == null) ? new File[0] : getClassPath(s);
0N/A
0N/A // Note: on bugid 4256530
0N/A // Prior implementations of this doPrivileged() block supplied
0N/A // a rather restrictive ACC via a call to the private method
0N/A // AppClassLoader.getContext(). This proved overly restrictive
0N/A // when loading classes. Specifically it prevent
0N/A // accessClassInPackage.sun.* grants from being honored.
0N/A //
28N/A return AccessController.doPrivileged(
28N/A new PrivilegedAction<AppClassLoader>() {
28N/A public AppClassLoader run() {
0N/A URL[] urls =
0N/A (s == null) ? new URL[0] : pathToURLs(path);
0N/A return new AppClassLoader(urls, extcl);
0N/A }
0N/A });
0N/A }
0N/A
0N/A /*
0N/A * Creates a new AppClassLoader
0N/A */
0N/A AppClassLoader(URL[] urls, ClassLoader parent) {
0N/A super(urls, parent, factory);
0N/A }
0N/A
0N/A /**
0N/A * Override loadClass so we can checkPackageAccess.
0N/A */
1042N/A public Class loadClass(String name, boolean resolve)
0N/A throws ClassNotFoundException
0N/A {
0N/A int i = name.lastIndexOf('.');
0N/A if (i != -1) {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkPackageAccess(name.substring(0, i));
0N/A }
0N/A }
0N/A return (super.loadClass(name, resolve));
0N/A }
0N/A
0N/A /**
0N/A * allow any classes loaded from classpath to exit the VM.
0N/A */
0N/A protected PermissionCollection getPermissions(CodeSource codesource)
0N/A {
0N/A PermissionCollection perms = super.getPermissions(codesource);
0N/A perms.add(new RuntimePermission("exitVM"));
0N/A return perms;
0N/A }
0N/A
0N/A /**
0N/A * This class loader supports dynamic additions to the class path
0N/A * at runtime.
0N/A *
0N/A * @see java.lang.instrument.Instrumentation#appendToSystemClassPathSearch
0N/A */
0N/A private void appendToClassPathForInstrumentation(String path) {
0N/A assert(Thread.holdsLock(this));
0N/A
0N/A // addURL is a no-op if path already contains the URL
0N/A super.addURL( getFileURL(new File(path)) );
0N/A }
0N/A
0N/A /**
0N/A * create a context that can read any directories (recursively)
0N/A * mentioned in the class path. In the case of a jar, it has to
0N/A * be the directory containing the jar, not just the jar, as jar
0N/A * files might refer to other jar files.
0N/A */
0N/A
0N/A private static AccessControlContext getContext(File[] cp)
0N/A throws java.net.MalformedURLException
0N/A {
0N/A PathPermissions perms =
0N/A new PathPermissions(cp);
0N/A
0N/A ProtectionDomain domain =
0N/A new ProtectionDomain(new CodeSource(perms.getCodeBase(),
0N/A (java.security.cert.Certificate[]) null),
0N/A perms);
0N/A
0N/A AccessControlContext acc =
0N/A new AccessControlContext(new ProtectionDomain[] { domain });
0N/A
0N/A return acc;
0N/A }
0N/A }
0N/A
2916N/A private static class BootClassPathHolder {
2916N/A static final URLClassPath bcp;
2916N/A static {
1365N/A URL[] urls;
2916N/A if (bootClassPath != null) {
1365N/A urls = AccessController.doPrivileged(
1365N/A new PrivilegedAction<URL[]>() {
1365N/A public URL[] run() {
2916N/A File[] classPath = getClassPath(bootClassPath);
1365N/A int len = classPath.length;
1365N/A Set<File> seenDirs = new HashSet<File>();
1365N/A for (int i = 0; i < len; i++) {
1365N/A File curEntry = classPath[i];
1365N/A // Negative test used to properly handle
1365N/A // nonexistent jars on boot class path
1365N/A if (!curEntry.isDirectory()) {
1365N/A curEntry = curEntry.getParentFile();
1365N/A }
1365N/A if (curEntry != null && seenDirs.add(curEntry)) {
1365N/A MetaIndex.registerDirectory(curEntry);
1365N/A }
0N/A }
1365N/A return pathToURLs(classPath);
0N/A }
0N/A }
1365N/A );
1365N/A } else {
1365N/A urls = new URL[0];
1365N/A }
2916N/A bcp = new URLClassPath(urls, factory);
0N/A }
1365N/A }
1365N/A
2916N/A public static URLClassPath getBootstrapClassPath() {
3655N/A return BootClassPathHolder.bcp;
0N/A }
0N/A
0N/A private static URL[] pathToURLs(File[] path) {
0N/A URL[] urls = new URL[path.length];
0N/A for (int i = 0; i < path.length; i++) {
0N/A urls[i] = getFileURL(path[i]);
0N/A }
0N/A // DEBUG
0N/A //for (int i = 0; i < urls.length; i++) {
0N/A // System.out.println("urls[" + i + "] = " + '"' + urls[i] + '"');
0N/A //}
0N/A return urls;
0N/A }
0N/A
0N/A private static File[] getClassPath(String cp) {
0N/A File[] path;
0N/A if (cp != null) {
0N/A int count = 0, maxCount = 1;
0N/A int pos = 0, lastPos = 0;
0N/A // Count the number of separators first
0N/A while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) {
0N/A maxCount++;
0N/A lastPos = pos + 1;
0N/A }
0N/A path = new File[maxCount];
0N/A lastPos = pos = 0;
0N/A // Now scan for each path component
0N/A while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) {
0N/A if (pos - lastPos > 0) {
0N/A path[count++] = new File(cp.substring(lastPos, pos));
0N/A } else {
0N/A // empty path component translates to "."
0N/A path[count++] = new File(".");
0N/A }
0N/A lastPos = pos + 1;
0N/A }
0N/A // Make sure we include the last path component
0N/A if (lastPos < cp.length()) {
0N/A path[count++] = new File(cp.substring(lastPos));
0N/A } else {
0N/A path[count++] = new File(".");
0N/A }
0N/A // Trim array to correct size
0N/A if (count != maxCount) {
0N/A File[] tmp = new File[count];
0N/A System.arraycopy(path, 0, tmp, 0, count);
0N/A path = tmp;
0N/A }
0N/A } else {
0N/A path = new File[0];
0N/A }
0N/A // DEBUG
0N/A //for (int i = 0; i < path.length; i++) {
0N/A // System.out.println("path[" + i + "] = " + '"' + path[i] + '"');
0N/A //}
0N/A return path;
0N/A }
0N/A
0N/A private static URLStreamHandler fileHandler;
0N/A
0N/A static URL getFileURL(File file) {
0N/A try {
0N/A file = file.getCanonicalFile();
0N/A } catch (IOException e) {}
0N/A
0N/A try {
0N/A return ParseUtil.fileToEncodedURL(file);
0N/A } catch (MalformedURLException e) {
0N/A // Should never happen since we specify the protocol...
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * The stream handler factory for loading system protocol handlers.
0N/A */
0N/A private static class Factory implements URLStreamHandlerFactory {
0N/A private static String PREFIX = "sun.net.www.protocol";
0N/A
0N/A public URLStreamHandler createURLStreamHandler(String protocol) {
0N/A String name = PREFIX + "." + protocol + ".Handler";
0N/A try {
0N/A Class c = Class.forName(name);
0N/A return (URLStreamHandler)c.newInstance();
0N/A } catch (ClassNotFoundException e) {
0N/A e.printStackTrace();
0N/A } catch (InstantiationException e) {
0N/A e.printStackTrace();
0N/A } catch (IllegalAccessException e) {
0N/A e.printStackTrace();
0N/A }
0N/A throw new InternalError("could not load " + protocol +
0N/A "system protocol handler");
0N/A }
0N/A }
0N/A}
0N/A
0N/Aclass PathPermissions extends PermissionCollection {
0N/A // use serialVersionUID from JDK 1.2.2 for interoperability
0N/A private static final long serialVersionUID = 8133287259134945693L;
0N/A
0N/A private File path[];
0N/A private Permissions perms;
0N/A
0N/A URL codeBase;
0N/A
0N/A PathPermissions(File path[])
0N/A {
0N/A this.path = path;
0N/A this.perms = null;
0N/A this.codeBase = null;
0N/A }
0N/A
0N/A URL getCodeBase()
0N/A {
0N/A return codeBase;
0N/A }
0N/A
0N/A public void add(java.security.Permission permission) {
0N/A throw new SecurityException("attempt to add a permission");
0N/A }
0N/A
0N/A private synchronized void init()
0N/A {
0N/A if (perms != null)
0N/A return;
0N/A
0N/A perms = new Permissions();
0N/A
0N/A // this is needed to be able to create the classloader itself!
0N/A perms.add(SecurityConstants.CREATE_CLASSLOADER_PERMISSION);
0N/A
0N/A // add permission to read any "java.*" property
0N/A perms.add(new java.util.PropertyPermission("java.*",
0N/A SecurityConstants.PROPERTY_READ_ACTION));
0N/A
28N/A AccessController.doPrivileged(new PrivilegedAction<Void>() {
28N/A public Void run() {
0N/A for (int i=0; i < path.length; i++) {
0N/A File f = path[i];
0N/A String path;
0N/A try {
0N/A path = f.getCanonicalPath();
0N/A } catch (IOException ioe) {
0N/A path = f.getAbsolutePath();
0N/A }
0N/A if (i == 0) {
0N/A codeBase = Launcher.getFileURL(new File(path));
0N/A }
0N/A if (f.isDirectory()) {
0N/A if (path.endsWith(File.separator)) {
0N/A perms.add(new FilePermission(path+"-",
0N/A SecurityConstants.FILE_READ_ACTION));
0N/A } else {
0N/A perms.add(new FilePermission(
0N/A path + File.separator+"-",
0N/A SecurityConstants.FILE_READ_ACTION));
0N/A }
0N/A } else {
0N/A int endIndex = path.lastIndexOf(File.separatorChar);
0N/A if (endIndex != -1) {
0N/A path = path.substring(0, endIndex+1) + "-";
0N/A perms.add(new FilePermission(path,
0N/A SecurityConstants.FILE_READ_ACTION));
0N/A } else {
0N/A // XXX?
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A });
0N/A }
0N/A
0N/A public boolean implies(java.security.Permission permission) {
0N/A if (perms == null)
0N/A init();
0N/A return perms.implies(permission);
0N/A }
0N/A
28N/A public java.util.Enumeration<Permission> elements() {
0N/A if (perms == null)
0N/A init();
0N/A synchronized (perms) {
0N/A return perms.elements();
0N/A }
0N/A }
0N/A
0N/A public String toString() {
0N/A if (perms == null)
0N/A init();
0N/A return perms.toString();
0N/A }
0N/A}