This project has retired. For details please refer to its
Attic page.
ServletContextAdapter xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.web.util;
22
23 import java.io.InputStream;
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.util.Enumeration;
27 import java.util.Hashtable;
28 import java.util.Set;
29
30 import javax.servlet.RequestDispatcher;
31 import javax.servlet.Servlet;
32 import javax.servlet.ServletConfig;
33 import javax.servlet.ServletContext;
34 import javax.servlet.ServletException;
35
36 /***
37 * Adapts a servlet config and a servlet context to become a unique servlet
38 * context.
39 *
40 * @version $Rev: 674918 $ $Date: 2008-07-08 21:10:14 +0200 (mar, 08 lug 2008) $
41 */
42 @SuppressWarnings("deprecation")
43 public class ServletContextAdapter implements ServletContext {
44
45 /***
46 * The root context to use.
47 */
48 private ServletContext rootContext;
49
50 /***
51 * The union of init parameters of {@link ServletConfig} and
52 * {@link ServletContext}.
53 */
54 private Hashtable<String, String> initParameters;
55
56
57 /***
58 * Constructor.
59 *
60 * @param config The servlet configuration object.
61 */
62 @SuppressWarnings("unchecked")
63 public ServletContextAdapter(ServletConfig config) {
64 this.rootContext = config.getServletContext();
65 initParameters = new Hashtable<String, String>();
66 Enumeration<String> enumeration = rootContext
67 .getInitParameterNames();
68 while (enumeration.hasMoreElements()) {
69 String paramName = enumeration.nextElement();
70 initParameters.put(paramName, rootContext
71 .getInitParameter(paramName));
72 }
73 enumeration = config.getInitParameterNames();
74 while (enumeration.hasMoreElements()) {
75 String paramName = enumeration.nextElement();
76 initParameters.put(paramName, config.getInitParameter(paramName));
77 }
78 }
79
80 /*** {@inheritDoc} */
81 public ServletContext getContext(String string) {
82 return rootContext.getContext(string);
83 }
84
85 /*** {@inheritDoc} */
86 public int getMajorVersion() {
87 return rootContext.getMajorVersion();
88 }
89
90 /*** {@inheritDoc} */
91 public int getMinorVersion() {
92 return rootContext.getMinorVersion();
93 }
94
95 /*** {@inheritDoc} */
96 public String getMimeType(String string) {
97 return rootContext.getMimeType(string);
98 }
99
100 /*** {@inheritDoc} */
101 @SuppressWarnings("unchecked")
102 public Set getResourcePaths(String string) {
103 return rootContext.getResourcePaths(string);
104 }
105
106 /*** {@inheritDoc} */
107 public URL getResource(String string) throws MalformedURLException {
108 return rootContext.getResource(string);
109 }
110
111 /*** {@inheritDoc} */
112 public InputStream getResourceAsStream(String string) {
113 return rootContext.getResourceAsStream(string);
114 }
115
116 /*** {@inheritDoc} */
117 public RequestDispatcher getRequestDispatcher(String string) {
118 return rootContext.getRequestDispatcher(string);
119 }
120
121 /*** {@inheritDoc} */
122 public RequestDispatcher getNamedDispatcher(String string) {
123 return rootContext.getNamedDispatcher(string);
124 }
125
126 /*** {@inheritDoc} */
127 public Servlet getServlet(String string) throws ServletException {
128 return rootContext.getServlet(string);
129 }
130
131 /*** {@inheritDoc} */
132 @SuppressWarnings({ "unchecked" })
133 public Enumeration getServlets() {
134 return rootContext.getServlets();
135 }
136
137 /*** {@inheritDoc} */
138 @SuppressWarnings({ "unchecked" })
139 public Enumeration getServletNames() {
140 return rootContext.getServletNames();
141 }
142
143 /*** {@inheritDoc} */
144 public void log(String string) {
145 rootContext.log(string);
146 }
147
148 /*** {@inheritDoc} */
149 public void log(Exception exception, String string) {
150 rootContext.log(exception, string);
151 }
152
153 /*** {@inheritDoc} */
154 public void log(String string, Throwable throwable) {
155 rootContext.log(string, throwable);
156 }
157
158 /*** {@inheritDoc} */
159 public String getRealPath(String string) {
160 return rootContext.getRealPath(string);
161 }
162
163 /*** {@inheritDoc} */
164 public String getServerInfo() {
165 return rootContext.getServerInfo();
166 }
167
168 /*** {@inheritDoc} */
169 public String getInitParameter(String string) {
170 return initParameters.get(string);
171 }
172
173 /*** {@inheritDoc} */
174 @SuppressWarnings("unchecked")
175 public Enumeration getInitParameterNames() {
176 return initParameters.keys();
177 }
178
179 /*** {@inheritDoc} */
180 public Object getAttribute(String string) {
181 return rootContext.getAttribute(string);
182 }
183
184 /*** {@inheritDoc} */
185 @SuppressWarnings("unchecked")
186 public Enumeration getAttributeNames() {
187 return rootContext.getAttributeNames();
188 }
189
190 /*** {@inheritDoc} */
191 public void setAttribute(String string, Object object) {
192 rootContext.setAttribute(string, object);
193 }
194
195 /*** {@inheritDoc} */
196 public void removeAttribute(String string) {
197 rootContext.removeAttribute(string);
198 }
199
200 /*** {@inheritDoc} */
201 public String getServletContextName() {
202 return rootContext.getServletContextName();
203 }
204
205 /*** {@inheritDoc} */
206 public String getContextPath() {
207 return rootContext.getContextPath();
208 }
209
210 /***
211 * Composes an enumeration into a single one.
212 */
213 @SuppressWarnings("unchecked")
214 class CompositeEnumeration implements Enumeration {
215
216 /***
217 * The first enumeration to consider.
218 */
219 private Enumeration first;
220
221 /***
222 * The second enumeration to consider.
223 */
224 private Enumeration second;
225
226
227 /***
228 * Constructor.
229 *
230 * @param first The first enumeration to consider.
231 * @param second The second enumeration to consider.
232 */
233 public CompositeEnumeration(Enumeration first, Enumeration second) {
234 this.first = first;
235 this.second = second;
236 }
237
238 /*** {@inheritDoc} */
239 public boolean hasMoreElements() {
240 return first.hasMoreElements() || second.hasMoreElements();
241 }
242
243 /*** {@inheritDoc} */
244 public Object nextElement() {
245 if (first.hasMoreElements()) {
246 return first.nextElement();
247 }
248
249 return second.nextElement();
250 }
251 }
252 }