1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.resource;
20
21 import java.io.InputStream;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.Iterator;
25 import java.util.Set;
26 import java.util.regex.Pattern;
27 import javax.faces.context.FacesContext;
28 import org.apache.myfaces.shared.resource.ContractResourceLoader;
29 import org.apache.myfaces.shared.resource.ResourceMeta;
30 import org.apache.myfaces.shared.resource.ResourceMetaImpl;
31
32
33
34
35
36 public class ExternalContextContractResourceLoader extends ContractResourceLoader
37 {
38
39
40
41
42
43
44 protected static final Pattern VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*/");
45
46
47
48
49
50
51 protected static final Pattern RESOURCE_VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*\\..*");
52
53 public ExternalContextContractResourceLoader(String prefix)
54 {
55 super(prefix);
56 }
57
58 protected Set<String> getResourcePaths(String contractName, String path)
59 {
60 return FacesContext.getCurrentInstance().getExternalContext().getResourcePaths(
61 getPrefix() + '/' + contractName + '/' + path);
62 }
63
64 @Override
65 public String getResourceVersion(String path, String contractName)
66 {
67 String resourceVersion = null;
68 Set<String> resourcePaths = this.getResourcePaths(contractName, path);
69 if (getPrefix() != null)
70 {
71 path = getPrefix() + '/' + path;
72 }
73
74 if (null != resourcePaths && !resourcePaths.isEmpty())
75 {
76
77
78
79
80
81
82 for (String resourcePath : resourcePaths)
83 {
84 String version = "";
85 if (path.length() < resourcePath.length())
86 {
87 version = resourcePath.substring(path.length());
88 }
89
90 if (RESOURCE_VERSION_CHECKER.matcher(version).matches())
91 {
92 version = version.substring(1, version.lastIndexOf('.'));
93 if (resourceVersion == null)
94 {
95 resourceVersion = version;
96 }
97 else if (getVersionComparator().compare(resourceVersion, version) < 0)
98 {
99 resourceVersion = version;
100 }
101 }
102 }
103
104 if (resourceVersion == null)
105 {
106 resourceVersion = VERSION_INVALID;
107 }
108 }
109 return resourceVersion;
110 }
111
112 @Override
113 public String getLibraryVersion(String path, String contractName)
114 {
115 String libraryVersion = null;
116 Set<String> libraryPaths = this.getResourcePaths(contractName, path);
117 path = getPrefix() + '/' + path;
118 if (null != libraryPaths && !libraryPaths.isEmpty())
119 {
120
121
122
123
124
125
126 for (Iterator<String> it = libraryPaths.iterator(); it.hasNext();)
127 {
128 String libraryPath = it.next();
129 String version = "";
130 if (path.length() < libraryPath.length())
131 {
132 version = libraryPath.substring(path.length());
133 }
134
135 if (VERSION_CHECKER.matcher(version).matches())
136 {
137 version = version.substring(1, version.length() - 1);
138 if (libraryVersion == null)
139 {
140 libraryVersion = version;
141 }
142 else if (getVersionComparator().compare(libraryVersion, version) < 0)
143 {
144 libraryVersion = version;
145 }
146 }
147 }
148 }
149 return libraryVersion;
150 }
151
152 @Override
153 public URL getResourceURL(ResourceMeta resourceMeta)
154 {
155 try
156 {
157 return FacesContext.getCurrentInstance().getExternalContext().getResource(
158 getPrefix() + '/' + resourceMeta.getContractName() + '/' + resourceMeta.getResourceIdentifier());
159 }
160 catch (MalformedURLException e)
161 {
162 return null;
163 }
164 }
165
166 @Override
167 public InputStream getResourceInputStream(ResourceMeta resourceMeta)
168 {
169 return FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(
170 getPrefix() + '/' + resourceMeta.getContractName() + '/' + resourceMeta.getResourceIdentifier());
171 }
172
173 @Override
174 public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
175 String resourceName, String resourceVersion, String contractName)
176 {
177 return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName,
178 resourceVersion, contractName);
179 }
180
181 @Override
182 public boolean libraryExists(String libraryName, String contractName)
183 {
184 if (getPrefix() != null && !"".equals(getPrefix()))
185 {
186 try
187 {
188 URL url =
189 FacesContext.getCurrentInstance().getExternalContext().getResource(
190 getPrefix() + '/' + contractName + '/' + libraryName);
191 if (url != null)
192 {
193 return true;
194 }
195 }
196 catch (MalformedURLException e)
197 {
198 return false;
199 }
200 }
201 else
202 {
203 try
204 {
205
206 URL url = FacesContext.getCurrentInstance().
207 getExternalContext().getResource(contractName + '/' +libraryName);
208
209 if (url != null)
210 {
211 return true;
212 }
213 }
214 catch (MalformedURLException e)
215 {
216 return false;
217 }
218 }
219 return false;
220 }
221
222 }