KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > deployment > JettyModuleBuilderTest


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.jetty6.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.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import org.apache.geronimo.testsupport.TestSupport;
32
33 import org.apache.geronimo.common.DeploymentException;
34 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinatorGBean;
35 import org.apache.geronimo.deployment.ModuleIDBuilder;
36 import org.apache.geronimo.deployment.service.GBeanBuilder;
37 import org.apache.geronimo.deployment.util.DeploymentUtil;
38 import org.apache.geronimo.deployment.util.UnpackedJarFile;
39 import org.apache.geronimo.gbean.AbstractName;
40 import org.apache.geronimo.gbean.AbstractNameQuery;
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.Module;
46 import org.apache.geronimo.j2ee.deployment.UnavailableWebServiceBuilder;
47 import org.apache.geronimo.j2ee.deployment.WebServiceBuilder;
48 import org.apache.geronimo.j2ee.deployment.NamingBuilderCollection;
49 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
50 import org.apache.geronimo.j2ee.management.impl.J2EEServerImpl;
51 import org.apache.geronimo.jetty6.JettyContainerImpl;
52 import org.apache.geronimo.jetty6.connector.HTTPConnector;
53 import org.apache.geronimo.kernel.Jsr77Naming;
54 import org.apache.geronimo.kernel.Kernel;
55 import org.apache.geronimo.kernel.KernelFactory;
56 import org.apache.geronimo.kernel.Naming;
57 import org.apache.geronimo.kernel.config.Configuration;
58 import org.apache.geronimo.kernel.config.ConfigurationData;
59 import org.apache.geronimo.kernel.config.ConfigurationManager;
60 import org.apache.geronimo.kernel.config.ConfigurationModuleType;
61 import org.apache.geronimo.kernel.config.ConfigurationStore;
62 import org.apache.geronimo.kernel.config.ConfigurationUtil;
63 import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager;
64 import org.apache.geronimo.kernel.config.InvalidConfigException;
65 import org.apache.geronimo.kernel.config.NoSuchConfigException;
66 import org.apache.geronimo.kernel.config.NullConfigurationStore;
67 import org.apache.geronimo.kernel.management.State;
68 import org.apache.geronimo.kernel.repository.Artifact;
69 import org.apache.geronimo.kernel.repository.ArtifactManager;
70 import org.apache.geronimo.kernel.repository.ArtifactResolver;
71 import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
72 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
73 import org.apache.geronimo.kernel.repository.Environment;
74 import org.apache.geronimo.kernel.repository.ImportType;
75 import org.apache.geronimo.kernel.repository.Repository;
76 import org.apache.geronimo.security.deployment.GeronimoSecurityBuilderImpl;
77 import org.apache.geronimo.system.serverinfo.BasicServerInfo;
78 import org.apache.geronimo.transaction.manager.GeronimoTransactionManagerGBean;
79 import org.apache.geronimo.xbeans.geronimo.j2ee.GerSecurityDocument;
80 import org.apache.xmlbeans.impl.schema.SchemaTypeImpl;
81
82 /**
83  * @version $Rev:385232 $ $Date: 2006-12-06 16:50:05 -0500 (Wed, 06 Dec 2006) $
84  */

