5
5
6
6
import java .io .IOException ;
7
7
import java .io .InputStream ;
8
+ import java .net .URL ;
9
+ import java .util .Enumeration ;
10
+ import java .util .Map ;
8
11
import java .util .Optional ;
9
12
import java .util .Properties ;
10
13
import java .util .concurrent .ConcurrentHashMap ;
@@ -25,17 +28,22 @@ public class WebJarVersionLocator {
25
28
private static final String NPM = "org.webjars.npm/" ;
26
29
private static final String PLAIN = "org.webjars/" ;
27
30
private static final String POM_PROPERTIES = "/pom.properties" ;
31
+ private static final String LOCATOR_PROPERTIES = "META-INF/resources/webjars-locator.properties" ;
32
+
33
+ private static final String CACHE_KEY_PREFIX = "version-" ;
28
34
29
35
private static final ClassLoader LOADER = WebJarVersionLocator .class .getClassLoader ();
30
36
31
37
private final WebJarCache cache ;
32
38
33
39
public WebJarVersionLocator () {
34
40
this .cache = new WebJarCacheDefault (new ConcurrentHashMap <>());
41
+ readLocatorProperties ();
35
42
}
36
43
37
44
WebJarVersionLocator (WebJarCache cache ) {
38
45
this .cache = cache ;
46
+ readLocatorProperties ();
39
47
}
40
48
41
49
/**
@@ -83,7 +91,7 @@ public String path(final String webJarName, final String exactPath) {
83
91
*/
84
92
@ Nullable
85
93
public String version (final String webJarName ) {
86
- final String cacheKey = "version-" + webJarName ;
94
+ final String cacheKey = CACHE_KEY_PREFIX + webJarName ;
87
95
final Optional <String > optionalVersion = cache .computeIfAbsent (cacheKey , (key ) -> {
88
96
InputStream resource = LOADER .getResourceAsStream (PROPERTIES_ROOT + NPM + webJarName + POM_PROPERTIES );
89
97
if (resource == null ) {
@@ -126,6 +134,36 @@ public String version(final String webJarName) {
126
134
return optionalVersion .orElse (null );
127
135
}
128
136
137
+ private void readLocatorProperties () {
138
+ try {
139
+ Enumeration <URL > resources = LOADER .getResources (LOCATOR_PROPERTIES );
140
+ while (resources .hasMoreElements ()) {
141
+ URL resourceUrl = resources .nextElement ();
142
+ try (InputStream resource = resourceUrl .openStream ()) {
143
+ Properties properties = new Properties ();
144
+ properties .load (resource );
145
+ for (Map .Entry <Object , Object > entry : properties .entrySet ()) {
146
+ String webJarName = entry .getKey ().toString ();
147
+ if (!webJarName .endsWith (".version" )) {
148
+ // ".version" suffix is required
149
+ continue ;
150
+ }
151
+
152
+ webJarName = webJarName .substring (0 , webJarName .lastIndexOf (".version" ));
153
+
154
+ String version = entry .getValue ().toString ();
155
+ if (hasResourcePath (webJarName , version )) {
156
+ // Only add configured versions if their path exists
157
+ cache .computeIfAbsent (CACHE_KEY_PREFIX + webJarName , x -> Optional .of (version ));
158
+ }
159
+ }
160
+ }
161
+ }
162
+ } catch (IOException e ) {
163
+ throw new RuntimeException ("unable to load locator properties" , e );
164
+ }
165
+ }
166
+
129
167
private boolean hasResourcePath (final String webJarName , final String path ) {
130
168
return LOADER .getResource (WEBJARS_PATH_PREFIX + "/" + webJarName + "/" + path ) != null ;
131
169
}
0 commit comments