/Users/lyon/j4p/src/classUtils/loaders/ByteCodeContainer.java
|
1 package classUtils.loaders;
2
3 //package net.compute;
4
5 import net.compute.DelegatingLoader;
6
7 import java.io.*;
8
9 /**
10 Send the ByteCodeContainer to a remote
11 JVM. It uses the RCL to define a new class and
12 compute an answer. Computation servers need these
13 things to be sent by the computation clients.
14 */
15
16 public class ByteCodeContainer
17 extends ClassLoader
18 implements Serializable {
19 String className;
20 byte byteCodes[];
21
22 public static ByteCodeContainer getLoader(ComputableObject o) {
23 return new ByteCodeContainer(o.getClass());
24 }
25
26 public ByteCodeContainer(Class c) {
27 className = c.getName();
28 Reloader rl = new Reloader("C:\\lyon\\j4p\\classes");
29 byteCodes = rl.getByteCodes(className);
30 }
31
32 public ByteCodeContainer(String cn, byte b[]) {
33 byteCodes = b;
34 className = cn;
35 }
36
37 public ByteCodeContainer(byte b[]) {
38 byteCodes = b;
39 className = null;
40 }
41
42 public void loadIt() {
43 loadClass(className, true);
44 }
45
46 public Serializable compute()
47 throws InstantiationException, IllegalAccessException {
48 Class c = defineClass(
49 className, byteCodes, 0, byteCodes.length);
50 Object o = c.newInstance();
51 if (!(o instanceof ComputableObject)) {
52 System.out.println("this is computable in remoteclass loader");
53 return null;
54 }
55 return ((ComputableObject) o).compute();
56 }
57
58 public void reload() {
59 // force the class to be reloaded,
60 // in case it has changed.
61 try {
62 Class c = defineClass(
63 className, byteCodes, 0, byteCodes.length);
64 System.out.println("resolving:" + className);
65 resolveClass(c);
66 } catch (Exception e) {
67
68 }
69 }
70
71 Class cl = null;
72
73 public Class getLoadedClass() {
74 loadIt();
75 return cl;
76 }
77
78 protected synchronized Class loadClass(String s, boolean b) {
79 cl = findLoadedClass(s);
80 try {
81 if (cl == null) // not in cache!
82 return findSystemClass(s);
83 } catch (Exception e) {
84 e.printStackTrace();
85 }
86 System.out.println("defining:" + s);
87 Class c = defineClass(
88 className, byteCodes, 0, byteCodes.length);
89 System.out.println("resolving:" + className);
90 if (c != null && b) resolveClass(c);
91 return c;
92 }
93
94 public static void main(String args[]) {
95 File f = futils.Futil.getReadFile("select a class file");
96 System.out.println("you selected:" + f);
97 String className = f.getName();
98 byte[] byteCodes = futils.Futil.readBytes(f);
99 ByteCodeContainer bcc = new ByteCodeContainer("", byteCodes);
100
101 Class c = DelegatingLoader.getClassFromFile(f);
102 System.out.println("you load a class:" + c);
103 Class interfaceClasses[] = c.getInterfaces();
104 print(interfaceClasses);
105 }
106
107 public static void print(Object o[]) {
108 for (int i = 0; i < o.length; i++)
109 System.out.println(o.toString());
110 }
111 }