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.ServletConfig;
027: import javax.servlet.ServletContext;
028:
029: /**
030: * Abstract wrapper around <code>ServletConfig</code> which overrides the
031: * <code>getServletContext()</code> method to return our own wrapper around
032: * <code>ServletContext</code>. This class provides a common implementation
033: * of the wrapper for the different servlet API.
034: *
035: * @version $Id: AbstractServletConfigWrapper.java 239054 2004-10-24 01:30:23Z felipeal $
036: */
037: public abstract class AbstractServletConfigWrapper implements
038: ServletConfig {
039: /**
040: * The original servlet config object
041: */
042: protected ServletConfig originalConfig;
043:
044: /**
045: * List of parameters set using the <code>setInitParameter()</code> method.
046: */
047: protected Hashtable initParameters;
048:
049: /**
050: * Simulated name of the servlet
051: */
052: protected String servletName;
053:
054: /**
055: * @param theOriginalConfig the original servlet config object
056: */
057: public AbstractServletConfigWrapper(ServletConfig theOriginalConfig) {
058: this .originalConfig = theOriginalConfig;
059: this .initParameters = new Hashtable();
060: }
061:
062: /**
063: * Sets a parameter as if it were set in the <code>web.xml</code> file.
064: *
065: * @param theName the parameter's name
066: * @param theValue the parameter's value
067: */
068: public void setInitParameter(String theName, String theValue) {
069: this .initParameters.put(theName, theValue);
070: }
071:
072: /**
073: * Sets the servlet name. That will be the value returned by the
074: * <code>getServletName()</code> method.
075: *
076: * @param theServletName the servlet's name
077: */
078: public void setServletName(String theServletName) {
079: this .servletName = theServletName;
080: }
081:
082: /**
083: * @return the original unmodified config object
084: * @since 1.5
085: */
086: public ServletConfig getOriginalConfig() {
087: return this .originalConfig;
088: }
089:
090: //--Overridden methods ----------------------------------------------------
091:
092: /**
093: * @return our own wrapped servlet context object
094: */
095: public ServletContext getServletContext() {
096: return new ServletContextWrapper(this .originalConfig
097: .getServletContext());
098: }
099:
100: /**
101: * @param theName the name of the parameter's value to return
102: * @return the value of the parameter, looking for it first in the list of
103: * parameters set using the <code>setInitParameter()</code> method
104: * and then in those set in <code>web.xml</code>.
105: */
106: public String getInitParameter(String theName) {
107: // Look first in the list of parameters set using the
108: // setInitParameter() method.
109: String value = (String) this .initParameters.get(theName);
110:
111: if (value == null) {
112: value = this .originalConfig.getInitParameter(theName);
113: }
114:
115: return value;
116: }
117:
118: /**
119: * @return the union of the parameters defined in the Redirector
120: * <code>web.xml</code> file and the one set using the
121: * <code>setInitParameter()</code> method.
122: */
123: public Enumeration getInitParameterNames() {
124: Vector names = new Vector();
125:
126: // Add parameters that were added using setInitParameter()
127: Enumeration en = this .initParameters.keys();
128:
129: while (en.hasMoreElements()) {
130: String value = (String) en.nextElement();
131:
132: names.add(value);
133: }
134:
135: // Add parameters from web.xml
136: en = this .originalConfig.getInitParameterNames();
137:
138: while (en.hasMoreElements()) {
139: String value = (String) en.nextElement();
140:
141: // Do not add parameters that have been overriden by calling
142: // the setInitParameter() method.
143: if (!names.contains(value)) {
144: names.add(value);
145: }
146: }
147:
148: return names.elements();
149: }
150:
151: /**
152: * @return the simulated servlet's name if defined or the redirector
153: * servlet's name
154: */
155: public String getServletName() {
156: if (this.servletName != null) {
157: return this.servletName;
158: }
159:
160: return this.originalConfig.getServletName();
161: }
162: }
|