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.config;
20  
21  import java.io.IOException;
22  import java.net.URL;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Enumeration;
26  import java.util.List;
27  
28  import javax.faces.context.ExternalContext;
29  import org.apache.myfaces.shared.config.MyfacesConfig;
30  
31  import org.apache.myfaces.shared.util.ClassUtils;
32  import org.apache.myfaces.spi.FacesConfigResourceProvider;
33  import org.apache.myfaces.util.ContainerUtils;
34  import org.apache.myfaces.config.util.GAEUtils;
35  import org.apache.myfaces.view.facelets.util.Classpath;
36  
37  /**
38   * 
39   * @since 2.0.2
40   * @author Leonardo Uribe
41   */
42  public class DefaultFacesConfigResourceProvider extends FacesConfigResourceProvider
43  {
44      private static final String META_INF_PREFIX = "META-INF/";
45  
46      private static final String FACES_CONFIG_SUFFIX = ".faces-config.xml";
47      
48      /**
49       * <p>Resource path used to acquire implicit resources buried
50       * inside application JARs.</p>
51       */
52      private static final String FACES_CONFIG_IMPLICIT = "META-INF/faces-config.xml";
53  
54  
55      public DefaultFacesConfigResourceProvider()
56      {
57          super();
58      }
59  
60      @Override
61      public Collection<URL> getMetaInfConfigurationResources(
62              ExternalContext context) throws IOException
63      {
64          List<URL> urlSet = new ArrayList<URL>();
65          
66          //This usually happens when maven-jetty-plugin is used
67          //Scan jars looking for paths including META-INF/faces-config.xml
68          Enumeration<URL> resources = getClassLoader().getResources(FACES_CONFIG_IMPLICIT);
69          while (resources.hasMoreElements())
70          {
71              urlSet.add(resources.nextElement());
72          }
73  
74          String jarFilesToScanParam = MyfacesConfig.getCurrentInstance(context).getGaeJsfJarFiles();
75          jarFilesToScanParam = jarFilesToScanParam != null ? jarFilesToScanParam.trim() : null;
76          if (ContainerUtils.isRunningOnGoogleAppEngine(context) && 
77              jarFilesToScanParam != null &&
78              jarFilesToScanParam.length() > 0)
79          {
80              Collection<URL> urlsGAE = GAEUtils.searchInWebLib(
81                      context, getClassLoader(), jarFilesToScanParam, META_INF_PREFIX, FACES_CONFIG_SUFFIX);
82              if (urlsGAE != null)
83              {
84                  urlSet.addAll(urlsGAE);
85              }
86          }
87          else
88          {
89              //Scan files inside META-INF ending with .faces-config.xml
90              URL[] urls = Classpath.search(getClassLoader(), META_INF_PREFIX, FACES_CONFIG_SUFFIX);
91              for (int i = 0; i < urls.length; i++)
92              {
93                  urlSet.add(urls[i]);
94              }
95          }
96          
97          return urlSet;
98      }
99  
100     private ClassLoader getClassLoader()
101     {
102         ClassLoader loader = ClassUtils.getContextClassLoader();
103         if (loader == null)
104         {
105             loader = this.getClass().getClassLoader();
106         }
107         return loader;
108     }
109 }