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.shared.resource;
20  
21  import java.io.InputStream;
22  import java.net.URL;
23  import java.util.Comparator;
24  
25  /**
26   * Base class for resource loaders.  Resource loaders can lookup resources 
27   * as URLs from arbitrary locations, including JAR files.
28   */
29  public abstract class ResourceLoader
30  {
31      
32      public static final String VERSION_INVALID = "INVALID";
33      
34      private String _prefix;
35      
36      public ResourceLoader(String prefix)
37      {
38          _prefix = prefix;
39      }
40  
41      public abstract String getResourceVersion(String path);
42  
43      /**
44       * Return the max available version found (if exists) or
45       * return null if no version available. 
46       */
47      public abstract String getLibraryVersion(String path);
48  
49      /**
50       * Return the max available version found (if exists) or
51       * return null if no version available. 
52       */
53      public abstract URL getResourceURL(ResourceMeta resourceMeta);
54  
55      public abstract InputStream getResourceInputStream(ResourceMeta resourceMeta);
56      
57      public abstract ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
58              String resourceName, String resourceVersion);
59  
60      public ResourceMeta createResourceMeta(String prefix, String libraryName, 
61              String libraryVersion, String resourceName, String resourceVersion, String contractName)
62      {
63          return createResourceMeta(prefix, libraryName, libraryVersion, resourceName, resourceVersion);
64      }
65      
66      public abstract boolean libraryExists(String libraryName);
67      
68      public boolean resourceExists(ResourceMeta resourceMeta)
69      {
70          return (getResourceURL(resourceMeta) != null);
71      }
72  
73      /*
74      public URL getResourceURL(String resourceId)
75      {
76          throw new UnsupportedOperationException(
77              "An implementation for getResourceURL(String resourceId) method is required for JSF 2.2");
78      }
79      
80      public boolean resourceIdExists(String resourceId)
81      {
82          return (getResourceURL(resourceId) != null);
83      }*/
84      
85      private Comparator<String> _versionComparator = null;
86  
87      protected Comparator<String> getVersionComparator()
88      {
89          if (_versionComparator == null)
90          {
91              _versionComparator = new VersionComparator();
92          }
93          return _versionComparator;
94      }
95  
96      protected void setVersionComparator(Comparator<String> versionComparator)
97      {
98          _versionComparator = versionComparator;
99      }
100 
101     public static class VersionComparator implements Comparator<String>
102     {
103 
104         public int compare(String s1, String s2)
105         {
106             int n1 = 0;
107             int n2 = 0;
108             String o1 = s1;
109             String o2 = s2;
110 
111             boolean p1 = true;
112             boolean p2 = true;
113 
114             while (n1 == n2 && (p1 || p2))
115             {
116                 int i1 = o1.indexOf('_');
117                 int i2 = o2.indexOf('_');
118                 if (i1 < 0)
119                 {
120                     if (o1.length() > 0)
121                     {
122                         p1 = false;
123                         n1 = Integer.valueOf(o1);
124                         o1 = "";
125                     }
126                     else
127                     {
128                         p1 = false;
129                         n1 = 0;
130                     }
131                 }
132                 else
133                 {
134                     n1 = Integer.valueOf(o1.substring(0, i1));
135                     o1 = o1.substring(i1 + 1);
136                 }
137                 if (i2 < 0)
138                 {
139                     if (o2.length() > 0)
140                     {
141                         p2 = false;
142                         n2 = Integer.valueOf(o2);
143                         o2 = "";
144                     }
145                     else
146                     {
147                         p2 = false;
148                         n2 = 0;
149                     }
150                 }
151                 else
152                 {
153                     n2 = Integer.valueOf(o2.substring(0, i2));
154                     o2 = o2.substring(i2 + 1);
155                 }
156             }
157 
158             if (n1 == n2)
159             {
160                 return s1.length() - s2.length();
161             }
162             return n1 - n2;
163         }
164     }
165     
166     public String getPrefix()
167     {
168         return _prefix;
169     }
170 
171     public void setPrefix(String prefix)
172     {
173         _prefix = prefix;
174     }
175 }