View Javadoc

1   /*
2    * $Id: ConfiguredServletContext.java 651946 2008-04-27 13:41:38Z 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  
22  package org.apache.struts2.tiles;
23  
24  
25  import javax.servlet.RequestDispatcher;
26  import javax.servlet.Servlet;
27  import javax.servlet.ServletContext;
28  import javax.servlet.ServletException;
29  import java.io.InputStream;
30  import java.net.MalformedURLException;
31  import java.net.URL;
32  import java.util.*;
33  
34  /***
35   * ServletContext implementation which allows Struts
36   * to inject initialization parameters into the context
37   * in order to reduce the amount of configuration required
38   * within web.xml for using Tiles.
39   *
40   * The specified init parameters are only utilized if
41   * they are not explicitaly defined in the web.xml
42   *
43   * @version $Rev: 651946 $
44   * @since Struts 2.0.1
45   */
46  @SuppressWarnings("deprecation")
47  public class ConfiguredServletContext implements ServletContext {
48  
49      private ServletContext rootContext;
50      private Map<String, String> initParameters;
51  
52  
53      public ConfiguredServletContext(ServletContext context, Map<String, String> initParameters) {
54          this.rootContext = context;
55          this.initParameters = initParameters;
56      }
57  
58      public ServletContext getContext(String string) {
59          return rootContext.getContext(string);
60      }
61  
62      public int getMajorVersion() {
63          return rootContext.getMajorVersion();
64      }
65  
66      public int getMinorVersion() {
67          return rootContext.getMinorVersion();
68      }
69  
70      public String getMimeType(String string) {
71          return rootContext.getMimeType(string);
72      }
73  
74      public Set getResourcePaths(String string) {
75          return rootContext.getResourcePaths(string);
76      }
77  
78      public URL getResource(String string) throws MalformedURLException {
79          return rootContext.getResource(string);
80      }
81  
82      public InputStream getResourceAsStream(String string) {
83          return rootContext.getResourceAsStream(string);
84      }
85  
86      public RequestDispatcher getRequestDispatcher(String string) {
87          return rootContext.getRequestDispatcher(string);
88      }
89  
90      public RequestDispatcher getNamedDispatcher(String string) {
91          return rootContext.getNamedDispatcher(string);
92      }
93  
94      @SuppressWarnings("deprecation")
95      public Servlet getServlet(String string) throws ServletException {
96          return rootContext.getServlet(string);
97      }
98  
99      @SuppressWarnings("deprecation")
100     public Enumeration getServlets() {
101         return rootContext.getServlets();  //To change body of implemented methods use File | Settings | File Templates.
102     }
103 
104     @SuppressWarnings("deprecation")
105     public Enumeration getServletNames() {
106         return rootContext.getServletNames();
107     }
108 
109     public void log(String string) {
110         rootContext.log(string);
111     }
112 
113     @SuppressWarnings("deprecation")
114     public void log(Exception exception, String string) {
115         rootContext.log(exception, string);
116     }
117 
118     public void log(String string, Throwable throwable) {
119         rootContext.log(string, throwable);
120     }
121 
122     public String getRealPath(String string) {
123         return rootContext.getRealPath(string);
124     }
125 
126     public String getServerInfo() {
127         return rootContext.getServerInfo();
128     }
129 
130     public String getInitParameter(String string) {
131         String parm = rootContext.getInitParameter(string);
132         if (parm == null) {
133             return initParameters.get(string);
134         }
135         return parm;
136     }
137 
138     public Enumeration getInitParameterNames() {
139         return new CompositeEnumeration(
140                 rootContext.getInitParameterNames(),
141                 initParameters.keySet().iterator());
142     }
143 
144     public Object getAttribute(String string) {
145         return rootContext.getAttribute(string);
146     }
147 
148     public Enumeration getAttributeNames() {
149         return rootContext.getAttributeNames();
150     }
151 
152     public void setAttribute(String string, Object object) {
153         rootContext.setAttribute(string, object);
154     }
155 
156     public void removeAttribute(String string) {
157         rootContext.removeAttribute(string);
158     }
159 
160     public String getServletContextName() {
161         return rootContext.getServletContextName();
162     }
163 
164     class CompositeEnumeration implements Enumeration {
165 
166         private Enumeration first;
167         private Iterator second;
168 
169 
170         public CompositeEnumeration(Enumeration first, Iterator second) {
171             this.first = first;
172             this.second = second;
173         }
174 
175         public boolean hasMoreElements() {
176             return first.hasMoreElements() || second.hasNext();
177         }
178 
179         public Object nextElement() {
180             if (first.hasMoreElements()) {
181                 return first.nextElement();
182             }
183 
184             return second.next();
185         }
186     }
187 }