1 17 package org.apache.geronimo.jetty.deployment; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.ObjectOutputStream ; 23 import java.net.URI ; 24 import java.net.URL ; 25 import java.util.Collections ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import javax.management.MalformedObjectNameException ; 32 import javax.management.ObjectName ; 33 import javax.naming.Reference ; 34 import javax.xml.namespace.QName ; 35 36 import junit.framework.TestCase; 37 import org.apache.geronimo.common.DeploymentException; 38 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator; 39 import org.apache.geronimo.deployment.DeploymentContext; 40 import org.apache.geronimo.deployment.util.UnpackedJarFile; 41 import org.apache.geronimo.gbean.GBeanData; 42 import org.apache.geronimo.gbean.GBeanInfo; 43 import org.apache.geronimo.gbean.GBeanInfoBuilder; 44 import org.apache.geronimo.j2ee.deployment.EARContext; 45 import org.apache.geronimo.j2ee.deployment.EJBReferenceBuilder; 46 import org.apache.geronimo.j2ee.deployment.Module; 47 import org.apache.geronimo.j2ee.deployment.RefContext; 48 import org.apache.geronimo.j2ee.deployment.ResourceReferenceBuilder; 49 import org.apache.geronimo.j2ee.deployment.ServiceReferenceBuilder; 50 import org.apache.geronimo.j2ee.deployment.WebServiceBuilder; 51 import org.apache.geronimo.j2ee.deployment.UnavailableWebServiceBuilder; 52 import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContext; 53 import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContextImpl; 54 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 55 import org.apache.geronimo.j2ee.management.impl.J2EEServerImpl; 56 import org.apache.geronimo.jetty.JettyContainerImpl; 57 import org.apache.geronimo.jetty.connector.HTTPConnector; 58 import org.apache.geronimo.kernel.KernelFactory; 59 import org.apache.geronimo.kernel.Kernel; 60 import org.apache.geronimo.kernel.config.ConfigurationManagerImpl; 61 import org.apache.geronimo.kernel.config.Configuration; 62 import org.apache.geronimo.kernel.config.ConfigurationModuleType; 63 import org.apache.geronimo.kernel.config.ConfigurationStore; 64 import org.apache.geronimo.kernel.config.InvalidConfigException; 65 import org.apache.geronimo.kernel.config.NoSuchConfigException; 66 import org.apache.geronimo.kernel.config.ConfigurationData; 67 import org.apache.geronimo.kernel.config.ConfigurationManager; 68 import org.apache.geronimo.kernel.jmx.JMXUtil; 69 import org.apache.geronimo.kernel.management.State; 70 import org.apache.geronimo.security.SecurityServiceImpl; 71 import org.apache.geronimo.system.serverinfo.ServerInfo; 72 import org.apache.geronimo.transaction.context.TransactionContextManager; 73 import org.apache.geronimo.transaction.manager.TransactionManagerImpl; 74 75 78 public class JettyModuleBuilderTest extends TestCase { 79 80 protected Kernel kernel; 81 private GBeanData container; 82 private ObjectName containerName; 83 private ObjectName connectorName; 84 private GBeanData connector; 85 private GBeanData securityServiceGBean; 86 private ObjectName securityServiceName; 87 private ObjectName serverInfoName; 88 private GBeanData serverInfoGBean; 89 private ObjectName tmName; 90 private ObjectName ctcName; 91 private GBeanData tm; 92 private GBeanData ctc; 93 private ObjectName tcmName; 94 private GBeanData tcm; 95 private ClassLoader cl; 96 private J2eeContext moduleContext = new J2eeContextImpl("jetty.test", "test", "null", NameFactory.WEB_MODULE, "jettyTest", null, null); 97 private JettyModuleBuilder builder; 98 private File basedir = new File (System.getProperty("basedir", ".")); 99 private URI parentId = URI.create("org/apache/geronimo/Foo"); 100 101 public void testDeployWar4() throws Exception { 102 File outputPath = new File (basedir, "target/test-resources/deployables/war4"); 103 recursiveDelete(outputPath); 104 outputPath.mkdirs(); 105 File path = new File (basedir, "src/test-resources/deployables/war4"); 106 UnpackedJarFile jarFile = new UnpackedJarFile(path); 107 Module module = builder.createModule(null, jarFile); 108 URI id = new URI ("war4"); 109 EARContext earContext = createEARContext(outputPath, id); 110 ObjectName serverName = earContext.getServerObjectName(); 111 GBeanData server = new GBeanData(serverName, J2EEServerImpl.GBEAN_INFO); 112 start(server); 113 builder.initContext(earContext, module, cl); 114 builder.addGBeans(earContext, module, cl); 115 earContext.close(); 116 module.close(); 117 GBeanData configData = earContext.getConfigurationGBeanData(); 118 configData.setAttribute("baseURL", path.toURL()); 119 kernel.loadGBean(configData, cl); 120 121 kernel.startRecursiveGBean(configData.getName()); 122 if (kernel.getGBeanState(configData.getName()) != State.RUNNING_INDEX) { 123 fail("gbean not started: " + configData.getName()); 124 } 125 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(ObjectName.getInstance("test:J2EEApplication=null,J2EEServer=bar,j2eeType=WebModule,name=war4"))); 126 Set names = kernel.listGBeans(ObjectName.getInstance("test:J2EEApplication=null,J2EEServer=bar,WebModule=war4,*")); 127 System.out.println("Object names: " + names); 128 for (Iterator iterator = names.iterator(); iterator.hasNext();) { 129 ObjectName objectName = (ObjectName ) iterator.next(); 130 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(objectName)); 131 } 132 GBeanData filterMapping2Data = kernel.getGBeanData(ObjectName.getInstance("test:J2EEApplication=null,J2EEServer=bar,Servlet=Servlet1,WebFilter=Filter2,WebModule=war4,j2eeType=WebFilterMapping")); 133 135 kernel.stopGBean(configData.getName()); 136 kernel.unloadGBean(configData.getName()); 137 138 kernel.loadGBean(configData, cl); 139 kernel.startRecursiveGBean(configData.getName()); 140 kernel.stopGBean(configData.getName()); 141 kernel.unloadGBean(configData.getName()); 142 } 143 144 private EARContext createEARContext(File outputPath, URI id) throws MalformedObjectNameException , DeploymentException { 145 EARContext earContext = new EARContext(outputPath, 146 id, 147 ConfigurationModuleType.WAR, 148 parentId, 149 kernel, 150 moduleContext.getJ2eeApplicationName(), 151 tcmName, 152 ctcName, 153 null, 154 null, 155 null, new RefContext(new EJBReferenceBuilder() { 156 157 public Reference createEJBLocalReference(String objectName, boolean isSession, String localHome, String local) throws DeploymentException { 158 return null; 159 } 160 161 public Reference createEJBRemoteReference(String objectName, boolean isSession, String home, String remote) throws DeploymentException { 162 return null; 163 } 164 165 public Reference createCORBAReference(URI corbaURL, String objectName, ObjectName containerName, String home) throws DeploymentException { 166 return null; 167 } 168 169 public Object createHandleDelegateReference() { 170 return null; 171 } 172 }, 173 new ResourceReferenceBuilder() { 174 175 public Reference createResourceRef(String containerId, Class iface) throws DeploymentException { 176 return null; 177 } 178 179 public Reference createAdminObjectRef(String containerId, Class iface) throws DeploymentException { 180 return null; 181 } 182 183 public ObjectName locateResourceName(ObjectName query) throws DeploymentException { 184 return null; 185 } 186 187 public GBeanData locateActivationSpecInfo(ObjectName resourceAdapterName, String messageListenerInterface) throws DeploymentException { 188 return null; 189 } 190 191 public GBeanData locateResourceAdapterGBeanData(ObjectName resourceAdapterModuleName) throws DeploymentException { 192 return null; 193 } 194 195 public GBeanData locateAdminObjectInfo(ObjectName resourceAdapterModuleName, String adminObjectInterfaceName) throws DeploymentException { 196 return null; 197 } 198 199 public GBeanData locateConnectionFactoryInfo(ObjectName resourceAdapterModuleName, String connectionFactoryInterfaceName) throws DeploymentException { 200 return null; 201 } 202 }, 203 new ServiceReferenceBuilder() { 204 public Object createService(Class serviceInterface, URI wsdlURI, URI jaxrpcMappingURI, QName serviceQName, Map portComponentRefMap, List handlerInfos, Map portLocationMap, Map credentialsNameMap, DeploymentContext deploymentContext, Module module, ClassLoader classLoader) throws DeploymentException { 206 return null; 207 } 208 }, kernel)); 209 return earContext; 210 } 211 212 private void recursiveDelete(File path) { 213 File [] listing = path.listFiles(); 215 for (int i = 0; i < ((listing == null) ? 0 : listing.length); i++) { 216 File file = listing[i]; 217 if (file.isDirectory()) { 218 recursiveDelete(file); 219 } 220 file.delete(); 221 } 222 } 223 224 protected void setUp() throws Exception { 225 cl = this.getClass().getClassLoader(); 226 containerName = NameFactory.getWebComponentName(null, null, null, null, "jettyContainer", "WebResource", moduleContext); 227 connectorName = NameFactory.getWebComponentName(null, null, null, null, "jettyConnector", "WebResource", moduleContext); 228 230 tmName = NameFactory.getComponentName(null, null, null, null, null, "TransactionManager", NameFactory.JTA_RESOURCE, moduleContext); 231 tcmName = NameFactory.getComponentName(null, null, null, null, null, "TransactionContextManager", NameFactory.JTA_RESOURCE, moduleContext); 232 ctcName = new ObjectName ("geronimo.test:role=ConnectionTrackingCoordinator"); 233 234 kernel = KernelFactory.newInstance().createKernel("foo"); 235 kernel.boot(); 236 237 GBeanData store = new GBeanData(JMXUtil.getObjectName("foo:j2eeType=ConfigurationStore,name=mock"), MockConfigStore.GBEAN_INFO); 238 kernel.loadGBean(store, this.getClass().getClassLoader()); 239 kernel.startGBean(store.getName()); 240 241 ObjectName configurationManagerName = new ObjectName (":j2eeType=ConfigurationManager,name=Basic"); 242 GBeanData configurationManagerData = new GBeanData(configurationManagerName, ConfigurationManagerImpl.GBEAN_INFO); 243 configurationManagerData.setReferencePatterns("Stores", Collections.singleton(store.getName())); 244 kernel.loadGBean(configurationManagerData, getClass().getClassLoader()); 245 kernel.startGBean(configurationManagerName); 246 ConfigurationManager configurationManager = (ConfigurationManager) kernel.getProxyManager().createProxy(configurationManagerName, ConfigurationManager.class); 247 248 ObjectName baseConfigName = configurationManager.load(parentId); 249 kernel.startGBean(baseConfigName); 250 251 ObjectName defaultServlets = ObjectName.getInstance("test:name=test,type=none,*"); 252 ObjectName pojoWebServiceTemplate = null; 253 WebServiceBuilder webServiceBuilder = new UnavailableWebServiceBuilder(); 254 255 serverInfoName = new ObjectName ("geronimo.system:name=ServerInfo"); 256 serverInfoGBean = new GBeanData(serverInfoName, ServerInfo.GBEAN_INFO); 257 serverInfoGBean.setAttribute("baseDirectory", "."); 258 start(serverInfoGBean); 259 260 securityServiceName = new ObjectName ("foo:j2eeType=SecurityService"); 262 securityServiceGBean = new GBeanData(securityServiceName, SecurityServiceImpl.GBEAN_INFO); 263 securityServiceGBean.setReferencePattern("ServerInfo", serverInfoName); 264 securityServiceGBean.setAttribute("policyConfigurationFactory", "org.apache.geronimo.security.jacc.GeronimoPolicyConfigurationFactory"); 265 securityServiceGBean.setAttribute("policyProvider", "org.apache.geronimo.security.jacc.GeronimoPolicy"); 266 start(securityServiceGBean); 267 268 269 builder = new JettyModuleBuilder(new URI ("null"), new Integer (1800), Collections.EMPTY_LIST, containerName, defaultServlets, null, null, pojoWebServiceTemplate, webServiceBuilder, null, kernel); 270 271 container = new GBeanData(containerName, JettyContainerImpl.GBEAN_INFO); 272 273 connector = new GBeanData(connectorName, HTTPConnector.GBEAN_INFO); 274 connector.setAttribute("port", new Integer (5678)); 275 connector.setReferencePattern("JettyContainer", containerName); 276 277 start(container); 278 start(connector); 279 280 tm = new GBeanData(tmName, TransactionManagerImpl.GBEAN_INFO); 281 Set patterns = new HashSet (); 282 patterns.add(ObjectName.getInstance("geronimo.server:j2eeType=JCAManagedConnectionFactory,*")); 283 tm.setAttribute("defaultTransactionTimeoutSeconds", new Integer (10)); 284 tm.setReferencePatterns("ResourceManagers", patterns); 285 start(tm); 286 tcm = new GBeanData(tcmName, TransactionContextManager.GBEAN_INFO); 287 tcm.setReferencePattern("TransactionManager", tmName); 288 start(tcm); 289 ctc = new GBeanData(ctcName, ConnectionTrackingCoordinator.GBEAN_INFO); 290 start(ctc); 291 292 293 } 294 295 protected void tearDown() throws Exception { 296 stop(ctcName); 297 stop(tmName); 298 stop(serverInfoName); 299 stop(securityServiceName); 300 stop(connectorName); 301 stop(containerName); 302 kernel.shutdown(); 303 } 304 305 private void start(GBeanData gbeanData) throws Exception { 306 kernel.loadGBean(gbeanData, cl); 307 kernel.startGBean(gbeanData.getName()); 308 if (kernel.getGBeanState(gbeanData.getName()) != State.RUNNING_INDEX) { 309 fail("gbean not started: " + gbeanData.getName()); 310 } 311 } 312 313 private void stop(ObjectName name) throws Exception { 314 kernel.stopGBean(name); 315 kernel.unloadGBean(name); 316 } 317 318 public static class MockConfigStore implements ConfigurationStore { 319 private final Kernel kernel; 320 321 public MockConfigStore(Kernel kernel) { 322 this.kernel = kernel; 323 } 324 325 public URI install(URL source) throws IOException , InvalidConfigException { 326 return null; 327 } 328 329 public void install(ConfigurationData configurationData, File source) throws IOException , InvalidConfigException { 330 } 331 332 public void uninstall(URI configID) throws NoSuchConfigException, IOException { 333 } 334 335 public ObjectName loadConfiguration(URI configId) throws NoSuchConfigException, IOException , InvalidConfigException { 336 ObjectName configurationObjectName = null; 337 try { 338 configurationObjectName = Configuration.getConfigurationObjectName(configId); 339 } catch (MalformedObjectNameException e) { 340 throw new InvalidConfigException(e); 341 } 342 GBeanData configData = new GBeanData(configurationObjectName, Configuration.GBEAN_INFO); 343 configData.setAttribute("id", configId); 344 configData.setAttribute("domain", "test"); 345 configData.setAttribute("server", "bar"); 346 configData.setAttribute("gBeanState", NO_OBJECTS_OS); 347 348 try { 349 kernel.loadGBean(configData, Configuration.class.getClassLoader()); 350 } catch (Exception e) { 351 throw new InvalidConfigException("Unable to register configuration", e); 352 } 353 354 return configurationObjectName; 355 } 356 357 public boolean containsConfiguration(URI configID) { 358 return true; 359 } 360 361 public void updateConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, Exception { 362 363 } 364 365 public String getObjectName() { 366 return null; 367 } 368 369 public List listConfiguations() { 370 return null; 371 } 372 373 public File createNewConfigurationDir() { 374 return null; 375 } 376 377 public final static GBeanInfo GBEAN_INFO; 378 379 private static final byte[] NO_OBJECTS_OS; 380 381 static { 382 GBeanInfoBuilder infoBuilder = new GBeanInfoBuilder(MockConfigStore.class, NameFactory.CONFIGURATION_STORE); 383 infoBuilder.addInterface(ConfigurationStore.class); 384 infoBuilder.addAttribute("kernel", Kernel.class, false); 385 infoBuilder.setConstructor(new String [] {"kernel"}); 386 GBEAN_INFO = infoBuilder.getBeanInfo(); 387 388 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 389 try { 390 ObjectOutputStream oos = new ObjectOutputStream (baos); 391 oos.flush(); 392 NO_OBJECTS_OS = baos.toByteArray(); 393 } catch (IOException e) { 394 throw new RuntimeException (e); 395 } 396 } 397 } 398 } 399 | Popular Tags |