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