KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcsimulator > Setup


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.tcsimulator;
5
6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
7
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOApplicationConfig;
10 import com.tc.objectserver.control.ExtraProcessServerControl;
11 import com.tc.objectserver.control.NullServerControl;
12 import com.tc.objectserver.control.ServerControl;
13 import com.tc.objectserver.control.ExtraProcessServerControl.DebugParams;
14 import com.tc.process.LinkedJavaProcess;
15 import com.tc.simulator.distrunner.ArgException;
16 import com.tc.simulator.distrunner.ArgParser;
17 import com.tc.simulator.distrunner.SpecFactoryImpl;
18 import com.tcsimulator.distrunner.ServerSpec;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 public class Setup {
30
31   private static final int SERVER_START_TIMEOUT = 30 * 1000;
32   private final ServerSpec testServerSpec;
33   private final String JavaDoc testAppClassName;
34   private final ServerControl server;
35   private final String JavaDoc localHostName;
36   private final boolean isServerMachine;
37   private final ConfigWriter configWriter;
38   private Sandbox sandbox;
39   private final boolean debugTestSetup = false;
40   private final boolean debugTestStarter = false;
41   private LinkedJavaProcess testStarterProcess;
42   private LinkedJavaProcess testSetupProcess;
43
44   private TestEnvironmentView testEnvironmentView;
45   private LinkedQueue eventQueue;
46   private LinkedQueue responseQueue;
47   private final boolean startedWithDSO;
48
49   public Setup(String JavaDoc[] args, ServerSpec serverSpec, String JavaDoc testAppClassName, Collection JavaDoc clientSpecs, int intensity,
50                boolean startedWithDSO) throws ClassNotFoundException JavaDoc {
51
52     this.testServerSpec = serverSpec;
53     this.testAppClassName = testAppClassName;
54     this.startedWithDSO = startedWithDSO;
55
56     InetAddress JavaDoc host;
57     try {
58       host = InetAddress.getLocalHost();
59     } catch (UnknownHostException JavaDoc e) {
60       throw new RuntimeException JavaDoc("Could not resolve local host name." + e);
61     }
62     localHostName = host.getHostName();
63
64     this.isServerMachine = testServerSpec.getHostName().equals(localHostName);
65
66     sandbox = new Sandbox(new File JavaDoc(testServerSpec.getTestHome()), Sandbox.TEST_SERVER);
67
68     if (this.isServerMachine) {
69       this.server = newServerControl();
70
71       Collection JavaDoc classesToVisit = new ArrayList JavaDoc();
72       classesToVisit.add(TestSetup.class);
73       classesToVisit.add(TestSpec.class);
74       classesToVisit.add(TestStarter.class);
75       classesToVisit.add(ContainerBuilder.class);
76       classesToVisit.add(GlobalVmNameGenerator.class);
77       classesToVisit.add(Class.forName(this.testAppClassName));
78       configWriter = new ConfigWriter(testServerSpec, classesToVisit, sandbox);
79     } else {
80       this.server = null;
81       this.configWriter = null;
82     }
83     ProcessFactory procFactory = new ProcessFactory(sandbox, testServerSpec);
84
85     this.testSetupProcess = procFactory.newDSOJavaProcessInstance(TestSetup.class.getName(), args, debugTestSetup);
86     this.testStarterProcess = procFactory.newDSOJavaProcessInstance(TestStarter.class.getName(), new String JavaDoc[] {},
87                                                                     debugTestStarter);
88     if (this.isServerMachine) {
89       List JavaDoc clientSpecsCopy = new ArrayList JavaDoc();
90       for (Iterator JavaDoc i = clientSpecs.iterator(); i.hasNext();) {
91         ClientSpec cSpec = (ClientSpec) i.next();
92         ClientSpec cCopy = cSpec.copy();
93         clientSpecsCopy.add(cCopy);
94       }
95
96       this.testEnvironmentView = new TestEnvironmentViewImpl(serverSpec.copy(), clientSpecsCopy, intensity);
97     }
98
99     if (this.startedWithDSO) {
100       this.eventQueue = new LinkedQueue();
101       this.responseQueue = new LinkedQueue();
102     }
103   }
104
105   public void execute() throws IOException JavaDoc, InterruptedException JavaDoc {
106     if (isServerMachine) {
107       configWriter.writeConfigFile();
108       startServer();
109
110       ((TestEnvironmentViewImpl) this.testEnvironmentView).setServerRunning(ServerView.RUNNING);
111
112       startTestSetup();
113
114       if (this.startedWithDSO) {
115         startEventQueueHandler();
116       }
117     }
118     startTestStarter();
119   }
120
121   private void startEventQueueHandler() {
122     System.out.println("Starting EventQueueHandler...");
123     EventQueueHandler eqhandler = new EventQueueHandler(this.eventQueue, this);
124     Thread JavaDoc t = new Thread JavaDoc(eqhandler);
125     t.setDaemon(true);
126     t.start();
127   }
128
129   public void crashServer() throws Exception JavaDoc {
130     synchronized (this.server) {
131       //if (!this.server.isRunning()) { throw new RuntimeException("Server is not running... server crash failed!"); }
132
this.server.crash();
133       if (this.server.isRunning()) { throw new RuntimeException JavaDoc("srever is still running... server crash failed!"); }
134
135       ((TestEnvironmentViewImpl) this.testEnvironmentView).setServerRunning(ServerView.NOT_RUNNING);
136       
137       this.responseQueue.put("Crash successful!");
138     }
139   }
140
141   public void restartServer() throws Exception JavaDoc {
142     synchronized (this.server) {
143       if (this.server.isRunning()) { throw new RuntimeException JavaDoc("Server is already running... server restart failed!"); }
144       this.server.start(SERVER_START_TIMEOUT);
145       if (!this.server.isRunning()) { throw new RuntimeException JavaDoc("Server is not running... server restart failed!"); }
146
147       ((TestEnvironmentViewImpl) this.testEnvironmentView).setServerRunning(ServerView.RUNNING);
148       
149       this.responseQueue.put("Restart successful!");
150     }
151   }
152
153   private ServerControl newServerControl() {
154     ServerControl rv;
155     boolean mergeOutput = true;
156     if (!isServerMachine) {
157       rv = new NullServerControl();
158     } else {
159       rv = new ExtraProcessServerControl(new DebugParams(), this.testServerSpec.getHostName(), this.testServerSpec
160           .getDsoPort(), this.testServerSpec.getJmxPort(), this.sandbox.getConfigFile().getAbsolutePath(), this.sandbox
161           .getServerHome(), this.sandbox.getInstallationRoot(), mergeOutput, this.testServerSpec.getJvmOpts(),
162                                          ArgParser.getUndefinedString());
163     }
164     return rv;
165   }
166
167   private void startServer() {
168     try {
169       server.crash();
170       server.start(SERVER_START_TIMEOUT);
171     } catch (Exception JavaDoc e) {
172       throw new RuntimeException JavaDoc(e);
173     }
174     if (!server.isRunning()) { throw new RuntimeException JavaDoc("Server still isn't running!"); }
175   }
176
177   private void startTestStarter() throws IOException JavaDoc, InterruptedException JavaDoc {
178     System.out.println("Starting TestStarter...");
179     testStarterProcess.start();
180     testStarterProcess.mergeSTDERR();
181     testStarterProcess.mergeSTDOUT();
182     testStarterProcess.waitFor();
183   }
184
185   private void startTestSetup() throws IOException JavaDoc, InterruptedException JavaDoc {
186     System.out.println("Starting TestSetup...");
187     testSetupProcess.start();
188     testSetupProcess.mergeSTDERR();
189     testSetupProcess.mergeSTDOUT();
190     testSetupProcess.waitFor();
191   }
192
193   public static void main(String JavaDoc[] args) throws Throwable JavaDoc {
194
195     Setup setup = null;
196     ArgParser argParser = null;
197     boolean testServerRequired = true;
198     boolean controlServerRequired = false;
199
200     try {
201       argParser = new ArgParser(args, new SpecFactoryImpl(), testServerRequired, controlServerRequired);
202     } catch (ArgException e) {
203       System.err.println("Error parsing arguments: " + e.getMessage());
204       System.err.println("\n\n" + ArgParser.usage());
205       System.exit(1);
206     }
207
208     // TODO: this needs to be fixed if multiple test servers are involved. for now assume there's only one test server.
209
setup = new Setup(args, argParser.getServerSpec(), argParser.getTestClassname(), argParser.getClientSpecs(),
210                       argParser.getIntensity(), argParser.getTestServerStartMode());
211     setup.execute();
212
213   }
214
215   public static void visitDSOApplicationConfig(ConfigVisitor visitor, DSOApplicationConfig cfg) {
216     cfg.addRoot("testEnvironmentView", Setup.class.getName() + ".testEnvironmentView");
217     cfg.addRoot("eventQueue", Setup.class.getName() + ".eventQueue");
218     cfg.addRoot("responseQueue", Setup.class.getName() + ".responseQueue");
219     cfg.addRoot("testEnvironmentView", "com.tcsimulator.webui.Manager.testEnvironmentView");
220     cfg.addRoot("eventQueue", "com.tcsimulator.webui.Manager.eventQueue");
221     cfg.addRoot("responseQueue", "com.tcsimulator.webui.Manager.responseQueue");
222   }
223
224 }
225
Popular Tags