Coverage Report - org.apache.myfaces.shared_impl.webapp.webxml.WebXml
 
Classes in this File Line Coverage Branch Coverage Complexity
WebXml
0%
0/101
0%
0/54
2.786
 
 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.shared_impl.webapp.webxml;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 import org.apache.myfaces.shared_impl.config.MyfacesConfig;
 24  
 
 25  
 import javax.faces.context.ExternalContext;
 26  
 import javax.faces.webapp.FacesServlet;
 27  
 import java.util.*;
 28  
 
 29  
 /**
 30  
  * @author Manfred Geiler (latest modification by $Author: lu4242 $)
 31  
  * @version $Revision: 775345 $ $Date: 2009-05-15 16:11:35 -0500 (Fri, 15 May 2009) $
 32  
  */
 33  0
 public class WebXml
 34  
 {
 35  0
     private static final Log log = LogFactory.getLog(WebXml.class);
 36  
 
 37  
 
 38  
     private static long refreshPeriod;
 39  
     private long parsingTime;
 40  
 
 41  0
     private Map _servlets = new HashMap();
 42  0
     private Map _servletMappings = new HashMap();
 43  0
     private Map _filters = new HashMap();
 44  0
     private Map _filterMappings = new HashMap();
 45  
 
 46  0
     private volatile List _facesServletMappings = null;
 47  0
     private volatile List _facesExtensionsFilterMappings = null;
 48  
     
 49  0
     private String _delegateFacesServlet = null;
 50  
 
 51  
     void addServlet(String servletName, String servletClass)
 52  
     {
 53  0
         if (_servlets.get(servletName) != null)
 54  
         {
 55  0
             log.warn("Servlet " + servletName + " defined more than once, first definition will be used.");
 56  
         }
 57  
         else
 58  
         {
 59  0
             _servlets.put(servletName, servletClass);
 60  
         }
 61  0
     }
 62  
 
 63  
     void addFilter(String filterName, String filterClass)
 64  
     {
 65  0
         if (_filters.get(filterName) != null)
 66  
         {
 67  0
             log.warn("Filter " + filterName + " defined more than once, first definition will be used.");
 68  
         }
 69  
         else
 70  
         {
 71  0
             _filters.put(filterName, filterClass);
 72  
         }
 73  0
     }
 74  
 
 75  
     boolean containsServlet(String servletName)
 76  
     {
 77  0
         return _servlets.containsKey(servletName);
 78  
     }
 79  
 
 80  
     boolean containsFilter(String filterName)
 81  
     {
 82  0
         return _filters.containsKey(filterName);
 83  
     }
 84  
 
 85  
     void addServletMapping(String servletName, String urlPattern)
 86  
     {
 87  0
         List mappings = (List)_servletMappings.get(servletName);
 88  0
         if (mappings == null)
 89  
         {
 90  0
             mappings = new ArrayList();
 91  0
             _servletMappings.put(servletName, mappings);
 92  
         }
 93  0
         mappings.add(urlPattern);
 94  0
     }
 95  
 
 96  
     void addFilterMapping(String filterName, String urlPattern)
 97  
     {
 98  0
         List mappings = (List)_filterMappings.get(filterName);
 99  0
         if (mappings == null)
 100  
         {
 101  0
             mappings = new ArrayList();
 102  0
             _filterMappings.put(filterName, mappings);
 103  
         }
 104  0
         mappings.add(urlPattern);
 105  0
     }
 106  
 
 107  
     public List getFacesServletMappings()
 108  
     {
 109  0
         if (_facesServletMappings != null) return _facesServletMappings;
 110  
 
 111  0
         List tempFacesServletMappings = new ArrayList();
 112  0
         for (Iterator it = _servlets.entrySet().iterator(); it.hasNext(); )
 113  
         {
 114  0
             Map.Entry entry = (Map.Entry)it.next();
 115  0
             String servletName = (String)entry.getKey();
 116  0
             if (null == entry.getValue())
 117  
             {
 118  
                 // the value is null in the case of jsp files listed as servlets
 119  
                 // in cactus
 120  
                 // <servlet>
 121  
                 //   <servlet-name>JspRedirector</servlet-name>
 122  
                 //   <jsp-file>/jspRedirector.jsp</jsp-file>
 123  
                 // </servlet>
 124  0
                 continue;
 125  
             }
 126  0
             Class servletClass = org.apache.myfaces.shared_impl.util.ClassUtils.simpleClassForName((String)entry.getValue());
 127  0
             if (FacesServlet.class.isAssignableFrom(servletClass) ||
 128  
                     DelegatedFacesServlet.class.isAssignableFrom(servletClass) ||
 129  
                     servletClass.getName().equals(_delegateFacesServlet))
 130  
             {
 131  0
                 List urlPatterns = (List)_servletMappings.get(servletName);
 132  0
                 if( urlPatterns != null )
 133  
                 {
 134  0
                     for (Iterator it2 = urlPatterns.iterator(); it2.hasNext(); )
 135  
                     {
 136  0
                         String urlpattern = (String)it2.next();
 137  0
                         tempFacesServletMappings.add(new org.apache.myfaces.shared_impl.webapp.webxml.ServletMapping(servletName,
 138  
                                                                                                              servletClass,
 139  
                                                                                                              urlpattern));
 140  0
                         if (log.isTraceEnabled())
 141  0
                             log.trace("adding mapping for servlet + " + servletName + " urlpattern = " + urlpattern);
 142  0
                     }
 143  
                 }
 144  0
             }
 145  
             else
 146  
             {
 147  0
                 if (log.isTraceEnabled()) log.trace("ignoring servlet + " + servletName + " " + servletClass + " (no FacesServlet)");
 148  
             }
 149  0
         }
 150  
         
 151  
         //Expose to all threads
 152  0
         _facesServletMappings = tempFacesServletMappings;
 153  
         
 154  0
         return _facesServletMappings;
 155  
     }
 156  
 
 157  
     /**
 158  
      * returns a list of {@see #org.apache.myfaces.shared_impl.webapp.webxml.FilterMapping}s representing a
 159  
      * extensions filter entry
 160  
      */
 161  
     public List getFacesExtensionsFilterMappings()
 162  
     {
 163  0
         if (_facesExtensionsFilterMappings != null) return _facesExtensionsFilterMappings;
 164  
 
 165  0
         List tempExtensionsFilterMappings = new ArrayList();
 166  0
         for (Iterator it = _filters.entrySet().iterator(); it.hasNext(); )
 167  
         {
 168  0
             Map.Entry entry = (Map.Entry)it.next();
 169  0
             String filterName = (String)entry.getKey();
 170  0
             String filterClassName = (String)entry.getValue();
 171  
             
 172  0
             if (!"org.apache.myfaces.component.html.util.ExtensionsFilter".equals(filterClassName) &&
 173  
                 !"org.apache.myfaces.webapp.filter.ExtensionsFilter".equals(filterClassName))
 174  
             {
 175  
                 // not an extensions filter
 176  0
                 continue;
 177  
             }
 178  
             
 179  0
             Class filterClass = org.apache.myfaces.shared_impl.util.ClassUtils.simpleClassForName(filterClassName);
 180  0
             List urlPatterns = (List)_filterMappings.get(filterName);
 181  0
             if( urlPatterns != null )
 182  
             {
 183  0
                 for (Iterator it2 = urlPatterns.iterator(); it2.hasNext(); )
 184  
                 {
 185  0
                     String urlpattern = (String)it2.next();
 186  0
                     tempExtensionsFilterMappings.add(new org.apache.myfaces.shared_impl.webapp.webxml.FilterMapping(
 187  
                         filterName, filterClass, urlpattern));
 188  0
                     if (log.isTraceEnabled())
 189  0
                         log.trace("adding mapping for filter + " + filterName + " urlpattern = " + urlpattern);
 190  0
                 }
 191  
             }
 192  0
         }
 193  
         
 194  
         //Expose to all threads
 195  0
         _facesExtensionsFilterMappings = tempExtensionsFilterMappings;
 196  
         
 197  0
         return _facesExtensionsFilterMappings;
 198  
     }
 199  
 
 200  
     protected void setParsingTime(long parsingTime)
 201  
     {
 202  0
         this.parsingTime = parsingTime;
 203  0
     }
 204  
     
 205  
     private void setDelegateFacesServlet(String delegateFacesServlet)
 206  
     {
 207  0
         this._delegateFacesServlet = delegateFacesServlet;
 208  0
     }
 209  
 
 210  
     protected boolean isOld(ExternalContext context)
 211  
     {
 212  0
         if (refreshPeriod > 0) {
 213  0
             long ttl = this.parsingTime + refreshPeriod;
 214  0
             if (System.currentTimeMillis() > ttl) {
 215  0
                 long lastModified = WebXmlParser.getWebXmlLastModified(context);
 216  0
                 return lastModified == 0 || lastModified > ttl;
 217  
             }
 218  
         }
 219  0
         return false;
 220  
     }
 221  
 
 222  0
     private static final String WEB_XML_ATTR = WebXml.class.getName();
 223  
     public static WebXml getWebXml(ExternalContext context)
 224  
     {
 225  0
         WebXml webXml = (WebXml)context.getApplicationMap().get(WEB_XML_ATTR);
 226  0
         if (webXml == null)
 227  
         {
 228  0
             init(context);
 229  0
             webXml = (WebXml)context.getApplicationMap().get(WEB_XML_ATTR);
 230  
         }
 231  0
         return webXml;
 232  
     }
 233  
 
 234  
     /**
 235  
      * should be called when initialising Servlet
 236  
      * @param context
 237  
      */
 238  
     public static void init(ExternalContext context)
 239  
     {
 240  0
         WebXmlParser parser = new WebXmlParser(context);
 241  0
         WebXml webXml = parser.parse();
 242  0
         context.getApplicationMap().put(WEB_XML_ATTR, webXml);
 243  0
         MyfacesConfig mfconfig = MyfacesConfig.getCurrentInstance(context);
 244  0
         long configRefreshPeriod = mfconfig.getConfigRefreshPeriod();
 245  0
         webXml.setParsingTime(System.currentTimeMillis());
 246  0
         webXml.setDelegateFacesServlet(mfconfig.getDelegateFacesServlet());
 247  0
         refreshPeriod = (configRefreshPeriod * 1000);
 248  0
     }
 249  
 
 250  
     public static void update(ExternalContext context)
 251  
     {
 252  0
         if (getWebXml(context).isOld(context)){
 253  0
             WebXml.init(context);
 254  
         }
 255  0
     }
 256  
 
 257  
 }