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.view.facelets.compiler;
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.List;
26  
27  import javax.faces.context.ExternalContext;
28  import org.apache.myfaces.shared.config.MyfacesConfig;
29  
30  import org.apache.myfaces.shared.util.ClassUtils;
31  import org.apache.myfaces.spi.FaceletConfigResourceProvider;
32  import org.apache.myfaces.util.ContainerUtils;
33  import org.apache.myfaces.config.util.GAEUtils;
34  import org.apache.myfaces.view.facelets.util.Classpath;
35  
36  /**
37   * 
38   * @since 2.0.2
39   * @author Leonardo Uribe
40   */
41  public class DefaultFaceletConfigResourceProvider extends FaceletConfigResourceProvider
42  {
43      private static final String META_INF_PREFIX = "META-INF/";
44  
45      private static final String FACELET_TAGLIB_SUFFIX = ".taglib.xml";
46  
47      public DefaultFaceletConfigResourceProvider()
48      {
49          super();
50      }
51  
52      @Override
53      public Collection<URL> getFaceletTagLibConfigurationResources(
54              ExternalContext context) throws IOException
55      {
56          List<URL> urlSet = new ArrayList<URL>();
57          
58          String jarFilesToScanParam = MyfacesConfig.getCurrentInstance(context).getGaeJsfJarFiles();
59          jarFilesToScanParam = jarFilesToScanParam != null ? jarFilesToScanParam.trim() : null;
60          if (ContainerUtils.isRunningOnGoogleAppEngine(context) && 
61              jarFilesToScanParam != null &&
62              jarFilesToScanParam.length() > 0)
63          {
64              Collection<URL> urlsGAE = GAEUtils.searchInWebLib(
65                      context, getClassLoader(), jarFilesToScanParam, META_INF_PREFIX, FACELET_TAGLIB_SUFFIX);
66              if (urlsGAE != null)
67              {
68                  urlSet.addAll(urlsGAE);
69              }
70          }
71          else
72          {
73              //Scan files inside META-INF ending with .faces-config.xml
74              URL[] urls = Classpath.search(getClassLoader(), META_INF_PREFIX, FACELET_TAGLIB_SUFFIX);
75              for (int i = 0; i < urls.length; i++)
76              {
77                  urlSet.add(urls[i]);
78              }
79          }
80          return urlSet;
81      }
82  
83      private ClassLoader getClassLoader()
84      {
85          ClassLoader loader = ClassUtils.getContextClassLoader();
86          if (loader == null)
87          {
88              loader = this.getClass().getClassLoader();
89          }
90          return loader;
91      }
92  }