001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.server;
021:
022: import java.util.Enumeration;
023: import java.util.Hashtable;
024: import java.util.Vector;
025:
026: import javax.servlet.FilterConfig;
027: import javax.servlet.ServletContext;
028:
029: /**
030: * Wrapper around <code>FilterConfig</code> which overrides the
031: * <code>getServletContext()</code> method to return our own wrapper around
032: * <code>ServletContext</code>.
033: *
034: * @version $Id: FilterConfigWrapper.java 239054 2004-10-24 01:30:23Z felipeal $
035: * @see ServletContext
036: */
037: public class FilterConfigWrapper implements FilterConfig {
038: /**
039: * The original filter config object
040: */
041: private FilterConfig originalConfig;
042:
043: /**
044: * List of parameters set using the <code>setInitParameter()</code> method.
045: */
046: private Hashtable initParameters;
047:
048: /**
049: * Simulated name of the filter
050: */
051: private String filterName;
052:
053: /**
054: * @param theOriginalConfig the original filter config object
055: */
056: public FilterConfigWrapper(FilterConfig theOriginalConfig) {
057: this .originalConfig = theOriginalConfig;
058: this .initParameters = new Hashtable();
059: }
060:
061: /**
062: * Sets a parameter as if it were set in the <code>web.xml</code> file.
063: *
064: * @param theName the parameter's name
065: * @param theValue the parameter's value
066: */
067: public void setInitParameter(String theName, String theValue) {
068: this .initParameters.put(theName, theValue);
069: }
070:
071: /**
072: * Sets the filter name. That will be the value returned by the
073: * <code>getFilterName()</code> method.
074: *
075: * @param theFilterName the filter name
076: */
077: public void setFilterName(String theFilterName) {
078: this .filterName = theFilterName;
079: }
080:
081: //--Overridden methods ----------------------------------------------------
082:
083: /**
084: * @return the simulated filter's name if defined or the redirector
085: * filter's name
086: */
087: public String getFilterName() {
088: if (this .filterName != null) {
089: return this .filterName;
090: }
091:
092: return this .originalConfig.getFilterName();
093: }
094:
095: /**
096: * @return our own wrapped servlet context object
097: */
098: public ServletContext getServletContext() {
099: return new ServletContextWrapper(this .originalConfig
100: .getServletContext());
101: }
102:
103: /**
104: * Return the union of the parameters defined in the Redirector
105: * <code>web.xml</code> file and the one set using the
106: * <code>setInitParameter()</code> method. The parameters with the same
107: * name (and same case) are only returned once.
108: *
109: * @return the init parameters
110: */
111: public Enumeration getInitParameterNames() {
112: Vector names = new Vector();
113:
114: // Add parameters that were added using setInitParameter()
115: Enumeration en = this .initParameters.keys();
116:
117: while (en.hasMoreElements()) {
118: String value = (String) en.nextElement();
119:
120: names.add(value);
121: }
122:
123: // Add parameters from web.xml
124: en = this .originalConfig.getInitParameterNames();
125:
126: while (en.hasMoreElements()) {
127: String value = (String) en.nextElement();
128:
129: if (!names.contains(value)) {
130: names.add(value);
131: }
132: }
133:
134: return names.elements();
135: }
136:
137: /**
138: * @param theName the name of the parameter's value to return
139: * @return the value of the parameter, looking for it first in the list of
140: * parameters set using the <code>setInitParameter()</code> method
141: * and then in those set in <code>web.xml</code>.
142: */
143: public String getInitParameter(String theName) {
144: // Look first in the list of parameters set using the
145: // setInitParameter() method.
146: String value = (String) this.initParameters.get(theName);
147:
148: if (value == null) {
149: value = this.originalConfig.getInitParameter(theName);
150: }
151:
152: return value;
153: }
154: }
|