1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.server;
22
23 import java.lang.reflect.Constructor;
24
25 import java.util.Enumeration;
26 import java.util.Hashtable;
27 import java.util.Vector;
28
29 import javax.servlet.ServletConfig;
30 import javax.servlet.ServletContext;
31
32 import org.apache.cactus.util.ChainedRuntimeException;
33
34 /**
35 * Abstract wrapper around <code>ServletConfig</code> which overrides the
36 * <code>getServletContext()</code> method to return our own wrapper around
37 * <code>ServletContext</code>. This class provides a common implementation
38 * of the wrapper for the different servlet API.
39 *
40 * @version $Id: AbstractServletConfigWrapper.java 292559 2005-09-29 21:36:43Z kenney $
41 */
42 public abstract class AbstractServletConfigWrapper
43 implements ServletConfig
44 {
45 /**
46 * The original servlet config object.
47 */
48 protected ServletConfig originalConfig;
49
50 /**
51 * List of parameters set using the <code>setInitParameter()</code> method.
52 */
53 protected Hashtable initParameters;
54
55 /**
56 * Simulated name of the servlet.
57 */
58 protected String servletName;
59
60 /**
61 * @param theOriginalConfig the original servlet config object
62 */
63 public AbstractServletConfigWrapper(ServletConfig theOriginalConfig)
64 {
65 this.originalConfig = theOriginalConfig;
66 this.initParameters = new Hashtable();
67 }
68
69 /**
70 * @param theOriginalConfig object
71 * @return AbstractServletConfigWrapper
72 */
73 public static AbstractServletConfigWrapper newInstance(
74 ServletConfig theOriginalConfig)
75 {
76 try
77 {
78 Class clazz = Class.forName(
79 "org.apache.cactus.server.ServletConfigWrapper");
80 Object[] args = new Object[] {theOriginalConfig};
81
82 Constructor constructor = clazz.getConstructor(new Class[] {
83 ServletConfig.class });
84
85 return (AbstractServletConfigWrapper) constructor.newInstance(args);
86 }
87 catch (Throwable t)
88 {
89 throw new ChainedRuntimeException(
90 "Failed to create ServletConfigWrapper", t);
91 }
92 }
93
94 /**
95 * Sets a parameter as if it were set in the <code>web.xml</code> file.
96 *
97 * @param theName the parameter's name
98 * @param theValue the parameter's value
99 */
100 public void setInitParameter(String theName, String theValue)
101 {
102 this.initParameters.put(theName, theValue);
103 }
104
105 /**
106 * Sets the servlet name. That will be the value returned by the
107 * <code>getServletName()</code> method.
108 *
109 * @param theServletName the servlet's name
110 */
111 public void setServletName(String theServletName)
112 {
113 this.servletName = theServletName;
114 }
115
116 /**
117 * @return the original unmodified config object
118 * @since 1.5
119 */
120 public ServletConfig getOriginalConfig()
121 {
122 return this.originalConfig;
123 }
124
125 //--Overridden methods ----------------------------------------------------
126
127 /**
128 * @return our own wrapped servlet context object
129 */
130 public ServletContext getServletContext()
131 {
132 return AbstractServletContextWrapper.newInstance(
133 this.originalConfig.getServletContext());
134 }
135
136 /**
137 * @param theName the name of the parameter's value to return
138 * @return the value of the parameter, looking for it first in the list of
139 * parameters set using the <code>setInitParameter()</code> method
140 * and then in those set in <code>web.xml</code>.
141 */
142 public String getInitParameter(String theName)
143 {
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 {
150 value = this.originalConfig.getInitParameter(theName);
151 }
152
153 return value;
154 }
155
156 /**
157 * @return the union of the parameters defined in the Redirector
158 * <code>web.xml</code> file and the one set using the
159 * <code>setInitParameter()</code> method.
160 */
161 public Enumeration getInitParameterNames()
162 {
163 Vector names = new Vector();
164
165 // Add parameters that were added using setInitParameter()
166 Enumeration en = this.initParameters.keys();
167
168 while (en.hasMoreElements())
169 {
170 String value = (String) en.nextElement();
171
172 names.add(value);
173 }
174
175 // Add parameters from web.xml
176 en = this.originalConfig.getInitParameterNames();
177
178 while (en.hasMoreElements())
179 {
180 String value = (String) en.nextElement();
181
182 // Do not add parameters that have been overriden by calling
183 // the setInitParameter() method.
184 if (!names.contains(value))
185 {
186 names.add(value);
187 }
188 }
189
190 return names.elements();
191 }
192
193 /**
194 * @return the simulated servlet's name if defined or the redirector
195 * servlet's name
196 */
197 public String getServletName()
198 {
199 if (this.servletName != null)
200 {
201 return this.servletName;
202 }
203
204 return this.originalConfig.getServletName();
205 }
206 }