< prev index next >

src/java.base/share/classes/jdk/internal/loader/BootLoader.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 2018, 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


  54 
  55 public class BootLoader {
  56     private BootLoader() { }
  57 
  58     // The unnamed module for the boot loader
  59     private static final Module UNNAMED_MODULE;
  60     private static final String JAVA_HOME = StaticProperty.javaHome();
  61 
  62     static {
  63         UNNAMED_MODULE = SharedSecrets.getJavaLangAccess().defineUnnamedModule(null);
  64         setBootLoaderUnnamedModule0(UNNAMED_MODULE);
  65     }
  66 
  67     // ServiceCatalog for the boot class loader
  68     private static final ServicesCatalog SERVICES_CATALOG = ServicesCatalog.create();
  69 
  70     // ClassLoaderValue map for the boot class loader
  71     private static final ConcurrentHashMap<?, ?> CLASS_LOADER_VALUE_MAP
  72         = new ConcurrentHashMap<>();
  73 




  74     /**
  75      * Returns the unnamed module for the boot loader.
  76      */
  77     public static Module getUnnamedModule() {
  78         return UNNAMED_MODULE;
  79     }
  80 
  81     /**
  82      * Returns the ServiceCatalog for modules defined to the boot class loader.
  83      */
  84     public static ServicesCatalog getServicesCatalog() {
  85         return SERVICES_CATALOG;
  86     }
  87 
  88     /**
  89      * Returns the ClassLoaderValue map for the boot class loader.
  90      */
  91     public static ConcurrentHashMap<?, ?> getClassLoaderValueMap() {
  92         return CLASS_LOADER_VALUE_MAP;
  93     }
  94 
  95     /**







  96      * Returns {@code true} if there is a class path associated with the
  97      * BootLoader.
  98      */
  99     public static boolean hasClassPath() {
 100         return ClassLoaders.bootLoader().hasClassPath();
 101     }
 102 
 103     /**
 104      * Registers a module with this class loader so that its classes
 105      * (and resources) become visible via this class loader.
 106      */
 107     public static void loadModule(ModuleReference mref) {
 108         ClassLoaders.bootLoader().loadModule(mref);
 109     }
 110 
 111     /**
 112      * Loads the Class object with the given name defined to the boot loader.
 113      */
 114     public static Class<?> loadClassOrNull(String name) {
 115         return ClassLoaders.bootLoader().loadClassOrNull(name);
 116     }
 117 
 118     /**
 119      * Loads the Class object with the given name in the given module
 120      * defined to the boot loader. Returns {@code null} if not found.
 121      */
 122     public static Class<?> loadClass(Module module, String name) {
 123         Class<?> c = loadClassOrNull(name);
 124         if (c != null && c.getModule() == module) {
 125             return c;
 126         } else {
 127             return null;
 128         }
 129     }
 130 
 131     /**
 132      * Loads a library from the system path.
 133      */
 134     public static void loadLibrary(String library) {
 135         if (System.getSecurityManager() == null) {
 136             SharedSecrets.getJavaLangAccess().loadLibrary(BootLoader.class, library);
 137         } else {
 138             AccessController.doPrivileged(
 139                 new java.security.PrivilegedAction<>() {
 140                     public Void run() {
 141                         SharedSecrets.getJavaLangAccess().loadLibrary(BootLoader.class, library);
 142                         return null;
 143                     }
 144                 });
 145         }
 146     }
 147 
 148     /**
 149      * Returns a URL to a resource in a module defined to the boot loader.
 150      */
 151     public static URL findResource(String mn, String name) throws IOException {
 152         return ClassLoaders.bootLoader().findResource(mn, name);
 153     }
 154 
 155     /**
 156      * Returns an input stream to a resource in a module defined to the
 157      * boot loader.
 158      */
 159     public static InputStream findResourceAsStream(String mn, String name)
 160         throws IOException
 161     {
 162         return ClassLoaders.bootLoader().findResourceAsStream(mn, name);
 163     }
 164 
 165     /**
 166      * Returns the URL to the given resource in any of the modules
 167      * defined to the boot loader and the boot class path.
 168      */


   1 /*
   2  * Copyright (c) 2015, 2020, 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


  54 
  55 public class BootLoader {
  56     private BootLoader() { }
  57 
  58     // The unnamed module for the boot loader
  59     private static final Module UNNAMED_MODULE;
  60     private static final String JAVA_HOME = StaticProperty.javaHome();
  61 
  62     static {
  63         UNNAMED_MODULE = SharedSecrets.getJavaLangAccess().defineUnnamedModule(null);
  64         setBootLoaderUnnamedModule0(UNNAMED_MODULE);
  65     }
  66 
  67     // ServiceCatalog for the boot class loader
  68     private static final ServicesCatalog SERVICES_CATALOG = ServicesCatalog.create();
  69 
  70     // ClassLoaderValue map for the boot class loader
  71     private static final ConcurrentHashMap<?, ?> CLASS_LOADER_VALUE_MAP
  72         = new ConcurrentHashMap<>();
  73 
  74     // native libraries loaded by the boot class loader
  75     private static final NativeLibraries NATIVE_LIBS
  76         = new NativeLibraries(null);
  77 
  78     /**
  79      * Returns the unnamed module for the boot loader.
  80      */
  81     public static Module getUnnamedModule() {
  82         return UNNAMED_MODULE;
  83     }
  84 
  85     /**
  86      * Returns the ServiceCatalog for modules defined to the boot class loader.
  87      */
  88     public static ServicesCatalog getServicesCatalog() {
  89         return SERVICES_CATALOG;
  90     }
  91 
  92     /**
  93      * Returns the ClassLoaderValue map for the boot class loader.
  94      */
  95     public static ConcurrentHashMap<?, ?> getClassLoaderValueMap() {
  96         return CLASS_LOADER_VALUE_MAP;
  97     }
  98 
  99     /**
 100      * Returns NativeLibraries for the boot class loader.
 101      */
 102     public static NativeLibraries getNativeLibraries() {
 103         return NATIVE_LIBS;
 104     }
 105 
 106     /**
 107      * Returns {@code true} if there is a class path associated with the
 108      * BootLoader.
 109      */
 110     public static boolean hasClassPath() {
 111         return ClassLoaders.bootLoader().hasClassPath();
 112     }
 113 
 114     /**
 115      * Registers a module with this class loader so that its classes
 116      * (and resources) become visible via this class loader.
 117      */
 118     public static void loadModule(ModuleReference mref) {
 119         ClassLoaders.bootLoader().loadModule(mref);
 120     }
 121 
 122     /**
 123      * Loads the Class object with the given name defined to the boot loader.
 124      */
 125     public static Class<?> loadClassOrNull(String name) {
 126         return ClassLoaders.bootLoader().loadClassOrNull(name);
 127     }
 128 
 129     /**
 130      * Loads the Class object with the given name in the given module
 131      * defined to the boot loader. Returns {@code null} if not found.
 132      */
 133     public static Class<?> loadClass(Module module, String name) {
 134         Class<?> c = loadClassOrNull(name);
 135         if (c != null && c.getModule() == module) {
 136             return c;
 137         } else {
 138             return null;
 139         }
 140     }
 141 
 142     /**

















 143      * Returns a URL to a resource in a module defined to the boot loader.
 144      */
 145     public static URL findResource(String mn, String name) throws IOException {
 146         return ClassLoaders.bootLoader().findResource(mn, name);
 147     }
 148 
 149     /**
 150      * Returns an input stream to a resource in a module defined to the
 151      * boot loader.
 152      */
 153     public static InputStream findResourceAsStream(String mn, String name)
 154         throws IOException
 155     {
 156         return ClassLoaders.bootLoader().findResourceAsStream(mn, name);
 157     }
 158 
 159     /**
 160      * Returns the URL to the given resource in any of the modules
 161      * defined to the boot loader and the boot class path.
 162      */


< prev index next >