KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > systest > impl > SeparateProcessAgent


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.systest.impl;
19
20 import org.apache.activemq.systest.AgentStopper;
21 import org.apache.activemq.systest.AgentSupport;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27
28 /**
29  * Starts a separate process on this machine until its asked to be killed.
30  *
31  * @version $Revision: 1.1 $
32  */

33 public class SeparateProcessAgent extends AgentSupport {
34
35     private String JavaDoc[] commands;
36     private Process JavaDoc process;
37     private long sleepTime = 10000;
38
39     public SeparateProcessAgent() {
40     }
41
42     public String JavaDoc[] getCommands() {
43         if (commands == null) {
44             commands = createCommand();
45         }
46         return commands;
47     }
48
49     public void setCommands(String JavaDoc[] command) {
50         this.commands = command;
51     }
52
53     public Process JavaDoc getProcess() {
54         return process;
55     }
56
57     public void start() throws Exception JavaDoc {
58         process = createProcess();
59         Thread JavaDoc thread = new Thread JavaDoc(new Runnable JavaDoc() {
60             public void run() {
61                 readInputStream(process.getInputStream());
62             }
63         });
64         thread.start();
65
66         Thread JavaDoc thread2 = new Thread JavaDoc(new Runnable JavaDoc() {
67             public void run() {
68                 waitForProcessExit();
69             }
70         });
71         thread2.start();
72         
73         // lets wait for the process to startup
74

75         System.out.println("Waiting a little while to give the broker process to start");
76         Thread.sleep(sleepTime);
77     }
78
79     public void stop(AgentStopper stopper) {
80         if (process != null) {
81             try {
82                 process.destroy();
83             }
84             catch (Exception JavaDoc e) {
85                 stopper.onException(this, e);
86             }
87             finally {
88                 process = null;
89             }
90         }
91     }
92
93     protected Process JavaDoc createProcess() throws Exception JavaDoc {
94         return Runtime.getRuntime().exec(commands);
95     }
96
97     protected String JavaDoc[] createCommand() {
98         throw new IllegalArgumentException JavaDoc("You must configure the 'commands' property");
99     }
100
101     protected void readInputStream(InputStream JavaDoc inputStream) {
102         try {
103             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream));
104             while (true) {
105                 String JavaDoc line = reader.readLine();
106                 if (line == null) {
107                     break;
108                 }
109                 System.out.println(line);
110             }
111         }
112         catch (IOException JavaDoc e) {
113             // ignore exceptions
114
// probably end of file
115
}
116     }
117
118     protected void waitForProcessExit() {
119         Process JavaDoc p = process;
120         try {
121             p.waitFor();
122         }
123         catch (InterruptedException JavaDoc e) {
124             System.out.println("Interrupted while waiting for process to complete: " + e);
125             e.printStackTrace();
126         }
127         int value = p.exitValue();
128         System.out.println("Process completed with exit value: " + value);
129
130     }
131 }
132
Popular Tags