KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > control > ExtraProcessServerControl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.control;
5
6 import org.apache.commons.lang.ArrayUtils;
7
8 import com.tc.admin.TCStop;
9 import com.tc.config.Directories;
10 import com.tc.config.schema.setup.StandardTVSConfigurationSetupManagerFactory;
11 import com.tc.process.LinkedJavaProcess;
12 import com.tc.process.StreamCopier;
13 import com.tc.server.TCServerMain;
14
15 import java.io.File JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 public class ExtraProcessServerControl extends ServerControlBase {
24   private final String JavaDoc name;
25   private final boolean mergeOutput;
26
27   protected LinkedJavaProcess process;
28   protected final String JavaDoc configFileLoc;
29   protected final List JavaDoc jvmArgs;
30   private final File JavaDoc runningDirectory;
31   private File JavaDoc out;
32   private FileOutputStream JavaDoc fileOut;
33   private StreamCopier outCopier;
34   private StreamCopier errCopier;
35
36   public ExtraProcessServerControl(String JavaDoc host, int dsoPort, int adminPort, String JavaDoc configFileLoc, boolean mergeOutput)
37       throws FileNotFoundException JavaDoc {
38     this(new DebugParams(), host, dsoPort, adminPort, configFileLoc, mergeOutput);
39   }
40
41   public ExtraProcessServerControl(DebugParams debugParams, String JavaDoc host, int dsoPort, int adminPort,
42                                    String JavaDoc configFileLoc, boolean mergeOutput) throws FileNotFoundException JavaDoc {
43     // 2006-07-11 andrew -- We should get rid of the reference to Directories.getInstallationRoot() here.
44
// Tests don't run in an environment where such a thing even exists. If the server needs an
45
// "installation directory", the tests should be creating one themselves.
46
this(debugParams, host, dsoPort, adminPort, configFileLoc, null, Directories.getInstallationRoot(), mergeOutput);
47   }
48
49   public ExtraProcessServerControl(DebugParams debugParams, String JavaDoc host, int dsoPort, int adminPort,
50                                    String JavaDoc configFileLoc, File JavaDoc runningDirectory, File JavaDoc installationRoot,
51                                    boolean mergeOutput) {
52     super(host, dsoPort, adminPort);
53     this.jvmArgs = new ArrayList JavaDoc();
54     this.configFileLoc = configFileLoc;
55     this.mergeOutput = mergeOutput;
56     this.name = "DSO process @ " + getHost() + ":" + getDsoPort();
57     this.runningDirectory = runningDirectory;
58     jvmArgs.add("-D" + Directories.TC_INSTALL_ROOT_PROPERTY_NAME + "=" + installationRoot);
59     jvmArgs.add("-D" + Directories.TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME + "=true");
60     debugParams.addDebugParamsTo(jvmArgs);
61   }
62
63   public ExtraProcessServerControl(DebugParams debugParams, String JavaDoc host, int dsoPort, int adminPort,
64                                    String JavaDoc configFileLoc, File JavaDoc runningDirectory, File JavaDoc installationRoot,
65                                    boolean mergeOutput, List JavaDoc jvmArgs, String JavaDoc undefString) {
66     super(host, dsoPort, adminPort);
67
68     this.jvmArgs = new ArrayList JavaDoc();
69     for (Iterator JavaDoc i = jvmArgs.iterator(); i.hasNext();) {
70       String JavaDoc next = (String JavaDoc) i.next();
71       if (!next.equals(undefString)) {
72         this.jvmArgs.add(next);
73       }
74     }
75
76     this.configFileLoc = configFileLoc;
77     this.mergeOutput = mergeOutput;
78     this.name = "DSO process @ " + getHost() + ":" + getDsoPort();
79     this.runningDirectory = runningDirectory;
80     this.jvmArgs.add("-D" + Directories.TC_INSTALL_ROOT_PROPERTY_NAME + "=" + installationRoot);
81     this.jvmArgs.add("-D" + Directories.TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME + "=true");
82     debugParams.addDebugParamsTo(jvmArgs);
83   }
84
85   public void mergeSTDOUT() {
86     this.process.mergeSTDOUT();
87   }
88
89   public void mergeSTDERR() {
90     this.process.mergeSTDERR();
91   }
92
93   protected String JavaDoc getMainClassName() {
94     return TCServerMain.class.getName();
95   }
96
97   protected String JavaDoc[] getMainClassArguments() {
98     return new String JavaDoc[] { StandardTVSConfigurationSetupManagerFactory.CONFIG_SPEC_ARGUMENT_WORD, this.configFileLoc };
99   }
100
101   public void writeOutputTo(File JavaDoc outputFile) {
102     if (mergeOutput) { throw new IllegalStateException JavaDoc(); }
103     this.out = outputFile;
104   }
105
106   public void start(long timeout) throws Exception JavaDoc {
107     System.err.println("Starting " + this.name + ": jvmArgs=" + jvmArgs + ", main=" + getMainClassName()
108                        + ", main args=" + ArrayUtils.toString(getMainClassArguments()));
109     process = createLinkedJavaProcess();
110     process.setJavaArguments((String JavaDoc[]) jvmArgs.toArray(new String JavaDoc[jvmArgs.size()]));
111     process.start();
112     if (mergeOutput) {
113       mergeSTDOUT();
114       mergeSTDERR();
115     } else if (out != null) {
116       fileOut = new FileOutputStream JavaDoc(out);
117       outCopier = new StreamCopier(process.STDOUT(), fileOut);
118       errCopier = new StreamCopier(process.STDERR(), fileOut);
119       outCopier.start();
120       errCopier.start();
121     }
122     waitUntilStarted(timeout);
123     System.err.println(this.name + " started.");
124   }
125
126   protected LinkedJavaProcess createLinkedJavaProcess() {
127     LinkedJavaProcess rv = new LinkedJavaProcess(getMainClassName(), getMainClassArguments());
128     rv.setDirectory(this.runningDirectory);
129     return rv;
130   }
131
132   public void crash() throws Exception JavaDoc {
133     System.out.println("Crashing server " + this.name + "...");
134     if (process != null) {
135       process.destroy();
136       waitUntilShutdown();
137     }
138     System.out.println(this.name + " crashed.");
139   }
140
141   public void attemptShutdown() throws Exception JavaDoc {
142     System.out.println("Shutting down server " + this.name + "...");
143     TCStop stopper = new TCStop(getHost(), getAdminPort());
144     stopper.stop();
145   }
146
147   public void shutdown() throws Exception JavaDoc {
148     attemptShutdown();
149     waitUntilShutdown();
150     System.out.println(this.name + " stopped.");
151   }
152
153   private void waitUntilStarted(long timeout) throws Exception JavaDoc {
154     long timeoutTime = System.currentTimeMillis() + timeout;
155     while (true) {
156       if (isRunning()) return;
157       if (System.currentTimeMillis() > timeoutTime) {
158         //
159
throw new RuntimeException JavaDoc("Timeout occurred waiting for server to start: " + timeout + " ms.");
160       }
161       Thread.sleep(1000);
162     }
163   }
164
165   public void waitUntilShutdown() throws Exception JavaDoc {
166     while (isRunning()) {
167       Thread.sleep(1000);
168     }
169   }
170
171   public int waitFor() throws Exception JavaDoc {
172     int rv = process.waitFor();
173
174     if (outCopier != null) {
175       try {
176         outCopier.join();
177       } catch (Exception JavaDoc e) {
178         e.printStackTrace();
179       }
180     }
181
182     if (errCopier != null) {
183       try {
184         errCopier.join();
185       } catch (Exception JavaDoc e) {
186         e.printStackTrace();
187       }
188     }
189
190     if (fileOut != null) {
191       try {
192         fileOut.close();
193       } catch (Exception JavaDoc e) {
194         e.printStackTrace();
195       }
196     }
197
198     return rv;
199   }
200
201   public static final class DebugParams {
202     private final boolean debug;
203     private final int debugPort;
204
205     public DebugParams() {
206       this(false, 0);
207     }
208
209     public DebugParams(int debugPort) {
210       this(true, debugPort);
211     }
212
213     private DebugParams(boolean debug, int debugPort) {
214       if (debugPort < 0) throw new AssertionError JavaDoc("Debug port must be >= 0: " + debugPort);
215       this.debugPort = debugPort;
216       this.debug = debug;
217     }
218
219     private void addDebugParamsTo(Collection JavaDoc jvmArgs) {
220       if (debug) {
221         jvmArgs.add("-Xdebug");
222         String JavaDoc address = debugPort > 0 ? "address=" + debugPort + "," : "";
223         jvmArgs.add("-Xrunjdwp:transport=dt_socket," + address + "server=y,suspend=n");
224       }
225     }
226   }
227
228 }
229
Popular Tags