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