01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tctest;
06:
07: import org.apache.commons.io.FileUtils;
08:
09: import com.tc.objectserver.control.ExtraL1ProcessControl;
10: import com.tc.simulator.app.ApplicationConfig;
11: import com.tc.simulator.listener.ListenerProvider;
12: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
13:
14: import java.io.File;
15: import java.net.URL;
16: import java.util.ArrayList;
17: import java.util.List;
18:
19: public abstract class ServerCrashingAppBase extends
20: AbstractErrorCatchingTransparentApp {
21:
22: public static final String CONFIG_FILE = "config-file";
23: public static final String PORT_NUMBER = "port-number";
24: public static final String HOST_NAME = "host-name";
25:
26: private ApplicationConfig config;
27:
28: public ServerCrashingAppBase(String appId,
29: ApplicationConfig config, ListenerProvider listenerProvider) {
30: super (appId, config, listenerProvider);
31: this .config = config;
32: }
33:
34: // used by external clients
35: public ServerCrashingAppBase() {
36: super ();
37: }
38:
39: public ApplicationConfig getConfig() {
40: return config;
41: }
42:
43: public String getHostName() {
44: return config.getAttribute(HOST_NAME);
45: }
46:
47: public int getPort() {
48: return Integer.parseInt(config.getAttribute(PORT_NUMBER));
49: }
50:
51: public String getConfigFilePath() {
52: return config.getAttribute(CONFIG_FILE);
53: }
54:
55: public String getConfigFileDirectoryPath() {
56: String configFile = getConfigFilePath();
57: int i = configFile.lastIndexOf(File.separatorChar);
58: return configFile.substring(0, i);
59: }
60:
61: protected ExtraL1ProcessControl spawnNewClient(String clientId,
62: Class clientClass) throws Exception {
63: final String hostName = getHostName();
64: final int port = getPort();
65: final File configFile = new File(getConfigFilePath());
66: File workingDir = new File(configFile.getParentFile(),
67: "client-" + clientId);
68: FileUtils.forceMkdir(workingDir);
69:
70: List jvmArgs = new ArrayList();
71: addTestTcPropertiesFile(jvmArgs);
72: ExtraL1ProcessControl client = new ExtraL1ProcessControl(
73: hostName, port, clientClass, configFile
74: .getAbsolutePath(), new String[0], workingDir,
75: jvmArgs);
76: client.start();
77: client.mergeSTDERR();
78: client.mergeSTDOUT();
79: client.waitFor();
80: System.err.println("\n### Started New Client");
81: return client;
82: }
83:
84: protected final void addTestTcPropertiesFile(List jvmArgs) {
85: URL url = getClass().getResource(
86: "/com/tc/properties/tests.properties");
87: if (url == null) {
88: return;
89: }
90: String pathToTestTcProperties = url.getPath();
91: if (pathToTestTcProperties == null
92: || pathToTestTcProperties.equals("")) {
93: return;
94: }
95: // System.err.println("\n##### -Dcom.tc.properties=" + pathToTestTcProperties);
96: jvmArgs.add("-Dcom.tc.properties=" + pathToTestTcProperties);
97: }
98:
99: }
|