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.resource.TempDirFileCacheResourceLoader;
26  import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
27  import org.apache.myfaces.shared.resource.BaseResourceHandlerSupport;
28  import org.apache.myfaces.shared.resource.ClassLoaderResourceLoader;
29  import org.apache.myfaces.shared.resource.ExternalContextResourceLoader;
30  import org.apache.myfaces.shared.resource.ResourceLoader;
31  import org.apache.myfaces.shared.util.WebConfigParamUtils;
32  
33  /**
34   * A ResourceHandlerSupport implementation for use with standard Java Servlet engines,
35   * ie an engine that supports javax.servlet, and uses a standard web.xml file.
36   * 
37   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
38   * @version $Revision: 1461879 $ $Date: 2013-03-27 17:18:46 -0500 (Wed, 27 Mar 2013) $
39   */
40  public class DefaultResourceHandlerSupport extends BaseResourceHandlerSupport
41  {
42  
43      private static final String META_INF_RESOURCES = "META-INF/resources";
44      private static final String RESOURCES = "/resources";
45      private static final String META_INF_INTERNAL_RESOURCES = "META-INF/internal-resources";
46  
47      private ResourceLoader[] _resourceLoaders;
48      
49      public DefaultResourceHandlerSupport()
50      {
51          super();
52      }
53  
54      public ResourceLoader[] getResourceLoaders()
55      {
56          if (_resourceLoaders == null)
57          {
58              FacesContext facesContext = FacesContext.getCurrentInstance(); 
59              
60              if (TempDirFileCacheResourceLoader.isValidCreateTemporalFiles(facesContext))
61              {
62                  //The ExternalContextResourceLoader has precedence over
63                  //ClassLoaderResourceLoader, so it goes first.
64                  String renderedJSFJS = WebConfigParamUtils.getStringInitParameter(facesContext.getExternalContext(),
65                          InternalClassLoaderResourceLoader.MYFACES_JSF_MODE,
66                          ResourceUtils.JSF_MYFACES_JSFJS_NORMAL);
67  
68                  if (facesContext.isProjectStage(ProjectStage.Development) ||
69                       !renderedJSFJS.equals(ResourceUtils.JSF_MYFACES_JSFJS_NORMAL))
70                  {
71                      _resourceLoaders = new ResourceLoader[] {
72                              new TempDirFileCacheResourceLoader(new ExternalContextResourceLoader(RESOURCES)),
73                              new TempDirFileCacheResourceLoader(
74                                               new InternalClassLoaderResourceLoader(META_INF_INTERNAL_RESOURCES)),
75                              new TempDirFileCacheResourceLoader(new ClassLoaderResourceLoader(META_INF_RESOURCES))
76                      };
77                  }
78                  else
79                  {
80                      _resourceLoaders = new ResourceLoader[] {
81                              new TempDirFileCacheResourceLoader(new ExternalContextResourceLoader(RESOURCES)),
82                              new TempDirFileCacheResourceLoader(new ClassLoaderResourceLoader(META_INF_RESOURCES))
83                      };
84                  }
85              }
86              else
87              {            
88                  //The ExternalContextResourceLoader has precedence over
89                  //ClassLoaderResourceLoader, so it goes first.
90                  String renderedJSFJS = WebConfigParamUtils.getStringInitParameter(facesContext.getExternalContext(),
91                          InternalClassLoaderResourceLoader.MYFACES_JSF_MODE,
92                          ResourceUtils.JSF_MYFACES_JSFJS_NORMAL);
93  
94                  if (facesContext.isProjectStage(ProjectStage.Development) ||
95                       !renderedJSFJS.equals(ResourceUtils.JSF_MYFACES_JSFJS_NORMAL))
96                  {
97                      _resourceLoaders = new ResourceLoader[] {
98                              new ExternalContextResourceLoader(RESOURCES),
99                              new InternalClassLoaderResourceLoader(META_INF_INTERNAL_RESOURCES),
100                             new ClassLoaderResourceLoader(META_INF_RESOURCES)
101                     };
102                 }
103                 else
104                 {
105                     _resourceLoaders = new ResourceLoader[] {
106                             new ExternalContextResourceLoader(RESOURCES),
107                             new ClassLoaderResourceLoader(META_INF_RESOURCES)
108                     };
109                 }
110             }
111         }
112         return _resourceLoaders;
113     }
114 }