1 |
ajm |
1.1 |
// Some imports |
2 |
|
|
import java.util.*; |
3 |
|
|
import java.lang.reflect.*; |
4 |
|
|
|
5 |
|
|
// This class is a wrapper for running applications |
6 |
|
|
// a basic thread extension |
7 |
|
|
class ApplicationThread extends Thread { |
8 |
|
|
|
9 |
|
|
// The constructor takes the name of the method we're gonna run |
10 |
|
|
// and the arguments we're gonna pass into it |
11 |
|
|
ApplicationThread(Method mainMethod, Object[] args) { |
12 |
|
|
_mainMethod = mainMethod; |
13 |
|
|
_args = args; |
14 |
|
|
} |
15 |
|
|
|
16 |
|
|
// The main bit of the thread just starts the functionality of the given |
17 |
|
|
// method running |
18 |
|
|
public void run() { |
19 |
|
|
try { |
20 |
|
|
System.out.println(" Starting: "+ _mainMethod.getDeclaringClass().toString()); |
21 |
|
|
_mainMethod.invoke(null, _args); |
22 |
|
|
System.out.println(" Finished: "+ _mainMethod.getDeclaringClass().toString()); |
23 |
|
|
} catch (Exception e){ |
24 |
|
|
System.err.println("ERROR: " + e); |
25 |
|
|
e.printStackTrace(System.out); |
26 |
|
|
} |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
Method _mainMethod = null; |
30 |
|
|
Object[] _args = null; |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
// The JVMManager takes a list of classes and instantiates them within the |
34 |
|
|
// single VM that it is running in |
35 |
|
|
// |
36 |
|
|
// Future enhancements include: |
37 |
|
|
// Passing of arguements to each class |
38 |
|
|
// Seperation of in/out/err for each app |
39 |
|
|
// GUI mode |
40 |
|
|
// VM Thread control (ie, Kill, HUP etc...) |
41 |
|
|
// Implementation of Java Security, specifically ProtectionDomain for each app |
42 |
|
|
// |
43 |
|
|
// Version 1.0 |
44 |
|
|
// |
45 |
|
|
// (c) Copyright 2000 Alex Moore |
46 |
|
|
// |
47 |
|
|
class JVMManager { |
48 |
|
|
public static void main(String args[]) { |
49 |
|
|
// no exceptions are yet caught - not good |
50 |
|
|
try { |
51 |
|
|
|
52 |
|
|
// get hook on the runtime |
53 |
|
|
Runtime myRuntime = Runtime.getRuntime(); |
54 |
|
|
// get hook on the system Class loader |
55 |
|
|
ClassLoader myClassLoader = ClassLoader.getSystemClassLoader(); |
56 |
|
|
System.out.println("JVM Manager Started."); |
57 |
|
|
System.out.println(" Total memory: " + myRuntime.totalMemory()); |
58 |
|
|
System.out.println(" Free memory: " + myRuntime.freeMemory()); |
59 |
|
|
System.out.println(); |
60 |
|
|
|
61 |
|
|
// for the time being we are just accepting a list of classes |
62 |
|
|
// so just get the command line and assume that is just a simple list of classes to run |
63 |
|
|
String[] appList = args; |
64 |
|
|
|
65 |
|
|
// step through each app, and start it rolling |
66 |
|
|
for (int i=0; i < appList.length; i++) { |
67 |
|
|
|
68 |
|
|
// Load the class |
69 |
|
|
Class myClass = myClassLoader.loadClass(appList[i]); |
70 |
|
|
// and we don't support passing of individual args, but we will ;-) so its just null |
71 |
|
|
String[] passedArgs = {}; |
72 |
|
|
|
73 |
|
|
System.out.println("*** Class Loaded: " + myClass.toString()); |
74 |
|
|
|
75 |
|
|
System.out.println(" Listing declared methods:"); |
76 |
|
|
|
77 |
|
|
// Just because we can, we're going to list the declared methods...no reason |
78 |
|
|
Method[] theMethods = myClass.getDeclaredMethods(); |
79 |
|
|
for (int x=0; x < theMethods.length; x++) { |
80 |
|
|
System.out.println(" "+ theMethods[x].toString()); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
// create an array of classes that are listed in the main method header |
84 |
|
|
// so we can get a hook to the main method |
85 |
|
|
Class[] passedArgsClass = {passedArgs.getClass()}; |
86 |
|
|
// get a hook on the main method |
87 |
|
|
Method myMethod = myClass.getMethod("main",passedArgsClass); |
88 |
|
|
|
89 |
|
|
System.out.println(" Got a hook to the main method."); |
90 |
|
|
System.out.println(" Creating and passing start up arguments to thread."); |
91 |
|
|
|
92 |
|
|
// Now we know where it is, we need to create a thread for it, passing in |
93 |
|
|
// a list of objects that represent its args. |
94 |
|
|
Object[] passedArgsObject = {passedArgs}; |
95 |
|
|
ApplicationThread anApp = new ApplicationThread(myMethod, passedArgsObject); |
96 |
|
|
|
97 |
|
|
// Finally we start it going |
98 |
|
|
System.out.println(" Starting Thread."); |
99 |
|
|
anApp.start(); |
100 |
|
|
|
101 |
|
|
// Now we go and start the rest of the Apps listed |
102 |
|
|
|
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
// At this point we are finished, though in the future the VM Manager |
106 |
|
|
// won't just die away, but will remain to carry out JVM administration and |
107 |
|
|
// thread management. |
108 |
|
|
System.out.println(); |
109 |
|
|
System.out.println("JVM Manager Finished."); |
110 |
|
|
System.out.println(" Free memory: " + myRuntime.freeMemory()); |
111 |
|
|
System.out.println(); |
112 |
|
|
|
113 |
|
|
} catch (Exception e) { |
114 |
|
|
System.err.println("ERROR: " + e); |
115 |
|
|
e.printStackTrace(System.out); |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
} |
119 |
|
|
|