/Users/lyon/j4p/src/classUtils/loaders/Reloader.java
|
1 package classUtils.loaders;
2
3 // distClasses.Reloader
4
5 //package net.compute;
6
7 /**
8 * Created by
9 * User: lyon
10 * Date: Sep 3, 2003
11 * Time: 11:17:45 AM
12 *
13 */
14
15 import futils.DirList;
16 import utils.StringUtils;
17
18 import java.io.BufferedInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.util.Vector;
25
26 // Due 11/5/03:
27 // Prompt the user for the class directory.
28 // Scan the directory and sub-directories for all the classes.
29 // Return and print
30 // all the classes that implement ComputableObject.
31
32 // For the midterm Due 11/19/03
33 // Ask the user, using a Swing GUI, which computableObjects get deployed.
34 // Use a given remote machine to deploy and compute the object.
35 // Seriazable ans = Compute(ComputableObject co, String machineNameOrAddress);
36 // System.out.println(ans);
37 //
38
39
40 /**
41 * Use the reloader to get the bytecode from
42 * a file for a class. Then, transmit the class byte
43 * codes to the computation server. Use the
44 * Reloader on the computation server to define the
45 * class. Then compute the computation.
46 * Send the answer back to the compute client.
47 * Finally, set the remote reloader to null and
48 * close the connections.
49 */
50
51 public class Reloader extends ClassLoader {
52
53 private String classPath;
54
55 public Reloader() {
56 DirList dl = new DirList(".class");
57 classPath = dl.getStartDir().toString();
58 System.out.println("classPath" + classPath);
59 File f[] = dl.getFiles();
60 //dl.printFiles();
61 //print(loadClasses(getClassNames(f)));
62 }
63
64 public static Class[] loadClasses(String classNames[]) {
65 Class[] ca = new Class[classNames.length];
66 for (int i = 0; i < ca.length; i++) {
67 try {
68 ca[i] = Class.forName(classNames[i]);
69 } catch (ClassNotFoundException e) {
70 System.out.println("classNotFound:" + classNames[i]);
71 }
72 }
73 return ca;
74 }
75
76 public static void print(Object o[]) {
77 for (int i = 0; i < o.length; i++)
78 System.out.println(o[i]);
79 }
80
81 public String[] getClassNames(File f[]) {
82 Vector v = new Vector();
83 for (int i = 0; i < f.length; i++) {
84 String s = f[i].toString();
85 v.addElement(makeClassString(s));
86 }
87 String cn[] = new String[v.size()];
88 v.copyInto(cn);
89 return cn;
90 }
91
92 public static char getClassPathSeparator() {
93 String s = System.getProperty("file.separator");
94 return s.charAt(0);
95 }
96
97 public String makeClassString(String s) {
98 char c = getClassPathSeparator();
99 s = StringUtils.sub(s, classPath + c, "");
100 s = StringUtils.sub(s, ".class", "");
101 s = s.replace(c, '.');
102 return s;
103 }
104
105 public static void main(String args[]) {
106 Reloader rl = new Reloader();
107 }
108
109 public Reloader(String classPath) {
110 this.classPath = classPath;
111 }
112
113 public void defineClass(byte b[], String className) {
114 super.defineClass(className, b, 0, b.length);
115 }
116
117 public synchronized Class loadClass(String className,
118 boolean resolveIt)
119 throws ClassNotFoundException {
120
121 Class result;
122 byte classData[];
123
124 // Check the loaded class cache
125 result = findLoadedClass(className);
126 if (result != null) {
127 // Return a cached class
128 System.out.println("The class has already been loaded");
129 return result;
130 }
131
132 // If Spoofed, don't delegate
133 // result is 0 if strings are equal.
134 if (className.compareTo("Spoofed") != 0) {
135
136 // Check with the system class loader
137 try {
138 return loadWithSystemClassLoader(className);
139 } catch (ClassNotFoundException e) {
140 System.out.println("unable to load with system class loader...");
141 }
142 }
143
144 // Don't attempt to load a system file except through
145 // the primordial class loader
146 // for example, java.lang.String
147 if (className.startsWith("java.")) {
148 throw new ClassNotFoundException();
149 }
150
151 // Try to load it from the classPath directory.
152 classData = getByteCodes(className);
153 System.out.println("classData has " + classData.length + " bytes");
154 if (classData == null) {
155 System.out.println("Reloader - Can't load class: "
156 + className);
157 throw new ClassNotFoundException();
158 }
159
160 System.out.println("defining class..." + className);
161 try {
162 result = defineClass(null, classData, 0,
163 classData.length);
164 } catch (ClassFormatError classFormatError) {
165 classFormatError.printStackTrace();
166 System.out.println("class format error!!");
167 System.exit(0);
168 }
169 System.out.println("It worked!!! newly defined class:" + result);
170 if (result == null) {
171 System.out.println("Reloader - Class format error: "
172 + className);
173 throw new ClassFormatError();
174 }
175
176 if (resolveIt) {
177 resolveClass(result);
178 }
179
180 // Return class from classPath directory
181 return result;
182 }
183
184 private Class loadWithSystemClassLoader(String className)
185 throws ClassNotFoundException {
186 return super.findSystemClass(className);
187 }
188
189 public byte[] getByteCodes(String typeName) {
190
191 FileInputStream fis;
192 String fileName = classPath + File.separatorChar
193 + typeName.replace('.', File.separatorChar)
194 + ".class";
195
196 try {
197 fis = new FileInputStream(fileName);
198 } catch (FileNotFoundException e) {
199 e.printStackTrace();
200 return null;
201 }
202
203 BufferedInputStream bis = new BufferedInputStream(fis);
204
205 ByteArrayOutputStream baos = new ByteArrayOutputStream();
206
207 try {
208 int c = bis.read();
209 while (c != -1) {
210 baos.write(c);
211 c = bis.read();
212 }
213 } catch (IOException e) {
214 e.printStackTrace();
215 return null;
216 }
217
218 return baos.toByteArray();
219 }
220
221 public static void testLoadOneClass() {
222
223 String classPath = "C:\\lyon\\tomcat\\common\\classes";
224 String className1 = "net.compute.ComputeThis";
225
226 try {
227
228 Reloader rl =
229 new Reloader(classPath);
230 System.out.println("loading class...");
231 Class c = rl.loadClass(className1);
232 byte b[] = rl.getByteCodes(className1);
233 print(b);
234 } catch (ClassNotFoundException e) {
235 e.printStackTrace();
236 }
237
238
239 }
240
241 public static void print(byte b[]) {
242 for (int i = 0; i < b.length; i++) {
243 System.out.print((char) (0xff & b[i]));
244 if ((i % 10) == 0) System.out.println();
245 }
246 }
247
248 private static void reloadClasses(String classPath, String className)
249 throws ClassNotFoundException {
250 System.out.println("loading classpath:" + classPath
251 + "loading class name:" + className + "------");
252 Reloader rl =
253 new Reloader(classPath);
254 System.out.println("loading class...");
255 Class c = rl.loadClass(className);
256 System.out.println("class returned:" + c);
257 //((Runnable) c.newInstance()).run();
258 }
259
260 public static File[] getClassFiles() {
261 DirList dl = new DirList(".class");
262 return dl.getFiles();
263 }
264 }
265
266