Skip to content

Commit 54556a3

Browse files
committed
Add support for webjars-locator.properties file
1 parent 4c44ceb commit 54556a3

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/main/java/org/webjars/WebJarVersionLocator.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import java.io.IOException;
77
import java.io.InputStream;
8+
import java.net.URL;
9+
import java.util.Enumeration;
10+
import java.util.Map;
811
import java.util.Optional;
912
import java.util.Properties;
1013
import java.util.concurrent.ConcurrentHashMap;
@@ -25,6 +28,7 @@ public class WebJarVersionLocator {
2528
private static final String NPM = "org.webjars.npm/";
2629
private static final String PLAIN = "org.webjars/";
2730
private static final String POM_PROPERTIES = "/pom.properties";
31+
private static final String LOCATOR_PROPERTIES = "META-INF/resources/webjars-locator.properties";
2832

2933
private static final String CACHE_KEY_PREFIX = "version-";
3034

@@ -34,10 +38,12 @@ public class WebJarVersionLocator {
3438

3539
public WebJarVersionLocator() {
3640
this.cache = new WebJarCacheDefault(new ConcurrentHashMap<>());
41+
readLocatorProperties();
3742
}
3843

3944
WebJarVersionLocator(WebJarCache cache) {
4045
this.cache = cache;
46+
readLocatorProperties();
4147
}
4248

4349
/**
@@ -128,6 +134,36 @@ public String version(final String webJarName) {
128134
return optionalVersion.orElse(null);
129135
}
130136

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+
131167
private boolean hasResourcePath(final String webJarName, final String path) {
132168
return LOADER.getResource(WEBJARS_PATH_PREFIX + "/" + webJarName + "/" + path) != null;
133169
}

0 commit comments

Comments
 (0)