KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > core > ApplicationContextFacade


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
19 package org.apache.catalina.core;
20
21
22 import java.io.InputStream JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.security.AccessController JavaDoc;
28 import java.security.PrivilegedActionException JavaDoc;
29 import java.security.PrivilegedExceptionAction JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import javax.servlet.RequestDispatcher JavaDoc;
35 import javax.servlet.Servlet JavaDoc;
36 import javax.servlet.ServletContext JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38
39 import org.apache.catalina.security.SecurityUtil;
40
41
42 /**
43  * Facade object which masks the internal <code>ApplicationContext</code>
44  * object from the web application.
45  *
46  * @author Remy Maucherat
47  * @author Jean-Francois Arcand
48  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
49  */

50
51 public final class ApplicationContextFacade
52     implements ServletContext JavaDoc {
53         
54     // ---------------------------------------------------------- Attributes
55
/**
56      * Cache Class object used for reflection.
57      */

58     private HashMap JavaDoc classCache;
59     
60     
61     /**
62      * Cache method object.
63      */

64     private HashMap JavaDoc objectCache;
65     
66     
67     // ----------------------------------------------------------- Constructors
68

69
70     /**
71      * Construct a new instance of this class, associated with the specified
72      * Context instance.
73      *
74      * @param context The associated Context instance
75      */

76     public ApplicationContextFacade(ApplicationContext context) {
77         super();
78         this.context = context;
79         
80         classCache = new HashMap JavaDoc();
81         objectCache = new HashMap JavaDoc();
82         initClassCache();
83     }
84     
85     
86     private void initClassCache(){
87         Class JavaDoc[] clazz = new Class JavaDoc[]{String JavaDoc.class};
88         classCache.put("getContext", clazz);
89         classCache.put("getMimeType", clazz);
90         classCache.put("getResourcePaths", clazz);
91         classCache.put("getResource", clazz);
92         classCache.put("getResourceAsStream", clazz);
93         classCache.put("getRequestDispatcher", clazz);
94         classCache.put("getNamedDispatcher", clazz);
95         classCache.put("getServlet", clazz);
96         classCache.put("getInitParameter", clazz);
97         classCache.put("setAttribute", new Class JavaDoc[]{String JavaDoc.class, Object JavaDoc.class});
98         classCache.put("removeAttribute", clazz);
99         classCache.put("getRealPath", clazz);
100         classCache.put("getAttribute", clazz);
101         classCache.put("log", clazz);
102     }
103
104
105     // ----------------------------------------------------- Instance Variables
106

107
108     /**
109      * Wrapped application context.
110      */

111     private ApplicationContext context = null;
112     
113
114
115     // ------------------------------------------------- ServletContext Methods
116

117
118     public ServletContext JavaDoc getContext(String JavaDoc uripath) {
119         ServletContext JavaDoc theContext = null;
120         if (SecurityUtil.isPackageProtectionEnabled()) {
121             theContext = (ServletContext JavaDoc)
122                 doPrivileged("getContext", new Object JavaDoc[]{uripath});
123         } else {
124             theContext = context.getContext(uripath);
125         }
126         if ((theContext != null) &&
127             (theContext instanceof ApplicationContext)){
128             theContext = ((ApplicationContext)theContext).getFacade();
129         }
130         return (theContext);
131     }
132
133
134     public int getMajorVersion() {
135         return context.getMajorVersion();
136     }
137
138
139     public int getMinorVersion() {
140         return context.getMinorVersion();
141     }
142
143
144     public String JavaDoc getMimeType(String JavaDoc file) {
145         if (SecurityUtil.isPackageProtectionEnabled()) {
146             return (String JavaDoc)doPrivileged("getMimeType", new Object JavaDoc[]{file});
147         } else {
148             return context.getMimeType(file);
149         }
150     }
151
152
153     public Set JavaDoc getResourcePaths(String JavaDoc path) {
154         if (SecurityUtil.isPackageProtectionEnabled()){
155             return (Set JavaDoc)doPrivileged("getResourcePaths", new Object JavaDoc[]{path});
156         } else {
157             return context.getResourcePaths(path);
158         }
159     }
160
161
162     public URL JavaDoc getResource(String JavaDoc path)
163         throws MalformedURLException JavaDoc {
164         if (System.getSecurityManager() != null) {
165             try {
166                 return (URL JavaDoc) invokeMethod(context, "getResource",
167                                           new Object JavaDoc[]{path});
168             } catch(Throwable JavaDoc t) {
169                 if (t instanceof MalformedURLException JavaDoc){
170                     throw (MalformedURLException JavaDoc)t;
171                 }
172                 return null;
173             }
174         } else {
175             return context.getResource(path);
176         }
177     }
178
179
180     public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
181         if (SecurityUtil.isPackageProtectionEnabled()) {
182             return (InputStream JavaDoc) doPrivileged("getResourceAsStream",
183                                               new Object JavaDoc[]{path});
184         } else {
185             return context.getResourceAsStream(path);
186         }
187     }
188
189
190     public RequestDispatcher JavaDoc getRequestDispatcher(final String JavaDoc path) {
191         if (SecurityUtil.isPackageProtectionEnabled()) {
192             return (RequestDispatcher JavaDoc) doPrivileged("getRequestDispatcher",
193                                                     new Object JavaDoc[]{path});
194         } else {
195             return context.getRequestDispatcher(path);
196         }
197     }
198
199
200     public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc name) {
201         if (SecurityUtil.isPackageProtectionEnabled()) {
202             return (RequestDispatcher JavaDoc) doPrivileged("getNamedDispatcher",
203                                                     new Object JavaDoc[]{name});
204         } else {
205             return context.getNamedDispatcher(name);
206         }
207     }
208
209
210     public Servlet JavaDoc getServlet(String JavaDoc name)
211         throws ServletException JavaDoc {
212         if (SecurityUtil.isPackageProtectionEnabled()) {
213             try {
214                 return (Servlet JavaDoc) invokeMethod(context, "getServlet",
215                                               new Object JavaDoc[]{name});
216             } catch (Throwable JavaDoc t) {
217                 if (t instanceof ServletException JavaDoc) {
218                     throw (ServletException JavaDoc) t;
219                 }
220                 return null;
221             }
222         } else {
223             return context.getServlet(name);
224         }
225     }
226
227
228     public Enumeration JavaDoc getServlets() {
229         if (SecurityUtil.isPackageProtectionEnabled()) {
230             return (Enumeration JavaDoc) doPrivileged("getServlets", null);
231         } else {
232             return context.getServlets();
233         }
234     }
235
236
237     public Enumeration JavaDoc getServletNames() {
238         if (SecurityUtil.isPackageProtectionEnabled()) {
239             return (Enumeration JavaDoc) doPrivileged("getServletNames", null);
240         } else {
241             return context.getServletNames();
242         }
243    }
244
245
246     public void log(String JavaDoc msg) {
247         if (SecurityUtil.isPackageProtectionEnabled()) {
248             doPrivileged("log", new Object JavaDoc[]{msg} );
249         } else {
250             context.log(msg);
251         }
252     }
253
254
255     public void log(Exception JavaDoc exception, String JavaDoc msg) {
256         if (SecurityUtil.isPackageProtectionEnabled()) {
257             doPrivileged("log", new Class JavaDoc[]{Exception JavaDoc.class, String JavaDoc.class},
258                          new Object JavaDoc[]{exception,msg});
259         } else {
260             context.log(exception, msg);
261         }
262     }
263
264
265     public void log(String JavaDoc message, Throwable JavaDoc throwable) {
266         if (SecurityUtil.isPackageProtectionEnabled()) {
267             doPrivileged("log", new Class JavaDoc[]{String JavaDoc.class, Throwable JavaDoc.class},
268                          new Object JavaDoc[]{message, throwable});
269         } else {
270             context.log(message, throwable);
271         }
272     }
273
274
275     public String JavaDoc getRealPath(String JavaDoc path) {
276         if (SecurityUtil.isPackageProtectionEnabled()) {
277             return (String JavaDoc) doPrivileged("getRealPath", new Object JavaDoc[]{path});
278         } else {
279             return context.getRealPath(path);
280         }
281     }
282
283
284     public String JavaDoc getServerInfo() {
285         if (SecurityUtil.isPackageProtectionEnabled()) {
286             return (String JavaDoc) doPrivileged("getServerInfo", null);
287         } else {
288             return context.getServerInfo();
289         }
290     }
291
292
293     public String JavaDoc getInitParameter(String JavaDoc name) {
294         if (SecurityUtil.isPackageProtectionEnabled()) {
295             return (String JavaDoc) doPrivileged("getInitParameter",
296                                          new Object JavaDoc[]{name});
297         } else {
298             return context.getInitParameter(name);
299         }
300     }
301
302
303     public Enumeration JavaDoc getInitParameterNames() {
304         if (SecurityUtil.isPackageProtectionEnabled()) {
305             return (Enumeration JavaDoc) doPrivileged("getInitParameterNames", null);
306         } else {
307             return context.getInitParameterNames();
308         }
309     }
310
311
312     public Object JavaDoc getAttribute(String JavaDoc name) {
313         if (SecurityUtil.isPackageProtectionEnabled()) {
314             return doPrivileged("getAttribute", new Object JavaDoc[]{name});
315         } else {
316             return context.getAttribute(name);
317         }
318      }
319
320
321     public Enumeration JavaDoc getAttributeNames() {
322         if (SecurityUtil.isPackageProtectionEnabled()) {
323             return (Enumeration JavaDoc) doPrivileged("getAttributeNames", null);
324         } else {
325             return context.getAttributeNames();
326         }
327     }
328
329
330     public void setAttribute(String JavaDoc name, Object JavaDoc object) {
331         if (SecurityUtil.isPackageProtectionEnabled()) {
332             doPrivileged("setAttribute", new Object JavaDoc[]{name,object});
333         } else {
334             context.setAttribute(name, object);
335         }
336     }
337
338
339     public void removeAttribute(String JavaDoc name) {
340         if (SecurityUtil.isPackageProtectionEnabled()) {
341             doPrivileged("removeAttribute", new Object JavaDoc[]{name});
342         } else {
343             context.removeAttribute(name);
344         }
345     }
346
347
348     public String JavaDoc getServletContextName() {
349         if (SecurityUtil.isPackageProtectionEnabled()) {
350             return (String JavaDoc) doPrivileged("getServletContextName", null);
351         } else {
352             return context.getServletContextName();
353         }
354     }
355
356        
357     public String JavaDoc getContextPath() {
358         if (SecurityUtil.isPackageProtectionEnabled()) {
359             return (String JavaDoc) doPrivileged("getContextPath", null);
360         } else {
361             return context.getContextPath();
362         }
363     }
364
365        
366     /**
367      * Use reflection to invoke the requested method. Cache the method object
368      * to speed up the process
369      * @param appContext The AppliationContext object on which the method
370      * will be invoked
371      * @param methodName The method to call.
372      * @param params The arguments passed to the called method.
373      */

