src/share/classes/sun/misc/Launcher.java

Print this page




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.FilePermission;
  31 import java.net.URL;
  32 import java.net.URLClassLoader;
  33 import java.net.MalformedURLException;
  34 import java.net.URLStreamHandler;
  35 import java.net.URLStreamHandlerFactory;
  36 import java.util.HashSet;
  37 import java.util.StringTokenizer;
  38 import java.util.Set;
  39 import java.util.Vector;
  40 import java.security.AccessController;
  41 import java.security.AllPermission;
  42 import java.security.PrivilegedAction;
  43 import java.security.PrivilegedExceptionAction;
  44 import java.security.AccessControlContext;
  45 import java.security.PermissionCollection;
  46 import java.security.Permissions;
  47 import java.security.Permission;
  48 import java.security.ProtectionDomain;
  49 import java.security.CodeSource;
  50 import sun.security.util.SecurityConstants;
  51 import sun.net.www.ParseUtil;
  52 
  53 /**
  54  * This class is used by the system to launch the main application.
  55 Launcher */
  56 public class Launcher {
  57     private static URLStreamHandlerFactory factory = new Factory();
  58     private static Launcher launcher = new Launcher();
  59     private static String bootClassPath =
  60         System.getProperty("sun.boot.class.path");
  61 


 100                 } catch (ClassNotFoundException e) {
 101                 } catch (ClassCastException e) {
 102                 }
 103             }
 104             if (sm != null) {
 105                 System.setSecurityManager(sm);
 106             } else {
 107                 throw new InternalError(
 108                     "Could not create SecurityManager: " + s);
 109             }
 110         }
 111     }
 112 
 113     /*
 114      * Returns the class loader used to launch the main application.
 115      */
 116     public ClassLoader getClassLoader() {
 117         return loader;
 118     }
 119 
 120     public static void addURLToAppClassLoader(URL u) {
 121         AccessController.checkPermission(new AllPermission());
 122         ClassLoader loader = Launcher.getLauncher().getClassLoader();
 123         ((Launcher.AppClassLoader) loader).addAppURL(u);
 124     }
 125 
 126     public static void addURLToExtClassLoader(URL u) {
 127         AccessController.checkPermission(new AllPermission());
 128         ClassLoader loader = Launcher.getLauncher().getClassLoader();
 129         ((Launcher.ExtClassLoader) loader.getParent()).addExtURL(u);
 130     }
 131 
 132     /*
 133      * The class loader used for loading installed extensions.
 134      */
 135     static class ExtClassLoader extends URLClassLoader {
 136 
 137         static {
 138             ClassLoader.registerAsParallelCapable();
 139         }
 140 
 141         /**
 142          * create an ExtClassLoader. The ExtClassLoader is created
 143          * within a context that limits which files it can read
 144          */
 145         public static ExtClassLoader getExtClassLoader() throws IOException
 146         {
 147             final File[] dirs = getExtDirs();
 148 
 149             try {
 150                 // Prior implementations of this doPrivileged() block supplied
 151                 // aa synthesized ACC via a call to the private method


 230                     // Look in architecture-specific subdirectory first
 231                     // Read from the saved system properties to avoid deadlock
 232                     String arch = VM.getSavedProperty("os.arch");
 233                     if (arch != null) {
 234                         File file = new File(new File(dir, arch), name);
 235                         if (file.exists()) {
 236                             return file.getAbsolutePath();
 237                         }
 238                     }
 239                     // Then check the extension directory
 240                     File file = new File(dir, name);
 241                     if (file.exists()) {
 242                         return file.getAbsolutePath();
 243                     }
 244                 }
 245                 prevDir = dir;
 246             }
 247             return null;
 248         }
 249 
 250         protected Class findClass(String name) throws ClassNotFoundException {
 251             BootClassLoaderHook.preLoadClass(name);
 252             return super.findClass(name);
 253         }
 254 
 255         private static AccessControlContext getContext(File[] dirs)
 256             throws IOException
 257         {
 258             PathPermissions perms =
 259                 new PathPermissions(dirs);
 260 
 261             ProtectionDomain domain = new ProtectionDomain(
 262                 new CodeSource(perms.getCodeBase(),
 263                     (java.security.cert.Certificate[]) null),
 264                 perms);
 265 
 266             AccessControlContext acc =
 267                 new AccessControlContext(new ProtectionDomain[] { domain });
 268 
 269             return acc;
 270         }
 271     }
 272 
 273     /**
 274      * The class loader used for loading from java.class.path.


 299                     URL[] urls =
 300                         (s == null) ? new URL[0] : pathToURLs(path);
 301                     return new AppClassLoader(urls, extcl);
 302                 }
 303             });
 304         }
 305 
 306         /*
 307          * Creates a new AppClassLoader
 308          */
 309         AppClassLoader(URL[] urls, ClassLoader parent) {
 310             super(urls, parent, factory);
 311         }
 312 
 313         /**
 314          * Override loadClass so we can checkPackageAccess.
 315          */
 316         public Class loadClass(String name, boolean resolve)
 317             throws ClassNotFoundException
 318         {
 319             BootClassLoaderHook.preLoadClass(name);
 320             int i = name.lastIndexOf('.');
 321             if (i != -1) {
 322                 SecurityManager sm = System.getSecurityManager();
 323                 if (sm != null) {
 324                     sm.checkPackageAccess(name.substring(0, i));
 325                 }
 326             }
 327             return (super.loadClass(name, resolve));
 328         }
 329 
 330         /**
 331          * allow any classes loaded from classpath to exit the VM.
 332          */
 333         protected PermissionCollection getPermissions(CodeSource codesource)
 334         {
 335             PermissionCollection perms = super.getPermissions(codesource);
 336             perms.add(new RuntimePermission("exitVM"));
 337             return perms;
 338         }
 339 


 356          * be the directory containing the jar, not just the jar, as jar
 357          * files might refer to other jar files.
 358          */
 359 
 360         private static AccessControlContext getContext(File[] cp)
 361             throws java.net.MalformedURLException
 362         {
 363             PathPermissions perms =
 364                 new PathPermissions(cp);
 365 
 366             ProtectionDomain domain =
 367                 new ProtectionDomain(new CodeSource(perms.getCodeBase(),
 368                     (java.security.cert.Certificate[]) null),
 369                 perms);
 370 
 371             AccessControlContext acc =
 372                 new AccessControlContext(new ProtectionDomain[] { domain });
 373 
 374             return acc;
 375         }
 376 
 377         void addAppURL(URL url) {
 378             super.addURL(url);
 379         }
 380     }
 381 
 382     private static class BootClassPathHolder {
 383         static final URLClassPath bcp;
 384         static {
 385             URL[] urls;
 386             if (bootClassPath != null) {
 387                 urls = AccessController.doPrivileged(
 388                     new PrivilegedAction<URL[]>() {
 389                         public URL[] run() {
 390                             File[] classPath = getClassPath(bootClassPath);
 391                             int len = classPath.length;
 392                             Set<File> seenDirs = new HashSet<File>();
 393                             for (int i = 0; i < len; i++) {
 394                                 File curEntry = classPath[i];
 395                                 // Negative test used to properly handle
 396                                 // nonexistent jars on boot class path
 397                                 if (!curEntry.isDirectory()) {
 398                                     curEntry = curEntry.getParentFile();
 399                                 }
 400                                 if (curEntry != null && seenDirs.add(curEntry)) {
 401                                     MetaIndex.registerDirectory(curEntry);
 402                                 }
 403                             }
 404                             return pathToURLs(classPath);
 405                         }
 406                     }
 407                 );
 408             } else {
 409                 urls = new URL[0];
 410             }
 411             bcp = new URLClassPath(urls, factory);
 412         }
 413     }
 414 
 415     public static URLClassPath getBootstrapClassPath() {
 416         URLClassPath bcp = BootClassPathHolder.bcp;
 417         // if DownloadManager is installed, return the bootstrap class path
 418         // maintained by the Java kernel
 419         BootClassLoaderHook hook = BootClassLoaderHook.getHook();
 420         return hook == null ? bcp : hook.getBootstrapClassPath(bcp, factory);
 421     }
 422 
 423     private static URL[] pathToURLs(File[] path) {
 424         URL[] urls = new URL[path.length];
 425         for (int i = 0; i < path.length; i++) {
 426             urls[i] = getFileURL(path[i]);
 427         }
 428         // DEBUG
 429         //for (int i = 0; i < urls.length; i++) {
 430         //  System.out.println("urls[" + i + "] = " + '"' + urls[i] + '"');
 431         //}
 432         return urls;
 433     }
 434 
 435     private static File[] getClassPath(String cp) {
 436         File[] path;
 437         if (cp != null) {
 438             int count = 0, maxCount = 1;
 439             int pos = 0, lastPos = 0;
 440             // Count the number of separators first




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.FilePermission;
  31 import java.net.URL;
  32 import java.net.URLClassLoader;
  33 import java.net.MalformedURLException;
  34 import java.net.URLStreamHandler;
  35 import java.net.URLStreamHandlerFactory;
  36 import java.util.HashSet;
  37 import java.util.StringTokenizer;
  38 import java.util.Set;
  39 import java.util.Vector;
  40 import java.security.AccessController;

  41 import java.security.PrivilegedAction;
  42 import java.security.PrivilegedExceptionAction;
  43 import java.security.AccessControlContext;
  44 import java.security.PermissionCollection;
  45 import java.security.Permissions;
  46 import java.security.Permission;
  47 import java.security.ProtectionDomain;
  48 import java.security.CodeSource;
  49 import sun.security.util.SecurityConstants;
  50 import sun.net.www.ParseUtil;
  51 
  52 /**
  53  * This class is used by the system to launch the main application.
  54 Launcher */
  55 public class Launcher {
  56     private static URLStreamHandlerFactory factory = new Factory();
  57     private static Launcher launcher = new Launcher();
  58     private static String bootClassPath =
  59         System.getProperty("sun.boot.class.path");
  60 


  99                 } catch (ClassNotFoundException e) {
 100                 } catch (ClassCastException e) {
 101                 }
 102             }
 103             if (sm != null) {
 104                 System.setSecurityManager(sm);
 105             } else {
 106                 throw new InternalError(
 107                     "Could not create SecurityManager: " + s);
 108             }
 109         }
 110     }
 111 
 112     /*
 113      * Returns the class loader used to launch the main application.
 114      */
 115     public ClassLoader getClassLoader() {
 116         return loader;
 117     }
 118 












 119     /*
 120      * The class loader used for loading installed extensions.
 121      */
 122     static class ExtClassLoader extends URLClassLoader {
 123 
 124         static {
 125             ClassLoader.registerAsParallelCapable();
 126         }
 127 
 128         /**
 129          * create an ExtClassLoader. The ExtClassLoader is created
 130          * within a context that limits which files it can read
 131          */
 132         public static ExtClassLoader getExtClassLoader() throws IOException
 133         {
 134             final File[] dirs = getExtDirs();
 135 
 136             try {
 137                 // Prior implementations of this doPrivileged() block supplied
 138                 // aa synthesized ACC via a call to the private method


 217                     // Look in architecture-specific subdirectory first
 218                     // Read from the saved system properties to avoid deadlock
 219                     String arch = VM.getSavedProperty("os.arch");
 220                     if (arch != null) {
 221                         File file = new File(new File(dir, arch), name);
 222                         if (file.exists()) {
 223                             return file.getAbsolutePath();
 224                         }
 225                     }
 226                     // Then check the extension directory
 227                     File file = new File(dir, name);
 228                     if (file.exists()) {
 229                         return file.getAbsolutePath();
 230                     }
 231                 }
 232                 prevDir = dir;
 233             }
 234             return null;
 235         }
 236 





 237         private static AccessControlContext getContext(File[] dirs)
 238             throws IOException
 239         {
 240             PathPermissions perms =
 241                 new PathPermissions(dirs);
 242 
 243             ProtectionDomain domain = new ProtectionDomain(
 244                 new CodeSource(perms.getCodeBase(),
 245                     (java.security.cert.Certificate[]) null),
 246                 perms);
 247 
 248             AccessControlContext acc =
 249                 new AccessControlContext(new ProtectionDomain[] { domain });
 250 
 251             return acc;
 252         }
 253     }
 254 
 255     /**
 256      * The class loader used for loading from java.class.path.


 281                     URL[] urls =
 282                         (s == null) ? new URL[0] : pathToURLs(path);
 283                     return new AppClassLoader(urls, extcl);
 284                 }
 285             });
 286         }
 287 
 288         /*
 289          * Creates a new AppClassLoader
 290          */
 291         AppClassLoader(URL[] urls, ClassLoader parent) {
 292             super(urls, parent, factory);
 293         }
 294 
 295         /**
 296          * Override loadClass so we can checkPackageAccess.
 297          */
 298         public Class loadClass(String name, boolean resolve)
 299             throws ClassNotFoundException
 300         {

 301             int i = name.lastIndexOf('.');
 302             if (i != -1) {
 303                 SecurityManager sm = System.getSecurityManager();
 304                 if (sm != null) {
 305                     sm.checkPackageAccess(name.substring(0, i));
 306                 }
 307             }
 308             return (super.loadClass(name, resolve));
 309         }
 310 
 311         /**
 312          * allow any classes loaded from classpath to exit the VM.
 313          */
 314         protected PermissionCollection getPermissions(CodeSource codesource)
 315         {
 316             PermissionCollection perms = super.getPermissions(codesource);
 317             perms.add(new RuntimePermission("exitVM"));
 318             return perms;
 319         }
 320 


 337          * be the directory containing the jar, not just the jar, as jar
 338          * files might refer to other jar files.
 339          */
 340 
 341         private static AccessControlContext getContext(File[] cp)
 342             throws java.net.MalformedURLException
 343         {
 344             PathPermissions perms =
 345                 new PathPermissions(cp);
 346 
 347             ProtectionDomain domain =
 348                 new ProtectionDomain(new CodeSource(perms.getCodeBase(),
 349                     (java.security.cert.Certificate[]) null),
 350                 perms);
 351 
 352             AccessControlContext acc =
 353                 new AccessControlContext(new ProtectionDomain[] { domain });
 354 
 355             return acc;
 356         }



 357     }

 358 
 359     private static class BootClassPathHolder {
 360         static final URLClassPath bcp;
 361         static {
 362             URL[] urls;
 363             if (bootClassPath != null) {
 364                 urls = AccessController.doPrivileged(
 365                     new PrivilegedAction<URL[]>() {
 366                         public URL[] run() {
 367                             File[] classPath = getClassPath(bootClassPath);
 368                             int len = classPath.length;
 369                             Set<File> seenDirs = new HashSet<File>();
 370                             for (int i = 0; i < len; i++) {
 371                                 File curEntry = classPath[i];
 372                                 // Negative test used to properly handle
 373                                 // nonexistent jars on boot class path
 374                                 if (!curEntry.isDirectory()) {
 375                                     curEntry = curEntry.getParentFile();
 376                                 }
 377                                 if (curEntry != null && seenDirs.add(curEntry)) {
 378                                     MetaIndex.registerDirectory(curEntry);
 379                                 }
 380                             }
 381                             return pathToURLs(classPath);
 382                         }
 383                     }
 384                 );
 385             } else {
 386                 urls = new URL[0];
 387             }
 388             bcp = new URLClassPath(urls, factory);
 389         }
 390     }
 391 
 392     public static URLClassPath getBootstrapClassPath() {
 393         return BootClassPathHolder.bcp;




 394     }
 395 
 396     private static URL[] pathToURLs(File[] path) {
 397         URL[] urls = new URL[path.length];
 398         for (int i = 0; i < path.length; i++) {
 399             urls[i] = getFileURL(path[i]);
 400         }
 401         // DEBUG
 402         //for (int i = 0; i < urls.length; i++) {
 403         //  System.out.println("urls[" + i + "] = " + '"' + urls[i] + '"');
 404         //}
 405         return urls;
 406     }
 407 
 408     private static File[] getClassPath(String cp) {
 409         File[] path;
 410         if (cp != null) {
 411             int count = 0, maxCount = 1;
 412             int pos = 0, lastPos = 0;
 413             // Count the number of separators first