KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.Serializable JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import javax.naming.NamingException JavaDoc;
32 import javax.servlet.Filter JavaDoc;
33 import javax.servlet.FilterConfig JavaDoc;
34 import javax.servlet.ServletContext JavaDoc;
35 import javax.servlet.ServletException JavaDoc;
36
37 import org.apache.AnnotationProcessor;
38 import org.apache.catalina.Context;
39 import org.apache.catalina.deploy.FilterDef;
40 import org.apache.catalina.security.SecurityUtil;
41 import org.apache.catalina.util.Enumerator;
42 import org.apache.catalina.util.StringManager;
43 import org.apache.tomcat.util.log.SystemLogHandler;
44
45
46 /**
47  * Implementation of a <code>javax.servlet.FilterConfig</code> useful in
48  * managing the filter instances instantiated when a web application
49  * is first started.
50  *
51  * @author Craig R. McClanahan
52  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
53  */

54
55 final class ApplicationFilterConfig implements FilterConfig JavaDoc, Serializable JavaDoc {
56
57
58     protected static StringManager sm =
59         StringManager.getManager(Constants.Package);
60     
61     // ----------------------------------------------------------- Constructors
62

63
64     /**
65      * Construct a new ApplicationFilterConfig for the specified filter
66      * definition.
67      *
68      * @param context The context with which we are associated
69      * @param filterDef Filter definition for which a FilterConfig is to be
70      * constructed
71      *
72      * @exception ClassCastException if the specified class does not implement
73      * the <code>javax.servlet.Filter</code> interface
74      * @exception ClassNotFoundException if the filter class cannot be found
75      * @exception IllegalAccessException if the filter class cannot be
76      * publicly instantiated
77      * @exception InstantiationException if an exception occurs while
78      * instantiating the filter object
79      * @exception ServletException if thrown by the filter's init() method
80      * @throws NamingException
81      * @throws InvocationTargetException
82      */

83     public ApplicationFilterConfig(Context JavaDoc context, FilterDef filterDef)
84         throws ClassCastException JavaDoc, ClassNotFoundException JavaDoc,
85                IllegalAccessException JavaDoc, InstantiationException JavaDoc,
86                ServletException JavaDoc, InvocationTargetException JavaDoc, NamingException JavaDoc {
87
88         super();
89
90         if (restrictedFilters == null) {
91             restrictedFilters = new Properties JavaDoc();
92             try {
93                 InputStream JavaDoc is =
94                     this.getClass().getClassLoader().getResourceAsStream
95                         ("org/apache/catalina/core/RestrictedFilters.properties");
96                 if (is != null) {
97                     restrictedFilters.load(is);
98                 } else {
99                     context.getLogger().error(sm.getString("applicationFilterConfig.restrictedFiltersResources"));
100                 }
101             } catch (IOException JavaDoc e) {
102                 context.getLogger().error(sm.getString("applicationFilterConfig.restrictedServletsResources"), e);
103             }
104         }
105         
106         this.context = context;
107         setFilterDef(filterDef);
108
109     }
110
111
112     // ----------------------------------------------------- Instance Variables
113

114
115     /**
116      * The Context with which we are associated.
117      */

118     private Context JavaDoc context = null;
119
120
121     /**
122      * The application Filter we are configured for.
123      */

124     private transient Filter JavaDoc filter = null;
125
126
127     /**
128      * The <code>FilterDef</code> that defines our associated Filter.
129      */

130     private FilterDef filterDef = null;
131
132
133     /**
134      * Restricted filters (which can only be loaded by a privileged webapp).
135      */

136     protected static Properties JavaDoc restrictedFilters = null;
137
138     
139     // --------------------------------------------------- FilterConfig Methods
140

141
142     /**
143      * Return the name of the filter we are configuring.
144      */

145     public String JavaDoc getFilterName() {
146
147         return (filterDef.getFilterName());
148
149     }
150
151
152     /**
153      * Return a <code>String</code> containing the value of the named
154      * initialization parameter, or <code>null</code> if the parameter
155      * does not exist.
156      *
157      * @param name Name of the requested initialization parameter
158      */

159     public String JavaDoc getInitParameter(String JavaDoc name) {
160
161         Map JavaDoc map = filterDef.getParameterMap();
162         if (map == null)
163             return (null);
164         else
165             return ((String JavaDoc) map.get(name));
166
167     }
168
169
170     /**
171      * Return an <code>Enumeration</code> of the names of the initialization
172      * parameters for this Filter.
173      */

174     public Enumeration JavaDoc getInitParameterNames() {
175
176         Map JavaDoc map = filterDef.getParameterMap();
177         if (map == null)
178             return (new Enumerator(new ArrayList JavaDoc()));
179         else
180             return (new Enumerator(map.keySet()));
181
182     }
183
184
185     /**
186      * Return the ServletContext of our associated web application.
187      */

188     public ServletContext JavaDoc getServletContext() {
189
190         return (this.context.getServletContext());
191
192     }
193
194
195     /**
196      * Return a String representation of this object.
197      */

198     public String JavaDoc toString() {
199
200         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("ApplicationFilterConfig[");
201         sb.append("name=");
202         sb.append(filterDef.getFilterName());
203         sb.append(", filterClass=");
204         sb.append(filterDef.getFilterClass());
205         sb.append("]");
206         return (sb.toString());
207
208     }
209
210
211     // -------------------------------------------------------- Package Methods
212

213
214     /**
215      * Return the application Filter we are configured for.
216      *
217      * @exception ClassCastException if the specified class does not implement
218      * the <code>javax.servlet.Filter</code> interface
219      * @exception ClassNotFoundException if the filter class cannot be found
220      * @exception IllegalAccessException if the filter class cannot be
221      * publicly instantiated
222      * @exception InstantiationException if an exception occurs while
223      * instantiating the filter object
224      * @exception ServletException if thrown by the filter's init() method
225      * @throws NamingException
226      * @throws InvocationTargetException
227      */

228     Filter JavaDoc getFilter() throws ClassCastException JavaDoc, ClassNotFoundException JavaDoc,
229         IllegalAccessException JavaDoc, InstantiationException JavaDoc, ServletException JavaDoc,
230         InvocationTargetException JavaDoc, NamingException JavaDoc {
231
232         // Return the existing filter instance, if any
233
if (this.filter != null)
234             return (this.filter);
235
236         // Identify the class loader we will be using
237
String JavaDoc filterClass = filterDef.getFilterClass();
238         ClassLoader JavaDoc classLoader = null;
239         if (filterClass.startsWith("org.apache.catalina."))
240             classLoader = this.getClass().getClassLoader();
241         else
242             classLoader = context.getLoader().getClassLoader();
243
244         ClassLoader JavaDoc oldCtxClassLoader =
245             Thread.currentThread().getContextClassLoader();
246
247         // Instantiate a new instance of this filter and return it
248
Class JavaDoc clazz = classLoader.loadClass(filterClass);
249         if (!isFilterAllowed(clazz)) {
250             throw new SecurityException JavaDoc
251                 (sm.getString("applicationFilterConfig.privilegedFilter",
252                         filterClass));
253         }
254         this.filter = (Filter JavaDoc) clazz.newInstance();
255         if (!context.getIgnoreAnnotations()) {
256             if (context instanceof StandardContext) {
257                AnnotationProcessor processor = ((StandardContext)context).getAnnotationProcessor();
258                processor.processAnnotations(this.filter);
259                processor.postConstruct(this.filter);
260             }
261         }
262         if (context instanceof StandardContext &&
263             ((StandardContext) context).getSwallowOutput()) {
264             try {
265                 SystemLogHandler.startCapture();
266                 filter.init(this);
267             } finally {
268                 String JavaDoc log = SystemLogHandler.stopCapture();
269                 if (log != null && log.length() > 0) {
270                     getServletContext().log(log);
271                 }
272             }
273         } else {
274             filter.init(this);
275         }
276         return (this.filter);
277
278
279     }
280
281
282     /**
283      * Return the filter definition we are configured for.
284      */

285     FilterDef getFilterDef() {
286
287         return (this.filterDef);
288
289     }
290
291
292     /**
293      * Return <code>true</code> if loading this filter is allowed.
294      */

295     protected boolean isFilterAllowed(Class JavaDoc filterClass) {
296
297         // Privileged webapps may load all servlets without restriction
298
if (context.getPrivileged()) {
299             return true;
300         }
301
302         Class JavaDoc clazz = filterClass;
303         while (clazz != null && !clazz.getName().equals("javax.servlet.Filter")) {
304             if ("restricted".equals(restrictedFilters.getProperty(clazz.getName()))) {
305                 return (false);
306             }
307             clazz = clazz.getSuperclass();
308         }
309         
310         return (true);
311
312     }
313
314
315     /**
316      * Release the Filter instance associated with this FilterConfig,
317      * if there is one.
318      */

319     void release() {
320
321         if (this.filter != null)
322         {
323             if (System.getSecurityManager() != null) {
324                 try {
325                     SecurityUtil.doAsPrivilege("destroy", filter);
326                 } catch(java.lang.Exception JavaDoc ex){
327                     context.getLogger().error("ApplicationFilterConfig.doAsPrivilege", ex);
328                 }
329                 SecurityUtil.remove(filter);
330             } else {
331                 filter.destroy();
332             }
333             if (!context.getIgnoreAnnotations()) {
334                 try {
335                     ((StandardContext)context).getAnnotationProcessor().preDestroy(this.filter);
336                 } catch (Exception JavaDoc e) {
337                     context.getLogger().error("ApplicationFilterConfig.preDestroy", e);
338                 }
339             }
340         }
341         this.filter = null;
342
343      }
344
345
346     /**
347      * Set the filter definition we are configured for. This has the side
348      * effect of instantiating an instance of the corresponding filter class.
349      *
350      * @param filterDef The new filter definition
351      *
352      * @exception ClassCastException if the specified class does not implement
353      * the <code>javax.servlet.Filter</code> interface
354      * @exception ClassNotFoundException if the filter class cannot be found
355      * @exception IllegalAccessException if the filter class cannot be
356      * publicly instantiated
357      * @exception InstantiationException if an exception occurs while
358      * instantiating the filter object
359      * @exception ServletException if thrown by the filter's init() method
360      * @throws NamingException
361      * @throws InvocationTargetException
362      */

363     void setFilterDef(FilterDef filterDef)
364         throws ClassCastException JavaDoc, ClassNotFoundException JavaDoc,
365                IllegalAccessException JavaDoc, InstantiationException JavaDoc,
366                ServletException JavaDoc, InvocationTargetException JavaDoc, NamingException JavaDoc {
367
368         this.filterDef = filterDef;
369         if (filterDef == null) {
370
371             // Release any previously allocated filter instance
372
if (this.filter != null){
373                 if( System.getSecurityManager() != null) {
374                     try{
375                         SecurityUtil.doAsPrivilege("destroy", filter);
376                     } catch(java.lang.Exception JavaDoc ex){
377                         context.getLogger().error("ApplicationFilterConfig.doAsPrivilege", ex);
378                     }
379                     SecurityUtil.remove(filter);
380                 } else {
381                     filter.destroy();
382                 }
383                 if (!context.getIgnoreAnnotations()) {
384                     try {
385                         ((StandardContext)context).getAnnotationProcessor().preDestroy(this.filter);
386                     } catch (Exception JavaDoc e) {
387                         context.getLogger().error("ApplicationFilterConfig.preDestroy", e);
388                     }
389                 }
390             }
391             this.filter = null;
392
393         } else {
394
395             // Allocate a new filter instance
396
Filter JavaDoc filter = getFilter();
397
398         }
399
400     }
401
402
403     // -------------------------------------------------------- Private Methods
404

405
406 }
407
Popular Tags