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 08:43:21 -0500 (Lun, 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 Object getContext()
78      {
79          return _servletContext;
80      }
81      
82      @Override
83      public String getInitParameter(final String s)
84      {
85          return _servletContext.getInitParameter(s);
86      }
87  
88      @Override
89      @SuppressWarnings("unchecked")
90      public Map<String, String> getInitParameterMap()
91      {
92          if (_initParameterMap == null)
93          {
94              // We cache it as an attribute in ServletContext itself (is this circular reference a problem?)
95              if ((_initParameterMap = (Map<String, String>) _servletContext.getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE)) == null)
96              {
97                  _initParameterMap = new InitParameterMap(_servletContext);
98                  _servletContext.setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE, _initParameterMap);
99              }
100         }
101         return _initParameterMap;
102     }
103     
104     @Override
105     public URL getResource(final String path) throws MalformedURLException
106     {
107         checkNull(path, "path");
108         return _servletContext.getResource(path);
109     }
110 
111     @Override
112     public InputStream getResourceAsStream(final String path)
113     {
114         checkNull(path, "path");
115         return _servletContext.getResourceAsStream(path);
116     }
117 
118     @Override
119     @SuppressWarnings("unchecked")
120     public Set<String> getResourcePaths(final String path)
121     {
122         checkNull(path, "path");
123         return _servletContext.getResourcePaths(path);
124     }
125 
126     @Override
127     public void log(final String message)
128     {
129         checkNull(message, "message");
130         _servletContext.log(message);
131     }
132 
133     @Override
134     public void log(final String message, final Throwable exception)
135     {
136         checkNull(message, "message");
137         checkNull(exception, "exception");
138         _servletContext.log(message, exception);
139     }
140     
141     // ~ Methods which verify some required behavior---------------------------
142     
143     protected void checkNull(final Object o, final String param)
144     {
145         if (o == null)
146         {
147             throw new NullPointerException(param + " can not be null.");
148         }
149     }
150     
151 }