001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jetty6.deployment;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.Collection;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.Iterator;
027: import java.util.Map;
028: import java.util.Set;
029: import java.util.ArrayList;
030:
031: import org.apache.geronimo.testsupport.TestSupport;
032:
033: import org.apache.geronimo.common.DeploymentException;
034: import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinatorGBean;
035: import org.apache.geronimo.deployment.ModuleIDBuilder;
036: import org.apache.geronimo.deployment.service.GBeanBuilder;
037: import org.apache.geronimo.deployment.util.DeploymentUtil;
038: import org.apache.geronimo.deployment.util.UnpackedJarFile;
039: import org.apache.geronimo.gbean.AbstractName;
040: import org.apache.geronimo.gbean.AbstractNameQuery;
041: import org.apache.geronimo.gbean.GBeanData;
042: import org.apache.geronimo.gbean.GBeanInfo;
043: import org.apache.geronimo.gbean.GBeanInfoBuilder;
044: import org.apache.geronimo.j2ee.deployment.EARContext;
045: import org.apache.geronimo.j2ee.deployment.Module;
046: import org.apache.geronimo.j2ee.deployment.UnavailableWebServiceBuilder;
047: import org.apache.geronimo.j2ee.deployment.WebServiceBuilder;
048: import org.apache.geronimo.j2ee.deployment.NamingBuilderCollection;
049: import org.apache.geronimo.j2ee.deployment.ModuleBuilderExtension;
050: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
051: import org.apache.geronimo.j2ee.management.impl.J2EEServerImpl;
052: import org.apache.geronimo.jetty6.JettyContainerImpl;
053: import org.apache.geronimo.jetty6.connector.HTTPSocketConnector;
054: import org.apache.geronimo.kernel.Jsr77Naming;
055: import org.apache.geronimo.kernel.Kernel;
056: import org.apache.geronimo.kernel.KernelFactory;
057: import org.apache.geronimo.kernel.Naming;
058: import org.apache.geronimo.kernel.mock.MockConfigStore;
059: import org.apache.geronimo.kernel.config.Configuration;
060: import org.apache.geronimo.kernel.config.ConfigurationData;
061: import org.apache.geronimo.kernel.config.ConfigurationManager;
062: import org.apache.geronimo.kernel.config.ConfigurationModuleType;
063: import org.apache.geronimo.kernel.config.ConfigurationStore;
064: import org.apache.geronimo.kernel.config.ConfigurationUtil;
065: import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager;
066: import org.apache.geronimo.kernel.config.InvalidConfigException;
067: import org.apache.geronimo.kernel.config.NoSuchConfigException;
068: import org.apache.geronimo.kernel.config.NullConfigurationStore;
069: import org.apache.geronimo.kernel.management.State;
070: import org.apache.geronimo.kernel.repository.Artifact;
071: import org.apache.geronimo.kernel.repository.ArtifactManager;
072: import org.apache.geronimo.kernel.repository.ArtifactResolver;
073: import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
074: import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
075: import org.apache.geronimo.kernel.repository.Environment;
076: import org.apache.geronimo.kernel.repository.ImportType;
077: import org.apache.geronimo.kernel.repository.Repository;
078: import org.apache.geronimo.security.deployment.GeronimoSecurityBuilderImpl;
079: import org.apache.geronimo.system.serverinfo.BasicServerInfo;
080: import org.apache.geronimo.transaction.manager.GeronimoTransactionManagerGBean;
081: import org.apache.geronimo.xbeans.geronimo.j2ee.GerSecurityDocument;
082: import org.apache.xmlbeans.impl.schema.SchemaTypeImpl;
083:
084: /**
085: * @version $Rev:385232 $ $Date: 2008-01-10 03:07:04 -0800 (Thu, 10 Jan 2008) $
086: */
087: public class JettyModuleBuilderTest extends TestSupport {
088:
089: private static Naming naming = new Jsr77Naming();
090: private Artifact baseId = new Artifact("test", "base", "1", "car");
091: private final AbstractName serverName = naming.createRootName(
092: baseId, "Server", "J2EEServer");
093:
094: protected Kernel kernel;
095: private AbstractName tmName;
096: private AbstractName ctcName;
097: private ClassLoader cl;
098: private JettyModuleBuilder builder;
099: private Artifact webModuleArtifact = new Artifact("foo", "bar",
100: "1", "car");
101: private Environment defaultEnvironment = new Environment();
102: private ConfigurationManager configurationManager;
103: private ConfigurationStore configStore;
104:
105: public void testDeployWar4() throws Exception {
106: File outputPath = new File(BASEDIR,
107: "target/test-resources/deployables/war4");
108: recursiveDelete(outputPath);
109: outputPath.mkdirs();
110: new File(outputPath, "war").mkdir();
111: File path = new File(BASEDIR,
112: "src/test/resources/deployables/war4");
113: UnpackedJarFile jarFile = new UnpackedJarFile(path);
114: Module module = builder.createModule(null, jarFile, kernel
115: .getNaming(), new ModuleIDBuilder());
116: Repository repository = null;
117:
118: AbstractName moduleName = module.getModuleName();
119: EARContext earContext = createEARContext(outputPath,
120: defaultEnvironment, repository, configStore, moduleName);
121: module.setEarContext(earContext);
122: module.setRootEarContext(earContext);
123: builder.initContext(earContext, module, cl);
124: builder
125: .addGBeans(earContext, module, cl,
126: Collections.EMPTY_SET);
127: ConfigurationData configurationData = earContext
128: .getConfigurationData();
129: earContext.close();
130: module.close();
131:
132: Artifact configurationId = configurationData.getId();
133: configurationManager.loadConfiguration(configurationData);
134: Configuration configuration = configurationManager
135: .getConfiguration(configurationId);
136: configurationManager.startConfiguration(configurationId);
137:
138: assertEquals(State.RUNNING_INDEX, kernel
139: .getGBeanState(moduleName));
140: Set names = configuration.findGBeans(new AbstractNameQuery(
141: moduleName.getArtifact(), Collections.EMPTY_MAP));
142: log.debug("names: " + names);
143: for (Iterator iterator = names.iterator(); iterator.hasNext();) {
144: AbstractName objectName = (AbstractName) iterator.next();
145: assertEquals(State.RUNNING_INDEX, kernel
146: .getGBeanState(objectName));
147: }
148:
149: configurationManager.stopConfiguration(configurationId);
150: configurationManager.unloadConfiguration(configurationId);
151:
152: }
153:
154: public void testContextRootWithSpaces() throws Exception {
155: File outputPath = new File(BASEDIR,
156: "target/test-resources/deployables/war-spaces-in-context");
157: recursiveDelete(outputPath);
158: outputPath.mkdirs();
159: new File(outputPath, "war").mkdir();
160: File path = new File(BASEDIR,
161: "src/test/resources/deployables/war-spaces-in-context");
162: UnpackedJarFile jarFile = new UnpackedJarFile(path);
163: Module module = builder.createModule(null, jarFile, kernel
164: .getNaming(), new ModuleIDBuilder());
165: Repository repository = null;
166:
167: AbstractName moduleName = module.getModuleName();
168: EARContext earContext = createEARContext(outputPath,
169: defaultEnvironment, repository, configStore, moduleName);
170: module.setEarContext(earContext);
171: module.setRootEarContext(earContext);
172: builder.initContext(earContext, module, cl);
173: builder
174: .addGBeans(earContext, module, cl,
175: Collections.EMPTY_SET);
176: ConfigurationData configurationData = earContext
177: .getConfigurationData();
178: earContext.close();
179: module.close();
180:
181: Artifact configurationId = configurationData.getId();
182: configurationManager.loadConfiguration(configurationData);
183: Configuration configuration = configurationManager
184: .getConfiguration(configurationId);
185: configurationManager.startConfiguration(configurationId);
186:
187: String contextRoot = (String) kernel.getAttribute(moduleName,
188: "contextPath");
189: assertNotNull(contextRoot);
190: assertEquals(contextRoot, contextRoot.trim());
191:
192: configurationManager.stopConfiguration(configurationId);
193: configurationManager.unloadConfiguration(configurationId);
194:
195: }
196:
197: private EARContext createEARContext(File outputPath,
198: Environment environment, Repository repository,
199: ConfigurationStore configStore, AbstractName moduleName)
200: throws DeploymentException {
201: Set repositories = repository == null ? Collections.EMPTY_SET
202: : Collections.singleton(repository);
203: ArtifactManager artifactManager = new DefaultArtifactManager();
204: ArtifactResolver artifactResolver = new DefaultArtifactResolver(
205: artifactManager, repositories, null);
206: return new EARContext(outputPath, null, environment,
207: ConfigurationModuleType.WAR, naming,
208: configurationManager, repositories,
209: new AbstractNameQuery(serverName), moduleName,
210: new AbstractNameQuery(tmName), new AbstractNameQuery(
211: ctcName), null, null, null);
212: }
213:
214: private void recursiveDelete(File path) {
215: //does not delete top level dir passed in
216: File[] listing = path.listFiles();
217: if (listing != null) {
218: for (int i = 0; i < listing.length; i++) {
219: File file = listing[i];
220: if (file.isDirectory()) {
221: recursiveDelete(file);
222: }
223: file.delete();
224: }
225: }
226: }
227:
228: protected void setUp() throws Exception {
229: super .setUp();
230:
231: ((SchemaTypeImpl) GerSecurityDocument.type)
232: .addSubstitutionGroupMember(org.apache.geronimo.xbeans.geronimo.security.GerSecurityDocument.type
233: .getDocumentElementName());
234:
235: cl = this .getClass().getClassLoader();
236: kernel = KernelFactory.newInstance().createKernel("test");
237: kernel.boot();
238:
239: ConfigurationData bootstrap = new ConfigurationData(baseId,
240: naming);
241:
242: GBeanData serverInfo = bootstrap.addGBean("ServerInfo",
243: BasicServerInfo.GBEAN_INFO);
244: serverInfo.setAttribute("baseDirectory", ".");
245:
246: AbstractName configStoreName = bootstrap.addGBean(
247: "MockConfigurationStore", MockConfigStore.GBEAN_INFO)
248: .getAbstractName();
249:
250: GBeanData artifactManagerData = bootstrap.addGBean(
251: "ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
252:
253: GBeanData artifactResolverData = bootstrap.addGBean(
254: "ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
255: artifactResolverData.setReferencePattern("ArtifactManager",
256: artifactManagerData.getAbstractName());
257:
258: GBeanData configurationManagerData = bootstrap.addGBean(
259: "ConfigurationManager",
260: EditableKernelConfigurationManager.GBEAN_INFO);
261: configurationManagerData.setReferencePattern("ArtifactManager",
262: artifactManagerData.getAbstractName());
263: configurationManagerData.setReferencePattern(
264: "ArtifactResolver", artifactResolverData
265: .getAbstractName());
266: configurationManagerData.setReferencePattern("Stores",
267: configStoreName);
268: bootstrap.addGBean(configurationManagerData);
269:
270: GBeanData serverData = new GBeanData(serverName,
271: J2EEServerImpl.GBEAN_INFO);
272: bootstrap.addGBean(serverData);
273:
274: Collection defaultServlets = new HashSet();
275: Collection defaultFilters = new HashSet();
276: Collection defaultFilterMappings = new HashSet();
277: Object pojoWebServiceTemplate = null;
278: WebServiceBuilder webServiceBuilder = new UnavailableWebServiceBuilder();
279:
280: GBeanData containerData = bootstrap.addGBean("JettyContainer",
281: JettyContainerImpl.GBEAN_INFO);
282: AbstractName containerName = containerData.getAbstractName();
283: containerData.setAttribute("jettyHome", new File(BASEDIR,
284: "target/var/jetty").toString());
285: containerData.setReferencePattern("ServerInfo", serverInfo
286: .getAbstractName());
287:
288: GBeanData connector = bootstrap.addGBean("JettyConnector",
289: HTTPSocketConnector.GBEAN_INFO);
290: connector.setAttribute("port", new Integer(5678));
291: connector.setAttribute("maxThreads", new Integer(50));
292: // connector.setAttribute("minThreads", new Integer(10));
293: connector.setReferencePattern("JettyContainer", containerName);
294:
295: GBeanData tm = bootstrap.addGBean("TransactionManager",
296: GeronimoTransactionManagerGBean.GBEAN_INFO);
297: tmName = tm.getAbstractName();
298: tm.setAttribute("defaultTransactionTimeoutSeconds",
299: new Integer(10));
300:
301: GBeanData ctc = bootstrap.addGBean(
302: "ConnectionTrackingCoordinator",
303: ConnectionTrackingCoordinatorGBean.GBEAN_INFO);
304: ctcName = ctc.getAbstractName();
305: ctc.setReferencePattern("TransactionManager", tmName);
306:
307: ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap,
308: getClass().getClassLoader());
309:
310: configurationManager = ConfigurationUtil
311: .getEditableConfigurationManager(kernel);
312: configStore = (ConfigurationStore) kernel
313: .getGBean(configStoreName);
314: configStore.install(bootstrap);
315:
316: defaultEnvironment.addDependency(baseId, ImportType.ALL);
317: defaultEnvironment.setConfigId(webModuleArtifact);
318: Collection<ModuleBuilderExtension> moduleBuilderExtensions = new ArrayList<ModuleBuilderExtension>();
319: builder = new JettyModuleBuilder(
320: defaultEnvironment,
321: new Integer(1800),
322: Collections.EMPTY_LIST,
323: new AbstractNameQuery(containerName),
324: //new AbstractNameQuery(containerName),
325: null, defaultServlets, defaultFilters,
326: defaultFilterMappings, null, null,
327: pojoWebServiceTemplate, Collections
328: .singleton(webServiceBuilder), null,
329: Collections.singleton(new GeronimoSecurityBuilderImpl(
330: null)), Collections.singleton(new GBeanBuilder(
331: null, null)), new NamingBuilderCollection(null,
332: null), moduleBuilderExtensions,
333: new MockResourceEnvironmentSetter(), kernel);
334: builder.doStart();
335: }
336:
337: protected void tearDown() throws Exception {
338: builder.doStop();
339: kernel.shutdown();
340: super.tearDown();
341: }
342:
343: }
|