001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tcsimulator;
005:
006: import com.tc.object.config.ConfigVisitor;
007: import com.tc.object.config.DSOApplicationConfig;
008: import com.tc.process.LinkedJavaProcess;
009: import com.tc.simulator.distrunner.ArgParser;
010: import com.tcsimulator.distrunner.ServerSpec;
011:
012: import java.io.File;
013: import java.io.IOException;
014: import java.net.InetAddress;
015: import java.util.ArrayList;
016: import java.util.Collection;
017: import java.util.HashSet;
018: import java.util.Iterator;
019:
020: public class TestStarter {
021:
022: private TestSpec testSpec;
023: private final boolean debug = false;
024:
025: public TestStarter() {
026: testSpec = new TestSpec();
027: }
028:
029: public static void main(String[] args) throws IOException,
030: InterruptedException {
031: TestStarter runner = new TestStarter();
032: runner.execute();
033: }
034:
035: public static void visitDSOApplicationConfig(ConfigVisitor visitor,
036: DSOApplicationConfig cfg) {
037: String classname = TestStarter.class.getName();
038: cfg.addRoot("testSpec", classname + ".testSpec");
039: cfg.addIncludePattern(classname);
040: cfg.addWriteAutolock("* " + classname + ".*(..)");
041: }
042:
043: private void println(String msg) {
044: System.out.println("TestStarter: " + msg);
045: }
046:
047: private void execute() throws IOException, InterruptedException {
048: InetAddress host = InetAddress.getLocalHost();
049: String hostName = host.getHostName();
050:
051: Collection procList = new ArrayList();
052:
053: int debugPortOffset = 0;
054:
055: synchronized (testSpec) {
056:
057: Collection containerSpecs = testSpec
058: .getContainerSpecsFor(hostName);
059:
060: if (containerSpecs.size() == 0) {
061: System.err
062: .println("No containers setup for this host: "
063: + hostName + ", " + testSpec);
064: return;
065: }
066:
067: for (Iterator i = containerSpecs.iterator(); i.hasNext();) {
068: ContainerSpec containerSpec = (ContainerSpec) i.next();
069: println("Found containerSpec: " + containerSpec);
070: LinkedJavaProcess proc = new LinkedJavaProcess(
071: ContainerBuilder.class.getName(),
072: new String[] { containerSpec.getVmName() });
073: String fileSeparator = System
074: .getProperty("file.separator");
075:
076: File terracottaDist = new File(new File(containerSpec
077: .getTestHome()).getAbsoluteFile(), "terracotta");
078: File dsoJava = new File(terracottaDist
079: .getAbsolutePath()
080: + fileSeparator
081: + "bin"
082: + fileSeparator
083: + "dso-java.sh");
084:
085: Collection jvmArgs = new HashSet();
086: for (Iterator iter = containerSpec.getJvmOpts()
087: .iterator(); iter.hasNext();) {
088: String next = (String) iter.next();
089: if (!next.equals(ArgParser.getUndefinedString())) {
090: jvmArgs.add(next);
091: }
092: }
093: ServerSpec sSpec = (ServerSpec) testSpec
094: .getServerSpecsFor(ServerSpec.TEST_SERVER)
095: .iterator().next();
096: jvmArgs.add("-Dtc.config=" + sSpec.getHostName() + ":"
097: + sSpec.getDsoPort());
098: jvmArgs.add("-Dtc.classloader.writeToDisk=true");
099: if (debug) {
100: jvmArgs.add("-Xdebug");
101: jvmArgs
102: .add("-Xrunjdwp:transport=dt_socket,address="
103: + (8000 + debugPortOffset)
104: + ",server=y,suspend=y");
105: debugPortOffset++;
106: }
107: Collection environment = new HashSet();
108: if ("Mac OS X".equals(System.getProperty("os.name"))
109: || "Linux"
110: .equals(System.getProperty("os.name"))) {
111: environment.add("TC_JAVA_HOME="
112: + System.getProperty("java.home"));
113: }
114: proc.setJavaExecutable(dsoJava);
115: File dir = new File(containerSpec.getTestHome(),
116: containerSpec.getVmName());
117: dir.mkdir();
118: proc.setDirectory(dir);
119: proc.setJavaArguments((String[]) jvmArgs
120: .toArray(new String[jvmArgs.size()]));
121: proc.setEnvironment((String[]) environment
122: .toArray(new String[environment.size()]));
123: proc.start();
124: proc.mergeSTDERR();
125: proc.mergeSTDOUT();
126: procList.add(proc);
127:
128: }
129:
130: }
131:
132: for (Iterator i = procList.iterator(); i.hasNext();) {
133: ((LinkedJavaProcess) i.next()).waitFor();
134: }
135:
136: }
137: }
|