1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/sun/rmi/rmic/iiop/ClassPathLoader.java Wed Apr 27 01:21:28 2016 +0800 1.3 @@ -0,0 +1,117 @@ 1.4 +/* 1.5 + * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 +package sun.rmi.rmic.iiop; 1.29 + 1.30 +import java.io.*; 1.31 +import sun.tools.java.ClassPath ; 1.32 +import sun.tools.java.ClassFile ; 1.33 + 1.34 +/** 1.35 + * A ClassLoader that will ultimately use a given sun.tools.java.ClassPath to 1.36 + * find the desired file. This works for any JAR files specified in the given 1.37 + * ClassPath as well -- reusing all of that wonderful sun.tools.java code. 1.38 + * 1.39 + *@author Everett Anderson 1.40 + */ 1.41 +public class ClassPathLoader extends ClassLoader 1.42 +{ 1.43 + private ClassPath classPath; 1.44 + 1.45 + public ClassPathLoader(ClassPath classPath) { 1.46 + this.classPath = classPath; 1.47 + } 1.48 + 1.49 + // Called by the super class 1.50 + protected Class findClass(String name) throws ClassNotFoundException 1.51 + { 1.52 + byte[] b = loadClassData(name); 1.53 + return defineClass(name, b, 0, b.length); 1.54 + } 1.55 + 1.56 + /** 1.57 + * Load the class with the given fully qualified name from the ClassPath. 1.58 + */ 1.59 + private byte[] loadClassData(String className) 1.60 + throws ClassNotFoundException 1.61 + { 1.62 + // Build the file name and subdirectory from the 1.63 + // class name 1.64 + String filename = className.replace('.', File.separatorChar) 1.65 + + ".class"; 1.66 + 1.67 + // Have ClassPath find the file for us, and wrap it in a 1.68 + // ClassFile. Note: This is where it looks inside jar files that 1.69 + // are specified in the path. 1.70 + ClassFile classFile = classPath.getFile(filename); 1.71 + 1.72 + if (classFile != null) { 1.73 + 1.74 + // Provide the most specific reason for failure in addition 1.75 + // to ClassNotFound 1.76 + Exception reportedError = null; 1.77 + byte data[] = null; 1.78 + 1.79 + try { 1.80 + // ClassFile is beautiful because it shields us from 1.81 + // knowing if it's a separate file or an entry in a 1.82 + // jar file. 1.83 + DataInputStream input 1.84 + = new DataInputStream(classFile.getInputStream()); 1.85 + 1.86 + // Can't rely on input available() since it will be 1.87 + // something unusual if it's a jar file! May need 1.88 + // to worry about a possible problem if someone 1.89 + // makes a jar file entry with a size greater than 1.90 + // max int. 1.91 + data = new byte[(int)classFile.length()]; 1.92 + 1.93 + try { 1.94 + input.readFully(data); 1.95 + } catch (IOException ex) { 1.96 + // Something actually went wrong reading the file. This 1.97 + // is a real error so save it to report it. 1.98 + data = null; 1.99 + reportedError = ex; 1.100 + } finally { 1.101 + // Just don't care if there's an exception on close! 1.102 + // I hate that close can throw an IOException! 1.103 + try { input.close(); } catch (IOException ex) {} 1.104 + } 1.105 + } catch (IOException ex) { 1.106 + // Couldn't get the input stream for the file. This is 1.107 + // probably also a real error. 1.108 + reportedError = ex; 1.109 + } 1.110 + 1.111 + if (data == null) 1.112 + throw new ClassNotFoundException(className, reportedError); 1.113 + 1.114 + return data; 1.115 + } 1.116 + 1.117 + // Couldn't find the file in the class path. 1.118 + throw new ClassNotFoundException(className); 1.119 + } 1.120 +}