duke@1: /* ohair@158: * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@158: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@158: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@158: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@158: * or visit www.oracle.com if you need additional information or have any ohair@158: * questions. duke@1: */ duke@1: package sun.rmi.rmic.iiop; duke@1: duke@1: import java.io.*; duke@1: import sun.tools.java.ClassPath ; duke@1: import sun.tools.java.ClassFile ; duke@1: duke@1: /** duke@1: * A ClassLoader that will ultimately use a given sun.tools.java.ClassPath to duke@1: * find the desired file. This works for any JAR files specified in the given duke@1: * ClassPath as well -- reusing all of that wonderful sun.tools.java code. duke@1: * duke@1: *@author Everett Anderson duke@1: */ duke@1: public class ClassPathLoader extends ClassLoader duke@1: { duke@1: private ClassPath classPath; duke@1: duke@1: public ClassPathLoader(ClassPath classPath) { duke@1: this.classPath = classPath; duke@1: } duke@1: duke@1: // Called by the super class duke@1: protected Class findClass(String name) throws ClassNotFoundException duke@1: { duke@1: byte[] b = loadClassData(name); duke@1: return defineClass(name, b, 0, b.length); duke@1: } duke@1: duke@1: /** duke@1: * Load the class with the given fully qualified name from the ClassPath. duke@1: */ duke@1: private byte[] loadClassData(String className) duke@1: throws ClassNotFoundException duke@1: { duke@1: // Build the file name and subdirectory from the duke@1: // class name duke@1: String filename = className.replace('.', File.separatorChar) duke@1: + ".class"; duke@1: duke@1: // Have ClassPath find the file for us, and wrap it in a duke@1: // ClassFile. Note: This is where it looks inside jar files that duke@1: // are specified in the path. duke@1: ClassFile classFile = classPath.getFile(filename); duke@1: duke@1: if (classFile != null) { duke@1: duke@1: // Provide the most specific reason for failure in addition duke@1: // to ClassNotFound duke@1: Exception reportedError = null; duke@1: byte data[] = null; duke@1: duke@1: try { duke@1: // ClassFile is beautiful because it shields us from duke@1: // knowing if it's a separate file or an entry in a duke@1: // jar file. duke@1: DataInputStream input duke@1: = new DataInputStream(classFile.getInputStream()); duke@1: duke@1: // Can't rely on input available() since it will be duke@1: // something unusual if it's a jar file! May need duke@1: // to worry about a possible problem if someone duke@1: // makes a jar file entry with a size greater than duke@1: // max int. duke@1: data = new byte[(int)classFile.length()]; duke@1: duke@1: try { duke@1: input.readFully(data); duke@1: } catch (IOException ex) { duke@1: // Something actually went wrong reading the file. This duke@1: // is a real error so save it to report it. duke@1: data = null; duke@1: reportedError = ex; duke@1: } finally { duke@1: // Just don't care if there's an exception on close! duke@1: // I hate that close can throw an IOException! duke@1: try { input.close(); } catch (IOException ex) {} duke@1: } duke@1: } catch (IOException ex) { duke@1: // Couldn't get the input stream for the file. This is duke@1: // probably also a real error. duke@1: reportedError = ex; duke@1: } duke@1: duke@1: if (data == null) duke@1: throw new ClassNotFoundException(className, reportedError); duke@1: duke@1: return data; duke@1: } duke@1: duke@1: // Couldn't find the file in the class path. duke@1: throw new ClassNotFoundException(className); duke@1: } duke@1: }