01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.test;
06:
07: import com.tc.util.runtime.Os;
08:
09: import java.io.BufferedReader;
10: import java.io.IOException;
11: import java.io.InputStream;
12: import java.io.InputStreamReader;
13:
14: public class ProcessInfo {
15: public static String ps_grep_java() {
16: try {
17: String[] args = new String[] { "sh", "-c",
18: "ps auxwww | grep java | grep -v grep" };
19:
20: if (Os.isWindows()) {
21: String execPath = TestConfigObject.getInstance()
22: .executableSearchPath();
23: args = new String[] { execPath + "\\pv.exe", "-l",
24: "java.exe" };
25: } else if (Os.isSolaris()) {
26: args = new String[] { "sh", "-c",
27: "/usr/ucb/ps auxwww | grep java | grep -v grep" };
28: }
29:
30: Process p = Runtime.getRuntime().exec(args);
31: StreamGobbler out = new StreamGobbler(p.getInputStream(),
32: "stdout");
33: StreamGobbler err = new StreamGobbler(p.getErrorStream(),
34: "stderr");
35:
36: out.start();
37: err.start();
38:
39: p.waitFor();
40: String result = out.getOutput().trim() + "\n"
41: + err.getOutput();
42:
43: return result.trim();
44:
45: } catch (Exception e) {
46: e.printStackTrace();
47: }
48: return "";
49: }
50: }
51:
52: class StreamGobbler extends Thread {
53: private InputStream is;
54: private String type;
55: private StringBuffer output = new StringBuffer(1024);
56:
57: StreamGobbler(InputStream is, String type) {
58: this .is = is;
59: this .type = type;
60: }
61:
62: public void run() {
63: try {
64: InputStreamReader isr = new InputStreamReader(is);
65: BufferedReader br = new BufferedReader(isr);
66: String line = null;
67: while ((line = br.readLine()) != null) {
68: output.append(line).append("\n");
69: }
70: } catch (IOException ioe) {
71: ioe.printStackTrace();
72: }
73: }
74:
75: public String getOutput() {
76: return output.toString();
77: }
78: }
|