1 17 package org.apache.geronimo.tomcat.deployment; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.security.PermissionCollection ; 24 import java.security.Permissions ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import org.apache.geronimo.testsupport.TestSupport; 33 34 import org.apache.commons.io.FileUtils; 35 import org.apache.geronimo.common.DeploymentException; 36 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinatorGBean; 37 import org.apache.geronimo.deployment.ModuleIDBuilder; 38 import org.apache.geronimo.deployment.service.GBeanBuilder; 39 import org.apache.geronimo.deployment.util.UnpackedJarFile; 40 import org.apache.geronimo.gbean.AbstractName; 41 import org.apache.geronimo.gbean.AbstractNameQuery; 42 import org.apache.geronimo.gbean.GBeanData; 43 import org.apache.geronimo.gbean.GBeanInfo; 44 import org.apache.geronimo.gbean.GBeanInfoBuilder; 45 import org.apache.geronimo.j2ee.deployment.EARContext; 46 import org.apache.geronimo.j2ee.deployment.Module; 47 import org.apache.geronimo.j2ee.deployment.UnavailableWebServiceBuilder; 48 import org.apache.geronimo.j2ee.deployment.WebServiceBuilder; 49 import org.apache.geronimo.j2ee.deployment.NamingBuilderCollection; 50 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 51 import org.apache.geronimo.j2ee.management.impl.J2EEServerImpl; 52 import org.apache.geronimo.kernel.Jsr77Naming; 53 import org.apache.geronimo.kernel.Kernel; 54 import org.apache.geronimo.kernel.KernelFactory; 55 import org.apache.geronimo.kernel.Naming; 56 import org.apache.geronimo.kernel.config.Configuration; 57 import org.apache.geronimo.kernel.config.ConfigurationData; 58 import org.apache.geronimo.kernel.config.ConfigurationManager; 59 import org.apache.geronimo.kernel.config.ConfigurationModuleType; 60 import org.apache.geronimo.kernel.config.ConfigurationStore; 61 import org.apache.geronimo.kernel.config.ConfigurationUtil; 62 import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager; 63 import org.apache.geronimo.kernel.config.InvalidConfigException; 64 import org.apache.geronimo.kernel.config.NoSuchConfigException; 65 import org.apache.geronimo.kernel.config.NullConfigurationStore; 66 import org.apache.geronimo.kernel.management.State; 67 import org.apache.geronimo.kernel.repository.Artifact; 68 import org.apache.geronimo.kernel.repository.ArtifactManager; 69 import org.apache.geronimo.kernel.repository.ArtifactResolver; 70 import org.apache.geronimo.kernel.repository.DefaultArtifactManager; 71 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver; 72 import org.apache.geronimo.kernel.repository.Environment; 73 import org.apache.geronimo.kernel.repository.ImportType; 74 import org.apache.geronimo.kernel.repository.Repository; 75 import org.apache.geronimo.security.SecurityServiceImpl; 76 import org.apache.geronimo.security.deployment.GeronimoSecurityBuilderImpl; 77 import org.apache.geronimo.security.jacc.ApplicationPolicyConfigurationManager; 78 import org.apache.geronimo.security.jacc.ComponentPermissions; 79 import org.apache.geronimo.system.serverinfo.BasicServerInfo; 80 import org.apache.geronimo.tomcat.ConnectorGBean; 81 import org.apache.geronimo.tomcat.EngineGBean; 82 import org.apache.geronimo.tomcat.HostGBean; 83 import org.apache.geronimo.tomcat.RealmGBean; 84 import org.apache.geronimo.tomcat.TomcatContainer; 85 import org.apache.geronimo.transaction.manager.GeronimoTransactionManagerGBean; 86 87 90 public class TomcatModuleBuilderTest extends TestSupport { 91 private static Naming naming = new Jsr77Naming(); 92 private Artifact baseId = new Artifact("test", "base", "1", "car"); 93 private final AbstractName serverName = naming.createRootName(baseId, "Server", "J2EEServer"); 94 95 protected Kernel kernel; 96 private AbstractName tmName; 97 private AbstractName ctcName; 98 private ClassLoader cl; 99 private TomcatModuleBuilder builder; 100 private Artifact webModuleArtifact = new Artifact("foo", "bar", "1", "car"); 101 private Environment defaultEnvironment = new Environment(); 102 private ConfigurationManager configurationManager; 103 private ConfigurationStore configStore; 104 105 public void testDeployWar4() throws Exception { 106 verifyStartable("war4"); 107 } 108 109 public void testDeployWar5() throws Exception { 110 verifyStartable("war5"); 111 } 112 113 public void testDeployWar6() throws Exception { 114 verifyStartable("war6-jee5"); 115 } 116 117 public void testContextRootWithSpaces() throws Exception { 118 WebModuleInfo info = deployWar("war-spaces-in-context"); 119 String contextRoot = (String ) kernel.getAttribute(info.moduleName, "contextPath"); 120 assertNotNull(contextRoot); 121 assertEquals(contextRoot, contextRoot.trim()); 122 undeployWar(info.configuration); 123 } 124 125 private void verifyStartable(String warName) throws Exception { 126 WebModuleInfo info = deployWar(warName); 127 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(info.moduleName)); 128 Set names = info.configuration.findGBeans(new AbstractNameQuery(info.moduleName.getArtifact(), Collections.EMPTY_MAP)); 129 log.debug("names: " + names); 130 for (Iterator iterator = names.iterator(); iterator.hasNext();) { 131 AbstractName objectName = (AbstractName) iterator.next(); 132 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(objectName)); 133 } 134 undeployWar(info.configuration); 135 } 136 137 private WebModuleInfo deployWar(String warName) throws Exception { 138 File outputPath = new File (BASEDIR, "target/test-resources/deployables/" + warName); 139 recursiveDelete(outputPath); 140 outputPath.mkdirs(); 141 File path = new File (BASEDIR, "src/test/resources/deployables/" + warName); 142 File dest = new File (BASEDIR, "target/test-resources/deployables/" + warName + "/war"); 143 recursiveCopy(path, dest); 144 UnpackedJarFile jarFile = new UnpackedJarFile(path); 145 Module module = builder.createModule(null, jarFile, kernel.getNaming(), new ModuleIDBuilder()); 146 Repository repository = null; 147 148 AbstractName moduleName = module.getModuleName(); 149 EARContext earContext = createEARContext(outputPath, defaultEnvironment, repository, configStore, moduleName); 150 AbstractName jaccBeanName = kernel.getNaming().createChildName(moduleName, "foo", NameFactory.JACC_MANAGER); 151 GBeanData jaccBeanData = new GBeanData(jaccBeanName, ApplicationPolicyConfigurationManager.GBEAN_INFO); 152 PermissionCollection excludedPermissions= new Permissions (); 153 PermissionCollection uncheckedPermissions= new Permissions (); 154 ComponentPermissions componentPermissions = new ComponentPermissions(excludedPermissions, uncheckedPermissions, new HashMap ()); 155 Map contextIDToPermissionsMap = new HashMap (); 156 contextIDToPermissionsMap.put("test_J2EEApplication=null_J2EEServer=bar_j2eeType=WebModule_name=geronimo/test/1.0/war", componentPermissions); 157 jaccBeanData.setAttribute("contextIdToPermissionsMap", contextIDToPermissionsMap); 158 jaccBeanData.setAttribute("roleDesignates", new HashMap ()); 160 earContext.addGBean(jaccBeanData); 161 earContext.setJaccManagerName(jaccBeanName); 162 module.setEarContext(earContext); 163 module.setRootEarContext(earContext); 164 builder.initContext(earContext, module, cl); 165 builder.addGBeans(earContext, module, cl, null); 166 ConfigurationData configurationData = earContext.getConfigurationData(); 167 earContext.close(); 168 module.close(); 169 170 Artifact configurationId = configurationData.getId(); 171 configurationManager.loadConfiguration(configurationData); 172 Configuration configuration = configurationManager.getConfiguration(configurationId); 173 configurationManager.startConfiguration(configurationId); 174 175 return new WebModuleInfo(moduleName, configuration); 176 } 177 178 private void undeployWar(Configuration configuration) throws Exception { 179 configurationManager.stopConfiguration(configuration.getId()); 180 configurationManager.unloadConfiguration(configuration.getId()); 181 } 182 183 private EARContext createEARContext(File outputPath, Environment environment, Repository repository, ConfigurationStore configStore, AbstractName moduleName) throws DeploymentException { 184 Set repositories = repository == null ? Collections.EMPTY_SET : Collections.singleton(repository); 185 ArtifactManager artifactManager = new DefaultArtifactManager(); 186 ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, repositories, null); 187 return new EARContext(outputPath, 188 null, 189 environment, 190 ConfigurationModuleType.WAR, 191 naming, 192 configurationManager, 193 repositories, 194 new AbstractNameQuery(serverName), 195 moduleName, 196 new AbstractNameQuery(tmName), 197 new AbstractNameQuery(ctcName), 198 null, 199 null, 200 null 201 ); 202 } 203 204 private void recursiveDelete(File path) { 205 File [] listing = path.listFiles(); 207 if (listing != null) { 208 for (int i = 0; i < listing.length; i++) { 209 File file = listing[i]; 210 if (file.isDirectory()) { 211 recursiveDelete(file); 212 } 213 file.delete(); 214 } 215 } 216 } 217 218 public void recursiveCopy(File src, File dest) throws IOException { 219 Collection files = FileUtils.listFiles(src,null,true); 220 Iterator iterator = files.iterator(); 221 while(iterator.hasNext()){ 222 File file = (File ) iterator.next(); 223 if (file.getAbsolutePath().indexOf(".svn") < 0){ 224 String pathToFile = file.getPath(); 225 String relativePath = pathToFile.substring(src.getPath().length(), pathToFile.length() - (file.getName().length())); 226 FileUtils.copyFileToDirectory(file,new File (dest.getPath() + relativePath)); 227 } 228 } 229 } 230 231 protected void setUp() throws Exception { 232 super.setUp(); 233 cl = this.getClass().getClassLoader(); 234 kernel = KernelFactory.newInstance().createKernel("test"); 235 kernel.boot(); 236 237 ConfigurationData bootstrap = new ConfigurationData(baseId, naming); 238 239 GBeanData serverInfo = bootstrap.addGBean("ServerInfo", BasicServerInfo.GBEAN_INFO); 240 serverInfo.setAttribute("baseDirectory", "."); 241 242 AbstractName configStoreName = bootstrap.addGBean("MockConfigurationStore", MockConfigStore.GBEAN_INFO).getAbstractName(); 243 244 GBeanData artifactManagerData = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO); 245 246 GBeanData artifactResolverData = bootstrap.addGBean("ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO); 247 artifactResolverData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName()); 248 249 GBeanData configurationManagerData = bootstrap.addGBean("ConfigurationManager", EditableKernelConfigurationManager.GBEAN_INFO); 250 configurationManagerData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName()); 251 configurationManagerData.setReferencePattern("ArtifactResolver", artifactResolverData.getAbstractName()); 252 configurationManagerData.setReferencePattern("Stores", configStoreName); 253 bootstrap.addGBean(configurationManagerData); 254 255 GBeanData serverData = new GBeanData(serverName, J2EEServerImpl.GBEAN_INFO); 256 bootstrap.addGBean(serverData); 257 258 GBeanData securityService = bootstrap.addGBean("SecurityService", SecurityServiceImpl.GBEAN_INFO); 259 securityService.setAttribute("policyConfigurationFactory", "org.apache.geronimo.security.jacc.GeronimoPolicyConfigurationFactory"); 260 securityService.setAttribute("policyProvider", "org.apache.geronimo.security.jacc.GeronimoPolicy"); 261 securityService.setReferencePattern("ServerInfo", serverInfo.getAbstractName()); 262 263 Map initParams = new HashMap (); 265 266 initParams.put("userClassNames", 267 "org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal"); 268 initParams.put("roleClassNames", 269 "org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal"); 270 GBeanData realm = bootstrap.addGBean("tomcatRealm", RealmGBean.GBEAN_INFO); 271 realm.setAttribute("className", 272 "org.apache.geronimo.tomcat.realm.TomcatJAASRealm"); 273 realm.setAttribute("initParams", initParams); 274 275 initParams = new HashMap (); 277 initParams.put("workDir", "work"); 278 initParams.put("name", "localhost"); 279 initParams.put("appBase", ""); 280 GBeanData host = bootstrap.addGBean("tomcatHost", HostGBean.GBEAN_INFO); 281 host.setAttribute("className", "org.apache.catalina.core.StandardHost"); 282 host.setAttribute("initParams", initParams); 283 284 initParams = new HashMap (); 286 initParams.put("name", "Geronimo"); 287 GBeanData engine = bootstrap.addGBean("tomcatEngine", EngineGBean.GBEAN_INFO); 288 engine.setAttribute("className", "org.apache.geronimo.tomcat.TomcatEngine"); 289 engine.setAttribute("initParams", initParams); 290 engine.setReferencePattern("DefaultHost", host.getAbstractName()); 291 engine.setReferencePattern("RealmGBean", realm.getAbstractName()); 292 engine.setReferencePattern("Hosts", host.getAbstractName()); 293 294 WebServiceBuilder webServiceBuilder = new MockWebServiceBuilder(); 295 296 GBeanData containerData = bootstrap.addGBean("TomcatContainer", TomcatContainer.GBEAN_INFO); 297 containerData.setAttribute("catalinaHome", new File (BASEDIR, "target/var/catalina").toString()); 298 containerData.setReferencePattern("EngineGBean", engine.getAbstractName()); 299 containerData.setReferencePattern("ServerInfo", serverInfo.getAbstractName()); 300 AbstractName containerName = containerData.getAbstractName(); 301 302 GBeanData connector = bootstrap.addGBean("TomcatConnector", ConnectorGBean.GBEAN_INFO); 303 connector.setAttribute("name", "HTTP"); 304 connector.setAttribute("port", new Integer (8181)); 305 connector.setReferencePattern("TomcatContainer", containerName); 306 307 GBeanData tm = bootstrap.addGBean("TransactionManager", GeronimoTransactionManagerGBean.GBEAN_INFO); 308 tmName = tm.getAbstractName(); 309 tm.setAttribute("defaultTransactionTimeoutSeconds", new Integer (10)); 310 311 GBeanData ctc = bootstrap.addGBean("ConnectionTrackingCoordinator", ConnectionTrackingCoordinatorGBean.GBEAN_INFO); 312 ctcName = ctc.getAbstractName(); 313 ctc.setReferencePattern("TransactionManager", tmName); 314 315 ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader()); 316 317 configurationManager = ConfigurationUtil.getEditableConfigurationManager(kernel); 318 configStore = (ConfigurationStore) kernel.getGBean(configStoreName); 319 configStore.install(bootstrap); 320 321 defaultEnvironment.addDependency(baseId, ImportType.ALL); 322 defaultEnvironment.setConfigId(webModuleArtifact); 323 builder = new TomcatModuleBuilder(defaultEnvironment, new AbstractNameQuery(containerName), Collections.singleton(webServiceBuilder), Collections.singleton(new GeronimoSecurityBuilderImpl()), Collections.singleton(new GBeanBuilder(null, null)), new NamingBuilderCollection(null, null), new MockResourceEnvironmentSetter(), null); 324 } 325 326 protected void tearDown() throws Exception { 327 kernel.shutdown(); 328 super.tearDown(); 329 } 330 331 public static class MockConfigStore extends NullConfigurationStore { 332 private Map configs = new HashMap (); 333 334 URL baseURL; 335 336 public MockConfigStore() { 337 } 338 339 public MockConfigStore(URL baseURL) { 340 this.baseURL = baseURL; 341 } 342 343 public void install(ConfigurationData configurationData) throws IOException , InvalidConfigException { 344 configs.put(configurationData.getId(), configurationData); 345 } 346 347 public void uninstall(Artifact configID) throws NoSuchConfigException, IOException { 348 configs.remove(configID); 349 } 350 351 public ConfigurationData loadConfiguration(Artifact configId) throws NoSuchConfigException, IOException , InvalidConfigException { 352 if (configs.containsKey(configId)) { 353 ConfigurationData configurationData = (ConfigurationData) configs.get(configId); 354 configurationData.setConfigurationStore(this); 355 return configurationData; 356 } else { 357 ConfigurationData configurationData = new ConfigurationData(configId, naming); 358 configurationData.setConfigurationStore(this); 359 return configurationData; 360 } 361 } 362 363 public boolean containsConfiguration(Artifact configID) { 364 return true; 365 } 366 367 public Set resolve(Artifact configId, String moduleName, String pattern) throws NoSuchConfigException, MalformedURLException { 368 return Collections.singleton(baseURL); 369 } 370 371 public final static GBeanInfo GBEAN_INFO; 372 373 static { 374 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(MockConfigStore.class, NameFactory.CONFIGURATION_STORE); 375 infoBuilder.addInterface(ConfigurationStore.class); 376 GBEAN_INFO = infoBuilder.getBeanInfo(); 377 } 378 } 379 380 private static class WebModuleInfo { 381 AbstractName moduleName; 382 Configuration configuration; 383 384 public WebModuleInfo (AbstractName moduleName, Configuration configuration){ 385 this.moduleName = moduleName; 386 this.configuration = configuration; 387 } 388 } 389 390 } 391 | Popular Tags |