View Javadoc

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