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.application;
20  
21  import javax.faces.application.ProjectStage;
22  import javax.faces.context.FacesContext;
23  
24  import org.apache.myfaces.resource.InternalClassLoaderResourceLoader;
25  import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
26  import org.apache.myfaces.shared.resource.BaseResourceHandlerSupport;
27  import org.apache.myfaces.shared.resource.ClassLoaderResourceLoader;
28  import org.apache.myfaces.shared.resource.ExternalContextResourceLoader;
29  import org.apache.myfaces.shared.resource.ResourceLoader;
30  import org.apache.myfaces.shared.util.WebConfigParamUtils;
31  
32  /**
33   * A ResourceHandlerSupport implementation for use with standard Java Servlet engines,
34   * ie an engine that supports javax.servlet, and uses a standard web.xml file.
35   * 
36   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
37   * @version $Revision: 1187068 $ $Date: 2011-10-20 16:44:52 -0500 (Thu, 20 Oct 2011) $
38   */
39  public class DefaultResourceHandlerSupport extends BaseResourceHandlerSupport
40  {
41  
42      private static final String META_INF_RESOURCES = "META-INF/resources";
43      private static final String RESOURCES = "/resources";
44      private static final String META_INF_INTERNAL_RESOURCES = "META-INF/internal-resources";
45  
46      private ResourceLoader[] _resourceLoaders;
47      
48      public DefaultResourceHandlerSupport()
49      {
50          super();
51      }
52  
53      public ResourceLoader[] getResourceLoaders()
54      {
55          if (_resourceLoaders == null)
56          {
57              FacesContext facesContext = FacesContext.getCurrentInstance(); 
58              //The ExternalContextResourceLoader has precedence over
59              //ClassLoaderResourceLoader, so it goes first.
60              String renderedJSFJS = WebConfigParamUtils.getStringInitParameter(facesContext.getExternalContext(),
61                      InternalClassLoaderResourceLoader.MYFACES_JSF_MODE,
62                      ResourceUtils.JSF_MYFACES_JSFJS_NORMAL);
63  
64              if (facesContext.isProjectStage(ProjectStage.Development) ||
65                   !renderedJSFJS.equals(ResourceUtils.JSF_MYFACES_JSFJS_NORMAL))
66              {
67                  _resourceLoaders = new ResourceLoader[] {
68                          new ExternalContextResourceLoader(RESOURCES),
69                          new InternalClassLoaderResourceLoader(META_INF_INTERNAL_RESOURCES),
70                          new ClassLoaderResourceLoader(META_INF_RESOURCES)
71                  };
72              }
73              else
74              {
75                  _resourceLoaders = new ResourceLoader[] {
76                          new ExternalContextResourceLoader(RESOURCES),
77                          new ClassLoaderResourceLoader(META_INF_RESOURCES)
78                  };
79              }
80          }
81          return _resourceLoaders;
82      }
83  }