001: /*
002: * <copyright>
003: *
004: * Copyright 2003-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.tools.server.examples;
027:
028: import java.io.*;
029: import java.util.*;
030:
031: import org.cougaar.tools.server.system.*;
032:
033: /**
034: * Example usage of the "system" utilities.
035: * <p>
036: * The output is OS- and runtime- specific, so this is not
037: * appropriate for automated regression testing.
038: *
039: * @see SystemAccessTest
040: */
041: public class SystemAccess {
042:
043: private static final String DEFAULT_COMMAND = "java"
044: + " -classpath " + System.getProperty("java.class.path")
045: + " org.cougaar.tools.server.examples.SystemAccessTest";
046:
047: public static void main(String[] args) throws Exception {
048:
049: String cmd;
050: if (args.length > 0) {
051: cmd = "";
052: for (int i = 0; i < args.length; i++) {
053: String ai = args[i];
054: //
055: // FIXME: what about whitespaces?
056: //
057: cmd += ai;
058: if (i < (args.length - 1)) {
059: cmd += " ";
060: }
061: }
062: } else {
063: cmd = DEFAULT_COMMAND;
064: }
065: System.out.println("Child command: " + cmd);
066:
067: System.out.println("Start child process");
068: System.out.println("***********************************");
069: long pid = startChild(cmd);
070: System.out.println("***********************************");
071: System.out.println("Child PID: " + pid);
072:
073: System.out.println("List child processes");
074: System.out.println("***********************************");
075: ProcessStatus[] psa = listProcesses(false);
076: // mark our child's "pid"
077: markProcesses(psa, pid);
078: System.out.println("***********************************");
079: printProcesses(psa);
080:
081: System.out.println("List all processes");
082: System.out.println("***********************************");
083: ProcessStatus[] psa2 = listProcesses(true);
084: // mark our child's "pid"
085: markProcesses(psa2, pid);
086: System.out.println("***********************************");
087: printProcesses(psa2);
088:
089: System.out.println("Dump the stack for " + pid);
090: System.out.println("***********************************");
091: dumpStack(pid);
092: System.out.println("***********************************");
093:
094: System.out.println("Tests completed");
095: System.exit(-1);
096: }
097:
098: private static final long startChild(String cmd) throws Exception {
099: // get OS-specific factory
100: SystemAccessFactory saf = SystemAccessFactory.getInstance();
101:
102: // create process-launcher
103: ProcessLauncher pl = saf.createProcessLauncher();
104:
105: // get the modified command line
106: String[] newCmd = pl.getCommandLine(cmd);
107:
108: // start the process
109: Process proc = Runtime.getRuntime().exec(newCmd);
110:
111: // read the process id
112: InputStream procIn = proc.getInputStream();
113: long pid = pl.parseProcessIdentifier(procIn);
114:
115: // let some other threads watch the output
116: OutputWatcher ow = new OutputWatcher(procIn, "StdOut: ");
117: OutputWatcher ew = new OutputWatcher(proc.getErrorStream(),
118: "StdErr: ");
119: (new Thread(ow)).start();
120: (new Thread(ew)).start();
121:
122: // should do "proc.waitFor()", but we're just testing...
123:
124: return pid;
125: }
126:
127: private static final boolean dumpStack(long pid) throws Exception {
128:
129: // get OS-specific factory
130: SystemAccessFactory saf = SystemAccessFactory.getInstance();
131:
132: // create Thread-Dumper
133: JavaThreadDumper jtd = saf.createJavaThreadDumper();
134: if (jtd == null) {
135: throw new UnsupportedOperationException(
136: "Java Thread-Dump not available");
137: }
138:
139: // get the command line
140: String[] cmd = jtd.getCommandLine(pid);
141:
142: // invoke the command
143: InputStream in = InvokeUtility.invokeCommand(cmd);
144:
145: // parse the response
146: boolean b = jtd.parseResponse(in);
147:
148: return b;
149: }
150:
151: private static final ProcessStatus[] listProcesses(boolean showAll)
152: throws Exception {
153:
154: // get OS-specific factory
155: SystemAccessFactory saf = SystemAccessFactory.getInstance();
156:
157: // create process-statys reader
158: ProcessStatusReader psr = saf.createProcessStatusReader();
159: if (psr == null) {
160: throw new UnsupportedOperationException(
161: "Process status listing not available");
162: }
163:
164: // get the command line
165: String[] cmd = psr.getCommandLine(showAll);
166:
167: // invoke the command
168: InputStream in = InvokeUtility.invokeCommand(cmd);
169:
170: // parse the response
171: ProcessStatus[] ret = psr.parseResponse(in);
172:
173: return ret;
174: }
175:
176: private static final void printProcesses(ProcessStatus[] psa) {
177: System.out.println("ProcessStatus[" + psa.length + "]:");
178: for (int i = 0; i < psa.length; i++) {
179: System.out.println(psa[i]);
180: }
181: }
182:
183: private static void unmarkProcesses(ProcessStatus[] psa) {
184: for (int i = 0; i < psa.length; i++) {
185: ProcessStatus pi = psa[i];
186: pi.unmark();
187: }
188: }
189:
190: private static void markProcesses(ProcessStatus[] psa, long pid) {
191: for (int i = 0; i < psa.length; i++) {
192: ProcessStatus pi = psa[i];
193: if (pi.getProcessIdentifier() == pid) {
194: pi.mark();
195: break;
196: }
197: }
198: }
199:
200: private static class OutputWatcher implements Runnable {
201: private final Reader in;
202: private final String pre;
203:
204: public OutputWatcher(InputStream in, String pre) {
205: this .in = new InputStreamReader(in);
206: this .pre = ((pre != null) ? pre : "");
207: }
208:
209: public void run() {
210: try {
211: char[] cbuf = new char[1024];
212: while (true) {
213: int len = in.read(cbuf);
214: if (len <= 0) {
215: return; // End of file or error
216: }
217: System.out
218: .println(pre + (new String(cbuf, 0, len)));
219: }
220: } catch (Exception e) {
221: }
222: }
223: }
224: }
|