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