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