00001 package com.quadcap.io.dir;
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.IO;
00053
00054
00055
00056
00057
00058
00059
00060 public class ClassLoader extends java.lang.
ClassLoader {
00061 Directory root;
00062 Vector
jars =
new Vector();
00063
00064 public ClassLoader(
Directory root) {
00065 super();
00066
this.root = root;
00067
try {
00068
init();
00069 }
catch (IOException ex) {
00070 }
00071 }
00072
00073
00074 public Class
loadClass(String name)
throws ClassNotFoundException {
00075
return loadClass(name,
true);
00076 }
00077
00078
00079 public synchronized Class loadClass(String name,
boolean resolve)
00080
throws ClassNotFoundException
00081 {
00082
00083 Class c = findLoadedClass(name);
00084
if (c == null) {
00085
try {
00086 c =
findClass(name);
00087 }
catch (ClassNotFoundException e) {
00088 c = super.loadClass(name, resolve);
00089 }
00090 }
00091
if (resolve) {
00092 resolveClass(c);
00093 }
00094
return c;
00095 }
00096
00097 public Class
findClass(String name)
throws ClassNotFoundException {
00098 byte[] b =
loadClassData(name);
00099
return defineClass(name, b, 0, b.length,
00100
this.getClass().getProtectionDomain());
00101 }
00102
00103 public URL
findResource(String name) {
00104 URL url = null;
00105
try {
00106 String fileName =
"META-INF" + name;
00107
for (
int i = 0; url == null && i <
jars.size(); i++) {
00108
Directory d = (
Directory)
jars.elementAt(i);
00109 url = d.
getURL(name);
00110 }
00111 }
catch (Throwable t) {
00112 url = null;
00113 }
00114
return url;
00115 }
00116
00117 private byte[]
loadClassData(String name)
throws ClassNotFoundException {
00118
try {
00119
Entry classFile =
locateClassFile(name);
00120 byte[] b =
new byte[(
int)(classFile.
getSize())];
00121 InputStream f = classFile.
getInputStream();
00122
IO.readFully(f, b);
00123 f.close();
00124
return b;
00125 }
catch (IOException e) {
00126
throw new ClassNotFoundException(
"error loading class: " +
00127 e.toString());
00128 }
00129 }
00130
00131 final Entry locateClassFile(String name)
throws ClassNotFoundException {
00132 String className = name.replace(
'.',
'/') +
".class";
00133
Entry classFile = null;
00134
for (
int i = 0; classFile == null && i <
jars.size(); i++) {
00135
Directory d = (
Directory)
jars.elementAt(i);
00136 classFile = d.
getEntry(className);
00137 }
00138
if (classFile == null) {
00139
throw new ClassNotFoundException(
"not found: " + name);
00140 }
00141
return classFile;
00142 }
00143
00144 static int tmpCount = 0;
00145
00146 public String
getClassPath() {
00147 StringBuffer sb =
new StringBuffer();
00148 String delim = System.getProperty(
"path.separator");
00149
for (
int i = 0; i <
jars.size(); i++) {
00150
Directory d = (
Directory)
jars.elementAt(i);
00151
if (i > 0) sb.append(delim);
00152 sb.append(d.
getRootPath());
00153 }
00154
return sb.toString();
00155 }
00156
00157 final void init() throws IOException {
00158 Enumeration e =
root.
entries();
00159
Directory classes = null;
00160 File tmpClasses = null;
00161
while (e.hasMoreElements()) {
00162 String path = e.nextElement().toString();
00163
if (path.endsWith(
".jar")) {
00164
Directory d = null;
00165 String realPath =
root.
getRealPath(path);
00166
if (realPath == null) {
00167
throw new IOException(
"No Path: " + path);
00168 }
00169 d =
Directory.
getDirectory(
new File(realPath));
00170
jars.addElement(d);
00171 }
00172 }
00173 }
00174 }
00175