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