KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > tomcat > TomcatWebAppContext


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
18 package org.apache.geronimo.tomcat;
19
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.naming.directory.DirContext JavaDoc;
34 import javax.transaction.TransactionManager JavaDoc;
35 import javax.transaction.UserTransaction JavaDoc;
36
37 import org.apache.catalina.Context;
38 import org.apache.catalina.Manager;
39 import org.apache.catalina.Realm;
40 import org.apache.catalina.Valve;
41 import org.apache.catalina.core.StandardContext;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.apache.geronimo.connector.outbound.connectiontracking.TrackedConnectionAssociator;
45 import org.apache.geronimo.gbean.AbstractName;
46 import org.apache.geronimo.gbean.GBeanInfo;
47 import org.apache.geronimo.gbean.GBeanInfoBuilder;
48 import org.apache.geronimo.gbean.GBeanLifecycle;
49 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
50 import org.apache.geronimo.j2ee.management.impl.InvalidObjectNameException;
51 import org.apache.geronimo.kernel.Kernel;
52 import org.apache.geronimo.kernel.ObjectNameUtil;
53 import org.apache.geronimo.management.J2EEApplication;
54 import org.apache.geronimo.management.J2EEServer;
55 import org.apache.geronimo.management.geronimo.WebConnector;
56 import org.apache.geronimo.management.geronimo.WebContainer;
57 import org.apache.geronimo.management.geronimo.WebModule;
58 import org.apache.geronimo.tomcat.util.SecurityHolder;
59 import org.apache.geronimo.transaction.GeronimoUserTransaction;
60 import org.apache.geronimo.webservices.WebServiceContainer;
61 import org.apache.geronimo.webservices.WebServiceContainerFactory;
62 import org.apache.naming.resources.DirContextURLStreamHandler;
63
64 /**
65  * Wrapper for a WebApplicationContext that sets up its J2EE environment.
66  *
67  * @version $Rev: 483201 $ $Date: 2006-12-06 14:52:09 -0500 (Wed, 06 Dec 2006) $
68  */

