KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > simulator > distrunner > ControlSetup


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.simulator.distrunner;
5
6 import com.tc.objectserver.control.ExtraProcessServerControl;
7 import com.tc.objectserver.control.ServerControl;
8 import com.tc.objectserver.control.ExtraProcessServerControl.DebugParams;
9 import com.tc.process.LinkedJavaProcess;
10 import com.tcsimulator.ConfigWriter;
11 import com.tcsimulator.ProcessFactory;
12 import com.tcsimulator.Sandbox;
13 import com.tcsimulator.Setup;
14 import com.tcsimulator.distrunner.ServerSpec;
15
16 import java.io.File JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.net.InetAddress JavaDoc;
19 import java.net.UnknownHostException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22
23 public class ControlSetup {
24
25   private final ServerSpec controlServerSpec;
26   private final ConfigWriter configWriter;
27   private final Sandbox sandbox;
28   private final ServerControl server;
29   private final boolean debugSetup = false;
30   private final LinkedJavaProcess setupProcess;
31   private final boolean isServerMachine;
32   private final String JavaDoc localHostName;
33   private final boolean startWithDSO;
34
35   public ControlSetup(String JavaDoc[] args, ServerSpec controlServerSpec, boolean startWithDSO, String JavaDoc javaHome) {
36
37     this.controlServerSpec = controlServerSpec;
38     this.startWithDSO = startWithDSO;
39
40     InetAddress JavaDoc host;
41     try {
42       host = InetAddress.getLocalHost();
43     } catch (UnknownHostException JavaDoc e) {
44       throw new RuntimeException JavaDoc("Could not resolve local host name." + e);
45     }
46     localHostName = host.getHostName();
47
48     if (this.localHostName.equals(this.controlServerSpec.getHostName())) {
49       this.isServerMachine = true;
50     } else {
51       this.isServerMachine = false;
52     }
53
54     this.sandbox = new Sandbox(new File JavaDoc(this.controlServerSpec.getTestHome()), Sandbox.CONTROL_SERVER);
55
56     if (this.isServerMachine) {
57       Collection JavaDoc classesToVisit = new ArrayList JavaDoc();
58       classesToVisit.add(Setup.class);
59       this.configWriter = new ConfigWriter(this.controlServerSpec, classesToVisit, sandbox);
60       this.server = newServerControl();
61     } else {
62       this.configWriter = null;
63       this.server = null;
64     }
65
66     ProcessFactory procFactory = new ProcessFactory(sandbox, this.controlServerSpec);
67
68     if (this.startWithDSO) {
69       this.setupProcess = procFactory.newDSOJavaProcessInstance(Setup.class.getName(), args, debugSetup);
70     } else {
71       this.setupProcess = procFactory.newJavaProcessInstance(Setup.class.getName(), args, debugSetup, javaHome);
72     }
73   }
74
75   private void execute() throws IOException JavaDoc, InterruptedException JavaDoc {
76     if (this.isServerMachine && this.startWithDSO) {
77       configWriter.writeConfigFile();
78       startServer();
79     }
80     startSetup();
81   }
82
83   private void startSetup() throws IOException JavaDoc, InterruptedException JavaDoc {
84     System.out.println("Starting Setup...");
85     setupProcess.start();
86     setupProcess.mergeSTDERR();
87     setupProcess.mergeSTDOUT();
88     setupProcess.waitFor();
89   }
90
91   private ServerControl newServerControl() {
92     boolean mergeOutput = true;
93     return new ExtraProcessServerControl(new DebugParams(), this.controlServerSpec.getHostName(),
94                                          this.controlServerSpec.getDsoPort(), this.controlServerSpec.getJmxPort(),
95                                          this.sandbox.getConfigFile().getAbsolutePath(), this.sandbox.getServerHome(),
96                                          this.sandbox.getInstallationRoot(), mergeOutput, this.controlServerSpec
97                                              .getJvmOpts(), ArgParser.getUndefinedString());
98   }
99
100   private void startServer() {
101     try {
102       this.server.crash();
103       this.server.start(30 * 1000);
104     } catch (Exception JavaDoc e) {
105       throw new RuntimeException JavaDoc(e);
106     }
107     if (!this.server.isRunning()) { throw new RuntimeException JavaDoc("Server still isn't running!"); }
108   }
109
110   public static void main(String JavaDoc[] args) throws IOException JavaDoc, InterruptedException JavaDoc {
111
112     boolean controlServerRequired = true;
113     boolean testServerRequired = true;
114     ArgParser parser = null;
115
116     try {
117       parser = new ArgParser(args, new SpecFactoryImpl(), testServerRequired, controlServerRequired);
118     } catch (ArgException e) {
119       System.err.println("Error parsing arguments: " + e.getMessage());
120       System.err.println("\n\n" + ArgParser.usage());
121       System.exit(1);
122     }
123
124     ControlSetup controlSetup = new ControlSetup(args, parser.getControlServerSpec(), parser.getTestServerStartMode(),
125                                                  parser.getJavaHome());
126     controlSetup.execute();
127   }
128 }
129
Popular Tags