This project has retired. For details please refer to its Attic page.
ServletContextAdapter xref
View Javadoc

1   /*
2    * $Id: ServletContextAdapter.java 1058093 2011-01-12 11:49:02Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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: 1058093 $ $Date: 2011-01-12 22:49:02 +1100 (Wed, 12 Jan 2011) $
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({ "rawtypes" })
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("rawtypes")
133     public Enumeration getServlets() {
134         return rootContext.getServlets();  //To change body of implemented methods use File | Settings | File Templates.
135     }
136 
137     /** {@inheritDoc} */
138     @SuppressWarnings("rawtypes")
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("rawtypes")
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("rawtypes")
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 }