85 public class JettyModuleBuilderTest extends TestSupport {
86
87     private static Naming naming = new Jsr77Naming();
88     private Artifact baseId = new Artifact("test", "base", "1", "car");
89     private final AbstractName serverName = naming.createRootName(baseId, "Server", "J2EEServer");
90
91     protected Kernel kernel;
92     private AbstractName tmName;
93     private AbstractName ctcName;
94     private ClassLoader JavaDoc cl;
95     private JettyModuleBuilder builder;
96     private Artifact webModuleArtifact = new Artifact("foo", "bar", "1", "car");
97     private Environment defaultEnvironment = new Environment();
98     private ConfigurationManager configurationManager;
99     private ConfigurationStore configStore;
100
101     public void testDeployWar4() throws Exception JavaDoc {
102         File JavaDoc outputPath = new File JavaDoc(BASEDIR, "target/test-resources/deployables/war4");
103         recursiveDelete(outputPath);
104         outputPath.mkdirs();
105         new File JavaDoc(outputPath, "war").mkdir();
106         File JavaDoc path = new File JavaDoc(BASEDIR, "src/test/resources/deployables/war4");
107         UnpackedJarFile jarFile = new UnpackedJarFile(path);
108         Module module = builder.createModule(null, jarFile, kernel.getNaming(), new ModuleIDBuilder());
109         Repository repository = null;
110
111         AbstractName moduleName = module.getModuleName();
112         EARContext earContext = createEARContext(outputPath, defaultEnvironment, repository, configStore, moduleName);
113         module.setEarContext(earContext);
114         module.setRootEarContext(earContext);
115         builder.initContext(earContext, module, cl);
116         builder.addGBeans(earContext, module, cl, Collections.EMPTY_SET);
117         ConfigurationData configurationData = earContext.getConfigurationData();
118         earContext.close();
119         module.close();
120
121         Artifact configurationId = configurationData.getId();
122         configurationManager.loadConfiguration(configurationData);
123         Configuration configuration = configurationManager.getConfiguration(configurationId);
124         configurationManager.startConfiguration(configurationId);
125
126         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(moduleName));
127         Set JavaDoc names = configuration.findGBeans(new AbstractNameQuery(moduleName.getArtifact(), Collections.EMPTY_MAP));
128         log.debug("names: " + names);
129         for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();) {
130             AbstractName objectName = (AbstractName) iterator.next();
131             assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(objectName));
132         }
133
134         configurationManager.stopConfiguration(configurationId);
135         configurationManager.unloadConfiguration(configurationId);
136
137     }
138
139     public void testContextRootWithSpaces() throws Exception JavaDoc {
140         File JavaDoc outputPath = new File JavaDoc(BASEDIR, "target/test-resources/deployables/war-spaces-in-context");
141         recursiveDelete(outputPath);
142         outputPath.mkdirs();
143         new File JavaDoc(outputPath, "war").mkdir();
144         File JavaDoc path = new File JavaDoc(BASEDIR, "src/test/resources/deployables/war-spaces-in-context");
145         UnpackedJarFile jarFile = new UnpackedJarFile(path);
146         Module module = builder.createModule(null, jarFile, kernel.getNaming(), new ModuleIDBuilder());
147         Repository repository = null;
148
149         AbstractName moduleName = module.getModuleName();
150         EARContext earContext = createEARContext(outputPath, defaultEnvironment, repository, configStore, moduleName);
151         module.setEarContext(earContext);
152         module.setRootEarContext(earContext);
153         builder.initContext(earContext, module, cl);
154         builder.addGBeans(earContext, module, cl, Collections.EMPTY_SET);
155         ConfigurationData configurationData = earContext.getConfigurationData();
156         earContext.close();
157         module.close();
158
159         Artifact configurationId = configurationData.getId();
160         configurationManager.loadConfiguration(configurationData);
161         Configuration configuration = configurationManager.getConfiguration(configurationId);
162         configurationManager.startConfiguration(configurationId);
163
164         String JavaDoc contextRoot = (String JavaDoc) kernel.getAttribute(moduleName, "contextPath");
165         assertNotNull(contextRoot);
166         assertEquals(contextRoot, contextRoot.trim());
167
168         configurationManager.stopConfiguration(configurationId);
169         configurationManager.unloadConfiguration(configurationId);
170
171     }
172
173     private EARContext createEARContext(File JavaDoc outputPath, Environment environment, Repository repository, ConfigurationStore configStore, AbstractName moduleName) throws DeploymentException {
174         Set JavaDoc repositories = repository == null ? Collections.EMPTY_SET : Collections.singleton(repository);
175         ArtifactManager artifactManager = new DefaultArtifactManager();
176         ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, repositories, null);
177         return new EARContext(outputPath,
178                 null,
179                 environment,
180                 ConfigurationModuleType.WAR,
181                 naming,
182                 configurationManager,
183                 repositories,
184                 new AbstractNameQuery(serverName),
185                 moduleName,
186                 new AbstractNameQuery(tmName),
187                 new AbstractNameQuery(ctcName),
188                 null,
189                 null,
190                 null
191         );
192     }
193
194     private void recursiveDelete(File JavaDoc path) {
195         //does not delete top level dir passed in
196
File JavaDoc[] listing = path.listFiles();
197         if (listing != null) {
198             for (int i = 0; i < listing.length; i++) {
199                 File JavaDoc file = listing[i];
200                 if (file.isDirectory()) {
201                     recursiveDelete(file);
202                 }
203                 file.delete();
204             }
205         }
206     }
207
208     protected void setUp() throws Exception JavaDoc {
209         super.setUp();
210
211         ((SchemaTypeImpl) GerSecurityDocument.type).addSubstitutionGroupMember(org.apache.geronimo.xbeans.geronimo.security.GerSecurityDocument.type.getDocumentElementName());
212
213         cl = this.getClass().getClassLoader();
214         kernel = KernelFactory.newInstance().createKernel("test");
215         kernel.boot();
216
217         ConfigurationData bootstrap = new ConfigurationData(baseId, naming);
218
219         bootstrap.addGBean("ServerInfo", BasicServerInfo.GBEAN_INFO).setAttribute("baseDirectory", ".");
220
221         AbstractName configStoreName = bootstrap.addGBean("MockConfigurationStore", MockConfigStore.GBEAN_INFO).getAbstractName();
222
223         GBeanData artifactManagerData = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
224
225         GBeanData artifactResolverData = bootstrap.addGBean("ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
226         artifactResolverData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
227
228         GBeanData configurationManagerData = bootstrap.addGBean("ConfigurationManager", EditableKernelConfigurationManager.GBEAN_INFO);
229         configurationManagerData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
230         configurationManagerData.setReferencePattern("ArtifactResolver", artifactResolverData.getAbstractName());
231         configurationManagerData.setReferencePattern("Stores", configStoreName);
232         bootstrap.addGBean(configurationManagerData);
233
234         GBeanData serverData = new GBeanData(serverName, J2EEServerImpl.GBEAN_INFO);
235         bootstrap.addGBean(serverData);
236
237         Collection JavaDoc defaultServlets = new HashSet JavaDoc();
238         Collection JavaDoc defaultFilters = new HashSet JavaDoc();
239         Collection JavaDoc defaultFilterMappings = new HashSet JavaDoc();
240         Object JavaDoc pojoWebServiceTemplate = null;
241         WebServiceBuilder webServiceBuilder = new UnavailableWebServiceBuilder();
242
243         GBeanData containerData = bootstrap.addGBean("JettyContainer", JettyContainerImpl.GBEAN_INFO);
244         AbstractName containerName = containerData.getAbstractName();
245
246         GBeanData connector = bootstrap.addGBean("JettyConnector", HTTPConnector.GBEAN_INFO);
247         connector.setAttribute("port", new Integer JavaDoc(5678));
248         connector.setAttribute("maxThreads", new Integer JavaDoc(50));
249 // connector.setAttribute("minThreads", new Integer(10));
250
connector.setReferencePattern("JettyContainer", containerName);
251
252         GBeanData tm = bootstrap.addGBean("TransactionManager", GeronimoTransactionManagerGBean.GBEAN_INFO);
253         tmName = tm.getAbstractName();
254         tm.setAttribute("defaultTransactionTimeoutSeconds", new Integer JavaDoc(10));
255
256         GBeanData ctc = bootstrap.addGBean("ConnectionTrackingCoordinator", ConnectionTrackingCoordinatorGBean.GBEAN_INFO);
257         ctcName = ctc.getAbstractName();
258         ctc.setReferencePattern("TransactionManager", tmName);
259
260         ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader());
261
262         configurationManager = ConfigurationUtil.getEditableConfigurationManager(kernel);
263         configStore = (ConfigurationStore) kernel.getGBean(configStoreName);
264         configStore.install(bootstrap);
265
266         defaultEnvironment.addDependency(baseId, ImportType.ALL);
267         defaultEnvironment.setConfigId(webModuleArtifact);
268         builder = new JettyModuleBuilder(defaultEnvironment,
269                 new Integer JavaDoc(1800),
270                 Collections.EMPTY_LIST,
271                 new AbstractNameQuery(containerName),
272                 null, defaultServlets,
273                 defaultFilters,
274                 defaultFilterMappings,
275                 pojoWebServiceTemplate,
276                 Collections.singleton(webServiceBuilder),
277                 null,
278                 Collections.singleton(new GeronimoSecurityBuilderImpl()),
279                 Collections.singleton(new GBeanBuilder(null, null)),
280                 new NamingBuilderCollection(null, null),
281                 new MockResourceEnvironmentSetter(),
282                 kernel);
283     }
284
285     protected void tearDown() throws Exception JavaDoc {
286         kernel.shutdown();
287         super.tearDown();
288     }
289
290
291     public static class MockConfigStore extends NullConfigurationStore {
292         private Map JavaDoc configs = new HashMap JavaDoc();
293
294         URL JavaDoc baseURL;
295
296         public MockConfigStore() {
297         }
298
299         public MockConfigStore(URL JavaDoc baseURL) {
300             this.baseURL = baseURL;
301         }
302
303         public void install(ConfigurationData configurationData) throws IOException JavaDoc, InvalidConfigException {
304             configs.put(configurationData.getId(), configurationData);
305         }
306
307         public void uninstall(Artifact configID) throws NoSuchConfigException, IOException JavaDoc {
308             configs.remove(configID);
309         }
310
311         public ConfigurationData loadConfiguration(Artifact configId) throws NoSuchConfigException, IOException JavaDoc, InvalidConfigException {
312             if (configs.containsKey(configId)) {
313                 ConfigurationData configurationData = (ConfigurationData) configs.get(configId);
314                 configurationData.setConfigurationStore(this);
315                 return configurationData;
316             } else {
317                 ConfigurationData configurationData = new ConfigurationData(configId, naming);
318                 configurationData.setConfigurationStore(this);
319                 return configurationData;
320             }
321         }
322
323         public boolean containsConfiguration(Artifact configID) {
324             return true;
325         }
326
327         public File JavaDoc createNewConfigurationDir(Artifact configId) {
328             try {
329                 return DeploymentUtil.createTempDir();
330             } catch (IOException JavaDoc e) {
331                 return null;
332             }
333         }
334
335         public Set JavaDoc resolve(Artifact configId, String JavaDoc moduleName, String JavaDoc pattern) throws NoSuchConfigException, MalformedURLException JavaDoc {
336             return Collections.singleton(baseURL);
337         }
338
339         public final static GBeanInfo GBEAN_INFO;
340
341         static {
342             GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(MockConfigStore.class, NameFactory.CONFIGURATION_STORE);
343             infoBuilder.addInterface(ConfigurationStore.class);
344             GBEAN_INFO = infoBuilder.getBeanInfo();
345         }
346     }
347 }
348
Popular Tags