001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/ApplicationFilterConfig.java,v 1.7 2001/07/22 20:25:08 pier Exp $
003: * $Revision: 1.7 $
004: * $Date: 2001/07/22 20:25:08 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.core;
065:
066: import java.util.ArrayList;
067: import java.util.Enumeration;
068: import java.util.Iterator;
069: import java.util.Map;
070: import javax.servlet.Filter;
071: import javax.servlet.FilterConfig;
072: import javax.servlet.ServletContext;
073: import javax.servlet.ServletException;
074: import org.apache.catalina.Context;
075: import org.apache.catalina.deploy.FilterDef;
076: import org.apache.catalina.util.Enumerator;
077:
078: /**
079: * Implementation of a <code>javax.servlet.FilterConfig</code> useful in
080: * managing the filter instances instantiated when a web application
081: * is first started.
082: *
083: * @author Craig R. McClanahan
084: * @version $Revision: 1.7 $ $Date: 2001/07/22 20:25:08 $
085: */
086:
087: final class ApplicationFilterConfig implements FilterConfig {
088:
089: // ----------------------------------------------------------- Constructors
090:
091: /**
092: * Construct a new ApplicationFilterConfig for the specified filter
093: * definition.
094: *
095: * @param context The context with which we are associated
096: * @param filterDef Filter definition for which a FilterConfig is to be
097: * constructed
098: *
099: * @exception ClassCastException if the specified class does not implement
100: * the <code>javax.servlet.Filter</code> interface
101: * @exception ClassNotFoundException if the filter class cannot be found
102: * @exception IllegalAccessException if the filter class cannot be
103: * publicly instantiated
104: * @exception InstantiationException if an exception occurs while
105: * instantiating the filter object
106: * @exception ServletException if thrown by the filter's init() method
107: */
108: public ApplicationFilterConfig(Context context, FilterDef filterDef)
109: throws ClassCastException, ClassNotFoundException,
110: IllegalAccessException, InstantiationException,
111: ServletException {
112:
113: super ();
114: this .context = context;
115: setFilterDef(filterDef);
116:
117: }
118:
119: // ----------------------------------------------------- Instance Variables
120:
121: /**
122: * The Context with which we are associated.
123: */
124: private Context context = null;
125:
126: /**
127: * The application Filter we are configured for.
128: */
129: private Filter filter = null;
130:
131: /**
132: * The <code>FilterDef</code> that defines our associated Filter.
133: */
134: private FilterDef filterDef = null;
135:
136: // --------------------------------------------------- FilterConfig Methods
137:
138: /**
139: * Return the name of the filter we are configuring.
140: */
141: public String getFilterName() {
142:
143: return (filterDef.getFilterName());
144:
145: }
146:
147: /**
148: * Return a <code>String</code> containing the value of the named
149: * initialization parameter, or <code>null</code> if the parameter
150: * does not exist.
151: *
152: * @param name Name of the requested initialization parameter
153: */
154: public String getInitParameter(String name) {
155:
156: Map map = filterDef.getParameterMap();
157: if (map == null)
158: return (null);
159: else
160: return ((String) map.get(name));
161:
162: }
163:
164: /**
165: * Return an <code>Enumeration</code> of the names of the initialization
166: * parameters for this Filter.
167: */
168: public Enumeration getInitParameterNames() {
169:
170: Map map = filterDef.getParameterMap();
171: if (map == null)
172: return (new Enumerator(new ArrayList()));
173: else
174: return (new Enumerator(map.keySet()));
175:
176: }
177:
178: /**
179: * Return the ServletContext of our associated web application.
180: */
181: public ServletContext getServletContext() {
182:
183: return (this .context.getServletContext());
184:
185: }
186:
187: /**
188: * Return a String representation of this object.
189: */
190: public String toString() {
191:
192: StringBuffer sb = new StringBuffer("ApplicationFilterConfig[");
193: sb.append("name=");
194: sb.append(filterDef.getFilterName());
195: sb.append(", filterClass=");
196: sb.append(filterDef.getFilterClass());
197: sb.append("]");
198: return (sb.toString());
199:
200: }
201:
202: // -------------------------------------------------------- Package Methods
203:
204: /**
205: * Return the application Filter we are configured for.
206: *
207: * @exception ClassCastException if the specified class does not implement
208: * the <code>javax.servlet.Filter</code> interface
209: * @exception ClassNotFoundException if the filter class cannot be found
210: * @exception IllegalAccessException if the filter class cannot be
211: * publicly instantiated
212: * @exception InstantiationException if an exception occurs while
213: * instantiating the filter object
214: * @exception ServletException if thrown by the filter's init() method
215: */
216: Filter getFilter() throws ClassCastException,
217: ClassNotFoundException, IllegalAccessException,
218: InstantiationException, ServletException {
219:
220: // Return the existing filter instance, if any
221: if (this .filter != null)
222: return (this .filter);
223:
224: // Identify the class loader we will be using
225: String filterClass = filterDef.getFilterClass();
226: ClassLoader classLoader = null;
227: if (filterClass.startsWith("org.apache.catalina."))
228: classLoader = this .getClass().getClassLoader();
229: else
230: classLoader = context.getLoader().getClassLoader();
231:
232: ClassLoader oldCtxClassLoader = Thread.currentThread()
233: .getContextClassLoader();
234:
235: // Instantiate a new instance of this filter and return it
236: Class clazz = classLoader.loadClass(filterClass);
237: this .filter = (Filter) clazz.newInstance();
238: filter.init(this );
239: return (this .filter);
240:
241: }
242:
243: /**
244: * Return the filter definition we are configured for.
245: */
246: FilterDef getFilterDef() {
247:
248: return (this .filterDef);
249:
250: }
251:
252: /**
253: * Release the Filter instance associated with this FilterConfig,
254: * if there is one.
255: */
256: void release() {
257:
258: if (this .filter != null)
259: filter.destroy();
260: this .filter = null;
261:
262: }
263:
264: /**
265: * Set the filter definition we are configured for. This has the side
266: * effect of instantiating an instance of the corresponding filter class.
267: *
268: * @param filterDef The new filter definition
269: *
270: * @exception ClassCastException if the specified class does not implement
271: * the <code>javax.servlet.Filter</code> interface
272: * @exception ClassNotFoundException if the filter class cannot be found
273: * @exception IllegalAccessException if the filter class cannot be
274: * publicly instantiated
275: * @exception InstantiationException if an exception occurs while
276: * instantiating the filter object
277: * @exception ServletException if thrown by the filter's init() method
278: */
279: void setFilterDef(FilterDef filterDef) throws ClassCastException,
280: ClassNotFoundException, IllegalAccessException,
281: InstantiationException, ServletException {
282:
283: this .filterDef = filterDef;
284: if (filterDef == null) {
285:
286: // Release any previously allocated filter instance
287: if (this .filter != null)
288: this .filter.destroy();
289: this .filter = null;
290:
291: } else {
292:
293: // Allocate a new filter instance
294: Filter filter = getFilter();
295:
296: }
297:
298: }
299:
300: // -------------------------------------------------------- Private Methods
301:
302: }
|