KickJava   Java API By Example, From Geeks To Geeks.

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


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.FilterConfig JavaDoc;
27 import javax.servlet.ServletContext JavaDoc;
28
29 /**
30  * Wrapper around <code>FilterConfig</code> which overrides the
31  * <code>getServletContext()</code> method to return our own wrapper around
32  * <code>ServletContext</code>.
33  *
34  * @version $Id: FilterConfigWrapper.java,v 1.2 2004/10/24 01:30:23 felipeal Exp $
35  * @see ServletContext
36  */

37 public class FilterConfigWrapper implements FilterConfig JavaDoc
38 {
39     /**
40      * The original filter config object
41      */

42     private FilterConfig JavaDoc originalConfig;
43
44     /**
45      * List of parameters set using the <code>setInitParameter()</code> method.
46      */

47     private Hashtable JavaDoc initParameters;
48
49     /**
50      * Simulated name of the filter
51      */

52     private String JavaDoc filterName;
53
54     /**
55      * @param theOriginalConfig the original filter config object
56      */

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

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

80     public void setFilterName(String JavaDoc theFilterName)
81     {
82         this.filterName = theFilterName;
83     }
84
85     //--Overridden methods ----------------------------------------------------
86

87     /**
88      * @return the simulated filter's name if defined or the redirector
89      * filter's name
90      */

91     public String JavaDoc getFilterName()
92     {
93         if (this.filterName != null)
94         {
95             return this.filterName;
96         }
97
98         return this.originalConfig.getFilterName();
99     }
100
101     /**
102      * @return our own wrapped servlet context object
103      */

104     public ServletContext JavaDoc getServletContext()
105     {
106         return new ServletContextWrapper(
107             this.originalConfig.getServletContext());
108     }
109
110     /**
111      * Return the union of the parameters defined in the Redirector
112      * <code>web.xml</code> file and the one set using the
113      * <code>setInitParameter()</code> method. The parameters with the same
114      * name (and same case) are only returned once.
115      *
116      * @return the init parameters
117      */

118     public Enumeration JavaDoc getInitParameterNames()
119     {
120         Vector JavaDoc names = new Vector JavaDoc();
121
122         // Add parameters that were added using setInitParameter()
123
Enumeration JavaDoc en = this.initParameters.keys();
124
125         while (en.hasMoreElements())
126         {
127             String JavaDoc value = (String JavaDoc) en.nextElement();
128
129             names.add(value);
130         }
131
132         // Add parameters from web.xml
133
en = this.originalConfig.getInitParameterNames();
134
135         while (en.hasMoreElements())
136         {
137             String JavaDoc value = (String JavaDoc) en.nextElement();
138
139             if (!names.contains(value))
140             {
141                 names.add(value);
142             }
143         }
144
145         return names.elements();
146     }
147
148     /**
149      * @param theName the name of the parameter's value to return
150      * @return the value of the parameter, looking for it first in the list of
151      * parameters set using the <code>setInitParameter()</code> method
152      * and then in those set in <code>web.xml</code>.
153      */

154     public String JavaDoc getInitParameter(String JavaDoc theName)
155     {
156         // Look first in the list of parameters set using the
157
// setInitParameter() method.
158
String JavaDoc value = (String JavaDoc) this.initParameters.get(theName);
159
160         if (value == null)
161         {
162             value = this.originalConfig.getInitParameter(theName);
163         }
164
165         return value;
166     }
167 }
168
Popular Tags