001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.core;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.Enumeration;
022: import java.util.Map;
023:
024: import javax.servlet.Filter;
025: import javax.servlet.FilterConfig;
026: import javax.servlet.ServletContext;
027: import javax.servlet.ServletException;
028:
029: import org.apache.catalina.Context;
030: import org.apache.catalina.deploy.FilterDef;
031: import org.apache.catalina.security.SecurityUtil;
032: import org.apache.catalina.util.Enumerator;
033: import org.apache.tomcat.util.log.SystemLogHandler;
034:
035: /**
036: * Implementation of a <code>javax.servlet.FilterConfig</code> useful in
037: * managing the filter instances instantiated when a web application
038: * is first started.
039: *
040: * @author Craig R. McClanahan
041: * @version $Revision: 1.8 $ $Date: 2004/02/27 14:58:42 $
042: */
043:
044: final class ApplicationFilterConfig implements FilterConfig,
045: Serializable {
046:
047: private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
048: .getLog(ApplicationFilterConfig.class);
049:
050: // ----------------------------------------------------------- Constructors
051:
052: /**
053: * Construct a new ApplicationFilterConfig for the specified filter
054: * definition.
055: *
056: * @param context The context with which we are associated
057: * @param filterDef Filter definition for which a FilterConfig is to be
058: * constructed
059: *
060: * @exception ClassCastException if the specified class does not implement
061: * the <code>javax.servlet.Filter</code> interface
062: * @exception ClassNotFoundException if the filter class cannot be found
063: * @exception IllegalAccessException if the filter class cannot be
064: * publicly instantiated
065: * @exception InstantiationException if an exception occurs while
066: * instantiating the filter object
067: * @exception ServletException if thrown by the filter's init() method
068: */
069: public ApplicationFilterConfig(Context context, FilterDef filterDef)
070: throws ClassCastException, ClassNotFoundException,
071: IllegalAccessException, InstantiationException,
072: ServletException {
073:
074: super ();
075: this .context = context;
076: setFilterDef(filterDef);
077:
078: }
079:
080: // ----------------------------------------------------- Instance Variables
081:
082: /**
083: * The Context with which we are associated.
084: */
085: private Context context = null;
086:
087: /**
088: * The application Filter we are configured for.
089: */
090: private transient Filter filter = null;
091:
092: /**
093: * The <code>FilterDef</code> that defines our associated Filter.
094: */
095: private FilterDef filterDef = null;
096:
097: // --------------------------------------------------- FilterConfig Methods
098:
099: /**
100: * Return the name of the filter we are configuring.
101: */
102: public String getFilterName() {
103:
104: return (filterDef.getFilterName());
105:
106: }
107:
108: /**
109: * Return a <code>String</code> containing the value of the named
110: * initialization parameter, or <code>null</code> if the parameter
111: * does not exist.
112: *
113: * @param name Name of the requested initialization parameter
114: */
115: public String getInitParameter(String name) {
116:
117: Map map = filterDef.getParameterMap();
118: if (map == null)
119: return (null);
120: else
121: return ((String) map.get(name));
122:
123: }
124:
125: /**
126: * Return an <code>Enumeration</code> of the names of the initialization
127: * parameters for this Filter.
128: */
129: public Enumeration getInitParameterNames() {
130:
131: Map map = filterDef.getParameterMap();
132: if (map == null)
133: return (new Enumerator(new ArrayList()));
134: else
135: return (new Enumerator(map.keySet()));
136:
137: }
138:
139: /**
140: * Return the ServletContext of our associated web application.
141: */
142: public ServletContext getServletContext() {
143:
144: return (this .context.getServletContext());
145:
146: }
147:
148: /**
149: * Return a String representation of this object.
150: */
151: public String toString() {
152:
153: StringBuffer sb = new StringBuffer("ApplicationFilterConfig[");
154: sb.append("name=");
155: sb.append(filterDef.getFilterName());
156: sb.append(", filterClass=");
157: sb.append(filterDef.getFilterClass());
158: sb.append("]");
159: return (sb.toString());
160:
161: }
162:
163: // -------------------------------------------------------- Package Methods
164:
165: /**
166: * Return the application Filter we are configured for.
167: *
168: * @exception ClassCastException if the specified class does not implement
169: * the <code>javax.servlet.Filter</code> interface
170: * @exception ClassNotFoundException if the filter class cannot be found
171: * @exception IllegalAccessException if the filter class cannot be
172: * publicly instantiated
173: * @exception InstantiationException if an exception occurs while
174: * instantiating the filter object
175: * @exception ServletException if thrown by the filter's init() method
176: */
177: Filter getFilter() throws ClassCastException,
178: ClassNotFoundException, IllegalAccessException,
179: InstantiationException, ServletException {
180:
181: // Return the existing filter instance, if any
182: if (this .filter != null)
183: return (this .filter);
184:
185: // Identify the class loader we will be using
186: String filterClass = filterDef.getFilterClass();
187: ClassLoader classLoader = null;
188: if (filterClass.startsWith("org.apache.catalina."))
189: classLoader = this .getClass().getClassLoader();
190: else
191: classLoader = context.getLoader().getClassLoader();
192:
193: ClassLoader oldCtxClassLoader = Thread.currentThread()
194: .getContextClassLoader();
195:
196: // Instantiate a new instance of this filter and return it
197: Class clazz = classLoader.loadClass(filterClass);
198: this .filter = (Filter) clazz.newInstance();
199: if (context instanceof StandardContext
200: && ((StandardContext) context).getSwallowOutput()) {
201: try {
202: SystemLogHandler.startCapture();
203: filter.init(this );
204: } finally {
205: String log = SystemLogHandler.stopCapture();
206: if (log != null && log.length() > 0) {
207: getServletContext().log(log);
208: }
209: }
210: } else {
211: filter.init(this );
212: }
213: return (this .filter);
214:
215: }
216:
217: /**
218: * Return the filter definition we are configured for.
219: */
220: FilterDef getFilterDef() {
221:
222: return (this .filterDef);
223:
224: }
225:
226: /**
227: * Release the Filter instance associated with this FilterConfig,
228: * if there is one.
229: */
230: void release() {
231:
232: if (this .filter != null) {
233: if (System.getSecurityManager() != null) {
234: try {
235: SecurityUtil.doAsPrivilege("destroy", filter);
236: SecurityUtil.remove(filter);
237: } catch (java.lang.Exception ex) {
238: log.error("ApplicationFilterConfig.doAsPrivilege",
239: ex);
240: }
241: } else {
242: filter.destroy();
243: }
244: }
245: this .filter = null;
246:
247: }
248:
249: /**
250: * Set the filter definition we are configured for. This has the side
251: * effect of instantiating an instance of the corresponding filter class.
252: *
253: * @param filterDef The new filter definition
254: *
255: * @exception ClassCastException if the specified class does not implement
256: * the <code>javax.servlet.Filter</code> interface
257: * @exception ClassNotFoundException if the filter class cannot be found
258: * @exception IllegalAccessException if the filter class cannot be
259: * publicly instantiated
260: * @exception InstantiationException if an exception occurs while
261: * instantiating the filter object
262: * @exception ServletException if thrown by the filter's init() method
263: */
264: void setFilterDef(FilterDef filterDef) throws ClassCastException,
265: ClassNotFoundException, IllegalAccessException,
266: InstantiationException, ServletException {
267:
268: this .filterDef = filterDef;
269: if (filterDef == null) {
270:
271: // Release any previously allocated filter instance
272: if (this .filter != null) {
273: if (System.getSecurityManager() != null) {
274: try {
275: SecurityUtil.doAsPrivilege("destroy", filter);
276: SecurityUtil.remove(filter);
277: } catch (java.lang.Exception ex) {
278: log
279: .error(
280: "ApplicationFilterConfig.doAsPrivilege",
281: ex);
282: }
283: } else {
284: filter.destroy();
285: }
286: }
287: this .filter = null;
288:
289: } else {
290:
291: // Allocate a new filter instance
292: Filter filter = getFilter();
293:
294: }
295:
296: }
297:
298: // -------------------------------------------------------- Private Methods
299:
300: }
|