KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > server22 > WebClassLoader


1 package com.quadcap.http.server22;
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.dir.Directory;
53 import com.quadcap.io.dir.Entry;
54
55 import com.quadcap.io.IO;
56
57 import com.quadcap.util.Debug;
58
59
60 /**
61  * This class implements a JSP class loader
62  *
63  * @author Stan Bailes
64  */

65 public class WebClassLoader extends ClassLoader JavaDoc {
66     File JavaDoc tmpDir;
67     Directory root;
68     Vector JavaDoc jars = new Vector JavaDoc();
69
70     public WebClassLoader(Directory root, File JavaDoc tmpDir) {
71         super();
72         this.tmpDir = tmpDir;
73         this.root = root;
74     }
75
76     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
77     return loadClass(name, true);
78     }
79
80 //- //#ifdef JDK11
81
//- Hashtable cache = new Hashtable();
82
//-
83
//- public Class loadClass(String name, boolean resolve)
84
//- throws ClassNotFoundException
85
//- {
86
//- Class c = (Class)cache.get(name);
87
//- if (c == null) {
88
//- c = findClass(name);
89
//- cache.put(name, c);
90
//- }
91
//- if (resolve)
92
//- resolveClass(c);
93
//- return c;
94
//- }
95
//-
96
//#endif
97

98     public Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
99         //#ifdef DEBUG
100
if (Trace.level() > 1) {
101             Debug.println("WebClassLoader[" + root +
102                           "].findClass(" + name + ")");
103         }
104         //#endif
105
byte[] b = loadClassData(name);
106         return defineClass(name, b, 0, b.length,
107                            this.getClass().getProtectionDomain());
108     }
109
110     public URL JavaDoc findResource(String JavaDoc name) {
111         URL JavaDoc url = null;
112         try {
113             String JavaDoc fileName = "WEB-INF/classes" + name;
114             url = root.getURL(fileName);
115             if (url == null) {
116                 for (int i = 0; url == null && i < jars.size(); i++) {
117                     Directory d = (Directory)jars.elementAt(i);
118                     url = d.getURL(name);
119                 }
120             }
121         } catch (Throwable JavaDoc t) {
122             url = null;
123         }
124         return url;
125     }
126
127     private byte[] loadClassData(String JavaDoc name) throws ClassNotFoundException JavaDoc {
128         try {
129             Entry classFile = locateClassFile(name);
130             byte[] b = new byte[(int)(classFile.getSize())];
131             InputStream JavaDoc f = classFile.getInputStream();
132             IO.readFully(f, b);
133             f.close();
134             return b;
135         } catch (IOException JavaDoc e) {
136             throw new ClassNotFoundException JavaDoc("error loading class: " +
137                          e.toString());
138         }
139     }
140
141     final Entry locateClassFile(String JavaDoc name) throws ClassNotFoundException JavaDoc {
142         String JavaDoc className = name.replace('.', '/') + ".class";
143         String JavaDoc fileName = "WEB-INF/classes/" + className;
144         Entry classFile = root.getEntry(fileName);
145         if (classFile == null) {
146             for (int i = 0; classFile == null && i < jars.size(); i++) {
147                 Directory d = (Directory)jars.elementAt(i);
148                 classFile = d.getEntry(className);
149             }
150         }
151         if (classFile == null) {
152             throw new ClassNotFoundException JavaDoc("not found: " + name);
153         }
154         return classFile;
155     }
156
157     static int tmpCount = 0;
158
159     public String JavaDoc getClassPath() {
160         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
161         String JavaDoc delim = System.getProperty("path.separator");
162         for (int i = 0; i < jars.size(); i++) {
163             Directory d = (Directory)jars.elementAt(i);
164             if (i > 0) sb.append(delim);
165             sb.append(d.getRootPath());
166         }
167         return sb.toString();
168     }
169     
170     final void init() throws IOException JavaDoc {
171         Enumeration JavaDoc e = root.entries();
172         Directory classes = null;
173         File JavaDoc tmpClasses = null;
174         while (e.hasMoreElements()) {
175             String JavaDoc path = e.nextElement().toString();
176             if (path.startsWith("WEB-INF/lib/") && path.endsWith(".jar")) {
177                 Directory d = null;
178                 String JavaDoc realPath = root.getRealPath(path);
179                 if (realPath == null) {
180                     Entry entry = root.getEntry(path);
181                     InputStream JavaDoc is = entry.getInputStream();
182                     File JavaDoc out;
183                     try {
184                         out = new File JavaDoc(tmpDir,
185                                        "tmp" + (tmpCount++) + ".jar");
186                         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(out);
187                         try {
188                             IO.copyStream(is, os);
189                         } finally {
190                             os.close();
191                         }
192                     } finally {
193                         is.close();
194                     }
195                     d = Directory.getDirectory(out);
196                 } else {
197                     d = Directory.getDirectory(new File JavaDoc(realPath));
198                 }
199                 jars.addElement(d);
200             } else if (path.startsWith("WEB-INF/classes")) {
201                 if (classes == null) {
202                     String JavaDoc realPath = root.getRealPath("WEB-INF/classes");
203                     if (realPath == null) {
204                         tmpClasses = new File JavaDoc(tmpDir, "classes");
205                         tmpClasses.mkdirs();
206                         classes = Directory.getDirectory(tmpClasses);
207                     } else {
208                         classes = Directory.getDirectory(new File JavaDoc(realPath));
209                     }
210                     jars.addElement(classes);
211                 }
212                 if (tmpClasses != null) {
213                     String JavaDoc subPath =
214                         path.substring("WEB-INF/classes/".length());
215                     if (subPath.length() > 0) {
216                         subPath = subPath.replace('/', File.separatorChar);
217                         Entry entry = root.getEntry(path);
218                         File JavaDoc out = new File JavaDoc(tmpClasses, subPath);
219                         if (entry.isDirectory()) {
220                             out.mkdir();
221                         } else {
222                             InputStream JavaDoc is = entry.getInputStream();
223                             try {
224                                 FileOutputStream JavaDoc os =
225                                     new FileOutputStream JavaDoc(out);
226                                 try {
227                                     IO.copyStream(is, os);
228                                 } finally {
229                                     os.close();
230                                 }
231                             } finally {
232                                 is.close();
233                             }
234                         }
235                     }
236                 }
237             }
238         }
239     }
240 }
241
242
Popular Tags