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  package org.apache.myfaces.context.servlet;
20  
21  import java.io.InputStream;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import javax.faces.context.ExternalContext;
28  import javax.faces.lifecycle.ClientWindow;
29  import javax.servlet.ServletContext;
30  
31  import org.apache.myfaces.context.ReleaseableExternalContext;
32  
33  /**
34   * Provides a base implementation of the ExternalContext for Servlet
35   * environments. This impl provides all methods which only rely on the
36   * ServletContext and thus are also provided at startup and shutdown.
37   * 
38   * @author Jakob Korherr (latest modification by $Author$)
39   * @version $Revision$ $Date$
40   */
41  public abstract class ServletExternalContextImplBase extends ExternalContext
42          implements ReleaseableExternalContext
43  {
44      
45      private static final String INIT_PARAMETER_MAP_ATTRIBUTE = InitParameterMap.class.getName();
46      
47      private ServletContext _servletContext;
48      private Map<String, Object> _applicationMap;
49      private Map<String, String> _initParameterMap;
50      private ClientWindow _clientWindow;
51  
52      
53      public ServletExternalContextImplBase(ServletContext servletContext)
54      {
55          _servletContext = servletContext;
56          _applicationMap = null;
57          _initParameterMap = null;
58          _clientWindow = null;
59      }
60      
61      public void release()
62      {
63          _servletContext = null;
64          _applicationMap = null;
65          _initParameterMap = null;
66          _clientWindow = null;
67      }
68      
69      // ~ Methods which only rely on the ServletContext-------------------------
70      
71      @Override
72      public Map<String, Object> getApplicationMap()
73      {
74          if (_applicationMap == null)
75          {
76              _applicationMap = new ApplicationMap(_servletContext);
77          }
78          return _applicationMap;
79      }
80      
81      @Override
82      public String getMimeType(String file)
83      {
84          checkNull(file, "file");
85          return _servletContext.getMimeType(file);
86      }
87      
88      @Override
89      public Object getContext()
90      {
91          return _servletContext;
92      }
93      
94      @Override
95      public String getContextName() 
96      {
97          return _servletContext.getServletContextName();
98      }
99      
100     @Override
101     public String getInitParameter(final String s)
102     {
103         if (s == null)
104         {
105             throw new NullPointerException("Init parameter name cannot be null");
106         }
107         return _servletContext.getInitParameter(s);
108     }
109 
110     @Override
111     @SuppressWarnings("unchecked")
112     public Map<String, String> getInitParameterMap()
113     {
114         if (_initParameterMap == null)
115         {
116             // We cache it as an attribute in ServletContext itself (is this circular reference a problem?)
117             _initParameterMap = (Map<String, String>) _servletContext.getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE);
118             if (_initParameterMap == null)
119             {
120                 _initParameterMap = new InitParameterMap(_servletContext);
121                 _servletContext.setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE, _initParameterMap);
122             }
123         }
124         return _initParameterMap;
125     }
126     
127     @Override
128     public URL getResource(final String path) throws MalformedURLException
129     {
130         checkNull(path, "path");
131         return _servletContext.getResource(path);
132     }
133 
134     @Override
135     public InputStream getResourceAsStream(final String path)
136     {
137         checkNull(path, "path");
138         return _servletContext.getResourceAsStream(path);
139     }
140 
141     @Override
142     @SuppressWarnings("unchecked")
143     public Set<String> getResourcePaths(final String path)
144     {
145         checkNull(path, "path");
146         return _servletContext.getResourcePaths(path);
147     }
148 
149     @Override
150     public void log(final String message)
151     {
152         checkNull(message, "message");
153         _servletContext.log(message);
154     }
155 
156     @Override
157     public void log(final String message, final Throwable exception)
158     {
159         checkNull(message, "message");
160         checkNull(exception, "exception");
161         _servletContext.log(message, exception);
162     }
163     
164     @Override
165     public String getRealPath(String path)
166     {
167         checkNull(path, "path");
168         return _servletContext.getRealPath(path);
169     }
170     
171     public ClientWindow getClientWindow()
172     {
173         return _clientWindow;
174     }
175     
176     public void setClientWindow(ClientWindow window)
177     {
178         _clientWindow = window;
179     }
180     
181     @Override
182     public String getApplicationContextPath() 
183     {
184         return _servletContext.getContextPath();
185         
186     }
187     
188     // ~ Methods which verify some required behavior---------------------------
189     
190     protected void checkNull(final Object o, final String param)
191     {
192         if (o == null)
193         {
194             throw new NullPointerException(param + " can not be null.");
195         }
196     }
197     
198 }