1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.gridsystems.utils;
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.regex.Pattern;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33
34
35
36
37
38
39 public final class SystemUtils {
40
41
42
43 public static final String WINDOWS_X86 = "Windows_NT+x86";
44
45 public static final String WINDOWS_IA64 = "Windows_NT+ia64";
46
47 public static final String SUNOS_SPARC = "SunOS+sparc";
48
49 public static final String SUNOS_I386 = "SunOS+i386";
50
51 public static final String LINUX_I686 = "Linux+i686";
52
53 public static final String LINUX_IA64 = "Linux+ia64";
54
55 public static final String LINUX_AMD64 = "Linux+x86_64";
56
57 public static final String HPUX_PA = "HP-UX+hppa2.0w";
58
59 public static final String HPUX_IA64 = "HP-UX+ia64";
60
61 public static final String AIX_POWERPC = "AIX+powerpc";
62
63 public static final String MACOSX_POWERPC = "Darwin+powerpc";
64
65 public static final String MACOSX_I386 = "Darwin+i386";
66
67
68
69
70
71
72 static OsMapEntry internalEntry;
73
74
75
76
77 private static String osName;
78
79
80
81
82 private static Log log = LogFactory.getLog(SystemUtils.class);
83
84
85
86
87 static final Map<String, OsMapEntry> MAP = new HashMap<String, OsMapEntry>();
88
89
90
91
92 static class OsMapEntry {
93
94 private final String name;
95
96 private final Pattern pattern;
97
98 private final String[] libPreSuffix;
99
100 private final Map<String, String> archs = new HashMap<String, String>();
101
102
103
104
105
106
107
108 public OsMapEntry(String key, String value) {
109 this.name = key;
110 String[] tokens = value.split("\\s*,\\s*");
111 int pos = 0;
112 this.pattern = Pattern.compile(tokens[pos++]);
113 this.libPreSuffix = new String[] { tokens[pos++], tokens[pos++] };
114
115 for (int i = pos; i < tokens.length; i++) {
116 String[] pair = tokens[i].split("\\s*=\\s*");
117 this.archs.put(pair[0], pair[1]);
118 }
119 }
120
121
122
123
124
125
126
127 public boolean matches(String s) {
128 return this.pattern.matcher(s).matches();
129 }
130
131
132
133
134
135
136 public String getName() {
137 return this.name;
138 }
139
140
141
142
143
144
145
146 public String mapArch(String s) {
147 String mapped = archs.get(s);
148 return (mapped == null) ? s : mapped;
149 }
150
151
152
153
154
155
156 public String[] getLibPreSuffix() {
157 return this.libPreSuffix;
158 }
159 }
160
161
162
163
164 static {
165
166
167 register("Windows_NT", "^Windows.*, lib,.dll, ia64=IA64");
168 register("SunOS", "^SunOS.*, lib,.so, x86=i386");
169 register("Linux", "^Linux.*, lib,.so, i386=i686,amd64=x86_64");
170 register("AIX", "^AIX.*, lib,.so");
171 register("HP-UX", "^HP-UX.*, lib,.sl, PA_RISC2.0=hppa2.0w,IA64N=ia64");
172 register("Darwin", "^Mac OS X.*, lib,.jnilib, ppc=powerpc");
173
174
175 InputStream is;
176 try {
177 ClassLoader cl = Thread.currentThread().getContextClassLoader();
178 is = cl.getResourceAsStream("os-map.properties");
179 if (is != null) {
180 Properties props = new Properties();
181 props.load(is);
182 is.close();
183
184 for (Object obj : props.keySet()) {
185 String key = (String)obj;
186 String value = props.getProperty(key);
187 register(new OsMapEntry(key, value));
188 }
189 }
190 } catch (Exception ignore) { }
191 }
192
193
194
195
196
197
198 private static void register(OsMapEntry entry) {
199 MAP.put(entry.name, entry);
200 }
201
202
203
204
205
206
207
208 private static void register(String key, String value) {
209 try {
210 register(new OsMapEntry(key, value));
211 log.debug("Registered OS Map " + key);
212 } catch (Exception ignore) {
213 }
214 }
215
216
217
218
219 private SystemUtils() {
220 }
221
222
223
224
225
226
227
228 public static boolean isWindows() {
229 OsMapEntry entry = getInternalEntry();
230 return entry != null && "Windows_NT".equals(entry.getName());
231 }
232
233
234
235
236
237
238 public static boolean isUnix() {
239 OsMapEntry entry = getInternalEntry();
240 return entry != null && !"Windows_NT".equals(entry.getName());
241 }
242
243
244
245
246
247
248 public static String getOsName() {
249 if (osName == null) {
250 String osId = System.getProperty("os.name");
251 String arch = System.getProperty("os.arch");
252 osName = getOsName(osId, arch);
253 }
254 return osName;
255 }
256
257
258
259
260
261
262 public static String getSimpleOsName() {
263 return getInternalEntry().getName();
264 }
265
266
267
268
269
270
271 public static String getSimpleArchName() {
272 String osArch = System.getProperty("os.arch");
273 return getInternalEntry().mapArch(osArch);
274 }
275
276
277
278
279
280
281
282 public static String getSimpleOsName(String osName) {
283 int pos = osName.indexOf('+');
284 while (pos != -1) {
285 String osPrefix = osName.substring(0, pos);
286 OsMapEntry entry = getEntry(osPrefix);
287 if (entry == null) {
288 pos = osName.indexOf('+', pos + 1);
289 } else {
290 return entry.getName();
291 }
292 }
293
294 return osName;
295 }
296
297
298
299
300
301
302
303
304 public static String getOsName(String osName, String osArch) {
305 OsMapEntry entry = getEntry(osName);
306 if (entry == null) {
307 log.debug("No entry found for [" + osName + "]. OS name not normalized");
308 return osName + "+" + osArch;
309 } else {
310 return entry.name + "+" + entry.mapArch(osArch);
311 }
312 }
313
314
315
316
317
318
319 static OsMapEntry getInternalEntry() {
320 if (internalEntry == null) {
321 String osId = System.getProperty("os.name");
322 internalEntry = getEntry(osId);
323 }
324 return internalEntry;
325 }
326
327
328
329
330
331
332
333 protected static OsMapEntry getEntry(String osName) {
334 for (OsMapEntry entry : MAP.values()) {
335 if (entry.matches(osName)) {
336 return entry;
337 }
338 }
339 return null;
340 }
341
342
343
344
345 public static String getOSVersion() {
346 return System.getProperty("os.version");
347 }
348
349
350
351
352
353
354 public static Properties getEnvironment() {
355 Map<String, String> envi = System.getenv();
356 Properties env = new Properties();
357 env.putAll(envi);
358 return env;
359
360 }
361
362
363
364
365
366
367
368
369
370 public static File extractDynamicLibrary(String dynamiclibraryname,
371 Class< ? > pathclass) throws Exception {
372 return JNIUtils.extractDynamicLibrary(dynamiclibraryname, pathclass);
373 }
374
375
376
377
378
379
380
381
382
383 public static File extractDynamicLibrary(
384 String dynamiclibraryname, String path) throws Exception {
385 return JNIUtils.extractDynamicLibrary(dynamiclibraryname, path);
386 }
387
388
389
390
391 public static String getTempDir() {
392 String tmpdir = System.getProperty("java.io.tmpdir", null);
393 return ((tmpdir == null) ? System.getProperty("user.home") : tmpdir);
394 }
395
396
397
398
399
400
401
402
403
404 public static Integer execAndSaveOutput(String command, String[] envp, File baseDir,
405 OutputStream output) {
406 String[] cmd = new String[] {command };
407 final boolean executeAsArray = false;
408 return execAndSaveOutput(cmd, envp, baseDir, output, executeAsArray);
409 }
410
411
412
413
414
415
416
417
418
419 public static Integer execAndSaveOutput(String[] command, String[] envp, File baseDir,
420 OutputStream output) {
421 final boolean executeAsArray = true;
422 return execAndSaveOutput(command, envp, baseDir, output, executeAsArray);
423 }
424
425
426
427
428
429
430
431
432
433
434
435 private static Integer execAndSaveOutput(String[] command, String[] envp, File baseDir,
436 OutputStream output, boolean executeAsArray) {
437 try {
438 Process p;
439 if (executeAsArray) {
440 p = Runtime.getRuntime().exec(command, envp, baseDir);
441 } else {
442 p = Runtime.getRuntime().exec(command[0], envp, baseDir);
443 }
444 SystemStream std = new SystemStream(p.getInputStream(), output);
445 SystemStream err = new SystemStream(p.getErrorStream(), output);
446 std.start();
447 err.start();
448
449 int exit = p.waitFor();
450
451 try {
452 std.stopThread();
453 } catch (Exception ex) {
454 log.error("Exception happened when stopping SystemStream", ex);
455 }
456 try {
457 err.stopThread();
458 } catch (Exception ex) {
459 log.error("Exception happened when stopping SystemStream", ex);
460 }
461
462 return new Integer(exit);
463 } catch (Exception ex) {
464 log.error("Error executing command: " + command, ex);
465 return null;
466 }
467 }
468
469
470
471
472
473
474
475
476 public static Integer execPiped(String command, String[] envp, File baseDir) {
477 final boolean executeAsArray = false;
478 String[] cmd = new String[] { command };
479 return execPiped(cmd, envp, baseDir, executeAsArray);
480 }
481
482
483
484
485
486
487
488
489
490
491 private static Integer execPiped(String[] command, String[] envp, File baseDir,
492 boolean executeAsArray) {
493
494 SystemStream[] ss = null;
495 try {
496 Process p;
497 if (executeAsArray) {
498 p = Runtime.getRuntime().exec(command, envp, baseDir);
499 } else {
500 p = Runtime.getRuntime().exec(command[0], envp, baseDir);
501 }
502 ss = new SystemStream[] {
503 new SystemStream(p.getErrorStream(), System.out),
504 new SystemStream(p.getInputStream(), System.out)
505 };
506 for (int i = 0; i < ss.length; i++) {
507 ss[i].start();
508 }
509
510 int exit = p.waitFor();
511
512 return new Integer(exit);
513 } catch (Exception ex) {
514 log.error("Error executing command '" + command + "'", ex);
515 return null;
516 } finally {
517 if (ss != null) {
518 for (int i = 0; i < ss.length; i++) {
519 try {
520 ss[i].stopThread();
521 } catch (Exception ex) {
522 log.error("Exception happened when stopping SystemStream(" + i + ")", ex);
523 }
524 }
525 }
526 }
527 }
528
529
530
531
532
533
534
535
536
537 public static Integer execAndSaveOutput(String[] command, String[] envp, File baseDir,
538 File outputFile) {
539 FileOutputStream output;
540 try {
541 output = new FileOutputStream(outputFile);
542 final boolean executeAsArray = true;
543 return execAndSaveOutput(command, envp, baseDir, output, executeAsArray);
544 } catch (FileNotFoundException e) {
545 log.error("Cannot create output file " + outputFile.toString());
546 return null;
547 }
548 }
549
550
551
552
553
554
555
556
557
558 public static Integer execAndSaveOutput(String command, String[] envp, File baseDir,
559 File outputFile) {
560 final boolean executeAsArray = false;
561 FileOutputStream output;
562 try {
563 output = new FileOutputStream(outputFile);
564 String[] cmd = new String[]{ command };
565 return execAndSaveOutput(cmd, envp, baseDir, output, executeAsArray);
566 } catch (FileNotFoundException e) {
567 log.error("Cannot create output file " + outputFile.toString());
568 return null;
569 }
570 }
571
572
573
574
575
576
577
578
579 public static Integer execPiped(String[] command, String[] envp, File baseDir) {
580 final boolean executeAsArray = true;
581 return execPiped(command, envp, baseDir, executeAsArray);
582 }
583
584
585
586
587
588 static class SystemStream extends Thread implements Runnable {
589
590 InputStream pi = null;
591
592 OutputStream po = null;
593
594 boolean interruptFlag = false;
595
596
597
598
599
600
601
602 public SystemStream(InputStream pi, OutputStream po) {
603 interruptFlag = false;
604 this.pi = pi;
605 this.po = po;
606 }
607
608
609
610
611 @Override public void run() {
612 int ch;
613 try {
614 while (!interruptFlag) {
615 ch = pi.read();
616 if (ch == -1) {
617 break;
618 }
619 po.write(ch);
620 po.flush();
621 }
622 } catch (Exception e) {
623 e.printStackTrace();
624 }
625 }
626
627
628
629
630
631 public void stopThread() {
632 this.interruptFlag = true;
633 }
634 }
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667 }