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.tc.simulator.distrunner;
006:
007: import com.tc.objectserver.control.ExtraProcessServerControl;
008: import com.tc.objectserver.control.ServerControl;
009: import com.tc.objectserver.control.ExtraProcessServerControl.DebugParams;
010: import com.tc.process.LinkedJavaProcess;
011: import com.tcsimulator.ConfigWriter;
012: import com.tcsimulator.ProcessFactory;
013: import com.tcsimulator.Sandbox;
014: import com.tcsimulator.Setup;
015: import com.tcsimulator.distrunner.ServerSpec;
016:
017: import java.io.File;
018: import java.io.IOException;
019: import java.net.InetAddress;
020: import java.net.UnknownHostException;
021: import java.util.ArrayList;
022: import java.util.Collection;
023:
024: public class ControlSetup {
025:
026: private final ServerSpec controlServerSpec;
027: private final ConfigWriter configWriter;
028: private final Sandbox sandbox;
029: private final ServerControl server;
030: private final boolean debugSetup = false;
031: private final LinkedJavaProcess setupProcess;
032: private final boolean isServerMachine;
033: private final String localHostName;
034: private final boolean startWithDSO;
035:
036: public ControlSetup(String[] args, ServerSpec controlServerSpec,
037: boolean startWithDSO, String javaHome) {
038:
039: this .controlServerSpec = controlServerSpec;
040: this .startWithDSO = startWithDSO;
041:
042: InetAddress host;
043: try {
044: host = InetAddress.getLocalHost();
045: } catch (UnknownHostException e) {
046: throw new RuntimeException(
047: "Could not resolve local host name." + e);
048: }
049: localHostName = host.getHostName();
050:
051: if (this .localHostName.equals(this .controlServerSpec
052: .getHostName())) {
053: this .isServerMachine = true;
054: } else {
055: this .isServerMachine = false;
056: }
057:
058: this .sandbox = new Sandbox(new File(this .controlServerSpec
059: .getTestHome()), Sandbox.CONTROL_SERVER);
060:
061: if (this .isServerMachine) {
062: Collection classesToVisit = new ArrayList();
063: classesToVisit.add(Setup.class);
064: this .configWriter = new ConfigWriter(
065: this .controlServerSpec, classesToVisit, sandbox);
066: this .server = newServerControl();
067: } else {
068: this .configWriter = null;
069: this .server = null;
070: }
071:
072: ProcessFactory procFactory = new ProcessFactory(sandbox,
073: this .controlServerSpec);
074:
075: if (this .startWithDSO) {
076: this .setupProcess = procFactory.newDSOJavaProcessInstance(
077: Setup.class.getName(), args, debugSetup);
078: } else {
079: this .setupProcess = procFactory.newJavaProcessInstance(
080: Setup.class.getName(), args, debugSetup, javaHome);
081: }
082: }
083:
084: private void execute() throws IOException, InterruptedException {
085: if (this .isServerMachine && this .startWithDSO) {
086: configWriter.writeConfigFile();
087: startServer();
088: }
089: startSetup();
090: }
091:
092: private void startSetup() throws IOException, InterruptedException {
093: System.out.println("Starting Setup...");
094: setupProcess.start();
095: setupProcess.mergeSTDERR();
096: setupProcess.mergeSTDOUT();
097: setupProcess.waitFor();
098: }
099:
100: private ServerControl newServerControl() {
101: boolean mergeOutput = true;
102: return new ExtraProcessServerControl(new DebugParams(),
103: this .controlServerSpec.getHostName(),
104: this .controlServerSpec.getDsoPort(),
105: this .controlServerSpec.getJmxPort(), this .sandbox
106: .getConfigFile().getAbsolutePath(),
107: this .sandbox.getServerHome(), mergeOutput,
108: this .controlServerSpec.getJvmOpts(), ArgParser
109: .getUndefinedString());
110: }
111:
112: private void startServer() {
113: try {
114: this .server.crash();
115: this .server.start();
116: } catch (Exception e) {
117: throw new RuntimeException(e);
118: }
119: if (!this .server.isRunning()) {
120: throw new RuntimeException("Server still isn't running!");
121: }
122: }
123:
124: public static void main(String[] args) throws IOException,
125: InterruptedException {
126:
127: boolean controlServerRequired = true;
128: boolean testServerRequired = true;
129: ArgParser parser = null;
130:
131: try {
132: parser = new ArgParser(args, new SpecFactoryImpl(),
133: testServerRequired, controlServerRequired);
134: } catch (ArgException e) {
135: System.err.println("Error parsing arguments: "
136: + e.getMessage());
137: System.err.println("\n\n" + ArgParser.usage());
138: System.exit(1);
139: }
140:
141: ControlSetup controlSetup = new ControlSetup(args, parser
142: .getControlServerSpec(), parser
143: .getTestServerStartMode(), parser.getJavaHome());
144: controlSetup.execute();
145: }
146: }
|