374     private Object JavaDoc doPrivileged(ApplicationContext appContext,
375                                 final String JavaDoc methodName,
376                                 final Object JavaDoc[] params) {
377         try{
378             return invokeMethod(appContext, methodName, params );
379         } catch (Throwable JavaDoc t){
380             throw new RuntimeException JavaDoc(t.getMessage());
381         }
382
383     }
384
385
386     /**
387      * Use reflection to invoke the requested method. Cache the method object
388      * to speed up the process
389      * will be invoked
390      * @param methodName The method to call.
391      * @param params The arguments passed to the called method.
392      */

393     private Object JavaDoc doPrivileged(final String JavaDoc methodName, final Object JavaDoc[] params){
394         try{
395             return invokeMethod(context, methodName, params);
396         }catch(Throwable JavaDoc t){
397             throw new RuntimeException JavaDoc(t.getMessage());
398         }
399     }
400
401     
402     /**
403      * Use reflection to invoke the requested method. Cache the method object
404      * to speed up the process
405      * @param appContext The AppliationContext object on which the method
406      * will be invoked
407      * @param methodName The method to call.
408      * @param params The arguments passed to the called method.
409      */

410     private Object JavaDoc invokeMethod(ApplicationContext appContext,
411                                 final String JavaDoc methodName,
412                                 Object JavaDoc[] params)
413         throws Throwable JavaDoc{
414
415         try{
416             Method JavaDoc method = (Method JavaDoc)objectCache.get(methodName);
417             if (method == null){
418                 method = appContext.getClass()
419                     .getMethod(methodName, (Class JavaDoc[])classCache.get(methodName));
420                 objectCache.put(methodName, method);
421             }
422             
423             return executeMethod(method,appContext,params);
424         } catch (Exception JavaDoc ex){
425             handleException(ex, methodName);
426             return null;
427         } finally {
428             params = null;
429         }
430     }
431     
432     /**
433      * Use reflection to invoke the requested method. Cache the method object
434      * to speed up the process
435      * @param methodName The method to invoke.
436      * @param clazz The class where the method is.
437      * @param params The arguments passed to the called method.
438      */

