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.commons.resourcehandler.webapp.config.impl;
20  
21  import java.io.IOException;
22  import java.net.URL;
23  
24  import javax.faces.application.ProjectStage;
25  import javax.faces.context.ExternalContext;
26  import javax.faces.context.FacesContext;
27  import javax.servlet.ServletContext;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.myfaces.commons.resourcehandler.webapp.config.WebConfigProvider;
32  import org.apache.myfaces.commons.resourcehandler.webapp.config.WebConfigProviderFactory;
33  import org.apache.myfaces.commons.resourcehandler.webapp.config.WebRegistration;
34  import org.apache.myfaces.commons.util.WebConfigParamUtils;
35  
36  /**
37   * 
38   * @author Leonardo Uribe
39   *
40   */
41  public class WebConfigProviderImpl extends WebConfigProvider
42  {
43      private static final String WEB_XML_PATH = "/WEB-INF/web.xml";
44  
45      private static final String WEB_REGISTRATION_KEY = WebRegistration.class.getName();
46      
47      private static final String INIT_PARAM_CONFIG_REFRESH_PERIOD = "org.apache.myfaces.CONFIG_REFRESH_PERIOD";
48      private static final long INIT_PARAM_CONFIG_REFRESH_PERIOD_DEFAULT = 2;
49      
50      private long refreshPeriod = -1;
51      
52      private long parsingTime = -1;
53      
54      private Boolean servlet30Mode;
55      
56      public WebConfigProviderImpl()
57      {
58      }
59  
60      public WebRegistration getWebRegistration(FacesContext context)
61      {
62          WebRegistration webConfig = (WebRegistration)context.getExternalContext().getApplicationMap().get(WEB_REGISTRATION_KEY);
63          if (webConfig == null)
64          {
65              init(context);
66              webConfig = (WebRegistration)context.getExternalContext().getApplicationMap().get(WEB_REGISTRATION_KEY);
67          }
68          return webConfig;
69      }
70  
71      public void init(FacesContext context)
72      {
73          if (servlet30Mode == null)
74          {
75              servlet30Mode = (context.getExternalContext().getContext() instanceof ServletContext && _ExternalSpecifications.isServlet30Available());
76          }
77          if (servlet30Mode)
78          {
79              ServletContext servletContext = (ServletContext) context.getExternalContext().getContext(); 
80              context.getExternalContext().getApplicationMap().put(WEB_REGISTRATION_KEY, _Servlet30Utils.getWebRegistrationFromServlet30Api(servletContext));
81          }
82          else
83          {
84              //Load the information parsing web.xml
85              context.getExternalContext().getApplicationMap().put(WEB_REGISTRATION_KEY, WebXmlConfigParser.getWebRegistrationFromParsedWebXml(context));
86          }
87          if (!context.isProjectStage(ProjectStage.Production))
88          {
89              long configRefreshPeriod = WebConfigParamUtils.getLongInitParameter(context.getExternalContext(), 
90                      INIT_PARAM_CONFIG_REFRESH_PERIOD, INIT_PARAM_CONFIG_REFRESH_PERIOD_DEFAULT);
91              parsingTime = System.currentTimeMillis();
92              refreshPeriod = (configRefreshPeriod * 1000);
93          }
94      }
95  
96      public void update(FacesContext context)
97      {
98          if (servlet30Mode == null)
99          {
100             servlet30Mode = (context.getExternalContext().getContext() instanceof ServletContext && _ExternalSpecifications.isServlet30Available());
101         }
102         if (!servlet30Mode  && !context.isProjectStage(ProjectStage.Production))
103         {
104             if (isOld(context.getExternalContext()))
105             {
106                 init(context);
107             }
108         }
109     }
110     
111     private boolean isOld(ExternalContext context)
112     {
113         if (refreshPeriod > 0) {
114             long ttl = this.parsingTime + refreshPeriod;
115             if (System.currentTimeMillis() > ttl) {
116                 long lastModified = getWebXmlLastModified(context);
117                 return lastModified == 0 || lastModified > ttl;
118             }
119         }
120         return false;
121     }
122 
123     
124     private static long getWebXmlLastModified(ExternalContext context)
125     {
126         try {
127             URL url = context.getResource(WEB_XML_PATH);
128             if (url != null)
129                 return url.openConnection().getLastModified();
130         } catch (IOException e) {
131             Log log = LogFactory.getLog(WebConfigProviderFactory.class);
132             if (log.isErrorEnabled())
133             {
134                 log.error("Could not find web.xml in path " + WEB_XML_PATH);
135             }
136         }
137         return 0L;
138     }
139 }