001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tcsimulator;
006:
007: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
008:
009: import com.tc.object.config.ConfigVisitor;
010: import com.tc.object.config.DSOApplicationConfig;
011: import com.tc.objectserver.control.ExtraProcessServerControl;
012: import com.tc.objectserver.control.NullServerControl;
013: import com.tc.objectserver.control.ServerControl;
014: import com.tc.objectserver.control.ExtraProcessServerControl.DebugParams;
015: import com.tc.process.LinkedJavaProcess;
016: import com.tc.simulator.distrunner.ArgException;
017: import com.tc.simulator.distrunner.ArgParser;
018: import com.tc.simulator.distrunner.SpecFactoryImpl;
019: import com.tcsimulator.distrunner.ServerSpec;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.net.InetAddress;
024: import java.net.UnknownHostException;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: public class Setup {
031:
032: private final ServerSpec testServerSpec;
033: private final String testAppClassName;
034: private final ServerControl server;
035: private final String localHostName;
036: private final boolean isServerMachine;
037: private final ConfigWriter configWriter;
038: private Sandbox sandbox;
039: private final boolean debugTestSetup = false;
040: private final boolean debugTestStarter = false;
041: private LinkedJavaProcess testStarterProcess;
042: private LinkedJavaProcess testSetupProcess;
043:
044: private TestEnvironmentView testEnvironmentView;
045: private LinkedQueue eventQueue;
046: private LinkedQueue responseQueue;
047: private final boolean startedWithDSO;
048:
049: public Setup(String[] args, ServerSpec serverSpec,
050: String testAppClassName, Collection clientSpecs,
051: int intensity, boolean startedWithDSO)
052: throws ClassNotFoundException {
053:
054: this .testServerSpec = serverSpec;
055: this .testAppClassName = testAppClassName;
056: this .startedWithDSO = startedWithDSO;
057:
058: InetAddress host;
059: try {
060: host = InetAddress.getLocalHost();
061: } catch (UnknownHostException e) {
062: throw new RuntimeException(
063: "Could not resolve local host name." + e);
064: }
065: localHostName = host.getHostName();
066:
067: this .isServerMachine = testServerSpec.getHostName().equals(
068: localHostName);
069:
070: sandbox = new Sandbox(new File(testServerSpec.getTestHome()),
071: Sandbox.TEST_SERVER);
072:
073: if (this .isServerMachine) {
074: this .server = newServerControl();
075:
076: Collection classesToVisit = new ArrayList();
077: classesToVisit.add(TestSetup.class);
078: classesToVisit.add(TestSpec.class);
079: classesToVisit.add(TestStarter.class);
080: classesToVisit.add(ContainerBuilder.class);
081: classesToVisit.add(GlobalVmNameGenerator.class);
082: classesToVisit.add(Class.forName(this .testAppClassName));
083: configWriter = new ConfigWriter(testServerSpec,
084: classesToVisit, sandbox);
085: } else {
086: this .server = null;
087: this .configWriter = null;
088: }
089: ProcessFactory procFactory = new ProcessFactory(sandbox,
090: testServerSpec);
091:
092: this .testSetupProcess = procFactory.newDSOJavaProcessInstance(
093: TestSetup.class.getName(), args, debugTestSetup);
094: this .testStarterProcess = procFactory
095: .newDSOJavaProcessInstance(TestStarter.class.getName(),
096: new String[] {}, debugTestStarter);
097: if (this .isServerMachine) {
098: List clientSpecsCopy = new ArrayList();
099: for (Iterator i = clientSpecs.iterator(); i.hasNext();) {
100: ClientSpec cSpec = (ClientSpec) i.next();
101: ClientSpec cCopy = cSpec.copy();
102: clientSpecsCopy.add(cCopy);
103: }
104:
105: this .testEnvironmentView = new TestEnvironmentViewImpl(
106: serverSpec.copy(), clientSpecsCopy, intensity);
107: }
108:
109: if (this .startedWithDSO) {
110: this .eventQueue = new LinkedQueue();
111: this .responseQueue = new LinkedQueue();
112: }
113: }
114:
115: public void execute() throws IOException, InterruptedException {
116: if (isServerMachine) {
117: configWriter.writeConfigFile();
118: startServer();
119:
120: ((TestEnvironmentViewImpl) this .testEnvironmentView)
121: .setServerRunning(ServerView.RUNNING);
122:
123: startTestSetup();
124:
125: if (this .startedWithDSO) {
126: startEventQueueHandler();
127: }
128: }
129: startTestStarter();
130: }
131:
132: private void startEventQueueHandler() {
133: System.out.println("Starting EventQueueHandler...");
134: EventQueueHandler eqhandler = new EventQueueHandler(
135: this .eventQueue, this );
136: Thread t = new Thread(eqhandler);
137: t.setDaemon(true);
138: t.start();
139: }
140:
141: public void crashServer() throws Exception {
142: synchronized (this .server) {
143: // if (!this.server.isRunning()) { throw new RuntimeException("Server is not running... server crash failed!"); }
144: this .server.crash();
145: if (this .server.isRunning()) {
146: throw new RuntimeException(
147: "srever is still running... server crash failed!");
148: }
149:
150: ((TestEnvironmentViewImpl) this .testEnvironmentView)
151: .setServerRunning(ServerView.NOT_RUNNING);
152:
153: this .responseQueue.put("Crash successful!");
154: }
155: }
156:
157: public void restartServer() throws Exception {
158: synchronized (this .server) {
159: if (this .server.isRunning()) {
160: throw new RuntimeException(
161: "Server is already running... server restart failed!");
162: }
163: this .server.start();
164: if (!this .server.isRunning()) {
165: throw new RuntimeException(
166: "Server is not running... server restart failed!");
167: }
168:
169: ((TestEnvironmentViewImpl) this .testEnvironmentView)
170: .setServerRunning(ServerView.RUNNING);
171:
172: this .responseQueue.put("Restart successful!");
173: }
174: }
175:
176: private ServerControl newServerControl() {
177: ServerControl rv;
178: boolean mergeOutput = true;
179: if (!isServerMachine) {
180: rv = new NullServerControl();
181: } else {
182: rv = new ExtraProcessServerControl(new DebugParams(),
183: this .testServerSpec.getHostName(),
184: this .testServerSpec.getDsoPort(),
185: this .testServerSpec.getJmxPort(), this .sandbox
186: .getConfigFile().getAbsolutePath(),
187: this .sandbox.getServerHome(), mergeOutput,
188: this .testServerSpec.getJvmOpts(), ArgParser
189: .getUndefinedString());
190: }
191: return rv;
192: }
193:
194: private void startServer() {
195: try {
196: server.crash();
197: server.start();
198: } catch (Exception e) {
199: throw new RuntimeException(e);
200: }
201: if (!server.isRunning()) {
202: throw new RuntimeException("Server still isn't running!");
203: }
204: }
205:
206: private void startTestStarter() throws IOException,
207: InterruptedException {
208: System.out.println("Starting TestStarter...");
209: testStarterProcess.start();
210: testStarterProcess.mergeSTDERR();
211: testStarterProcess.mergeSTDOUT();
212: testStarterProcess.waitFor();
213: }
214:
215: private void startTestSetup() throws IOException,
216: InterruptedException {
217: System.out.println("Starting TestSetup...");
218: testSetupProcess.start();
219: testSetupProcess.mergeSTDERR();
220: testSetupProcess.mergeSTDOUT();
221: testSetupProcess.waitFor();
222: }
223:
224: public static void main(String[] args) throws Throwable {
225:
226: Setup setup = null;
227: ArgParser argParser = null;
228: boolean testServerRequired = true;
229: boolean controlServerRequired = false;
230:
231: try {
232: argParser = new ArgParser(args, new SpecFactoryImpl(),
233: testServerRequired, controlServerRequired);
234: } catch (ArgException e) {
235: System.err.println("Error parsing arguments: "
236: + e.getMessage());
237: System.err.println("\n\n" + ArgParser.usage());
238: System.exit(1);
239: }
240:
241: // TODO: this needs to be fixed if multiple test servers are involved. for now assume there's only one test server.
242: setup = new Setup(args, argParser.getServerSpec(), argParser
243: .getTestClassname(), argParser.getClientSpecs(),
244: argParser.getIntensity(), argParser
245: .getTestServerStartMode());
246: setup.execute();
247:
248: }
249:
250: public static void visitDSOApplicationConfig(ConfigVisitor visitor,
251: DSOApplicationConfig cfg) {
252: cfg.addRoot("testEnvironmentView", Setup.class.getName()
253: + ".testEnvironmentView");
254: cfg
255: .addRoot("eventQueue", Setup.class.getName()
256: + ".eventQueue");
257: cfg.addRoot("responseQueue", Setup.class.getName()
258: + ".responseQueue");
259: cfg.addRoot("testEnvironmentView",
260: "com.tcsimulator.webui.Manager.testEnvironmentView");
261: cfg.addRoot("eventQueue",
262: "com.tcsimulator.webui.Manager.eventQueue");
263: cfg.addRoot("responseQueue",
264: "com.tcsimulator.webui.Manager.responseQueue");
265: }
266:
267: }
|