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.test.server.appserver.deployment;
006:
007: import com.tc.config.schema.test.L2ConfigBuilder;
008: import com.tc.config.schema.test.TerracottaConfigBuilder;
009: import com.tc.objectserver.control.ExtraProcessServerControl;
010: import com.tc.test.server.util.TcConfigBuilder;
011:
012: import java.io.File;
013: import java.io.FileOutputStream;
014: import java.io.IOException;
015: import java.io.PrintWriter;
016: import java.util.ArrayList;
017: import java.util.List;
018:
019: public class DSOServer extends AbstractStoppable {
020:
021: private static final String SERVER_TEST_CONFIG = "server-config.xml";
022:
023: private ExtraProcessServerControl serverProc = null;
024: private final boolean withPersistentStore;
025:
026: private int serverPort = 9510;
027: private int adminPort = 9520;
028: private List jvmArgs = new ArrayList();
029:
030: private final File workingDir;
031: private TcConfigBuilder configBuilder;
032:
033: public DSOServer(boolean withPersistentStore, File workingDir) {
034: this .withPersistentStore = withPersistentStore;
035: this .workingDir = workingDir;
036: }
037:
038: public DSOServer(boolean withPersistentStore, File workingDir,
039: TcConfigBuilder configBuilder) {
040: this .withPersistentStore = withPersistentStore;
041: this .workingDir = workingDir;
042: this .configBuilder = configBuilder;
043: this .serverPort = configBuilder.getDsoPort();
044: this .adminPort = configBuilder.getJmxPort();
045: }
046:
047: protected void doStart() throws Exception {
048: File configFile = writeConfig();
049: serverProc = new ExtraProcessServerControl("localhost",
050: serverPort, adminPort, configFile.getAbsolutePath(),
051: false);
052: serverProc
053: .writeOutputTo(new File(workingDir, "dso-server.log"));
054: serverProc.getJvmArgs().addAll(jvmArgs);
055: serverProc.start();
056: }
057:
058: protected void doStop() throws Exception {
059: logger.debug("Stopping...");
060: serverProc.shutdown();
061: logger.debug("...stopped");
062: }
063:
064: private File writeConfig() throws IOException {
065: File configFile = new File(workingDir, SERVER_TEST_CONFIG);
066: if (configBuilder != null) {
067: configBuilder.saveToFile(configFile);
068: } else {
069: TerracottaConfigBuilder builder = TerracottaConfigBuilder
070: .newMinimalInstance();
071: builder.getSystem().setConfigurationModel("development");
072:
073: L2ConfigBuilder l2 = builder.getServers().getL2s()[0];
074: l2.setDSOPort(serverPort);
075: l2.setJMXPort(adminPort);
076: l2.setData(workingDir + File.separator + "data");
077: l2.setLogs(workingDir + File.separator + "logs");
078: if (withPersistentStore) {
079: l2.setPersistenceMode("permanent-store"); // XXX make this one configurable
080: }
081:
082: String configAsString = builder.toString();
083:
084: FileOutputStream fileOutputStream = new FileOutputStream(
085: configFile);
086: PrintWriter out = new PrintWriter((fileOutputStream));
087: out.println(configAsString);
088: out.flush();
089: out.close();
090: }
091: return configFile;
092: }
093:
094: public String toString() {
095: return "DSO server; serverport:" + serverPort + "; adminPort:"
096: + adminPort;
097: }
098:
099: public int getServerPort() {
100: return serverPort;
101: }
102:
103: public int getAdminPort() {
104: return adminPort;
105: }
106:
107: public List getJvmArgs() {
108: return jvmArgs;
109: }
110:
111: }
|