ClassPathLoader.java revision 608:7e06bf1dcb09
1100384Speter/*
2100384Speter * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
3100384Speter * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4100384Speter *
5100384Speter * This code is free software; you can redistribute it and/or modify it
6100384Speter * under the terms of the GNU General Public License version 2 only, as
7100384Speter * published by the Free Software Foundation.  Oracle designates this
8100384Speter * particular file as subject to the "Classpath" exception as provided
9100384Speter * by Oracle in the LICENSE file that accompanied this code.
10100384Speter *
11100384Speter * This code is distributed in the hope that it will be useful, but WITHOUT
12100384Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13100384Speter * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14100384Speter * version 2 for more details (a copy is included in the LICENSE file that
15100384Speter * accompanied this code).
16100384Speter *
17100384Speter * You should have received a copy of the GNU General Public License version
18100384Speter * 2 along with this work; if not, write to the Free Software Foundation,
19100384Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20100384Speter *
21100384Speter * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22100384Speter * or visit www.oracle.com if you need additional information or have any
23100384Speter * questions.
24100384Speter */
25100384Speterpackage sun.rmi.rmic.iiop;
26100384Speter
27118031Sobrienimport java.io.*;
28118031Sobrienimport sun.tools.java.ClassPath ;
29118031Sobrienimport sun.tools.java.ClassFile ;
30104738Speter
31104738Speter/**
32100384Speter * A ClassLoader that will ultimately use a given sun.tools.java.ClassPath to
33100384Speter * find the desired file.  This works for any JAR files specified in the given
34100384Speter * ClassPath as well -- reusing all of that wonderful sun.tools.java code.
35100384Speter *
36100384Speter *@author Everett Anderson
37100384Speter */
38100384Speterpublic class ClassPathLoader extends ClassLoader
39100384Speter{
40100384Speter    private ClassPath classPath;
41100384Speter
42100384Speter    public ClassPathLoader(ClassPath classPath) {
43100384Speter        this.classPath = classPath;
44100384Speter    }
45100384Speter
46100384Speter    // Called by the super class
47100384Speter    protected Class findClass(String name) throws ClassNotFoundException
48100384Speter    {
49100384Speter        byte[] b = loadClassData(name);
50100384Speter        return defineClass(name, b, 0, b.length);
51100384Speter    }
52100384Speter
53100384Speter    /**
54100384Speter     * Load the class with the given fully qualified name from the ClassPath.
55100384Speter     */
56100384Speter    private byte[] loadClassData(String className)
57100384Speter        throws ClassNotFoundException
58100384Speter    {
59100384Speter        // Build the file name and subdirectory from the
60113859Sjhb        // class name
61100384Speter        String filename = className.replace('.', File.separatorChar)
62100384Speter                          + ".class";
63100384Speter
64100384Speter        // Have ClassPath find the file for us, and wrap it in a
65100384Speter        // ClassFile.  Note:  This is where it looks inside jar files that
66100384Speter        // are specified in the path.
67100384Speter        ClassFile classFile = classPath.getFile(filename);
68100384Speter
69100384Speter        if (classFile != null) {
70100384Speter
71100384Speter            // Provide the most specific reason for failure in addition
72100384Speter            // to ClassNotFound
73100384Speter            Exception reportedError = null;
74100384Speter            byte data[] = null;
75100384Speter
76100384Speter            try {
77100384Speter                // ClassFile is beautiful because it shields us from
78119333Speter                // knowing if it's a separate file or an entry in a
79119333Speter                // jar file.
80119333Speter                DataInputStream input
81100384Speter                    = new DataInputStream(classFile.getInputStream());
82121719Speter
83121719Speter                // Can't rely on input available() since it will be
84121719Speter                // something unusual if it's a jar file!  May need
85121719Speter                // to worry about a possible problem if someone
86121719Speter                // makes a jar file entry with a size greater than
87100384Speter                // max int.
88100384Speter                data = new byte[(int)classFile.length()];
89100384Speter
90100384Speter                try {
91100384Speter                    input.readFully(data);
92100384Speter                } catch (IOException ex) {
93100384Speter                    // Something actually went wrong reading the file.  This
94100384Speter                    // is a real error so save it to report it.
95100384Speter                    data = null;
96100384Speter                    reportedError = ex;
97119333Speter                } finally {
98100384Speter                    // Just don't care if there's an exception on close!
99100384Speter                    // I hate that close can throw an IOException!
100100384Speter                    try { input.close(); } catch (IOException ex) {}
101100384Speter                }
102100384Speter            } catch (IOException ex) {
103100384Speter                // Couldn't get the input stream for the file.  This is
104100384Speter                // probably also a real error.
105100384Speter                reportedError = ex;
106100384Speter            }
107100384Speter
108100384Speter            if (data == null)
109100384Speter                throw new ClassNotFoundException(className, reportedError);
110100384Speter
111100384Speter            return data;
112100384Speter        }
113100384Speter
114111119Simp        // Couldn't find the file in the class path.
115100384Speter        throw new ClassNotFoundException(className);
116100384Speter    }
117100384Speter}
118100384Speter