-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJProcessManager.java
More file actions
189 lines (172 loc) · 4.46 KB
/
JProcessManager.java
File metadata and controls
189 lines (172 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package javaforce;
import java.net.*;
import java.awt.Window;
import java.util.*;
import java.security.Permission;
/**
* JProcessManager is used to manage multiple processes within one JVM.<br> You
* can create new processes and kill running processes.<br>
*
* @see JProcess
*/
public class JProcessManager {
private ArrayList<JProcess> list;
private int nextpid = 1;
private JSecurityManager sm;
private SecurityManager orgsm;
private ThreadGroup rootThreadGroup;
private Object lock;
private Thread mainThread;
public JProcessManager() {
list = new ArrayList<JProcess>();
orgsm = System.getSecurityManager();
sm = new JSecurityManager();
System.setSecurityManager(sm);
rootThreadGroup = new ThreadGroup("JProcessManager");
lock = new Object();
}
/**
* Create a new instance of JProcess.<br>
*
* @param className the name of the class that contains the main(String [])
* method.
* @param args a list of arguments to pass to main(String []).
* @param paths a list of URLs for the classpath. ie:
* "file:///c:/path/file.jar".
* @return JProcess an unstarted process.
*/
public JProcess createJProcess(String className, String[] args, URL[] paths, String user_dir) {
JProcess process;
synchronized (lock) {
process = JProcess.createInstance(className, args, paths, rootThreadGroup, nextpid++, user_dir);
if (process == null) {
return null;
}
list.add(process);
}
return process;
}
/**
* Returns a list of running process.
*/
public ArrayList<JProcess> getList() {
return list;
}
/**
* Kills a process based on its pid.
*
* @see JProcess.getPid
*/
public synchronized boolean kill(int pid) {
JProcess process = getProcessFor(pid);
if (process == null) {
return false;
}
synchronized (lock) {
list.remove(process);
}
process.kill();
System.gc();
return false;
}
/**
* Returns the current JProcess that is running in the current thread.
*/
public JProcess getCurrentProcess() {
return sm.getCurrentProcess();
}
private class JSecurityManager extends SecurityManager {
public void checkExit(int status) {
JProcess process = getCurrentProcess();
if (process != null) {
kill(process.getPid());
throw new SecurityException();
}
}
public void checkPermission(Permission perm) {
}
public void checkPermission(Permission perm, Object ctx) {
}
public boolean checkTopLevelWindow(Object obj) {
if (!(obj instanceof Window)) {
return true;
}
JProcess process = getCurrentProcess();
if (process == null) {
return true;
}
process.addWindow((Window) obj);
return true;
}
public JProcess getCurrentProcess() {
ThreadGroup group = getThreadGroup();
JProcess match = null;
match = getProcessFor(group);
if (match != null) {
return match;
}
Class[] class_context = getClassContext();
for (int i = 0; i < class_context.length; i++) {
ClassLoader loader = class_context[i].getClassLoader();
if (loader != null) {
match = getProcessFor(loader);
if (match != null) {
return match;
}
}
}
return null;
}
}
/**
* Returns a JProcess for the specified ThreadGroup
*
* @see JProcess.getThreadGroup
*/
JProcess getProcessFor(ThreadGroup threadGroup) {
JProcess process;
synchronized (lock) {
for (int a = 0; a < list.size(); a++) {
process = list.get(a);
if (process.getThreadGroup().parentOf(threadGroup)) {
return process;
}
}
}
return null;
}
/**
* Returns a JProcess for the specified ClassLoader
*
* @see JProcess.getClassLoader
*/
JProcess getProcessFor(ClassLoader classLoader) {
JProcess process;
synchronized (lock) {
for (int a = 0; a < list.size(); a++) {
process = list.get(a);
if (process.getClassLoader() == classLoader) {
return process;
}
}
}
return null;
}
/**
* Returns a JProcess for the specified pid
*
* @see JProcess.getPid
*/
JProcess getProcessFor(int pid) {
JProcess process;
synchronized (lock) {
for (int a = 0; a < list.size(); a++) {
process = list.get(a);
if (process.getPid() == pid) {
return process;
}
}
}
return null;
}
};