1 /* Reattore HTTP Server
2
3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 $Id: CommandLineVar.java,v 1.3 2003/03/05 04:31:57 michaelh Exp $
20 */
21
22 package juju.reattore.perfcap.var.impl;
23
24 import java.io.*;
25 import java.util.regex.*;
26 import java.util.*;
27
28 import org.apache.commons.logging.*;
29
30 import juju.reattore.perfcap.var.Variable;
31 import juju.reattore.util.Spawner;
32
33 /*** A 'Variable' that actually starts a background process.
34
35 @tag command
36 @group Variable
37 */
38 public class CommandLineVar
39 implements Variable {
40
41 private static Log log = LogFactory.getLog(CommandLineVar.class);
42
43 private String dir;
44 private String startupCmd;
45 private String shutdownCmd;
46 private String name = "Command";
47
48 private Spawner proc;
49
50 private int startDelay = 1000;
51 private Pattern waitFor;
52
53 private boolean first = true;
54
55 /*** The working directory to run from.
56
57 @param dir Working dir.
58 @default The current directory.
59 */
60 public void setDir(String dir) {
61 this.dir = dir;
62 }
63
64 /*** The Runtime.exec command to run to start the process.
65
66 @param cmd The command.
67 */
68 public void setStartupCmd(String cmd) {
69 this.startupCmd = cmd;
70 }
71
72 /*** The Runtime.exec command to run to stop the process. If not
73 set, defaults to killing the process.
74
75 @param cmd The command.
76 */
77 public void setShutdownCmd(String cmd) {
78 this.shutdownCmd = cmd;
79 }
80
81 /*** The regex string that signifies that the process has started.
82
83 @param waitFor The Matcher.match regex.
84 */
85 public void setWaitFor(String waitFor) {
86 if (waitFor != null) {
87 this.waitFor = Pattern.compile(waitFor);
88 }
89 else {
90 this.waitFor = null;
91 }
92 }
93
94 /*** The time in ms to wait for the process to start.
95
96 @param delay Time in ms.
97 @default 1000
98 */
99 public void setStartDelay(int delay) {
100 this.startDelay = delay;
101 }
102
103 /*** The name to identify this process as. Also returned as the
104 value.
105
106 @param name The name.
107 @default Command
108 */
109 public void setName(String name) {
110 this.name = name;
111 }
112
113 /*** @see Variable */
114 public String getName() {
115 return name != null ? name : startupCmd;
116 }
117
118 /*** @see Variable */
119 public Object getValue() {
120 return getName();
121 }
122
123 /*** @see Variable */
124 public boolean hasNext() {
125 return first;
126 }
127
128 /*** @see Variable */
129 public void next() {
130 first = false;
131 }
132
133 /*** @see Variable */
134 public void begin()
135 throws Exception {
136
137 log.info("Running '" + startupCmd + "' in dir: " + dir);
138
139 proc = new Spawner();
140 proc.setCommand(startupCmd);
141 proc.setDir(dir);
142
143 proc.spawn();
144
145 first = true;
146
147 if (waitFor != null) {
148 List out = proc.getOutput();
149
150 found:
151 do {
152 synchronized (out) {
153 Iterator it = out.iterator();
154 while (it.hasNext()) {
155 String line = (String)it.next();
156
157 if (waitFor.matcher(line).matches()) {
158 break found;
159 }
160 }
161
162 out.wait();
163 }
164 }
165 while (true);
166 }
167 else {
168 Thread.sleep(startDelay);
169 }
170 }
171
172 /*** @see Variable */
173 public void end()
174 throws Exception {
175
176 if (proc == null) {
177 /* Do nothing */
178 }
179 else {
180 if (shutdownCmd == null) {
181 log.info("Terminating process");
182 proc.destroy();
183
184 log.info("Exit value: " + proc.exitValue());
185 }
186 else {
187 log.info("Waiting for main process");
188 proc.waitDone();
189
190 log.info("Exit value: " + proc.exitValue());
191
192 log.info("Shutting down using " + shutdownCmd);
193
194 Spawner shut = new Spawner();
195 shut.setCommand(shutdownCmd);
196
197 shut.spawn();
198 shut.waitDone();
199
200 Thread.sleep(2000);
201 }
202 }
203 }
204 }
This page was automatically generated by Maven