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

Print this page
rev 9051 : 6760902: inconsistent behavior in system class loader for classes and resources
Reviewed-by: duke
   1 /*
   2  * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  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


 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.
 257      * runs in a restricted security context.
 258      */
 259     static class AppClassLoader extends URLClassLoader {
 260 
 261         static {
 262             ClassLoader.registerAsParallelCapable();
 263         }
 264 
 265         public static ClassLoader getAppClassLoader(final ClassLoader extcl)
 266             throws IOException
 267         {
 268             final String s = System.getProperty("java.class.path");
 269             final File[] path = (s == null) ? new File[0] : getClassPath(s);
 270 
 271             // Note: on bugid 4256530
 272             // Prior implementations of this doPrivileged() block supplied
 273             // a rather restrictive ACC via a call to the private method
 274             // AppClassLoader.getContext(). This proved overly restrictive
 275             // when loading  classes. Specifically it prevent
 276             // accessClassInPackage.sun.* grants from being honored.
 277             //
 278             return AccessController.doPrivileged(
 279                 new PrivilegedAction<AppClassLoader>() {
 280                     public AppClassLoader run() {
 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


 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
 414             while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) {
 415                 maxCount++;
 416                 lastPos = pos + 1;
 417             }
 418             path = new File[maxCount];
 419             lastPos = pos = 0;
 420             // Now scan for each path component
 421             while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) {
 422                 if (pos - lastPos > 0) {
 423                     path[count++] = new File(cp.substring(lastPos, pos));
 424                 } else {
 425                     // empty path component translates to "."
 426                     path[count++] = new File(".");
 427                 }
 428                 lastPos = pos + 1;
 429             }
 430             // Make sure we include the last path component
 431             if (lastPos < cp.length()) {
 432                 path[count++] = new File(cp.substring(lastPos));
 433             } else {
 434                 path[count++] = new File(".");
 435             }
 436             // Trim array to correct size
 437             if (count != maxCount) {
 438                 File[] tmp = new File[count];
 439                 System.arraycopy(path, 0, tmp, 0, count);
 440                 path = tmp;
 441             }
 442         } else {
 443             path = new File[0];
 444         }
 445         // DEBUG
 446         //for (int i = 0; i < path.length; i++) {
 447         //  System.out.println("path[" + i + "] = " + '"' + path[i] + '"');
 448         //}
 449         return path;
 450     }
 451 
 452     private static URLStreamHandler fileHandler;
 453 


   1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  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


 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.
 257      * runs in a restricted security context.
 258      */
 259     static class AppClassLoader extends URLClassLoader {
 260 
 261         static {
 262             ClassLoader.registerAsParallelCapable();
 263         }
 264 
 265         public static ClassLoader getAppClassLoader(final ClassLoader extcl)
 266             throws IOException
 267         {
 268             final String s = System.getProperty("java.class.path");
 269             final File[] path = (s == null) ? new File[0] : getClassPath(s, true);
 270 
 271             // Note: on bugid 4256530
 272             // Prior implementations of this doPrivileged() block supplied
 273             // a rather restrictive ACC via a call to the private method
 274             // AppClassLoader.getContext(). This proved overly restrictive
 275             // when loading  classes. Specifically it prevent
 276             // accessClassInPackage.sun.* grants from being honored.
 277             //
 278             return AccessController.doPrivileged(
 279                 new PrivilegedAction<AppClassLoader>() {
 280                     public AppClassLoader run() {
 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


 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                             // Skip empty path in boot class path i.e. not default to use CWD
 368                             File[] classPath = getClassPath(bootClassPath, false);
 369                             int len = classPath.length;
 370                             Set<File> seenDirs = new HashSet<File>();
 371                             for (int i = 0; i < len; i++) {
 372                                 File curEntry = classPath[i];
 373                                 // Negative test used to properly handle
 374                                 // nonexistent jars on boot class path
 375                                 if (!curEntry.isDirectory()) {
 376                                     curEntry = curEntry.getParentFile();
 377                                 }
 378                                 if (curEntry != null && seenDirs.add(curEntry)) {
 379                                     MetaIndex.registerDirectory(curEntry);
 380                                 }
 381                             }
 382                             return pathToURLs(classPath);
 383                         }
 384                     }
 385                 );
 386             } else {
 387                 urls = new URL[0];
 388             }
 389             bcp = new URLClassPath(urls, factory);
 390         }
 391     }
 392 
 393     public static URLClassPath getBootstrapClassPath() {
 394         return BootClassPathHolder.bcp;
 395     }
 396 
 397     private static URL[] pathToURLs(File[] path) {
 398         URL[] urls = new URL[path.length];
 399         for (int i = 0; i < path.length; i++) {
 400             urls[i] = getFileURL(path[i]);
 401         }
 402         // DEBUG
 403         //for (int i = 0; i < urls.length; i++) {
 404         //  System.out.println("urls[" + i + "] = " + '"' + urls[i] + '"');
 405         //}
 406         return urls;
 407     }
 408 
 409     private static File[] getClassPath(String cp, boolean defaultToCwd) {
 410         File[] path;
 411         if (cp != null) {
 412             int count = 0, maxCount = 1;
 413             int pos = 0, lastPos = 0;
 414             // Count the number of separators first
 415             while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) {
 416                 maxCount++;
 417                 lastPos = pos + 1;
 418             }
 419             path = new File[maxCount];
 420             lastPos = pos = 0;
 421             // Now scan for each path component
 422             while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) {
 423                 if (pos - lastPos > 0) {
 424                     path[count++] = new File(cp.substring(lastPos, pos));
 425                 } else if (defaultToCwd) {
 426                     // empty path component translates to "."
 427                     path[count++] = new File(".");
 428                 }
 429                 lastPos = pos + 1;
 430             }
 431             // Make sure we include the last path component
 432             if (lastPos < cp.length()) {
 433                 path[count++] = new File(cp.substring(lastPos));
 434             } else if (defaultToCwd) {
 435                 path[count++] = new File(".");
 436             }
 437             // Trim array to correct size
 438             if (count != maxCount) {
 439                 File[] tmp = new File[count];
 440                 System.arraycopy(path, 0, tmp, 0, count);
 441                 path = tmp;
 442             }
 443         } else {
 444             path = new File[0];
 445         }
 446         // DEBUG
 447         //for (int i = 0; i < path.length; i++) {
 448         //  System.out.println("path[" + i + "] = " + '"' + path[i] + '"');
 449         //}
 450         return path;
 451     }
 452 
 453     private static URLStreamHandler fileHandler;
 454