KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.jetty.deployment;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectOutputStream JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.naming.Reference JavaDoc;
34 import javax.xml.namespace.QName JavaDoc;
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 /**
76  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
77  */

78 public class JettyModuleBuilderTest extends TestCase {
79
80     protected Kernel kernel;
81     private GBeanData container;
82     private ObjectName JavaDoc containerName;
83     private ObjectName JavaDoc connectorName;
84     private GBeanData connector;
85     private GBeanData securityServiceGBean;
86     private ObjectName JavaDoc securityServiceName;
87     private ObjectName JavaDoc serverInfoName;
88     private GBeanData serverInfoGBean;
89     private ObjectName JavaDoc tmName;
90     private ObjectName JavaDoc ctcName;
91     private GBeanData tm;
92     private GBeanData ctc;
93     private ObjectName JavaDoc tcmName;
94     private GBeanData tcm;
95     private ClassLoader JavaDoc cl;
96     private J2eeContext moduleContext = new J2eeContextImpl("jetty.test", "test", "null", NameFactory.WEB_MODULE, "jettyTest", null, null);
97     private JettyModuleBuilder builder;
98     private File JavaDoc basedir = new File JavaDoc(System.getProperty("basedir", "."));
99     private URI JavaDoc parentId = URI.create("org/apache/geronimo/Foo");
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         File JavaDoc path = new File JavaDoc(basedir, "src/test-resources/deployables/war4");
106         UnpackedJarFile jarFile = new UnpackedJarFile(path);
107         Module module = builder.createModule(null, jarFile);
108         URI JavaDoc id = new URI JavaDoc("war4");
109         EARContext earContext = createEARContext(outputPath, id);
110         ObjectName JavaDoc 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 JavaDoc names = kernel.listGBeans(ObjectName.getInstance("test:J2EEApplication=null,J2EEServer=bar,WebModule=war4,*"));
127         System.out.println("Object names: " + names);
128         for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();) {
129             ObjectName JavaDoc objectName = (ObjectName JavaDoc) 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 // assertEquals(Collections.singleton(ObjectName.getInstance("test:J2EEApplication=null,J2EEServer=bar,Servlet=Servlet1,WebFilter=Filter1,WebModule=war4,j2eeType=WebFilterMapping")), filterMapping2Data.getReferencePatterns("Previous"));
134

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 JavaDoc outputPath, URI JavaDoc id) throws MalformedObjectNameException JavaDoc, 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 JavaDoc createEJBLocalReference(String JavaDoc objectName, boolean isSession, String JavaDoc localHome, String JavaDoc local) throws DeploymentException {
158                         return null;
159                     }
160
161                     public Reference JavaDoc createEJBRemoteReference(String JavaDoc objectName, boolean isSession, String JavaDoc home, String JavaDoc remote) throws DeploymentException {
162                         return null;
163                     }
164
165                     public Reference JavaDoc createCORBAReference(URI JavaDoc corbaURL, String JavaDoc objectName, ObjectName JavaDoc containerName, String JavaDoc home) throws DeploymentException {
166                         return null;
167                     }
168
169                     public Object JavaDoc createHandleDelegateReference() {
170                         return null;
171                     }
172                 },
173                 new ResourceReferenceBuilder() {
174
175                     public Reference JavaDoc createResourceRef(String JavaDoc containerId, Class JavaDoc iface) throws DeploymentException {
176                         return null;
177                     }
178
179                     public Reference JavaDoc createAdminObjectRef(String JavaDoc containerId, Class JavaDoc iface) throws DeploymentException {
180                         return null;
181                     }
182
183                     public ObjectName JavaDoc locateResourceName(ObjectName JavaDoc query) throws DeploymentException {
184                         return null;
185                     }
186
187                     public GBeanData locateActivationSpecInfo(ObjectName JavaDoc resourceAdapterName, String JavaDoc messageListenerInterface) throws DeploymentException {
188                         return null;
189                     }
190
191                     public GBeanData locateResourceAdapterGBeanData(ObjectName JavaDoc resourceAdapterModuleName) throws DeploymentException {
192                         return null;
193                     }
194
195                     public GBeanData locateAdminObjectInfo(ObjectName JavaDoc resourceAdapterModuleName, String JavaDoc adminObjectInterfaceName) throws DeploymentException {
196                         return null;
197                     }
198
199                     public GBeanData locateConnectionFactoryInfo(ObjectName JavaDoc resourceAdapterModuleName, String JavaDoc connectionFactoryInterfaceName) throws DeploymentException {
200                         return null;
201                     }
202                 },
203                 new ServiceReferenceBuilder() {
204                     //it could return a Service or a Reference, we don't care
205
public Object JavaDoc createService(Class JavaDoc serviceInterface, URI JavaDoc wsdlURI, URI JavaDoc jaxrpcMappingURI, QName JavaDoc serviceQName, Map JavaDoc portComponentRefMap, List JavaDoc handlerInfos, Map JavaDoc portLocationMap, Map JavaDoc credentialsNameMap, DeploymentContext deploymentContext, Module module, ClassLoader JavaDoc classLoader) throws DeploymentException {
206                         return null;
207                     }
208                 }, kernel));
209                 return earContext;
210     }
211
212     private void recursiveDelete(File JavaDoc path) {
213         //does not delete top level dir passed in
214
File JavaDoc[] listing = path.listFiles();
215         for (int i = 0; i < ((listing == null) ? 0 : listing.length); i++) {
216             File JavaDoc file = listing[i];
217             if (file.isDirectory()) {
218                 recursiveDelete(file);
219             }
220             file.delete();
221         }
222     }
223
224     protected void setUp() throws Exception JavaDoc {
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         // webModuleName = NameFactory.getWebComponentName(null, null, null, null, NameFactory.WEB_MODULE, "WebResource", moduleContext);
229

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 JavaDoc("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 JavaDoc configurationManagerName = new ObjectName JavaDoc(":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 JavaDoc baseConfigName = configurationManager.load(parentId);
249         kernel.startGBean(baseConfigName);
250
251         ObjectName JavaDoc defaultServlets = ObjectName.getInstance("test:name=test,type=none,*");
252         ObjectName JavaDoc pojoWebServiceTemplate = null;
253         WebServiceBuilder webServiceBuilder = new UnavailableWebServiceBuilder();
254
255         serverInfoName = new ObjectName JavaDoc("geronimo.system:name=ServerInfo");
256         serverInfoGBean = new GBeanData(serverInfoName, ServerInfo.GBEAN_INFO);
257         serverInfoGBean.setAttribute("baseDirectory", ".");
258         start(serverInfoGBean);
259
260         //install the policy configuration factory
261
securityServiceName = new ObjectName JavaDoc("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 JavaDoc("null"), new Integer JavaDoc(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 JavaDoc(5678));
275         connector.setReferencePattern("JettyContainer", containerName);
276
277         start(container);
278         start(connector);
279
280         tm = new GBeanData(tmName, TransactionManagerImpl.GBEAN_INFO);
281         Set JavaDoc patterns = new HashSet JavaDoc();
282         patterns.add(ObjectName.getInstance("geronimo.server:j2eeType=JCAManagedConnectionFactory,*"));
283         tm.setAttribute("defaultTransactionTimeoutSeconds", new Integer JavaDoc(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 JavaDoc {
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 JavaDoc {
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 JavaDoc name) throws Exception JavaDoc {
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 JavaDoc install(URL JavaDoc source) throws IOException JavaDoc, InvalidConfigException {
326             return null;
327         }
328
329         public void install(ConfigurationData configurationData, File JavaDoc source) throws IOException JavaDoc, InvalidConfigException {
330         }
331
332         public void uninstall(URI JavaDoc configID) throws NoSuchConfigException, IOException JavaDoc {
333         }
334
335         public ObjectName JavaDoc loadConfiguration(URI JavaDoc configId) throws NoSuchConfigException, IOException JavaDoc, InvalidConfigException {
336             ObjectName JavaDoc configurationObjectName = null;
337             try {
338                 configurationObjectName = Configuration.getConfigurationObjectName(configId);
339             } catch (MalformedObjectNameException JavaDoc 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 JavaDoc e) {
351                 throw new InvalidConfigException("Unable to register configuration", e);
352             }
353
354             return configurationObjectName;
355         }
356
357         public boolean containsConfiguration(URI JavaDoc configID) {
358             return true;
359         }
360
361         public void updateConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, Exception JavaDoc {
362
363         }
364
365         public String JavaDoc getObjectName() {
366             return null;
367         }
368
369         public List JavaDoc listConfiguations() {
370             return null;
371         }
372
373         public File JavaDoc 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 JavaDoc[] {"kernel"});
386             GBEAN_INFO = infoBuilder.getBeanInfo();
387
388             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
389             try {
390                 ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
391                 oos.flush();
392                 NO_OBJECTS_OS = baos.toByteArray();
393             } catch (IOException JavaDoc e) {
394                 throw new RuntimeException JavaDoc(e);
395             }
396         }
397     }
398 }
399
Popular Tags