001: /*
002: * All content copyright (c) 2003-2007 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.logging.TCLogger;
008: import com.tc.logging.TCLogging;
009: import com.tc.test.TestConfigObject;
010: import com.tc.test.server.appserver.AppServerFactory;
011: import com.tc.test.server.appserver.AppServerInstallation;
012: import com.tc.test.server.util.AppServerUtil;
013: import com.tc.test.server.util.TcConfigBuilder;
014: import com.tc.util.PortChooser;
015: import com.tc.util.TIMUtil;
016: import com.tc.util.runtime.Os;
017: import com.tc.util.runtime.Vm;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.net.URL;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import junit.framework.Assert;
027:
028: public class ServerManager {
029:
030: protected static TCLogger logger = TCLogging
031: .getLogger(ServerManager.class);
032: private static int appServerIndex = 0;
033: private final boolean DEBUG_MODE = false;
034:
035: private List serversToStop = new ArrayList();
036: private DSOServer dsoServer;
037:
038: private final TestConfigObject config;
039: private AppServerFactory factory;
040: private AppServerInstallation installation;
041: private File sandbox;
042: private File tempDir;
043: private File installDir;
044: private File warDir;
045: private TcConfigBuilder serverTcConfig = new TcConfigBuilder();
046:
047: public ServerManager(final Class testClass) throws Exception {
048: PropertiesHackForRunningInEclipse
049: .initializePropertiesWhenRunningInEclipse();
050: config = TestConfigObject.getInstance();
051: factory = AppServerFactory.createFactoryFromProperties(config);
052: installDir = config.appserverServerInstallDir();
053: tempDir = TempDirectoryUtil.getTempDirectory(testClass);
054: sandbox = AppServerUtil.createSandbox(tempDir);
055: warDir = new File(sandbox, "war");
056: installation = AppServerUtil.createAppServerInstallation(
057: factory, installDir, sandbox);
058:
059: if (DEBUG_MODE) {
060: serverTcConfig.setDsoPort(9510);
061: serverTcConfig.setJmxPort(9520);
062: } else {
063: PortChooser pc = new PortChooser();
064: serverTcConfig.setDsoPort(pc.chooseRandomPort());
065: serverTcConfig.setJmxPort(pc.chooseRandomPort());
066: }
067: }
068:
069: public void addServerToStop(Stoppable stoppable) {
070: getServersToStop().add(0, stoppable);
071: }
072:
073: void stop() {
074: logger.info("Stopping all servers");
075: for (Iterator it = getServersToStop().iterator(); it.hasNext();) {
076: Stoppable stoppable = (Stoppable) it.next();
077: try {
078: if (!stoppable.isStopped()) {
079: logger.debug("About to stop server: "
080: + stoppable.toString());
081: stoppable.stop();
082: }
083: } catch (Exception e) {
084: logger.error(stoppable, e);
085: }
086: }
087:
088: AppServerUtil.shutdownAndArchive(sandbox, new File(tempDir,
089: "sandbox"));
090: }
091:
092: void timeout() {
093: logger
094: .info("Test has timed out. Force shutdown and archive...");
095: AppServerUtil.forceShutdownAndArchive(sandbox, new File(
096: tempDir, "sandbox"));
097: }
098:
099: protected boolean cleanTempDir() {
100: return false;
101: }
102:
103: void start(boolean withPersistentStore) throws Exception {
104: startDSO(withPersistentStore);
105: }
106:
107: private void startDSO(boolean withPersistentStore) throws Exception {
108: dsoServer = new DSOServer(withPersistentStore, tempDir,
109: serverTcConfig);
110: if (!Vm.isIBM() && !(Os.isMac() && Vm.isJDK14())) {
111: dsoServer.getJvmArgs().add(
112: "-XX:+HeapDumpOnOutOfMemoryError");
113: }
114: dsoServer.getJvmArgs().add("-Xmx128m");
115: logger.debug("Starting DSO server with sandbox: "
116: + sandbox.getAbsolutePath());
117: dsoServer.start();
118: addServerToStop(dsoServer);
119: }
120:
121: public void restartDSO(boolean withPersistentStore)
122: throws Exception {
123: logger.debug("Restarting DSO server : " + dsoServer);
124: dsoServer.stop();
125: startDSO(withPersistentStore);
126: }
127:
128: /**
129: * tcConfigPath: resource path
130: */
131: public WebApplicationServer makeWebApplicationServer(
132: String tcConfigPath) throws Exception {
133: return makeWebApplicationServer(new TcConfigBuilder(
134: tcConfigPath));
135: }
136:
137: public WebApplicationServer makeWebApplicationServer(
138: TcConfigBuilder tcConfigBuilder) throws Exception {
139: int i = ServerManager.appServerIndex++;
140:
141: WebApplicationServer appServer = new GenericServer(config,
142: factory, installation,
143: prepareClientTcConfig(tcConfigBuilder), i, tempDir);
144: addServerToStop(appServer);
145: return appServer;
146: }
147:
148: public FileSystemPath getTcConfigFile(String tcConfigPath) {
149: URL url = getClass().getResource(tcConfigPath);
150: Assert.assertNotNull("could not find: " + tcConfigPath, url);
151: Assert.assertTrue("should be file:" + url.toString(), url
152: .toString().startsWith("file:"));
153: FileSystemPath pathToTcConfigFile = FileSystemPath
154: .makeExistingFile(url.toString().substring(
155: "file:".length()));
156: return pathToTcConfigFile;
157: }
158:
159: private TcConfigBuilder prepareClientTcConfig(
160: TcConfigBuilder clientConfig) {
161: TcConfigBuilder aCopy = clientConfig.copy();
162: aCopy.setDsoPort(getServerTcConfig().getDsoPort());
163: aCopy.setJmxPort(getServerTcConfig().getJmxPort());
164:
165: int appId = AppServerFactory.getCurrentAppServerId();
166: switch (appId) {
167: case AppServerFactory.JETTY:
168: aCopy.addModule(TIMUtil.JETTY_6_1, TIMUtil
169: .getVersion(TIMUtil.JETTY_6_1));
170: break;
171: case AppServerFactory.WEBSPHERE:
172: aCopy.addModule(TIMUtil.WEBSPHERE_6_1_0_7, TIMUtil
173: .getVersion(TIMUtil.WEBSPHERE_6_1_0_7));
174: break;
175: default:
176: // nothing for now
177: }
178:
179: return aCopy;
180: }
181:
182: void setServersToStop(List serversToStop) {
183: this .serversToStop = serversToStop;
184: }
185:
186: List getServersToStop() {
187: return serversToStop;
188: }
189:
190: public DeploymentBuilder makeDeploymentBuilder(String warFileName) {
191: return new WARBuilder(warFileName, warDir, config);
192: }
193:
194: public DeploymentBuilder makeDeploymentBuilder() throws IOException {
195: return new WARBuilder(warDir, config);
196: }
197:
198: public void stopAllWebServers() {
199: for (Iterator it = getServersToStop().iterator(); it.hasNext();) {
200: Stoppable stoppable = (Stoppable) it.next();
201: try {
202: if (!(stoppable instanceof DSOServer || stoppable
203: .isStopped()))
204: stoppable.stop();
205: } catch (Exception e) {
206: logger.error("Unable to stop server: " + stoppable, e);
207: }
208: }
209: }
210:
211: public TestConfigObject getTestConfig() {
212: return this .config;
213: }
214:
215: public File getSandbox() {
216: return sandbox;
217: }
218:
219: public File getTempDir() {
220: return tempDir;
221: }
222:
223: public TcConfigBuilder getServerTcConfig() {
224: return serverTcConfig;
225: }
226: }
|