69 public class TomcatWebAppContext implements GBeanLifecycle, TomcatContext, WebModule {
70
71     private static Log log = LogFactory.getLog(TomcatWebAppContext.class);
72
73     protected final TomcatContainer container;
74
75     private final ClassLoader JavaDoc classLoader;
76
77     protected Context JavaDoc context = null;
78
79     private String JavaDoc path = null;
80
81     private String JavaDoc docBase = null;
82
83     private String JavaDoc virtualServer = null;
84
85     private final Realm realm;
86
87     private final List JavaDoc valveChain;
88
89 // private final CatalinaCluster catalinaCluster;
90

91     private final Manager JavaDoc manager;
92
93     private final boolean crossContext;
94
95     private final boolean disableCookies;
96
97     private final UserTransaction JavaDoc userTransaction;
98
99     private final Map JavaDoc componentContext;
100
101     private final Kernel kernel;
102
103     private final Set JavaDoc unshareableResources;
104
105     private final Set JavaDoc applicationManagedSecurityResources;
106
107     private final TrackedConnectionAssociator trackedConnectionAssociator;
108
109     private final SecurityHolder securityHolder;
110
111     private final J2EEServer server;
112
113     private final Map JavaDoc webServices;
114
115     private final String JavaDoc objectName;
116
117     private final String JavaDoc originalSpecDD;
118
119     private final URL JavaDoc configurationBaseURL;
120     
121     // JSR 77
122

123     private final String JavaDoc j2EEServer;
124     
125     private final String JavaDoc j2EEApplication;
126
127     public TomcatWebAppContext(
128             ClassLoader JavaDoc classLoader,
129             String JavaDoc objectName,
130             String JavaDoc originalSpecDD,
131             URL JavaDoc configurationBaseUrl,
132             SecurityHolder securityHolder,
133             String JavaDoc virtualServer,
134             Map JavaDoc componentContext,
135             Set JavaDoc unshareableResources,
136             Set JavaDoc applicationManagedSecurityResources,
137             TransactionManager JavaDoc transactionManager,
138             TrackedConnectionAssociator trackedConnectionAssociator,
139             TomcatContainer container,
140             ObjectRetriever tomcatRealm,
141             ValveGBean tomcatValveChain,
142 // CatalinaClusterGBean cluster,
143
ManagerGBean manager,
144             boolean crossContext,
145             boolean disableCookies,
146             Map JavaDoc webServices,
147             J2EEServer server,
148             J2EEApplication application,
149             Kernel kernel)
150             throws Exception JavaDoc {
151
152         assert classLoader != null;
153         assert configurationBaseUrl != null;
154         assert transactionManager != null;
155         assert trackedConnectionAssociator != null;
156         assert componentContext != null;
157         assert container != null;
158
159
160         this.objectName = objectName;
161         URI JavaDoc root;
162 // TODO is there a simpler way to do this?
163
if (configurationBaseUrl.getProtocol().equalsIgnoreCase("file")) {
164             root = new URI JavaDoc("file", configurationBaseUrl.getPath(), null);
165         } else {
166             root = URI.create(configurationBaseUrl.toString());
167         }
168         this.setDocBase(root.getPath());
169         this.container = container;
170         this.originalSpecDD = originalSpecDD;
171
172         this.virtualServer = virtualServer;
173         this.securityHolder = securityHolder;
174
175         this.userTransaction = new GeronimoUserTransaction(transactionManager);
176         this.componentContext = componentContext;
177         this.unshareableResources = unshareableResources;
178         this.applicationManagedSecurityResources = applicationManagedSecurityResources;
179         this.trackedConnectionAssociator = trackedConnectionAssociator;
180
181         this.server = server;
182
183         this.configurationBaseURL = configurationBaseUrl;
184
185         if (tomcatRealm != null){
186             realm = (Realm)tomcatRealm.getInternalObject();
187             if (realm == null){
188                 throw new IllegalArgumentException JavaDoc("tomcatRealm must be an instance of org.apache.catalina.Realm.");
189             }
190         } else{
191             realm = null;
192         }
193
194         //Add the valve list
195
if (tomcatValveChain != null){
196             ArrayList JavaDoc chain = new ArrayList JavaDoc();
197             ValveGBean valveGBean = tomcatValveChain;
198             while(valveGBean != null){
199                 chain.add((Valve)valveGBean.getInternalObject());
200                 valveGBean = valveGBean.getNextValve();
201             }
202             valveChain = chain;
203         } else {
204             valveChain = null;
205         }
206
207         //Add the cluster
208
// if (cluster != null) {
209
// catalinaCluster = (CatalinaCluster) cluster.getInternalObject();
210
// } else {
211
// catalinaCluster = null;
212
// }
213

214         //Add the manager
215
if (manager != null) {
216             this.manager = (Manager JavaDoc) manager.getInternalObject();
217         } else {
218             this.manager = null;
219         }
220
221         this.crossContext = crossContext;
222
223         this.disableCookies = disableCookies;
224
225         this.webServices = createWebServices(webServices, kernel);
226
227         this.classLoader = classLoader;
228
229         this.kernel = kernel;
230         
231         if (objectName != null) {
232             ObjectName JavaDoc myObjectName = ObjectNameUtil.getObjectName(objectName);
233             verifyObjectName(myObjectName);
234             j2EEServer = myObjectName.getKeyProperty(NameFactory.J2EE_SERVER);
235             j2EEApplication = myObjectName.getKeyProperty(NameFactory.J2EE_APPLICATION);
236         } else {
237             // StandardContext uses default value of these as "none"
238
j2EEServer = null;
239             j2EEApplication = null;
240         }
241     }
242
243     private Map JavaDoc createWebServices(Map JavaDoc webServiceFactoryMap, Kernel kernel) throws Exception JavaDoc {
244         Map JavaDoc webServices = new HashMap JavaDoc();
245         if (webServiceFactoryMap != null) {
246             for (Iterator JavaDoc iterator = webServiceFactoryMap.entrySet().iterator(); iterator.hasNext();) {
247                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
248                 String JavaDoc servletName = (String JavaDoc) entry.getKey();
249                 AbstractName factoryName = (AbstractName) entry.getValue();
250                 WebServiceContainerFactory webServiceContainerFactory = (WebServiceContainerFactory) kernel.getGBean(factoryName);
251                 WebServiceContainer webServiceContainer = webServiceContainerFactory.getWebServiceContainer();
252                 webServices.put(servletName, webServiceContainer);
253             }
254         }
255         return webServices;
256     }
257
258     public String JavaDoc getObjectName() {
259         return objectName;
260     }
261
262     public String JavaDoc getJ2EEApplication() {
263         return j2EEApplication;
264     }
265
266     public String JavaDoc getJ2EEServer() {
267         return j2EEServer;
268     }
269
270     public boolean isStateManageable() {
271         return true;
272     }
273
274     public boolean isStatisticsProvider() {
275         return false;
276     }
277
278     public boolean isEventProvider() {
279         return true;
280     }
281
282     public URL JavaDoc getWARDirectory() {
283         return configurationBaseURL;
284     }
285
286     public String JavaDoc getWARName() {
287         //todo: make this return something more consistent
288
try {
289             return ObjectName.getInstance(objectName).getKeyProperty(NameFactory.J2EE_NAME);
290         } catch (MalformedObjectNameException JavaDoc e) {
291             return null;
292         }
293     }
294
295     public WebContainer getContainer() {
296         return container;
297     }
298
299     public String JavaDoc getServer() {
300         return server == null? null: server.getObjectName();
301     }
302
303     public String JavaDoc getDocBase() {
304         return docBase;
305     }
306
307     public void setDocBase(String JavaDoc docBase) {
308         this.docBase = docBase;
309     }
310
311     public UserTransaction JavaDoc getUserTransaction() {
312         return userTransaction;
313     }
314
315     public Map JavaDoc getComponentContext() {
316         return componentContext;
317     }
318
319     public String JavaDoc getVirtualServer() {
320         return virtualServer;
321     }
322
323     public ClassLoader JavaDoc getClassLoader() {
324         return classLoader;
325     }
326
327     public Kernel getKernel() {
328         return kernel;
329     }
330
331     public boolean isDisableCookies() {
332         return disableCookies;
333     }
334
335     public Context JavaDoc getContext() {
336         return context;
337     }
338
339     public void setContext(Context JavaDoc context) {
340         this.context = context;
341     }
342
343     public URL JavaDoc getURLFor() {
344         WebConnector[] connectors = (WebConnector[]) container.getConnectors();
345         Map JavaDoc map = new HashMap JavaDoc();
346         for (int i = 0; i < connectors.length; i++) {
347             WebConnector connector = connectors[i];
348             map.put(connector.getProtocol(), connector.getConnectUrl());
349         }
350         String JavaDoc urlPrefix;
351         if((urlPrefix = (String JavaDoc) map.get("HTTP")) == null) {
352             if((urlPrefix = (String JavaDoc) map.get("HTTPS")) == null) {
353                 urlPrefix = (String JavaDoc) map.get("AJP");
354             }
355         }
356         if(urlPrefix == null) {
357             return null;
358         }
359         try {
360             return new URL JavaDoc(urlPrefix + getContextPath());
361         } catch (MalformedURLException JavaDoc e) {
362             log.error("Bad URL to connect to web app", e);
363             return null;
364         }
365     }
366
367     public String JavaDoc getContextPath() {
368         return path;
369     }
370
371     public void setContextPath(String JavaDoc path) {
372         this.path = path.startsWith("/") ? path : "/" + path;
373     }
374
375     public SecurityHolder getSecurityHolder() {
376         return securityHolder;
377     }
378
379
380     public Set JavaDoc getApplicationManagedSecurityResources() {
381         return applicationManagedSecurityResources;
382     }
383
384     public TrackedConnectionAssociator getTrackedConnectionAssociator() {
385         return trackedConnectionAssociator;
386     }
387
388     public Set JavaDoc getUnshareableResources() {
389         return unshareableResources;
390     }
391
392     public Realm getRealm() {
393         return realm;
394     }
395
396     public List JavaDoc getValveChain() {
397         return valveChain;
398     }
399
400 // public CatalinaCluster getCluster() {
401
// return catalinaCluster;
402
// }
403

404     public Manager JavaDoc getManager() {
405         return manager;
406     }
407
408     public boolean isCrossContext() {
409         return crossContext;
410     }
411
412     public Map JavaDoc getWebServices(){
413         return webServices;
414     }
415
416     public String JavaDoc[] getServlets(){
417         String JavaDoc[] result = null;
418         if ((context != null) && (context instanceof StandardContext)) {
419             result = ((StandardContext) context).getServlets();
420         }
421
422         return result;
423     }
424
425     /**
426      * ObjectName must match this pattern: <p/>
427      * domain:j2eeType=WebModule,name=MyName,J2EEServer=MyServer,J2EEApplication=MyApplication
428      */

429     private void verifyObjectName(ObjectName JavaDoc objectName) {
430         if (objectName.isPattern()) {
431             throw new InvalidObjectNameException(
432                     "ObjectName can not be a pattern", objectName);
433         }
434         Hashtable JavaDoc keyPropertyList = objectName.getKeyPropertyList();
435         if (!NameFactory.WEB_MODULE.equals(keyPropertyList.get("j2eeType"))) {
436             throw new InvalidObjectNameException(
437                     "WebModule object name j2eeType property must be 'WebModule'",
438                     objectName);
439         }
440         if (!keyPropertyList.containsKey(NameFactory.J2EE_NAME)) {
441             throw new InvalidObjectNameException(
442                     "WebModule object must contain a name property", objectName);
443         }
444         if (!keyPropertyList.containsKey(NameFactory.J2EE_SERVER)) {
445             throw new InvalidObjectNameException(
446                     "WebModule object name must contain a J2EEServer property",
447                     objectName);
448         }
449         if (!keyPropertyList.containsKey(NameFactory.J2EE_APPLICATION)) {
450             throw new InvalidObjectNameException(
451                     "WebModule object name must contain a J2EEApplication property",
452                     objectName);
453         }
454         if (keyPropertyList.size() != 4) {
455             throw new InvalidObjectNameException(
456                     "WebModule object name can only have j2eeType, name, J2EEApplication, and J2EEServer properties",
457                     objectName);
458         }
459     }
460
461     public String JavaDoc[] getJavaVMs() {
462         return server == null? new String JavaDoc[0]: server.getJavaVMs();
463     }
464
465     public String JavaDoc getDeploymentDescriptor() {
466         return originalSpecDD;
467     }
468
469     public void doStart() throws Exception JavaDoc {
470
471         // See the note of TomcatContainer::addContext
472
container.addContext(this);
473         // Is it necessary - doesn't Tomcat Embedded take care of it?
474
// super.start();
475
//register the classloader <> dir context association so that tomcat's jndi based getResources works.
476
DirContext JavaDoc resources = context.getResources();
477         DirContextURLStreamHandler.bind(classLoader, resources);
478
479         log.debug("TomcatWebAppContext started for " + path);
480     }
481
482     public void doStop() throws Exception JavaDoc {
483         container.removeContext(this);
484         DirContextURLStreamHandler.unbind(classLoader);
485
486         // No more logging will occur for this ClassLoader. Inform the LogFactory to avoid a memory leak.
487
// LogFactory.release(classLoader);
488

489         log.debug("TomcatWebAppContext stopped");
490     }
491
492     public void doFail() {
493         container.removeContext(this);
494
495         // No more logging will occur for this ClassLoader. Inform the LogFactory to avoid a memory leak.
496
// LogFactory.release(classLoader);
497

498         log.warn("TomcatWebAppContext failed");
499     }
500
501     public static final GBeanInfo GBEAN_INFO;
502
503     static {
504         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("Tomcat WebApplication Context", TomcatWebAppContext.class, NameFactory.WEB_MODULE);
505
506         infoBuilder.addAttribute("classLoader", ClassLoader JavaDoc.class, false);
507         infoBuilder.addAttribute("objectName", String JavaDoc.class, false);
508         infoBuilder.addAttribute("deploymentDescriptor", String JavaDoc.class, true);
509         infoBuilder.addAttribute("configurationBaseUrl", URL JavaDoc.class, true);
510
511         infoBuilder.addAttribute("contextPath", String JavaDoc.class, true);
512
513         infoBuilder.addAttribute("securityHolder", SecurityHolder.class, true);
514         infoBuilder.addAttribute("virtualServer", String JavaDoc.class, true);
515         infoBuilder.addAttribute("componentContext", Map JavaDoc.class, true);
516         infoBuilder.addAttribute("unshareableResources", Set JavaDoc.class, true);
517         infoBuilder.addAttribute("applicationManagedSecurityResources", Set JavaDoc.class, true);
518         infoBuilder.addReference("TransactionManager", TransactionManager JavaDoc.class, NameFactory.TRANSACTION_MANAGER);
519         infoBuilder.addReference("TrackedConnectionAssociator", TrackedConnectionAssociator.class, NameFactory.JCA_CONNECTION_TRACKER);
520
521         infoBuilder.addReference("Container", TomcatContainer.class, NameFactory.GERONIMO_SERVICE);
522         infoBuilder.addReference("TomcatRealm", ObjectRetriever.class);
523         infoBuilder.addReference("TomcatValveChain", ValveGBean.class);
524 // infoBuilder.addReference("Cluster", CatalinaClusterGBean.class, CatalinaClusterGBean.J2EE_TYPE);
525
infoBuilder.addReference("Manager", ManagerGBean.class);
526         infoBuilder.addAttribute("crossContext", boolean.class, true);
527         infoBuilder.addAttribute("disableCookies", boolean.class, true);
528         infoBuilder.addAttribute("webServices", Map JavaDoc.class, true);
529         infoBuilder.addReference("J2EEServer", J2EEServer.class);
530         infoBuilder.addReference("J2EEApplication", J2EEApplication.class);
531         infoBuilder.addAttribute("kernel", Kernel.class, false);
532
533         infoBuilder.addInterface(WebModule.class);
534
535         infoBuilder.setConstructor(new String JavaDoc[] {
536                 "classLoader",
537                 "objectName",
538                 "deploymentDescriptor",
539                 "configurationBaseUrl",
540                 "securityHolder",
541                 "virtualServer",
542                 "componentContext",
543                 "unshareableResources",
544                 "applicationManagedSecurityResources",
545                 "TransactionManager",
546                 "TrackedConnectionAssociator",
547                 "Container",
548                 "TomcatRealm",
549                 "TomcatValveChain",
550 // "Cluster",
551
"Manager",
552                 "crossContext",
553                 "disableCookies",
554                 "webServices",
555                 "J2EEServer",
556                 "J2EEApplication",
557                 "kernel"
558                 }
559         );
560
561         GBEAN_INFO = infoBuilder.getBeanInfo();
562     }
563
564     public static GBeanInfo getGBeanInfo() {
565         return GBEAN_INFO;
566     }
567 }
568
Popular Tags