KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > io > dir > ClassLoader


1 package com.quadcap.io.dir;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.Enumeration JavaDoc;
42 import java.util.Hashtable JavaDoc;
43 import java.util.Vector JavaDoc;
44
45 import java.io.File JavaDoc;
46 import java.io.FileOutputStream JavaDoc;
47 import java.io.InputStream JavaDoc;
48 import java.io.IOException JavaDoc;
49
50 import java.net.URL JavaDoc;
51
52 import com.quadcap.io.IO;
53
54
55 /**
56  * This class implements a JSP class loader
57  *
58  * @author Stan Bailes
59  */

60 public class ClassLoader extends java.lang.ClassLoader JavaDoc {
61     Directory root;
62     Vector JavaDoc jars = new Vector JavaDoc();
63
64     public ClassLoader(Directory root) {
65         super();
66         this.root = root;
67         try {
68             init();
69         } catch (IOException JavaDoc ex) {
70         }
71     }
72     
73
74     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
75     return loadClass(name, true);
76     }
77
78     // reverse the normal order....
79
public synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
80     throws ClassNotFoundException JavaDoc
81     {
82     // First, check if the class has already been loaded
83
Class JavaDoc c = findLoadedClass(name);
84     if (c == null) {
85         try {
86             c = findClass(name);
87         } catch (ClassNotFoundException JavaDoc e) {
88                 c = super.loadClass(name, resolve);
89         }
90     }
91     if (resolve) {
92         resolveClass(c);
93     }
94     return c;
95     }
96
97     public Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
98         byte[] b = loadClassData(name);
99         return defineClass(name, b, 0, b.length,
100                            this.getClass().getProtectionDomain());
101     }
102
103     public URL JavaDoc findResource(String JavaDoc name) {
104         URL JavaDoc url = null;
105         try {
106             String JavaDoc fileName = "META-INF" + name;
107             for (int i = 0; url == null && i < jars.size(); i++) {
108                 Directory d = (Directory)jars.elementAt(i);
109                 url = d.getURL(name);
110             }
111         } catch (Throwable JavaDoc t) {
112             url = null;
113         }
114         return url;
115     }
116
117     private byte[] loadClassData(String JavaDoc name) throws ClassNotFoundException JavaDoc {
118         try {
119             Entry classFile = locateClassFile(name);
120             byte[] b = new byte[(int)(classFile.getSize())];
121             InputStream JavaDoc f = classFile.getInputStream();
122             IO.readFully(f, b);
123             f.close();
124             return b;
125         } catch (IOException JavaDoc e) {
126             throw new ClassNotFoundException JavaDoc("error loading class: " +
127                          e.toString());
128         }
129     }
130
131     final Entry locateClassFile(String JavaDoc name) throws ClassNotFoundException JavaDoc {
132         String JavaDoc className = name.replace('.', '/') + ".class";
133         Entry classFile = null;
134         for (int i = 0; classFile == null && i < jars.size(); i++) {
135             Directory d = (Directory)jars.elementAt(i);
136             classFile = d.getEntry(className);
137         }
138         if (classFile == null) {
139             throw new ClassNotFoundException JavaDoc("not found: " + name);
140         }
141         return classFile;
142     }
143
144     static int tmpCount = 0;
145
146     public String JavaDoc getClassPath() {
147         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
148         String JavaDoc delim = System.getProperty("path.separator");
149         for (int i = 0; i < jars.size(); i++) {
150             Directory d = (Directory)jars.elementAt(i);
151             if (i > 0) sb.append(delim);
152             sb.append(d.getRootPath());
153         }
154         return sb.toString();
155     }
156     
157     final void init() throws IOException JavaDoc {
158         Enumeration JavaDoc e = root.entries();
159         Directory classes = null;
160         File JavaDoc tmpClasses = null;
161         while (e.hasMoreElements()) {
162             String JavaDoc path = e.nextElement().toString();
163             if (path.endsWith(".jar")) {
164                 Directory d = null;
165                 String JavaDoc realPath = root.getRealPath(path);
166                 if (realPath == null) {
167                     throw new IOException JavaDoc("No Path: " + path);
168                 }
169                 d = Directory.getDirectory(new File JavaDoc(realPath));
170                 jars.addElement(d);
171             }
172         }
173     }
174 }
175
176
Popular Tags