KickJava   Java API By Example, From Geeks To Geeks.

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


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;
18
19 import java.beans.PropertyChangeListener JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.naming.NamingException JavaDoc;
26 import javax.security.auth.Subject JavaDoc;
27 import javax.security.jacc.PolicyContext JavaDoc;
28 import javax.servlet.Servlet JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30
31 import org.apache.catalina.Container;
32 import org.apache.catalina.LifecycleException;
33 import org.apache.catalina.Loader;
34 import org.apache.catalina.Manager;
35 import org.apache.catalina.Valve;
36 import org.apache.catalina.Wrapper;
37 import org.apache.catalina.connector.Request;
38 import org.apache.catalina.connector.Response;
39 import org.apache.catalina.core.StandardContext;
40 import org.apache.catalina.valves.ValveBase;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43 import org.apache.geronimo.common.DeploymentException;
44 import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
45 import org.apache.geronimo.security.ContextManager;
46 import org.apache.geronimo.security.IdentificationPrincipal;
47 import org.apache.geronimo.security.SubjectId;
48 import org.apache.geronimo.security.deploy.DefaultPrincipal;
49 import org.apache.geronimo.security.util.ConfigurationUtil;
50 import org.apache.geronimo.tomcat.interceptor.BeforeAfter;
51 import org.apache.geronimo.tomcat.interceptor.ComponentContextBeforeAfter;
52 import org.apache.geronimo.tomcat.interceptor.InstanceContextBeforeAfter;
53 import org.apache.geronimo.tomcat.interceptor.PolicyContextBeforeAfter;
54 import org.apache.geronimo.tomcat.util.SecurityHolder;
55 import org.apache.geronimo.tomcat.valve.DefaultSubjectValve;
56 import org.apache.geronimo.tomcat.valve.GeronimoBeforeAfterValve;
57 import org.apache.geronimo.webservices.POJOWebServiceServlet;
58 import org.apache.geronimo.webservices.WebServiceContainer;
59 import org.apache.geronimo.webservices.WebServiceContainerInvoker;
60
61
62 public class GeronimoStandardContext extends StandardContext {
63
64     private static final Log log = LogFactory.getLog(GeronimoStandardContext.class);
65
66     private static final long serialVersionUID = 3834587716552831032L;
67
68     private Subject JavaDoc defaultSubject = null;
69
70     private Map JavaDoc webServiceMap = null;
71
72     private boolean pipelineInitialized;
73
74     private BeforeAfter beforeAfter = null;
75     private int contextCount = 0;
76
77     public void setContextProperties(TomcatContext ctx) throws DeploymentException {
78         //try to make sure this mbean properties match those of the TomcatWebAppContext
79
if (ctx instanceof TomcatWebAppContext) {
80             TomcatWebAppContext tctx = (TomcatWebAppContext) ctx;
81             setJavaVMs(tctx.getJavaVMs());
82             setServer(tctx.getServer());
83             setJ2EEApplication(tctx.getJ2EEApplication());
84             setJ2EEServer(tctx.getJ2EEServer());
85         }
86         // Create ReadOnlyContext
87
javax.naming.Context JavaDoc enc = null;
88         Map JavaDoc componentContext = ctx.getComponentContext();
89         try {
90             if (componentContext != null) {
91                 enc = EnterpriseNamingContext.createEnterpriseNamingContext(componentContext, ctx.getUserTransaction(), ctx.getKernel(), ctx.getClassLoader());
92             }
93         } catch (NamingException JavaDoc ne) {
94             log.error(ne);
95         }
96
97         int index = 0;
98         BeforeAfter interceptor = new InstanceContextBeforeAfter(null,
99                 index++,
100                 index++, ctx.getUnshareableResources(),
101                 ctx.getApplicationManagedSecurityResources(),
102                 ctx.getTrackedConnectionAssociator());
103
104         // Set ComponentContext BeforeAfter
105
if (enc != null) {
106             interceptor = new ComponentContextBeforeAfter(interceptor, index++, enc);
107         }
108
109         //Set a PolicyContext BeforeAfter
110
SecurityHolder securityHolder = ctx.getSecurityHolder();
111         if (securityHolder != null) {
112             if (securityHolder.getPolicyContextID() != null) {
113
114                 PolicyContext.setContextID(securityHolder.getPolicyContextID());
115
116                 /**
117                  * Register our default subject with the ContextManager
118                  */

119                 DefaultPrincipal defaultPrincipal = securityHolder.getDefaultPrincipal();
120                 if (defaultPrincipal != null) {
121                     defaultSubject = ConfigurationUtil.generateDefaultSubject(defaultPrincipal, ctx.getClassLoader());
122                     ContextManager.registerSubject(defaultSubject);
123                     SubjectId id = ContextManager.getSubjectId(defaultSubject);
124                     defaultSubject.getPrincipals().add(new IdentificationPrincipal(id));
125                 }
126
127                 interceptor = new PolicyContextBeforeAfter(interceptor, index++, index++, securityHolder.getPolicyContextID());
128             }
129         }
130
131         //Set the BeforeAfters as a valve
132
GeronimoBeforeAfterValve geronimoBAValve = new GeronimoBeforeAfterValve(interceptor, index);
133         addValve(geronimoBAValve);
134         beforeAfter = interceptor;
135         contextCount = index;
136
137         //Not clear if user defined valves should be involved in init processing. Probably not since
138
//request and response are null.
139

140         addValve(new SystemMethodValve());
141
142         // Add User Defined Valves
143
List JavaDoc valveChain = ctx.getValveChain();
144         if (valveChain != null) {
145             Iterator JavaDoc iterator = valveChain.iterator();
146             while (iterator.hasNext()) {
147                 Valve valve = (Valve) iterator.next();
148                 addValve(valve);
149             }
150         }
151
152 // CatalinaCluster cluster = ctx.getCluster();
153
// if (cluster != null)
154
// this.setCluster(cluster);
155

156         Manager manager = ctx.getManager();
157         if (manager != null)
158             this.setManager(manager);
159
160         pipelineInitialized = true;
161         this.webServiceMap = ctx.getWebServices();
162
163         this.setCrossContext(ctx.isCrossContext());
164
165         this.setCookies(!ctx.isDisableCookies());
166
167         //Set the Dispatch listener
168
this.addInstanceListener("org.apache.geronimo.tomcat.listener.DispatchListener");
169     }
170
171     public synchronized void start() throws LifecycleException {
172         if (pipelineInitialized) {
173             try {
174                 Valve valve = getFirst();
175                 valve.invoke(null, null);
176                 //Install the DefaultSubjectValve after the authentication valve so the default subject is supplied
177
//only if no real subject is authenticated.
178

179                 Valve defaultSubjectValve = new DefaultSubjectValve(defaultSubject);
180                 addValve(defaultSubjectValve);
181             } catch (IOException JavaDoc e) {
182                 if (e.getCause() instanceof LifecycleException) {
183                     throw (LifecycleException) e.getCause();
184                 }
185                 throw new LifecycleException(e);
186             } catch (ServletException JavaDoc e) {
187                 throw new LifecycleException(e);
188             }
189         } else
190             super.start();
191     }
192
193     public synchronized void stop() throws LifecycleException {
194         // Remove the defaultSubject
195
if (defaultSubject != null) {
196             ContextManager.unregisterSubject(defaultSubject);
197         }
198
199         super.stop();
200     }
201
202     public void addChild(Container child) {
203         Wrapper wrapper = (Wrapper) child;
204
205         String JavaDoc servletClassName = wrapper.getServletClass();
206         if (servletClassName == null) {
207             super.addChild(child);
208             return;
209         }
210
211         ClassLoader JavaDoc cl = this.getParentClassLoader();
212
213         Class JavaDoc baseServletClass;
214         Class JavaDoc servletClass;
215         try {
216             baseServletClass = cl.loadClass(Servlet JavaDoc.class.getName());
217             servletClass = cl.loadClass(servletClassName);
218             //Check if the servlet is of type Servlet class
219
if (!baseServletClass.isAssignableFrom(servletClass)) {
220                 //Nope - its probably a webservice, so lets see...
221
if (webServiceMap != null) {
222                     WebServiceContainer webServiceContainer = (WebServiceContainer) webServiceMap.get(wrapper.getName());
223
224                     if (webServiceContainer != null) {
225                         //Yep its a web service
226
//So swap it out with a POJOWebServiceServlet
227
wrapper.setServletClass("org.apache.geronimo.webservices.POJOWebServiceServlet");
228
229                         //Set the WebServiceContainer stuff
230
String JavaDoc webServicecontainerID = wrapper.getName() + WebServiceContainerInvoker.WEBSERVICE_CONTAINER + webServiceContainer.hashCode();
231                         getServletContext().setAttribute(webServicecontainerID, webServiceContainer);
232                         wrapper.addInitParameter(WebServiceContainerInvoker.WEBSERVICE_CONTAINER, webServicecontainerID);
233
234                         //Set the SEI Class in the attribute
235
String JavaDoc pojoClassID = wrapper.getName() + POJOWebServiceServlet.POJO_CLASS + servletClass.hashCode();
236                         getServletContext().setAttribute(pojoClassID, servletClass);
237                         wrapper.addInitParameter(POJOWebServiceServlet.POJO_CLASS, pojoClassID);
238                     }
239                 }
240             }
241         } catch (ClassNotFoundException JavaDoc e) {
242             throw new RuntimeException JavaDoc(e.getMessage(), e);
243         }
244
245         super.addChild(child);
246     }
247
248     public synchronized void setLoader(final Loader delegate) {
249         Loader loader = new Loader() {
250
251             public void backgroundProcess() {
252                 delegate.backgroundProcess();
253             }
254
255             public ClassLoader JavaDoc getClassLoader() {
256                 // Implementation Note: the actual CL to be used by this
257
// context is the Geronimo one and not the Tomcat one.
258
return parentClassLoader;
259             }
260
261             public Container getContainer() {
262                 return delegate.getContainer();
263             }
264
265             public void setContainer(Container container) {
266                 delegate.setContainer(container);
267             }
268
269             public boolean getDelegate() {
270                 return delegate.getDelegate();
271             }
272
273             public void setDelegate(boolean delegateBoolean) {
274                 delegate.setDelegate(delegateBoolean);
275             }
276
277             public String JavaDoc getInfo() {
278                 return delegate.getInfo();
279             }
280
281             public boolean getReloadable() {
282                 return false;
283             }
284
285             public void setReloadable(boolean reloadable) {
286                 if (reloadable) {
287                     throw new UnsupportedOperationException JavaDoc("Reloadable context is not supported.");
288                 }
289             }
290
291             public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
292                 delegate.addPropertyChangeListener(listener);
293             }
294
295             public void addRepository(String JavaDoc repository) {
296                 delegate.addRepository(repository);
297             }
298
299             public String JavaDoc[] findRepositories() {
300                 return delegate.findRepositories();
301             }
302
303             public boolean modified() {
304                 return delegate.modified();
305             }
306
307             public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
308                 delegate.removePropertyChangeListener(listener);
309             }
310         };
311
312         super.setLoader(loader);
313     }
314
315     private class SystemMethodValve extends ValveBase {
316
317         public void invoke(Request request, Response response) throws IOException JavaDoc, ServletException JavaDoc {
318             if (request == null && response == null) {
319                 try {
320                     GeronimoStandardContext.super.start();
321                 } catch (LifecycleException e) {
322                     throw (IOException JavaDoc) new IOException JavaDoc("wrapping lifecycle exception").initCause(e);
323                 }
324             } else {
325                 getNext().invoke(request, response);
326             }
327
328         }
329     }
330
331
332     public BeforeAfter getBeforeAfter() {
333         return beforeAfter;
334     }
335
336     public int getContextCount() {
337         return contextCount;
338     }
339
340 }
341
Popular Tags