1 /*
   2  * Copyright (c) 2009, 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
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.net.URLStreamHandlerFactory;
  31 import sun.misc.URLClassPath;
  32 
  33 /**
  34  * BootClassLoaderHook defines an interface for a hook to inject
  35  * into the bootstrap class loader.
  36  *
  37  * In jkernel build, the sun.jkernel.DownloadManager is set as
  38  * a BootClassLoaderHook by the jkernel VM after the VM is initialized.
  39  *
  40  * In other JDK builds, no hook is set.
  41  */
  42 public abstract class BootClassLoaderHook {
  43     private static BootClassLoaderHook bootLoaderHook = null;
  44     public static synchronized BootClassLoaderHook getHook() {
  45         return bootLoaderHook;
  46     }
  47 
  48     public static synchronized void setHook(BootClassLoaderHook hook) {
  49         if (!VM.isBooted()) {
  50             throw new InternalError("hook can only be set after VM is booted");
  51         }
  52         if (bootLoaderHook != null) {
  53             throw new InternalError("hook should not be reinitialized");
  54         }
  55         bootLoaderHook = hook;
  56     }
  57 
  58     protected BootClassLoaderHook() {
  59     }
  60 
  61     /**
  62      * A method to be invoked before a class loader loads
  63      * a bootstrap class.
  64      *
  65      * @param classname the binary name of the class
  66      */
  67     public static void preLoadClass(String classname) {
  68         BootClassLoaderHook hook = getHook();
  69         if (hook != null) {
  70             hook.loadBootstrapClass(classname);
  71         }
  72     }
  73 
  74     /**
  75      * A method to be invoked before a class loader loads
  76      * a resource.
  77      *
  78      * @param resourcename the resource name
  79      */
  80     public static void preLoadResource(String resourcename) {
  81         BootClassLoaderHook hook = getHook();
  82         if (hook != null) {
  83             hook.getBootstrapResource(resourcename);
  84         }
  85     }
  86 
  87     /**
  88      * A method to be invoked before a library is loaded.
  89      *
  90      * @param libname the name of the library
  91      */
  92     public static void preLoadLibrary(String libname) {
  93         BootClassLoaderHook hook = getHook();
  94         if (hook != null) {
  95             hook.loadLibrary(libname);
  96         }
  97     }
  98 
  99     /**
 100      * Returns a pathname of a JAR or class that the hook loads
 101      * per this loadClass request; or null.
 102      *
 103      * @param classname the binary name of the class
 104      */
 105     public abstract String loadBootstrapClass(String className);
 106 
 107     /**
 108      * Returns a pathname of a resource file that the hook loads
 109      * per this getResource request; or null.
 110      *
 111      * @param resourceName the resource name
 112      */
 113     public abstract String getBootstrapResource(String resourceName);
 114 
 115     /**
 116      * Returns true if the hook successfully performs an operation per
 117      * this loadLibrary request; or false if it fails.
 118      *
 119      * @param libname the name of the library
 120      */
 121     public abstract boolean loadLibrary(String libname);
 122 
 123     /**
 124      * Returns a bootstrap class path constructed by the hook.
 125      *
 126      * @param bcp VM's bootstrap class path
 127      * @param factory Launcher's URL stream handler
 128      */
 129     public abstract URLClassPath getBootstrapClassPath(URLClassPath bcp,
 130             URLStreamHandlerFactory factory);
 131 
 132     /**
 133      * Returns true if the current thread is in the process of doing
 134      * a prefetching operation.
 135      */
 136     public abstract boolean isCurrentThreadPrefetching();
 137 
 138     /**
 139      * Returns true if the hook successfully prefetches the specified file.
 140      *
 141      * @param name a platform independent pathname
 142      */
 143     public abstract boolean prefetchFile(String name);
 144 }