KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > server > AbstractServletConfigWrapper


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.server;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import javax.servlet.ServletConfig JavaDoc;
27 import javax.servlet.ServletContext JavaDoc;
28
29 /**
30  * Abstract wrapper around <code>ServletConfig</code> which overrides the
31  * <code>getServletContext()</code> method to return our own wrapper around
32  * <code>ServletContext</code>. This class provides a common implementation
33  * of the wrapper for the different servlet API.
34  *
35  * @version $Id: AbstractServletConfigWrapper.java,v 1.2 2004/10/24 01:30:23 felipeal Exp $
36  */

37 public abstract class AbstractServletConfigWrapper
38     implements ServletConfig JavaDoc
39 {
40     /**
41      * The original servlet config object
42      */

43     protected ServletConfig JavaDoc originalConfig;
44
45     /**
46      * List of parameters set using the <code>setInitParameter()</code> method.
47      */

48     protected Hashtable JavaDoc initParameters;
49
50     /**
51      * Simulated name of the servlet
52      */

53     protected String JavaDoc servletName;
54
55     /**
56      * @param theOriginalConfig the original servlet config object
57      */

58     public AbstractServletConfigWrapper(ServletConfig JavaDoc theOriginalConfig)
59     {
60         this.originalConfig = theOriginalConfig;
61         this.initParameters = new Hashtable JavaDoc();
62     }
63
64     /**
65      * Sets a parameter as if it were set in the <code>web.xml</code> file.
66      *
67      * @param theName the parameter's name
68      * @param theValue the parameter's value
69      */

70     public void setInitParameter(String JavaDoc theName, String JavaDoc theValue)
71     {
72         this.initParameters.put(theName, theValue);
73     }
74
75     /**
76      * Sets the servlet name. That will be the value returned by the
77      * <code>getServletName()</code> method.
78      *
79      * @param theServletName the servlet's name
80      */

81     public void setServletName(String JavaDoc theServletName)
82     {
83         this.servletName = theServletName;
84     }
85
86     /**
87      * @return the original unmodified config object
88      * @since 1.5
89      */

90     public ServletConfig JavaDoc getOriginalConfig()
91     {
92         return this.originalConfig;
93     }
94
95     //--Overridden methods ----------------------------------------------------
96

97     /**
98      * @return our own wrapped servlet context object
99      */

100     public ServletContext JavaDoc getServletContext()
101     {
102         return new ServletContextWrapper(
103             this.originalConfig.getServletContext());
104     }
105
106     /**
107      * @param theName the name of the parameter's value to return
108      * @return the value of the parameter, looking for it first in the list of
109      * parameters set using the <code>setInitParameter()</code> method
110      * and then in those set in <code>web.xml</code>.
111      */

112     public String JavaDoc getInitParameter(String JavaDoc theName)
113     {
114         // Look first in the list of parameters set using the
115
// setInitParameter() method.
116
String JavaDoc value = (String JavaDoc) this.initParameters.get(theName);
117
118         if (value == null)
119         {
120             value = this.originalConfig.getInitParameter(theName);
121         }
122
123         return value;
124     }
125
126     /**
127      * @return the union of the parameters defined in the Redirector
128      * <code>web.xml</code> file and the one set using the
129      * <code>setInitParameter()</code> method.
130      */

131     public Enumeration JavaDoc getInitParameterNames()
132     {
133         Vector JavaDoc names = new Vector JavaDoc();
134
135         // Add parameters that were added using setInitParameter()
136
Enumeration JavaDoc en = this.initParameters.keys();
137
138         while (en.hasMoreElements())
139         {
140             String JavaDoc value = (String JavaDoc) en.nextElement();
141
142             names.add(value);
143         }
144
145         // Add parameters from web.xml
146
en = this.originalConfig.getInitParameterNames();
147
148         while (en.hasMoreElements())
149         {
150             String JavaDoc value = (String JavaDoc) en.nextElement();
151
152             // Do not add parameters that have been overriden by calling
153
// the setInitParameter() method.
154
if (!names.contains(value))
155             {
156                 names.add(value);
157             }
158         }
159
160         return names.elements();
161     }
162
163     /**
164      * @return the simulated servlet's name if defined or the redirector
165      * servlet's name
166      */

167     public String JavaDoc getServletName()
168     {
169         if (this.servletName != null)
170         {
171             return this.servletName;
172         }
173
174         return this.originalConfig.getServletName();
175     }
176 }
177
Popular Tags