|
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 |
| package edu.rice.cs.plt.concurrent; |
|
36 |
| |
|
37 |
| import java.io.File; |
|
38 |
| import java.io.IOException; |
|
39 |
| import java.util.Collections; |
|
40 |
| import java.util.Enumeration; |
|
41 |
| import java.util.HashMap; |
|
42 |
| import java.util.LinkedList; |
|
43 |
| import java.util.List; |
|
44 |
| import java.util.Map; |
|
45 |
| import java.util.Properties; |
|
46 |
| |
|
47 |
| import edu.rice.cs.plt.collect.CollectUtil; |
|
48 |
| import edu.rice.cs.plt.io.IOUtil; |
|
49 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
50 |
| import edu.rice.cs.plt.iter.SizedIterable; |
|
51 |
| import edu.rice.cs.plt.lambda.Lambda2; |
|
52 |
| import edu.rice.cs.plt.lambda.WrappedException; |
|
53 |
| import edu.rice.cs.plt.reflect.ReflectUtil; |
|
54 |
| import edu.rice.cs.plt.text.TextUtil; |
|
55 |
| |
|
56 |
| import static edu.rice.cs.plt.io.IOUtil.attemptAbsoluteFiles; |
|
57 |
| import static edu.rice.cs.plt.iter.IterUtil.snapshot; |
|
58 |
| import static edu.rice.cs.plt.collect.CollectUtil.snapshot; |
|
59 |
| import static edu.rice.cs.plt.debug.DebugUtil.debug; |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| public class JVMBuilder implements Lambda2<String, Iterable<? extends String>, Process> { |
|
81 |
| |
|
82 |
| private static final String DEFAULT_JAVA_COMMAND = findJavaCommand(System.getProperty("java.home", "")); |
|
83 |
| private static final SizedIterable<String> DEFAULT_JVM_ARGS = IterUtil.empty(); |
|
84 |
| private static final SizedIterable<File> DEFAULT_CLASS_PATH = attemptAbsoluteFiles(ReflectUtil.SYSTEM_CLASS_PATH); |
|
85 |
| private static final File DEFAULT_DIR = IOUtil.WORKING_DIRECTORY; |
|
86 |
| private static final Map<String, String> DEFAULT_PROPERTIES = Collections.emptyMap(); |
|
87 |
| private static final Map<String, String> DEFAULT_ENVIRONMENT = null; |
|
88 |
| |
|
89 |
| public static final JVMBuilder DEFAULT = new JVMBuilder(); |
|
90 |
| |
|
91 |
| private final String _javaCommand; |
|
92 |
| private final SizedIterable<String> _jvmArgs; |
|
93 |
| private final SizedIterable<File> _classPath; |
|
94 |
| private final File _dir; |
|
95 |
| private final Map<String, String> _properties; |
|
96 |
| private final Map<String, String> _environment; |
|
97 |
| |
|
98 |
3
| private JVMBuilder() {
|
|
99 |
3
| this(DEFAULT_JAVA_COMMAND, DEFAULT_JVM_ARGS, DEFAULT_CLASS_PATH, DEFAULT_DIR, DEFAULT_PROPERTIES,
|
|
100 |
| DEFAULT_ENVIRONMENT, true); |
|
101 |
| } |
|
102 |
| |
|
103 |
0
| public JVMBuilder(String javaCommand) {
|
|
104 |
0
| this(findJavaCommand(javaCommand), DEFAULT_JVM_ARGS, DEFAULT_CLASS_PATH, DEFAULT_DIR,
|
|
105 |
| DEFAULT_PROPERTIES, DEFAULT_ENVIRONMENT, true); |
|
106 |
| } |
|
107 |
| |
|
108 |
0
| public JVMBuilder(String javaCommand, Iterable<? extends String> jvmArgs) {
|
|
109 |
0
| this(findJavaCommand(javaCommand), snapshot(jvmArgs), DEFAULT_CLASS_PATH, DEFAULT_DIR,
|
|
110 |
| DEFAULT_PROPERTIES, DEFAULT_ENVIRONMENT, true); |
|
111 |
| } |
|
112 |
| |
|
113 |
1
| public JVMBuilder(Iterable<? extends File> classPath) {
|
|
114 |
1
| this(DEFAULT_JAVA_COMMAND, DEFAULT_JVM_ARGS, attemptAbsoluteFiles(classPath), DEFAULT_DIR,
|
|
115 |
| DEFAULT_PROPERTIES, DEFAULT_ENVIRONMENT, true); |
|
116 |
| } |
|
117 |
| |
|
118 |
1
| public JVMBuilder(File dir) {
|
|
119 |
1
| this(DEFAULT_JAVA_COMMAND, DEFAULT_JVM_ARGS, DEFAULT_CLASS_PATH, dir, DEFAULT_PROPERTIES,
|
|
120 |
| DEFAULT_ENVIRONMENT, true); |
|
121 |
| } |
|
122 |
| |
|
123 |
0
| public JVMBuilder(String javaCommand, Iterable<? extends String> jvmArgs, Iterable<? extends File> classPath,
|
|
124 |
| File dir, Map<? extends String, ? extends String> properties, |
|
125 |
| Map<? extends String, ? extends String> environment) { |
|
126 |
0
| this(findJavaCommand(javaCommand), snapshot(jvmArgs), attemptAbsoluteFiles(classPath), dir,
|
|
127 |
0
| snapshot(properties), (environment == null) ? null : snapshot(environment), true);
|
|
128 |
| } |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
17
| private JVMBuilder(String javaCommand, SizedIterable<String> jvmArgs, SizedIterable<File> classPath,
|
|
135 |
| File dir, Map<String, String> properties, Map<String, String> environment, boolean dummy) { |
|
136 |
17
| _javaCommand = javaCommand;
|
|
137 |
17
| _jvmArgs = jvmArgs;
|
|
138 |
17
| _classPath = classPath;
|
|
139 |
17
| _dir = dir;
|
|
140 |
17
| _properties = properties;
|
|
141 |
17
| _environment = environment;
|
|
142 |
| } |
|
143 |
| |
|
144 |
0
| public String javaCommand() { return _javaCommand; }
|
|
145 |
| |
|
146 |
0
| public JVMBuilder javaCommand(String javaCommand) {
|
|
147 |
0
| return new JVMBuilder(findJavaCommand(javaCommand), _jvmArgs, _classPath, _dir, _properties, _environment, true);
|
|
148 |
| } |
|
149 |
| |
|
150 |
0
| public JVMBuilder javaCommand(File javaCommand) {
|
|
151 |
0
| return new JVMBuilder(findJavaCommand(javaCommand), _jvmArgs, _classPath, _dir, _properties, _environment, true);
|
|
152 |
| } |
|
153 |
| |
|
154 |
0
| public SizedIterable<String> jvmArguments() { return _jvmArgs; }
|
|
155 |
| |
|
156 |
0
| public JVMBuilder jvmArguments(Iterable<? extends String> jvmArgs) {
|
|
157 |
0
| return new JVMBuilder(_javaCommand, IterUtil.snapshot(jvmArgs), _classPath, _dir, _properties, _environment, true);
|
|
158 |
| } |
|
159 |
| |
|
160 |
| |
|
161 |
0
| public JVMBuilder jvmArguments(String... jvmArgs) {
|
|
162 |
0
| return new JVMBuilder(_javaCommand, IterUtil.make(jvmArgs), _classPath, _dir, _properties, _environment, true);
|
|
163 |
| } |
|
164 |
| |
|
165 |
0
| public SizedIterable<File> classPath() { return _classPath; }
|
|
166 |
| |
|
167 |
0
| public JVMBuilder classPath(Iterable<? extends File> classPath) {
|
|
168 |
0
| return new JVMBuilder(_javaCommand, _jvmArgs, attemptAbsoluteFiles(classPath), _dir,
|
|
169 |
| _properties, _environment, true); |
|
170 |
| } |
|
171 |
| |
|
172 |
0
| public JVMBuilder classPath(String classPath) {
|
|
173 |
0
| return new JVMBuilder(_javaCommand, _jvmArgs, attemptAbsoluteFiles(IOUtil.parsePath(classPath)), _dir,
|
|
174 |
| _properties, _environment, true); |
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
0
| public JVMBuilder classPath(File... classPath) {
|
|
179 |
0
| return new JVMBuilder(_javaCommand, _jvmArgs, attemptAbsoluteFiles(IterUtil.asIterable(classPath)), _dir,
|
|
180 |
| _properties, _environment, true); |
|
181 |
| } |
|
182 |
| |
|
183 |
0
| public File directory() { return _dir; }
|
|
184 |
| |
|
185 |
0
| public JVMBuilder directory(File dir) {
|
|
186 |
0
| return new JVMBuilder(_javaCommand, _jvmArgs, _classPath, dir, _properties, _environment, true);
|
|
187 |
| } |
|
188 |
| |
|
189 |
0
| public JVMBuilder directory(String dir) {
|
|
190 |
0
| return new JVMBuilder(_javaCommand, _jvmArgs, _classPath, new File(dir), _properties, _environment, true);
|
|
191 |
| } |
|
192 |
| |
|
193 |
| |
|
194 |
0
| public Map<String, String> properties() { return CollectUtil.immutable(_properties); }
|
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
12
| public Map<String, String> propertiesCopy() { return snapshot(_properties); }
|
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
0
| public JVMBuilder properties(Properties ps) {
|
|
207 |
0
| return new JVMBuilder(_javaCommand, _jvmArgs, _classPath, _dir, copyProps(ps), _environment, true);
|
|
208 |
| } |
|
209 |
| |
|
210 |
| |
|
211 |
12
| public JVMBuilder properties(Map<? extends String, ? extends String> ps) {
|
|
212 |
12
| return new JVMBuilder(_javaCommand, _jvmArgs, _classPath, _dir, snapshot(ps), _environment, true);
|
|
213 |
| } |
|
214 |
| |
|
215 |
| |
|
216 |
4
| public JVMBuilder addProperty(String key, String value) {
|
|
217 |
4
| Map<String, String> newProps = propertiesCopy();
|
|
218 |
4
| newProps.put(key, value);
|
|
219 |
4
| return properties(newProps);
|
|
220 |
| } |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
8
| public JVMBuilder addDefaultProperties(Properties ps) { return addDefaultProperties(copyProps(ps)); }
|
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
8
| public JVMBuilder addDefaultProperties(Map<? extends String, ? extends String> ps) {
|
|
234 |
0
| if (_properties.keySet().containsAll(ps.keySet())) { return this; }
|
|
235 |
| else { |
|
236 |
8
| Map<String, String> newProps = propertiesCopy();
|
|
237 |
8
| for (Map.Entry<? extends String, ? extends String> entry : ps.entrySet()) {
|
|
238 |
16
| if (!newProps.containsKey(entry.getKey())) { newProps.put(entry.getKey(), entry.getValue()); }
|
|
239 |
| } |
|
240 |
8
| return properties(newProps);
|
|
241 |
| } |
|
242 |
| } |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
4
| public JVMBuilder addDefaultProperty(String key, String value) {
|
|
249 |
4
| return _properties.containsKey(key) ? this : addProperty(key, value);
|
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
0
| public Map<String, String> environment() {
|
|
254 |
0
| return (_environment == null) ? null : CollectUtil.immutable(_environment);
|
|
255 |
| } |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
0
| public Map<String, String> environmentCopy() {
|
|
264 |
0
| return snapshot((_environment == null) ? System.getenv() : _environment);
|
|
265 |
| } |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
0
| public JVMBuilder environment(Map<? extends String, ? extends String> env) {
|
|
272 |
0
| return new JVMBuilder(_javaCommand, _jvmArgs, _classPath, _dir, _properties,
|
|
273 |
0
| (_environment == null) ? null : snapshot(env), true);
|
|
274 |
| } |
|
275 |
| |
|
276 |
| |
|
277 |
0
| public JVMBuilder addEnvironmentVar(String key, String value) {
|
|
278 |
0
| Map<String, String> newEnv = environmentCopy();
|
|
279 |
0
| newEnv.put(key, value);
|
|
280 |
0
| return environment(newEnv);
|
|
281 |
| } |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
0
| public JVMBuilder addDefaultEnvironmentVars(Map<? extends String, ? extends String> env) {
|
|
288 |
0
| if (_environment != null && _environment.keySet().containsAll(env.keySet())) { return this; }
|
|
289 |
| else { |
|
290 |
0
| Map<String, String> newEnv = environmentCopy();
|
|
291 |
0
| for (Map.Entry<? extends String, ? extends String> entry : env.entrySet()) {
|
|
292 |
0
| if (!newEnv.containsKey(entry.getKey())) { newEnv.put(entry.getKey(), entry.getValue()); }
|
|
293 |
| } |
|
294 |
0
| return environment(newEnv);
|
|
295 |
| } |
|
296 |
| } |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
0
| public JVMBuilder AddDefaultEnvironmentVar(String key, String value) {
|
|
303 |
0
| if (_environment != null && _environment.containsKey(key)) { return this; }
|
|
304 |
0
| else { return addEnvironmentVar(key, value); }
|
|
305 |
| } |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
3
| public Process start(String mainClass, String... mainParams) throws IOException {
|
|
313 |
3
| return start(mainClass, IterUtil.asIterable(mainParams));
|
|
314 |
| } |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
11
| public Process start(String mainClass, Iterable<? extends String> mainParams) throws IOException {
|
|
325 |
11
| List<String> commandL = new LinkedList<String>();
|
|
326 |
11
| commandL.add(_javaCommand);
|
|
327 |
11
| CollectUtil.addAll(commandL, _jvmArgs);
|
|
328 |
11
| commandL.add("-classpath");
|
|
329 |
11
| commandL.add(IOUtil.pathToString(_classPath));
|
|
330 |
11
| for (Map.Entry<String, String> prop : _properties.entrySet()) {
|
|
331 |
20
| commandL.add("-D" + prop.getKey() + "=" + prop.getValue());
|
|
332 |
| } |
|
333 |
11
| commandL.add(mainClass);
|
|
334 |
11
| CollectUtil.addAll(commandL, mainParams);
|
|
335 |
11
| String[] command = IterUtil.toArray(commandL, String.class);
|
|
336 |
| |
|
337 |
11
| String[] env;
|
|
338 |
11
| if (_environment == null) { env = null; }
|
|
339 |
| else { |
|
340 |
0
| List<String> envL = new LinkedList<String>();
|
|
341 |
0
| for (Map.Entry<String, String> binding : _environment.entrySet()) {
|
|
342 |
0
| envL.add(binding.getKey() + "=" + binding.getValue());
|
|
343 |
| } |
|
344 |
0
| env = IterUtil.toArray(envL, String.class);
|
|
345 |
| } |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
11
| return Runtime.getRuntime().exec(command, env, _dir);
|
|
350 |
| } |
|
351 |
| |
|
352 |
0
| public Process value(String mainClass, Iterable<? extends String> mainParams) {
|
|
353 |
0
| try { return start(mainClass, mainParams); }
|
|
354 |
0
| catch (IOException e) { throw new WrappedException(e); }
|
|
355 |
| } |
|
356 |
| |
|
357 |
3
| private static String findJavaCommand(String command) {
|
|
358 |
3
| return findJavaCommand(new File(command));
|
|
359 |
| } |
|
360 |
| |
|
361 |
| |
|
362 |
3
| private static String findJavaCommand(File f) {
|
|
363 |
0
| if (IOUtil.attemptIsFile(f)) { return f.getPath(); }
|
|
364 |
3
| else if (IOUtil.attemptIsDirectory(f)) {
|
|
365 |
| |
|
366 |
3
| f = IOUtil.attemptAbsoluteFile(f);
|
|
367 |
3
| String os = System.getProperty("os.name", "");
|
|
368 |
3
| File[] candidates = new File[]{ new File(f, "../bin"), new File(f, "bin"), f };
|
|
369 |
| |
|
370 |
3
| if (!TextUtil.containsIgnoreCase(os, "netware")) {
|
|
371 |
3
| if (TextUtil.containsIgnoreCase(os, "windows")) {
|
|
372 |
0
| for (File dir : candidates) {
|
|
373 |
0
| File result = new File(dir, "javaw.exe");
|
|
374 |
0
| if (IOUtil.attemptExists(result)) { return result.getPath(); }
|
|
375 |
0
| result = new File(dir, "java.exe");
|
|
376 |
0
| if (IOUtil.attemptExists(result)) { return result.getPath(); }
|
|
377 |
| } |
|
378 |
| } |
|
379 |
| else { |
|
380 |
3
| for (File dir : candidates) {
|
|
381 |
3
| File result = new File(dir, "java");
|
|
382 |
3
| if (IOUtil.attemptExists(result)) { return result.getPath(); }
|
|
383 |
| } |
|
384 |
| } |
|
385 |
| } |
|
386 |
| } |
|
387 |
| |
|
388 |
0
| return f.toString();
|
|
389 |
| } |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
8
| private static Map<String, String> copyProps(Properties p) {
|
|
398 |
8
| Map<String, String> result = new HashMap<String, String>();
|
|
399 |
8
| @SuppressWarnings("unchecked") Enumeration<String> names = (Enumeration<String>) p.propertyNames();
|
|
400 |
8
| while (names.hasMoreElements()) {
|
|
401 |
16
| String name = names.nextElement();
|
|
402 |
16
| result.put(name, p.getProperty(name));
|
|
403 |
| } |
|
404 |
8
| return result;
|
|
405 |
| } |
|
406 |
| |
|
407 |
| } |