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  import java.util.Iterator;
25  import javax.faces.application.ResourceVisitOption;
26  import javax.faces.context.FacesContext;
27  
28  /**
29   * Base class for resource loaders.  Resource loaders can lookup resources 
30   * as URLs from arbitrary locations, including JAR files.
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 ResourceMeta createResourceMeta(String prefix, String libraryName, 
64              String libraryVersion, String resourceName, String resourceVersion, String contractName)
65      {
66          return createResourceMeta(prefix, libraryName, libraryVersion, resourceName, resourceVersion);
67      }
68      
69      public abstract boolean libraryExists(String libraryName);
70      
71      public boolean resourceExists(ResourceMeta resourceMeta)
72      {
73          return (getResourceURL(resourceMeta) != null);
74      }
75  
76      /*
77      public URL getResourceURL(String resourceId)
78      {
79          throw new UnsupportedOperationException(
80              "An implementation for getResourceURL(String resourceId) method is required for JSF 2.2");
81      }
82      
83      public boolean resourceIdExists(String resourceId)
84      {
85          return (getResourceURL(resourceId) != null);
86      }*/
87      
88      public Iterator<String> iterator(FacesContext facesContext, 
89              String path, int maxDepth, ResourceVisitOption... options)
90      {
91          return null;
92      }
93      
94      private Comparator<String> _versionComparator = null;
95  
96      protected Comparator<String> getVersionComparator()
97      {
98          if (_versionComparator == null)
99          {
100             _versionComparator = new VersionComparator();
101         }
102         return _versionComparator;
103     }
104 
105     protected void setVersionComparator(Comparator<String> versionComparator)
106     {
107         _versionComparator = versionComparator;
108     }
109 
110     public static class VersionComparator implements Comparator<String>
111     {
112 
113         public int compare(String s1, String s2)
114         {
115             int n1 = 0;
116             int n2 = 0;
117             String o1 = s1;
118             String o2 = s2;
119 
120             boolean p1 = true;
121             boolean p2 = true;
122 
123             while (n1 == n2 && (p1 || p2))
124             {
125                 int i1 = o1.indexOf('_');
126                 int i2 = o2.indexOf('_');
127                 if (i1 < 0)
128                 {
129                     if (o1.length() > 0)
130                     {
131                         p1 = false;
132                         n1 = Integer.valueOf(o1);
133                         o1 = "";
134                     }
135                     else
136                     {
137                         p1 = false;
138                         n1 = 0;
139                     }
140                 }
141                 else
142                 {
143                     n1 = Integer.valueOf(o1.substring(0, i1));
144                     o1 = o1.substring(i1 + 1);
145                 }
146                 if (i2 < 0)
147                 {
148                     if (o2.length() > 0)
149                     {
150                         p2 = false;
151                         n2 = Integer.valueOf(o2);
152                         o2 = "";
153                     }
154                     else
155                     {
156                         p2 = false;
157                         n2 = 0;
158                     }
159                 }
160                 else
161                 {
162                     n2 = Integer.valueOf(o2.substring(0, i2));
163                     o2 = o2.substring(i2 + 1);
164                 }
165             }
166 
167             if (n1 == n2)
168             {
169                 return s1.length() - s2.length();
170             }
171             return n1 - n2;
172         }
173     }
174     
175     public String getPrefix()
176     {
177         return _prefix;
178     }
179 
180     public void setPrefix(String prefix)
181     {
182         _prefix = prefix;
183     }
184 }