test/hotspot/jtreg/vmTestbase/nsk/share/CustomClassLoader.java
changeset 49934 44839fbb20db
equal deleted inserted replaced
49933:c63bdf53a1a7 49934:44839fbb20db
       
     1 /*
       
     2  * Copyright (c) 2003, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 package nsk.share;
       
    25 
       
    26 import java.io.*;
       
    27 
       
    28 /**
       
    29  * The <code>CustomClassLoader</code> class is used in <code>ClassUnloader</code>.
       
    30  *
       
    31  * <p>This class loader can load classes and notify <code>ClassUnloader</code>
       
    32  * about own finalization to make sure that all loaded classes have been unloaded.
       
    33  *
       
    34  * <p>By default this class loader loads class from .class file located in directory
       
    35  * specified with <code>setClassPath()</code> method. To use any other technique
       
    36  * of class loading one should implement derived class, which would override
       
    37  * <code>findClass</code> method.
       
    38  *
       
    39  * @see nsk.share.ClassUnloader
       
    40  *
       
    41  * @see #setClassPath(String)
       
    42  * @see #findClass(String)
       
    43  */
       
    44 public class CustomClassLoader extends ClassLoader {
       
    45 
       
    46     private ClassUnloader classUnloader;
       
    47     protected String classPath;
       
    48 
       
    49     /**
       
    50      * Initializes a newly created <code>CustomClassloader</code> object
       
    51      * not yet linked with any <code>ClassUnloader</code> object.
       
    52      *
       
    53      */
       
    54     public CustomClassLoader() {
       
    55         super(CustomClassLoader.class.getClassLoader());
       
    56         this.classUnloader = null;
       
    57     }
       
    58 
       
    59     /**
       
    60      * Initializes a newly created <code>CustomClassloader</code> object
       
    61      * linked with specified <code>ClassUnloader</code> object.
       
    62      *
       
    63      * @param classUnloader an instance of <code>ClassUnloader</code>
       
    64      */
       
    65     public CustomClassLoader(ClassUnloader classUnloader) {
       
    66         super(CustomClassLoader.class.getClassLoader());
       
    67         this.classUnloader = classUnloader;
       
    68     }
       
    69 
       
    70     /**
       
    71      * Links this class loader with specified <code>ClassUnloader</code> object.
       
    72      *
       
    73      * @param classUnloader an instance of <code>ClassUnloader</code>
       
    74      */
       
    75     public void setClassUnloader(ClassUnloader classUnloader) {
       
    76         this.classUnloader = classUnloader;
       
    77     }
       
    78 
       
    79     /**
       
    80      * Specifies path to .class file location.
       
    81      *
       
    82      * @param classPath a path to .class file location
       
    83      */
       
    84     public void setClassPath(String classPath) {
       
    85         this.classPath = classPath;
       
    86     }
       
    87 
       
    88     /**
       
    89      * Finds and loads class for specified class name.
       
    90      * This method loads class from .class file located in a directory
       
    91      * previously specified by <code>setClassPath()</code>.
       
    92      *
       
    93      * @param name The name of the class.
       
    94      *
       
    95      * @throws ClassNotFoundException if no .class file found
       
    96      *          for specified class name
       
    97      *
       
    98      * @see #setClassPath(String)
       
    99      */
       
   100     protected synchronized Class findClass(String name) throws ClassNotFoundException {
       
   101         java.nio.file.Path path = ClassFileFinder.findClassFile(name, classPath);
       
   102         if (path == null) {
       
   103             throw new ClassNotFoundException(name);
       
   104         }
       
   105         String classFileName = path.toString();
       
   106 
       
   107         FileInputStream in;
       
   108         try {
       
   109             in = new FileInputStream(classFileName);
       
   110             if (in == null) {
       
   111                 throw new ClassNotFoundException(classFileName);
       
   112             }
       
   113         } catch (FileNotFoundException e) {
       
   114             throw new ClassNotFoundException(classFileName, e);
       
   115         }
       
   116 
       
   117         int len;
       
   118         byte data[];
       
   119         try {
       
   120             len = in.available();
       
   121             data = new byte[len];
       
   122             for (int total = 0; total < data.length; ) {
       
   123                 total += in.read(data, total, data.length - total);
       
   124             }
       
   125         } catch (IOException e) {
       
   126             throw new ClassNotFoundException(classFileName, e);
       
   127         } finally {
       
   128             try {
       
   129                 in.close();
       
   130             } catch (IOException e) {
       
   131                 throw new ClassNotFoundException(classFileName, e);
       
   132             }
       
   133         }
       
   134 
       
   135         return defineClass(name, data, 0, data.length);
       
   136     }
       
   137 
       
   138     /**
       
   139      * Notifies <code>ClassUnloader</code> about finalization.
       
   140      */
       
   141     protected void finalize() throws Throwable {
       
   142 
       
   143         // notify ClassUnloader about finalization
       
   144         if (classUnloader != null) {
       
   145             classUnloader.finalized = true;
       
   146         }
       
   147 
       
   148         super.finalize();
       
   149     }
       
   150 }