KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > integrationtests > framework > ServerManager


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.spring.integrationtests.framework;
5
6 import org.apache.commons.io.FileUtils;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import com.tc.test.TestConfigObject;
11 import com.tc.test.server.AbstractDBServer;
12 import com.tc.test.server.HSqlDBServer;
13 import com.tc.test.server.appserver.AppServerInstallation;
14 import com.tc.test.server.appserver.NewAppServerFactory;
15 import com.tc.test.server.tcconfig.StandardTerracottaAppServerConfig;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import junit.framework.Assert;
25
26 public class ServerManager {
27   private static int appServerIndex = 0;
28
29   private List JavaDoc serversToStop = new ArrayList JavaDoc();
30   protected Log logger = LogFactory.getLog(getClass());
31   private DSOServer dsoServer;
32
33   private final TestConfigObject config;
34   private NewAppServerFactory factory;
35
36   static final boolean MONKEY_MODE = true;
37
38   public ServerManager(Class JavaDoc testClass) throws Exception JavaDoc {
39     this.testClass = testClass;
40     PropertiesHackForRunningInEclipse.initializePropertiesWhenRunningInEclipse();
41
42     config = TestConfigObject.getInstance();
43     factory = NewAppServerFactory.createFactoryFromProperties(config);
44
45     tempDir = TempDirectoryUtil.getTempDirectory(testClass);
46
47     workingDir = workingDir();
48
49     String JavaDoc appserverURLBase = config.appserverURLBase();
50     String JavaDoc appserverHome = config.appserverHome();
51
52     if (appserverHome != null && !appserverHome.trim().equals("")) {
53       File JavaDoc home = new File JavaDoc(appserverHome);
54       installation = factory.createInstallation(home, workingDir);
55
56     } else if (appserverURLBase != null && !appserverURLBase.trim().equals("")) {
57       URL JavaDoc host = new URL JavaDoc(appserverURLBase);
58       installation = factory.createInstallation(host, serverInstallDir(), workingDir);
59
60     } else {
61       throw new AssertionError JavaDoc(
62                                "No container installation available. You must define one of the following config properties:\n"
63                                    + TestConfigObject.APP_SERVER_HOME + "\nor\n"
64                                    + TestConfigObject.APP_SERVER_REPOSITORY_URL_BASE);
65     }
66   }
67
68   private static final String JavaDoc SERVER_INSTALL = "server-install";
69   private AppServerInstallation installation;
70   private File JavaDoc workingDir;
71
72   private File JavaDoc tempDir;
73
74   private final Class JavaDoc testClass;
75
76   private synchronized File JavaDoc serverInstallDir() {
77     return makeDir(config.appserverServerInstallDir() + File.separator + SERVER_INSTALL);
78   }
79
80   // FIXME reuse the same dir and cleanup each time
81

82   private synchronized File JavaDoc workingDir() throws IOException JavaDoc {
83     String JavaDoc osName = config.osName();
84     File JavaDoc dir = null;
85
86     if (osName != null && osName.startsWith("Windows")) {
87       dir = makeDir(config.appserverWorkingDir() + File.separator);
88     } else {
89       dir = makeDir(tempDir.getAbsolutePath());
90     }
91     FileUtils.cleanDirectory(dir);
92     return dir;
93   }
94
95   private File JavaDoc makeDir(String JavaDoc dirPath) {
96     File JavaDoc dir = new File JavaDoc(dirPath);
97     if (dir.exists()) return dir;
98     dir.mkdirs();
99     return dir;
100   }
101
102   void addServerToStop(Stoppable stoppable) {
103     getServersToStop().add(0, stoppable);
104   }
105
106   void stop() {
107     for (Iterator JavaDoc it = getServersToStop().iterator(); it.hasNext();) {
108       Stoppable stoppable = (Stoppable) it.next();
109       try {
110         if (!stoppable.isStopped()) stoppable.stop();
111       } catch (Exception JavaDoc e) {
112         logger.error(stoppable, e);
113       }
114     }
115     File JavaDoc toDir = new File JavaDoc(tempDir, "working");
116
117     if (MONKEY_MODE) {
118       logger.warn("working dir under short-name: " + workingDir);
119       logger.warn("working dir under temp: " + toDir);
120
121       if (!workingDir.getPath().equals(toDir.getPath())) {
122         logger.warn("Moving " + workingDir + "->" + toDir);
123
124         if (!workingDir.renameTo(toDir)) {
125           logger.warn("could not rename: " + workingDir + "->" + toDir);
126         } else {
127           logger.warn("Moved " + workingDir + "->" + toDir);
128         }
129       }
130     }
131   }
132
133   protected boolean cleanTempDir() {
134     return true;
135   }
136
137   void start(boolean withPersistentStore) throws Exception JavaDoc {
138     startDSO(withPersistentStore);
139   }
140
141   private void startDSO(boolean withPersistentStore) throws Exception JavaDoc {
142     dsoServer = new DSOServer(withPersistentStore, workingDir);
143     dsoServer.start();
144     addServerToStop(dsoServer);
145   }
146
147   public void restartDSO(boolean withPersistentStore) throws Exception JavaDoc {
148     dsoServer.stop();
149     startDSO(withPersistentStore);
150   }
151
152   public WebApplicationServer makeWebApplicationServer(String JavaDoc tcConfigPath) throws Exception JavaDoc {
153     return makeWebApplicationServer(getTcConfigFile(tcConfigPath));
154   }
155
156   public AbstractDBServer makeDBServer(String JavaDoc dbType, String JavaDoc dbName, int serverPort) {
157     // XXX this should use server factory
158
AbstractDBServer svr = new HSqlDBServer(dbName, serverPort);
159     this.addServerToStop(svr);
160     return svr;
161   }
162
163   public FileSystemPath getTcConfigFile(String JavaDoc tcConfigPath) {
164     URL JavaDoc url = getClass().getResource(tcConfigPath);
165     Assert.assertNotNull("could not find: " + tcConfigPath, url);
166     Assert.assertTrue("should be file:" + url.toString(), url.toString().startsWith("file:"));
167     FileSystemPath pathToTcConfigFile = FileSystemPath.makeExistingFile(url.toString().substring("file:".length()));
168     return pathToTcConfigFile;
169   }
170
171   public WebApplicationServer makeWebApplicationServer(FileSystemPath tcConfigPath) throws Exception JavaDoc {
172     int i = ServerManager.appServerIndex++;
173
174     WebApplicationServer tomcatServer = new GenericServer(config, factory, installation, tcConfigPath, i, tempDir);
175     addServerToStop(tomcatServer);
176     return tomcatServer;
177   }
178
179   public WebApplicationServer makeWebApplicationServer(StandardTerracottaAppServerConfig tcConfig) throws Exception JavaDoc {
180     int i = ServerManager.appServerIndex++;
181
182     WebApplicationServer tomcatServer = new GenericServer(config, factory, installation, tcConfig, i, tempDir);
183     addServerToStop(tomcatServer);
184     return tomcatServer;
185   }
186
187   void setServersToStop(List JavaDoc serversToStop) {
188     this.serversToStop = serversToStop;
189   }
190
191   List JavaDoc getServersToStop() {
192     return serversToStop;
193   }
194
195   public DeploymentBuilder makeDeploymentBuilder(String JavaDoc warFileName) {
196     return new WARBuilder(warFileName, tempDir, config);
197   }
198
199   public DeploymentBuilder makeDeploymentBuilder() throws IOException JavaDoc {
200     return new WARBuilder(tempDir, config);
201   }
202
203   public StandardTerracottaAppServerConfig getConfig() {
204     return factory.createTcConfig(installation.dataDirectory());
205   }
206
207   public void stopAllWebServers() {
208     for (Iterator JavaDoc it = getServersToStop().iterator(); it.hasNext();) {
209       Stoppable stoppable = (Stoppable) it.next();
210       try {
211         if (!(stoppable instanceof DSOServer || stoppable.isStopped())) stoppable.stop();
212       } catch (Exception JavaDoc e) {
213         logger.error(stoppable, e);
214       }
215     }
216   }
217
218   public TestConfigObject getTestConfig() {
219     return this.config;
220   }
221 }
222
Popular Tags