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.resource;
20  
21  import java.io.InputStream;
22  import java.net.URL;
23  import java.util.Iterator;
24  import javax.faces.application.ResourceVisitOption;
25  import javax.faces.context.FacesContext;
26  import org.apache.myfaces.shared.resource.ClassLoaderResourceLoaderIterator;
27  import org.apache.myfaces.shared.resource.ContractResourceLoader;
28  import org.apache.myfaces.shared.resource.ResourceMeta;
29  import org.apache.myfaces.shared.resource.ResourceMetaImpl;
30  import org.apache.myfaces.shared.util.ClassUtils;
31  
32  /**
33   *
34   * @author Leonardo Uribe
35   */
36  public class ClassLoaderContractResourceLoader extends ContractResourceLoader
37  {
38  
39      public ClassLoaderContractResourceLoader(String prefix)
40      {
41          super(prefix);
42      }
43  
44      @Override
45      public String getLibraryVersion(String path, String contractName)
46      {
47          return null;
48      }
49  
50      @Override
51      public InputStream getResourceInputStream(ResourceMeta resourceMeta)
52      {
53          InputStream is = null;
54          if (getPrefix() != null && !"".equals(getPrefix()))
55          {
56              String name = getPrefix() + '/' + resourceMeta.getContractName() 
57                  + '/' + resourceMeta.getResourceIdentifier();
58              is = getClassLoader().getResourceAsStream(name);
59              if (is == null)
60              {
61                  is = this.getClass().getClassLoader().getResourceAsStream(name);
62              }
63              return is;
64          }
65          else
66          {
67              String name = resourceMeta.getContractName() 
68                  + '/' + resourceMeta.getResourceIdentifier();
69              is = getClassLoader().getResourceAsStream(name);
70              if (is == null)
71              {
72                  is = this.getClass().getClassLoader().getResourceAsStream(name);
73              }
74              return is;
75          }
76      }
77  
78      @Override
79      public URL getResourceURL(ResourceMeta resourceMeta)
80      {
81          URL url = null;
82          if (getPrefix() != null && !"".equals(getPrefix()))
83          {
84              String name = getPrefix() + '/' + resourceMeta.getContractName() + 
85                  '/' + resourceMeta.getResourceIdentifier();
86              url = getClassLoader().getResource(name);
87              if (url == null)
88              {
89                  url = this.getClass().getClassLoader().getResource(name);
90              }
91              return url;
92          }
93          else
94          {
95              String name = resourceMeta.getContractName() + '/' + resourceMeta.getResourceIdentifier();
96              url = getClassLoader().getResource(name);
97              if (url == null)
98              {
99                  url = this.getClass().getClassLoader().getResource(name);
100             }
101             return url;
102         }
103     }
104 
105     @Override
106     public String getResourceVersion(String path, String contractName)
107     {
108         return null;
109     }
110 
111     @Override
112     public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
113                                            String resourceName, String resourceVersion, 
114                                            String contractName)
115     {
116         return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, 
117             resourceVersion, contractName);
118     }
119 
120     /**
121      * Returns the ClassLoader to use when looking up resources under the top level package. By default, this is the
122      * context class loader.
123      * 
124      * @return the ClassLoader used to lookup resources
125      */
126     protected ClassLoader getClassLoader()
127     {
128         return ClassUtils.getContextClassLoader();
129     }
130 
131     @Override
132     public boolean libraryExists(String libraryName, String contractName)
133     {
134         if (getPrefix() != null && !"".equals(getPrefix()))
135         {
136             String name = getPrefix() + '/' + 
137                 contractName + '/' + libraryName;
138             URL url = getClassLoader().getResource(name);
139             if (url == null)
140             {
141                 url = this.getClass().getClassLoader().getResource(name);
142             }
143             if (url != null)
144             {
145                 return true;
146             }
147         }
148         else
149         {
150             String name = contractName + '/' + libraryName;
151             URL url = getClassLoader().getResource(name);
152             if (url == null)
153             {
154                 url = this.getClass().getClassLoader().getResource(name);
155             }
156             if (url != null)
157             {
158                 return true;
159             }
160         }
161         return false;
162     }
163     
164     @Override
165     public Iterator<String> iterator(FacesContext facesContext, String path, 
166             int maxDepth, ResourceVisitOption... options)
167     {
168         String basePath = path;
169         
170         if (getPrefix() != null)
171         {
172             basePath = getPrefix() + '/' + (path.startsWith("/") ? path.substring(1) : path);
173         }
174 
175         URL url = getClassLoader().getResource(basePath);
176 
177         return new ClassLoaderResourceLoaderIterator(url, basePath, maxDepth, options);
178     }
179 
180 }