KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > tomcat > deployment > TomcatModuleBuilderTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.tomcat.deployment;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.security.PermissionCollection JavaDoc;
24 import java.security.Permissions JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
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 /**
88  * @version $Rev:385232 $ $Date: 2006-12-06 14:52:09 -0500 (Wed, 06 Dec 2006) $
89  */

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 JavaDoc 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 JavaDoc {
106         verifyStartable("war4");
107     }
108
109     public void testDeployWar5() throws Exception JavaDoc {
110         verifyStartable("war5");
111     }
112
113     public void testDeployWar6() throws Exception JavaDoc {
114         verifyStartable("war6-jee5");
115     }
116     
117     public void testContextRootWithSpaces() throws Exception JavaDoc {
118         WebModuleInfo info = deployWar("war-spaces-in-context");
119         String JavaDoc contextRoot = (String JavaDoc) kernel.getAttribute(info.moduleName, "contextPath");
120         assertNotNull(contextRoot);
121         assertEquals(contextRoot, contextRoot.trim());
122         undeployWar(info.configuration);
123     }
124
125     private void verifyStartable(String JavaDoc warName) throws Exception JavaDoc {
126         WebModuleInfo info = deployWar(warName);
127         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(info.moduleName));
128         Set JavaDoc names = info.configuration.findGBeans(new AbstractNameQuery(info.moduleName.getArtifact(), Collections.EMPTY_MAP));
129         log.debug("names: " + names);
130         for (Iterator JavaDoc 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 JavaDoc warName) throws Exception JavaDoc {
138         File JavaDoc outputPath = new File JavaDoc(BASEDIR, "target/test-resources/deployables/" + warName);
139         recursiveDelete(outputPath);
140         outputPath.mkdirs();
141         File JavaDoc path = new File JavaDoc(BASEDIR, "src/test/resources/deployables/" + warName);
142         File JavaDoc dest = new File JavaDoc(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 JavaDoc excludedPermissions= new Permissions JavaDoc();
153         PermissionCollection JavaDoc uncheckedPermissions= new Permissions JavaDoc();
154         ComponentPermissions componentPermissions = new ComponentPermissions(excludedPermissions, uncheckedPermissions, new HashMap JavaDoc());
155         Map JavaDoc contextIDToPermissionsMap = new HashMap JavaDoc();
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("principalRoleMap", new HashMap());
159
jaccBeanData.setAttribute("roleDesignates", new HashMap JavaDoc());
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 JavaDoc{
179         configurationManager.stopConfiguration(configuration.getId());
180         configurationManager.unloadConfiguration(configuration.getId());
181     }
182
183     private EARContext createEARContext(File JavaDoc outputPath, Environment environment, Repository repository, ConfigurationStore configStore, AbstractName moduleName) throws DeploymentException {
184         Set JavaDoc 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 JavaDoc path) {
205         //does not delete top level dir passed in
206
File JavaDoc[] listing = path.listFiles();
207         if (listing != null) {
208             for (int i = 0; i < listing.length; i++) {
209                 File JavaDoc file = listing[i];
210                 if (file.isDirectory()) {
211                     recursiveDelete(file);
212                 }
213                 file.delete();
214             }
215         }
216     }
217
218     public void recursiveCopy(File JavaDoc src, File JavaDoc dest) throws IOException JavaDoc {
219         Collection JavaDoc files = FileUtils.listFiles(src,null,true);
220         Iterator JavaDoc iterator = files.iterator();
221         while(iterator.hasNext()){
222             File JavaDoc file = (File JavaDoc) iterator.next();
223             if (file.getAbsolutePath().indexOf(".svn") < 0){
224                 String JavaDoc pathToFile = file.getPath();
225                 String JavaDoc relativePath = pathToFile.substring(src.getPath().length(), pathToFile.length() - (file.getName().length()));
226                 FileUtils.copyFileToDirectory(file,new File JavaDoc(dest.getPath() + relativePath));
227             }
228         }
229     }
230
231     protected void setUp() throws Exception JavaDoc {
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         // Default Realm
264
Map JavaDoc initParams = new HashMap JavaDoc();
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         // Default Host
276
initParams = new HashMap JavaDoc();
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         // Default Engine
285
initParams = new HashMap JavaDoc();
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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc {
327         kernel.shutdown();
328         super.tearDown();
329     }
330
331     public static class MockConfigStore extends NullConfigurationStore {
332         private Map JavaDoc configs = new HashMap JavaDoc();
333
334         URL JavaDoc baseURL;
335
336         public MockConfigStore() {
337         }
338
339         public MockConfigStore(URL JavaDoc baseURL) {
340             this.baseURL = baseURL;
341         }
342
343         public void install(ConfigurationData configurationData) throws IOException JavaDoc, InvalidConfigException {
344             configs.put(configurationData.getId(), configurationData);
345         }
346
347         public void uninstall(Artifact configID) throws NoSuchConfigException, IOException JavaDoc {
348             configs.remove(configID);
349         }
350
351         public ConfigurationData loadConfiguration(Artifact configId) throws NoSuchConfigException, IOException JavaDoc, 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 JavaDoc resolve(Artifact configId, String JavaDoc moduleName, String JavaDoc pattern) throws NoSuchConfigException, MalformedURLException JavaDoc {
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