1 4 package com.tctest.spring.integrationtests.framework; 5 6 import org.apache.commons.httpclient.HttpClient; 7 import org.codehaus.cargo.container.deployable.WAR; 8 import org.codehaus.cargo.container.property.RemotePropertySet; 9 import org.codehaus.cargo.container.tomcat.Tomcat5xRemoteContainer; 10 import org.codehaus.cargo.container.tomcat.Tomcat5xRemoteDeployer; 11 import org.codehaus.cargo.container.tomcat.TomcatPropertySet; 12 import org.codehaus.cargo.container.tomcat.TomcatRuntimeConfiguration; 13 import org.codehaus.cargo.util.log.SimpleLogger; 14 import org.springframework.jmx.support.MBeanServerConnectionFactoryBean; 15 import org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor; 16 import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean; 17 import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter; 18 import org.springframework.remoting.rmi.RmiProxyFactoryBean; 19 import org.springframework.remoting.rmi.RmiServiceExporter; 20 import org.xml.sax.SAXException ; 21 22 import com.meterware.httpunit.WebConversation; 23 import com.meterware.httpunit.WebResponse; 24 import com.tc.test.TestConfigObject; 25 import com.tc.test.server.ServerResult; 26 import com.tc.test.server.appserver.AppServer; 27 import com.tc.test.server.appserver.AppServerInstallation; 28 import com.tc.test.server.appserver.NewAppServerFactory; 29 import com.tc.test.server.appserver.StandardAppServerParameters; 30 import com.tc.test.server.tcconfig.SpringTerracottaAppServerConfig; 31 import com.tc.test.server.tcconfig.StandardTerracottaAppServerConfig; 32 import com.tc.test.server.tcconfig.TerracottaServerConfigGenerator; 33 import com.tc.test.server.util.AppServerUtil; 34 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.net.MalformedURLException ; 38 import java.util.HashMap ; 39 import java.util.Map ; 40 41 import javax.management.MBeanServerConnection ; 42 43 import junit.framework.Assert; 44 45 public class GenericServer extends AbstractStoppable implements WebApplicationServer { 46 47 private int jmxRemotePort; 48 49 private int rmiRegistryPort; 50 51 int contextId = 1; 52 53 private NewAppServerFactory factory; 54 55 private AppServer server; 56 57 private StandardAppServerParameters parameters; 58 59 private ServerResult result; 60 61 private final AppServerInstallation installation; 62 63 private final Map proxyBuilderMap = new HashMap (); 64 65 static final boolean MONKEY_MODE = ServerManager.MONKEY_MODE; 66 67 private ProxyBuilder proxyBuilder = null; 68 69 public GenericServer(TestConfigObject config, NewAppServerFactory factory, AppServerInstallation installation, FileSystemPath tcConfigPath, int serverId, File tempDir) throws Exception { 70 this(config, factory, installation, new SpringTerracottaAppServerConfig(tcConfigPath.getFile()), serverId, tempDir); 71 } 72 73 public GenericServer(TestConfigObject config, NewAppServerFactory factory, AppServerInstallation installation, 74 StandardTerracottaAppServerConfig terracottaConfig, int serverId, File tempDir) throws Exception { 75 this.factory = factory; 76 this.installation = installation; 77 this.rmiRegistryPort = AppServerUtil.getPort(); 78 this.jmxRemotePort = AppServerUtil.getPort(); 79 80 parameters = (StandardAppServerParameters) factory.createParameters("server_" + serverId); 81 82 parameters.enableDSO(new TerracottaServerConfigGenerator(tempDir, 83 terracottaConfig), new File (config.normalBootJar())); 84 parameters.appendSysProp("com.sun.management.jmxremote"); 85 parameters.appendSysProp("com.sun.management.jmxremote.authenticate", false); 86 parameters.appendSysProp("com.sun.management.jmxremote.ssl", false); 87 88 parameters.appendSysProp("com.sun.management.jmxremote.port", this.jmxRemotePort); 89 parameters.appendSysProp("rmi.registry.port", this.rmiRegistryPort); 90 91 String [] params = { "tc.classloader.writeToDisk", "tc.objectmanager.dumpHierarchy", "aspectwerkz.deployment.info", 92 "aspectwerkz.details", "aspectwerkz.gen.closures", "aspectwerkz.dump.pattern", "aspectwerkz.dump.closures", 93 "aspectwerkz.dump.factories", "aspectwerkz.aspectmodules" }; 94 for (int i = 0; i < params.length; i++) { 95 if(Boolean.getBoolean(params[i])) { 96 parameters.appendSysProp(params[i], true); 97 } 98 } 99 100 if (!MONKEY_MODE) { 101 int debugPort = AppServerUtil.getPort(); 102 logger.info("Debug port=" + debugPort); 103 parameters.appendJvmArgs(" -Xdebug -Xrunjdwp:transport=dt_socket,address=" + debugPort + ",server=y,suspend=n "); 104 parameters.appendSysProp("aspectwerkz.transform.verbose", true); 106 parameters.appendSysProp("aspectwerkz.transform.details", true); 107 } 108 109 proxyBuilderMap.put(RmiServiceExporter.class, new RMIProxyBuilder()); 110 proxyBuilderMap.put(HttpInvokerServiceExporter.class, new HttpInvokerProxyBuilder()); 111 } 112 113 public StandardAppServerParameters getServerParameters() { 114 return parameters; 115 } 116 117 private class RMIProxyBuilder implements ProxyBuilder { 118 public Object createProxy(Class serviceType, String url, Map initialContext) throws Exception { 119 String rmiURL = "rmi://localhost:" + rmiRegistryPort + "/" + url; 120 logger.debug("Getting proxy for: " + rmiRegistryPort + " on " + result.serverPort()); 121 RmiProxyFactoryBean prfb = new RmiProxyFactoryBean(); 122 prfb.setServiceUrl(rmiURL); 123 prfb.setServiceInterface(serviceType); 124 prfb.afterPropertiesSet(); 125 return prfb.getObject(); 126 } 127 } 128 129 public class HttpInvokerProxyBuilder implements ProxyBuilder { 130 private HttpClient client; 131 public Object createProxy(Class serviceType, String url, Map initialContext) throws Exception { 132 String serviceURL = "http://localhost:" + result.serverPort() + "/" + url; 133 logger.debug("Getting proxy for: " + serviceURL); 134 HttpInvokerProxyFactoryBean prfb = new HttpInvokerProxyFactoryBean(); 135 prfb.setServiceUrl(serviceURL); 136 prfb.setServiceInterface(serviceType); 137 CommonsHttpInvokerRequestExecutor executor; 138 if (initialContext != null) { 139 client = (HttpClient)initialContext.get(ProxyBuilder.HTTP_CLIENT_KEY); 140 } 141 142 if (client == null) { 143 executor = new CommonsHttpInvokerRequestExecutor(); 144 client = executor.getHttpClient(); 145 if (initialContext != null) { 146 initialContext.put(ProxyBuilder.HTTP_CLIENT_KEY, client); 147 } 148 } else { 149 executor = new CommonsHttpInvokerRequestExecutor(client); 150 } 151 152 prfb.setHttpInvokerRequestExecutor(executor); 153 prfb.afterPropertiesSet(); 154 return prfb.getObject(); 155 } 156 157 public HttpClient getClient() { 158 return client; 159 } 160 161 public void setClient(HttpClient client) { 162 this.client = client; 163 } 164 } 165 166 public Object getProxy(Class serviceType, String url) throws Exception { 167 if (this.proxyBuilder != null) { 168 return proxyBuilder.createProxy(serviceType, url, null); 169 } 170 Map initCtx = new HashMap (); 171 initCtx.put(ProxyBuilder.EXPORTER_TYPE_KEY, RmiServiceExporter.class); 172 return getProxy(serviceType, url, initCtx); 173 } 174 175 public Object getProxy(Class serviceType, String url, Map initialContext) throws Exception { 176 Class exporterClass = (Class )initialContext.get(ProxyBuilder.EXPORTER_TYPE_KEY); 177 this.proxyBuilder = (ProxyBuilder) proxyBuilderMap.get(exporterClass); 178 return this.proxyBuilder.createProxy(serviceType, url, initialContext); 179 } 180 181 public MBeanServerConnection getMBeanServerConnection() throws Exception { 182 MBeanServerConnectionFactoryBean factoryBean = new MBeanServerConnectionFactoryBean(); 183 factoryBean.setServiceUrl("service:jmx:rmi:///jndi/rmi://localhost:" + this.jmxRemotePort + "/jmxrmi"); 184 factoryBean.afterPropertiesSet(); 185 return (MBeanServerConnection ) factoryBean.getObject(); 186 } 187 188 public WebApplicationServer addWarDeployment(Deployment warDeployment, String context) { 189 parameters.addWar(context, warDeployment.getFileSystemPath().getFile()); 190 return this; 191 } 192 193 protected void doStart() throws Exception { 194 server = factory.createAppServer(installation); 195 result = server.start(parameters); 196 } 197 198 protected void doStop() throws Exception { 199 server.stop(); 200 } 201 202 public WebResponse ping(String url) throws MalformedURLException , IOException , SAXException { 203 return ping(url, new WebConversation()); 204 } 205 206 public WebResponse ping(String url, WebConversation wc) throws MalformedURLException , IOException , SAXException { 207 String fullURL = "http://localhost:" + result.serverPort() + url; 208 logger.debug("Getting page: " + fullURL); 209 210 WebResponse page = wc.getResponse(fullURL); 211 Assert.assertEquals(200, page.getResponseCode()); 212 logger.debug("Got page: " + fullURL); 213 return page; 214 } 215 216 public void redeployWar(Deployment warDeployment, String context) { 217 getRemoteDeployer().redeploy(makeWar(context, warDeployment.getFileSystemPath())); 218 } 219 220 public void deployWar(Deployment warDeployment, String context) { 221 getRemoteDeployer().deploy(makeWar(context, warDeployment.getFileSystemPath())); 222 } 223 224 public void undeployWar(Deployment warDeployment, String context) { 225 getRemoteDeployer().undeploy(makeWar(context, warDeployment.getFileSystemPath())); 226 } 227 228 230 private WAR makeWar(String warContext, FileSystemPath warPath) { 231 WAR war = new WAR(warPath.toString()); 232 war.setContext(warContext); 233 war.setLogger(new SimpleLogger()); 234 return war; 235 } 236 237 239 private Tomcat5xRemoteDeployer getRemoteDeployer() { 240 TomcatRuntimeConfiguration runtimeConfiguration = new TomcatRuntimeConfiguration(); 241 runtimeConfiguration.setProperty(RemotePropertySet.USERNAME, "admin"); 242 runtimeConfiguration.setProperty(RemotePropertySet.PASSWORD, ""); 243 runtimeConfiguration.setProperty(TomcatPropertySet.MANAGER_URL, "http://localhost:" + result.serverPort() 244 + "/manager"); 245 246 Tomcat5xRemoteContainer remoteContainer = new Tomcat5xRemoteContainer(runtimeConfiguration); 247 Tomcat5xRemoteDeployer deployer = new Tomcat5xRemoteDeployer(remoteContainer); 248 return deployer; 249 } 250 251 253 public Server restart() throws Exception { 254 stop(); 255 start(); 256 return this; 257 } 258 259 public String toString() { 260 return "Generic Server" + (result!=null ? "; port:"+result.serverPort() : ""); 261 } 262 } 263 | Popular Tags |