Coverage Report - org.apache.myfaces.context.servlet.ServletExternalContextImplBase
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletExternalContextImplBase
0%
0/50
0%
0/10
1.389
 
 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  0
     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  0
     {
 55  0
         _servletContext = servletContext;
 56  0
         _applicationMap = null;
 57  0
         _initParameterMap = null;
 58  0
         _clientWindow = null;
 59  0
     }
 60  
     
 61  
     public void release()
 62  
     {
 63  0
         _servletContext = null;
 64  0
         _applicationMap = null;
 65  0
         _initParameterMap = null;
 66  0
         _clientWindow = null;
 67  0
     }
 68  
     
 69  
     // ~ Methods which only rely on the ServletContext-------------------------
 70  
     
 71  
     @Override
 72  
     public Map<String, Object> getApplicationMap()
 73  
     {
 74  0
         if (_applicationMap == null)
 75  
         {
 76  0
             _applicationMap = new ApplicationMap(_servletContext);
 77  
         }
 78  0
         return _applicationMap;
 79  
     }
 80  
     
 81  
     @Override
 82  
     public String getMimeType(String file)
 83  
     {
 84  0
         checkNull(file, "file");
 85  0
         return _servletContext.getMimeType(file);
 86  
     }
 87  
     
 88  
     @Override
 89  
     public Object getContext()
 90  
     {
 91  0
         return _servletContext;
 92  
     }
 93  
     
 94  
     @Override
 95  
     public String getContextName() 
 96  
     {
 97  0
         return _servletContext.getServletContextName();
 98  
     }
 99  
     
 100  
     @Override
 101  
     public String getInitParameter(final String s)
 102  
     {
 103  0
         if (s == null)
 104  
         {
 105  0
             throw new NullPointerException("Init parameter name cannot be null");
 106  
         }
 107  0
         return _servletContext.getInitParameter(s);
 108  
     }
 109  
 
 110  
     @Override
 111  
     @SuppressWarnings("unchecked")
 112  
     public Map<String, String> getInitParameterMap()
 113  
     {
 114  0
         if (_initParameterMap == null)
 115  
         {
 116  
             // We cache it as an attribute in ServletContext itself (is this circular reference a problem?)
 117  0
             _initParameterMap = (Map<String, String>) _servletContext.getAttribute(INIT_PARAMETER_MAP_ATTRIBUTE);
 118  0
             if (_initParameterMap == null)
 119  
             {
 120  0
                 _initParameterMap = new InitParameterMap(_servletContext);
 121  0
                 _servletContext.setAttribute(INIT_PARAMETER_MAP_ATTRIBUTE, _initParameterMap);
 122  
             }
 123  
         }
 124  0
         return _initParameterMap;
 125  
     }
 126  
     
 127  
     @Override
 128  
     public URL getResource(final String path) throws MalformedURLException
 129  
     {
 130  0
         checkNull(path, "path");
 131  0
         return _servletContext.getResource(path);
 132  
     }
 133  
 
 134  
     @Override
 135  
     public InputStream getResourceAsStream(final String path)
 136  
     {
 137  0
         checkNull(path, "path");
 138  0
         return _servletContext.getResourceAsStream(path);
 139  
     }
 140  
 
 141  
     @Override
 142  
     @SuppressWarnings("unchecked")
 143  
     public Set<String> getResourcePaths(final String path)
 144  
     {
 145  0
         checkNull(path, "path");
 146  0
         return _servletContext.getResourcePaths(path);
 147  
     }
 148  
 
 149  
     @Override
 150  
     public void log(final String message)
 151  
     {
 152  0
         checkNull(message, "message");
 153  0
         _servletContext.log(message);
 154  0
     }
 155  
 
 156  
     @Override
 157  
     public void log(final String message, final Throwable exception)
 158  
     {
 159  0
         checkNull(message, "message");
 160  0
         checkNull(exception, "exception");
 161  0
         _servletContext.log(message, exception);
 162  0
     }
 163  
     
 164  
     @Override
 165  
     public String getRealPath(String path)
 166  
     {
 167  0
         checkNull(path, "path");
 168  0
         return _servletContext.getRealPath(path);
 169  
     }
 170  
     
 171  
     public ClientWindow getClientWindow()
 172  
     {
 173  0
         return _clientWindow;
 174  
     }
 175  
     
 176  
     public void setClientWindow(ClientWindow window)
 177  
     {
 178  0
         _clientWindow = window;
 179  0
     }
 180  
     
 181  
     @Override
 182  
     public String getApplicationContextPath() 
 183  
     {
 184  0
         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  0
         if (o == null)
 193  
         {
 194  0
             throw new NullPointerException(param + " can not be null.");
 195  
         }
 196  0
     }
 197  
     
 198  
 }