1 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 ; 18 import java.io.IOException ; 19 import java.net.URL ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import junit.framework.Assert; 25 26 public class ServerManager { 27 private static int appServerIndex = 0; 28 29 private List serversToStop = new ArrayList (); 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 testClass) throws Exception { 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 appserverURLBase = config.appserverURLBase(); 50 String appserverHome = config.appserverHome(); 51 52 if (appserverHome != null && !appserverHome.trim().equals("")) { 53 File home = new File (appserverHome); 54 installation = factory.createInstallation(home, workingDir); 55 56 } else if (appserverURLBase != null && !appserverURLBase.trim().equals("")) { 57 URL host = new URL (appserverURLBase); 58 installation = factory.createInstallation(host, serverInstallDir(), workingDir); 59 60 } else { 61 throw new AssertionError ( 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 SERVER_INSTALL = "server-install"; 69 private AppServerInstallation installation; 70 private File workingDir; 71 72 private File tempDir; 73 74 private final Class testClass; 75 76 private synchronized File serverInstallDir() { 77 return makeDir(config.appserverServerInstallDir() + File.separator + SERVER_INSTALL); 78 } 79 80 82 private synchronized File workingDir() throws IOException { 83 String osName = config.osName(); 84 File 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 makeDir(String dirPath) { 96 File dir = new File (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 it = getServersToStop().iterator(); it.hasNext();) { 108 Stoppable stoppable = (Stoppable) it.next(); 109 try { 110 if (!stoppable.isStopped()) stoppable.stop(); 111 } catch (Exception e) { 112 logger.error(stoppable, e); 113 } 114 } 115 File toDir = new File (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 { 138 startDSO(withPersistentStore); 139 } 140 141 private void startDSO(boolean withPersistentStore) throws Exception { 142 dsoServer = new DSOServer(withPersistentStore, workingDir); 143 dsoServer.start(); 144 addServerToStop(dsoServer); 145 } 146 147 public void restartDSO(boolean withPersistentStore) throws Exception { 148 dsoServer.stop(); 149 startDSO(withPersistentStore); 150 } 151 152 public WebApplicationServer makeWebApplicationServer(String tcConfigPath) throws Exception { 153 return makeWebApplicationServer(getTcConfigFile(tcConfigPath)); 154 } 155 156 public AbstractDBServer makeDBServer(String dbType, String dbName, int serverPort) { 157 AbstractDBServer svr = new HSqlDBServer(dbName, serverPort); 159 this.addServerToStop(svr); 160 return svr; 161 } 162 163 public FileSystemPath getTcConfigFile(String tcConfigPath) { 164 URL 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 { 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 { 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 serversToStop) { 188 this.serversToStop = serversToStop; 189 } 190 191 List getServersToStop() { 192 return serversToStop; 193 } 194 195 public DeploymentBuilder makeDeploymentBuilder(String warFileName) { 196 return new WARBuilder(warFileName, tempDir, config); 197 } 198 199 public DeploymentBuilder makeDeploymentBuilder() throws IOException { 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 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 e) { 213 logger.error(stoppable, e); 214 } 215 } 216 } 217 218 public TestConfigObject getTestConfig() { 219 return this.config; 220 } 221 } 222 | Popular Tags |