jdk/src/share/classes/sun/misc/BootClassLoaderHook.java
changeset 8852 c228cf346138
parent 8851 e630c590eb10
parent 8717 f75a1efb1412
child 8853 6aa795396cc8
child 9067 c0b85430843d
equal deleted inserted replaced
8851:e630c590eb10 8852:c228cf346138
     1 /*
       
     2  * Copyright (c) 2009, 2010, 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  * With jkernel now removed, no hook is set
       
    38  */
       
    39 public abstract class BootClassLoaderHook {
       
    40     private static BootClassLoaderHook bootLoaderHook = null;
       
    41     public static synchronized BootClassLoaderHook getHook() {
       
    42         return bootLoaderHook;
       
    43     }
       
    44 
       
    45     public static synchronized void setHook(BootClassLoaderHook hook) {
       
    46         if (!VM.isBooted()) {
       
    47             throw new InternalError("hook can only be set after VM is booted");
       
    48         }
       
    49         if (bootLoaderHook != null) {
       
    50             throw new InternalError("hook should not be reinitialized");
       
    51         }
       
    52         bootLoaderHook = hook;
       
    53     }
       
    54 
       
    55     protected BootClassLoaderHook() {
       
    56     }
       
    57 
       
    58     /**
       
    59      * A method to be invoked before a class loader loads
       
    60      * a bootstrap class.
       
    61      *
       
    62      * @param classname the binary name of the class
       
    63      */
       
    64     public static void preLoadClass(String classname) {
       
    65         BootClassLoaderHook hook = getHook();
       
    66         if (hook != null) {
       
    67             hook.loadBootstrapClass(classname);
       
    68         }
       
    69     }
       
    70 
       
    71     /**
       
    72      * A method to be invoked before a class loader loads
       
    73      * a resource.
       
    74      *
       
    75      * @param resourcename the resource name
       
    76      */
       
    77     public static void preLoadResource(String resourcename) {
       
    78         BootClassLoaderHook hook = getHook();
       
    79         if (hook != null) {
       
    80             hook.getBootstrapResource(resourcename);
       
    81         }
       
    82     }
       
    83 
       
    84     /**
       
    85      * A method to be invoked before a library is loaded.
       
    86      *
       
    87      * @param libname the name of the library
       
    88      */
       
    89     public static void preLoadLibrary(String libname) {
       
    90         BootClassLoaderHook hook = getHook();
       
    91         if (hook != null) {
       
    92             hook.loadLibrary(libname);
       
    93         }
       
    94     }
       
    95 
       
    96     /**
       
    97      * Returns a pathname of a JAR or class that the hook loads
       
    98      * per this loadClass request; or null.
       
    99      *
       
   100      * @param classname the binary name of the class
       
   101      */
       
   102     public abstract String loadBootstrapClass(String className);
       
   103 
       
   104     /**
       
   105      * Returns a pathname of a resource file that the hook loads
       
   106      * per this getResource request; or null.
       
   107      *
       
   108      * @param resourceName the resource name
       
   109      */
       
   110     public abstract String getBootstrapResource(String resourceName);
       
   111 
       
   112     /**
       
   113      * Returns true if the hook successfully performs an operation per
       
   114      * this loadLibrary request; or false if it fails.
       
   115      *
       
   116      * @param libname the name of the library
       
   117      */
       
   118     public abstract boolean loadLibrary(String libname);
       
   119 
       
   120     /**
       
   121      * Returns a bootstrap class path constructed by the hook.
       
   122      *
       
   123      * @param bcp VM's bootstrap class path
       
   124      * @param factory Launcher's URL stream handler
       
   125      */
       
   126     public abstract URLClassPath getBootstrapClassPath(URLClassPath bcp,
       
   127             URLStreamHandlerFactory factory);
       
   128 
       
   129     /**
       
   130      * Returns true if the current thread is in the process of doing
       
   131      * a prefetching operation.
       
   132      */
       
   133     public abstract boolean isCurrentThreadPrefetching();
       
   134 
       
   135     /**
       
   136      * Returns true if the hook successfully prefetches the specified file.
       
   137      *
       
   138      * @param name a platform independent pathname
       
   139      */
       
   140     public abstract boolean prefetchFile(String name);
       
   141 }