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.commons.resourcehandler.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   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
30   * @version $Revision: 882702 $ $Date: 2009-11-20 15:16:07 -0500 (Vie, 20 Nov 2009) $
31   */
32  public abstract class ResourceLoader
33  {
34      
35      public static final String VERSION_INVALID = "INVALID";
36      
37      private String _prefix;
38      
39      public ResourceLoader(String prefix)
40      {
41          _prefix = prefix;
42      }
43  
44      public abstract String getResourceVersion(String path);
45  
46      /**
47       * Return the max available version found (if exists) or
48       * return null if no version available. 
49       */
50      public abstract String getLibraryVersion(String path);
51  
52      /**
53       * Return the max available version found (if exists) or
54       * return null if no version available. 
55       */
56      public abstract URL getResourceURL(ResourceMeta resourceMeta);
57  
58      public abstract InputStream getResourceInputStream(ResourceMeta resourceMeta);
59      
60      public abstract ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
61              String resourceName, String resourceVersion);
62      
63      public abstract boolean libraryExists(String libraryName);
64      
65      public boolean resourceExists(ResourceMeta resourceMeta)
66      {
67          return (getResourceURL(resourceMeta) != null);
68      }
69      
70      private Comparator<String> _versionComparator = null;
71  
72      protected Comparator<String> getVersionComparator()
73      {
74          if (_versionComparator == null)
75          {
76              _versionComparator = new VersionComparator();
77          }
78          return _versionComparator;
79      }
80  
81      protected void setVersionComparator(Comparator<String> versionComparator)
82      {
83          _versionComparator = versionComparator;
84      }
85  
86      public class VersionComparator implements Comparator<String>
87      {
88  
89          public int compare(String s1, String s2)
90          {
91              int n1 = 0;
92              int n2 = 0;
93              String o1 = s1;
94              String o2 = s2;
95  
96              boolean p1 = true;
97              boolean p2 = true;
98  
99              while (n1 == n2 && (p1 || p2))
100             {
101                 int i1 = o1.indexOf('_');
102                 int i2 = o2.indexOf('_');
103                 if (i1 < 0)
104                 {
105                     if (o1.length() > 0)
106                     {
107                         p1 = false;
108                         n1 = Integer.valueOf(o1);
109                         o1 = "";
110                     }
111                     else
112                     {
113                         p1 = false;
114                         n1 = 0;
115                     }
116                 }
117                 else
118                 {
119                     n1 = Integer.valueOf(o1.substring(0, i1));
120                     o1 = o1.substring(i1 + 1);
121                 }
122                 if (i2 < 0)
123                 {
124                     if (o2.length() > 0)
125                     {
126                         p2 = false;
127                         n2 = Integer.valueOf(o2);
128                         o2 = "";
129                     }
130                     else
131                     {
132                         p2 = false;
133                         n2 = 0;
134                     }
135                 }
136                 else
137                 {
138                     n2 = Integer.valueOf(o2.substring(0, i2));
139                     o2 = o2.substring(i2 + 1);
140                 }
141             }
142 
143             if (n1 == n2)
144             {
145                 return s1.length() - s2.length();
146             }
147             return n1 - n2;
148         }
149     }
150     
151     public String getPrefix()
152     {
153         return _prefix;
154     }
155 
156     public void setPrefix(String prefix)
157     {
158         _prefix = prefix;
159     }
160 }