00001 package edu.ksu.cis.bandera.util;
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 import java.io.*;
00039 import java.net.*;
00040 import java.util.*;
00041
00042 public class FileClassLoader extends ClassLoader {
00043
00044 private String root;
00045 public static FileClassLoader v = new FileClassLoader();
00046 private FileClassLoader() {
00047
00048
00049 root = null;
00050 }
00051 public FileClassLoader (String rootDir) {
00052 if (rootDir == null) throw new IllegalArgumentException ("Null root directory");
00053 root = rootDir;
00054 }
00055 public static Class load(String name) throws ClassNotFoundException
00056 {
00057 return v.loadClass(name);
00058 }
00059 protected Class loadClass (String name, boolean resolve) throws ClassNotFoundException {
00060
00061
00062
00063
00064
00065 Class c = findLoadedClass (name);
00066 if (c == null) {
00067 try {
00068 c = findSystemClass (name);
00069 } catch (Exception e) {
00070
00071 }
00072 }
00073
00074 if (c == null) {
00075
00076
00077 String filename = name.replace ('.', File.separatorChar) + ".class";
00078
00079 try {
00080
00081 byte data[] = loadClassData(filename);
00082
00083
00084 c = defineClass (name, data, 0, data.length);
00085
00086
00087 if (c == null) throw new ClassNotFoundException (name);
00088 } catch (IOException e) {
00089 throw new ClassNotFoundException ("Error reading file: " + filename);
00090 }
00091 }
00092
00093
00094 if (resolve) resolveClass (c);
00095
00096
00097 return c ;
00098 }
00099 private byte[] loadClassData (String filename) throws IOException {
00100
00101 File f = null;
00102
00103 if (root == null)
00104 {
00105
00106 String classPath = System.getProperty("java.class.path");
00107 StringTokenizer tok = new StringTokenizer(classPath, File.pathSeparator);
00108 boolean found = false;
00109
00110 while (tok.hasMoreTokens())
00111 {
00112 f = new File(tok.nextToken(), filename);
00113 if (f.exists()) { found = true; break; }
00114 }
00115 if (!found) throw new IOException(filename+" cannot be found in class path: "+classPath);
00116 } else f = new File (root, filename);
00117
00118
00119 int size = (int)f.length();
00120
00121
00122 byte buff[] = new byte[size];
00123
00124
00125 FileInputStream fis = new FileInputStream(f);
00126 DataInputStream dis = new DataInputStream (fis);
00127
00128
00129 dis.readFully (buff);
00130
00131
00132 dis.close();
00133
00134
00135 return buff;
00136 }
00137 public void setRootDirectory(String r)
00138 {
00139 if (r != null) root = r;
00140 }
00141 }