00001
package com.quadcap.http.server22;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.util.Enumeration;
00042
import java.util.Hashtable;
00043
import java.util.Vector;
00044
00045
import java.io.File;
00046
import java.io.FileOutputStream;
00047
import java.io.InputStream;
00048
import java.io.IOException;
00049
00050
import java.net.URL;
00051
00052
import com.quadcap.io.dir.Directory;
00053
import com.quadcap.io.dir.Entry;
00054
00055
import com.quadcap.io.IO;
00056
00057
import com.quadcap.util.Debug;
00058
00059
00060
00061
00062
00063
00064
00065 public class WebClassLoader extends ClassLoader {
00066 File
tmpDir;
00067 Directory
root;
00068 Vector
jars =
new Vector();
00069
00070 public WebClassLoader(Directory root, File tmpDir) {
00071 super();
00072
this.tmpDir = tmpDir;
00073
this.root = root;
00074 }
00075
00076 public Class
loadClass(String name)
throws ClassNotFoundException {
00077
return loadClass(name,
true);
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 public Class
findClass(String name)
throws ClassNotFoundException {
00099
00100
if (
Trace.level() > 1) {
00101
Debug.println(
"WebClassLoader[" +
root +
00102
"].findClass(" + name +
")");
00103 }
00104
00105 byte[] b =
loadClassData(name);
00106
return defineClass(name, b, 0, b.length,
00107
this.getClass().getProtectionDomain());
00108 }
00109
00110 public URL
findResource(String name) {
00111 URL url = null;
00112
try {
00113 String fileName =
"WEB-INF/classes" + name;
00114 url =
root.getURL(fileName);
00115
if (url == null) {
00116
for (
int i = 0; url == null && i <
jars.size(); i++) {
00117 Directory d = (Directory)
jars.elementAt(i);
00118 url = d.getURL(name);
00119 }
00120 }
00121 }
catch (Throwable t) {
00122 url = null;
00123 }
00124
return url;
00125 }
00126
00127 private byte[]
loadClassData(String name)
throws ClassNotFoundException {
00128
try {
00129
Entry classFile =
locateClassFile(name);
00130 byte[] b =
new byte[(
int)(classFile.
getSize())];
00131 InputStream f = classFile.
getInputStream();
00132
IO.readFully(f, b);
00133 f.close();
00134
return b;
00135 }
catch (IOException e) {
00136
throw new ClassNotFoundException(
"error loading class: " +
00137 e.toString());
00138 }
00139 }
00140
00141 final Entry locateClassFile(String name)
throws ClassNotFoundException {
00142 String className = name.replace(
'.',
'/') +
".class";
00143 String fileName =
"WEB-INF/classes/" + className;
00144
Entry classFile =
root.getEntry(fileName);
00145
if (classFile == null) {
00146
for (
int i = 0; classFile == null && i <
jars.size(); i++) {
00147 Directory d = (Directory)
jars.elementAt(i);
00148 classFile = d.getEntry(className);
00149 }
00150 }
00151
if (classFile == null) {
00152
throw new ClassNotFoundException(
"not found: " + name);
00153 }
00154
return classFile;
00155 }
00156
00157 static int tmpCount = 0;
00158
00159 public String
getClassPath() {
00160 StringBuffer sb =
new StringBuffer();
00161 String delim = System.getProperty(
"path.separator");
00162
for (
int i = 0; i <
jars.size(); i++) {
00163 Directory d = (Directory)
jars.elementAt(i);
00164
if (i > 0) sb.append(delim);
00165 sb.append(d.getRootPath());
00166 }
00167
return sb.toString();
00168 }
00169
00170 final void init() throws IOException {
00171 Enumeration e =
root.entries();
00172 Directory classes = null;
00173 File tmpClasses = null;
00174
while (e.hasMoreElements()) {
00175 String path = e.nextElement().toString();
00176
if (path.startsWith(
"WEB-INF/lib/") && path.endsWith(
".jar")) {
00177 Directory d = null;
00178 String realPath =
root.getRealPath(path);
00179
if (realPath == null) {
00180
Entry entry =
root.getEntry(path);
00181 InputStream is = entry.
getInputStream();
00182 File out;
00183
try {
00184 out =
new File(
tmpDir,
00185
"tmp" + (
tmpCount++) +
".jar");
00186 FileOutputStream os =
new FileOutputStream(out);
00187
try {
00188
IO.copyStream(is, os);
00189 } finally {
00190 os.close();
00191 }
00192 } finally {
00193 is.close();
00194 }
00195 d = Directory.getDirectory(out);
00196 }
else {
00197 d = Directory.getDirectory(
new File(realPath));
00198 }
00199
jars.addElement(d);
00200 }
else if (path.startsWith(
"WEB-INF/classes")) {
00201
if (classes == null) {
00202 String realPath =
root.getRealPath(
"WEB-INF/classes");
00203
if (realPath == null) {
00204 tmpClasses =
new File(
tmpDir,
"classes");
00205 tmpClasses.mkdirs();
00206 classes = Directory.getDirectory(tmpClasses);
00207 }
else {
00208 classes = Directory.getDirectory(
new File(realPath));
00209 }
00210
jars.addElement(classes);
00211 }
00212
if (tmpClasses != null) {
00213 String subPath =
00214 path.substring(
"WEB-INF/classes/".length());
00215
if (subPath.length() > 0) {
00216 subPath = subPath.replace(
'/', File.separatorChar);
00217
Entry entry =
root.getEntry(path);
00218 File out =
new File(tmpClasses, subPath);
00219
if (entry.
isDirectory()) {
00220 out.mkdir();
00221 }
else {
00222 InputStream is = entry.
getInputStream();
00223
try {
00224 FileOutputStream os =
00225
new FileOutputStream(out);
00226
try {
00227
IO.copyStream(is, os);
00228 } finally {
00229 os.close();
00230 }
00231 } finally {
00232 is.close();
00233 }
00234 }
00235 }
00236 }
00237 }
00238 }
00239 }
00240 }
00241