439     private Object JavaDoc doPrivileged(final String JavaDoc methodName,
440                                 final Class JavaDoc[] clazz,
441                                 Object JavaDoc[] params){
442
443         try{
444             Method JavaDoc method = context.getClass()
445                     .getMethod(methodName, (Class JavaDoc[])clazz);
446             return executeMethod(method,context,params);
447         } catch (Exception JavaDoc ex){
448             try{
449                 handleException(ex, methodName);
450             }catch (Throwable JavaDoc t){
451                 throw new RuntimeException JavaDoc(t.getMessage());
452             }
453             return null;
454         } finally {
455             params = null;
456         }
457     }
458     
459     
460     /**
461      * Executes the method of the specified <code>ApplicationContext</code>
462      * @param method The method object to be invoked.
463      * @param context The AppliationContext object on which the method
464      * will be invoked
465      * @param params The arguments passed to the called method.
466      */

467     private Object JavaDoc executeMethod(final Method JavaDoc method,
468                                  final ApplicationContext context,
469                                  final Object JavaDoc[] params)
470             throws PrivilegedActionException JavaDoc,
471                    IllegalAccessException JavaDoc,
472                    InvocationTargetException JavaDoc {
473                                      
474         if (SecurityUtil.isPackageProtectionEnabled()){
475            return AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc(){
476                 public Object JavaDoc run() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc{
477                     return method.invoke(context, params);
478                 }
479             });
480         } else {
481             return method.invoke(context, params);
482         }
483     }
484
485     
486     /**
487      *
488      * Throw the real exception.
489      * @param ex The current exception
490      */

491     private void handleException(Exception JavaDoc ex, String JavaDoc methodName)
492         throws Throwable JavaDoc {
493
494         Throwable JavaDoc realException;
495         
496         if (ex instanceof PrivilegedActionException JavaDoc) {
497             ex = ((PrivilegedActionException JavaDoc) ex).getException();
498         }
499         
500         if (ex instanceof InvocationTargetException JavaDoc) {
501             realException =
502                 ((InvocationTargetException JavaDoc) ex).getTargetException();
503         } else {
504             realException = ex;
505         }
506         
507         throw realException;
508     }
509 }
510
Popular Tags