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