Skip to content

Commit f16438b

Browse files
committed
Fixed classpath config source
1 parent bb0775f commit f16438b

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

config/src/main/java/io/scalecube/config/source/ClassPathConfigSource.java

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.scalecube.config.utils.ThrowableUtil;
66
import java.io.File;
77
import java.io.IOException;
8+
import java.net.MalformedURLException;
89
import java.net.URI;
910
import java.net.URISyntaxException;
1011
import java.net.URL;
@@ -17,7 +18,6 @@
1718
import java.util.Collection;
1819
import java.util.Collections;
1920
import java.util.Enumeration;
20-
import java.util.HashSet;
2121
import java.util.LinkedHashSet;
2222
import java.util.List;
2323
import java.util.Map;
@@ -27,8 +27,12 @@
2727
import java.util.function.Predicate;
2828
import java.util.jar.JarEntry;
2929
import java.util.jar.JarFile;
30+
import java.util.stream.Collectors;
3031

3132
public final class ClassPathConfigSource extends FilteredPathConfigSource {
33+
private static final String CLASSPATH = System.getProperty("java.class.path");
34+
private static final String PATH_SEPARATOR = System.getProperty("path.separator");
35+
3236
private final ClassLoader classLoader;
3337

3438
private Map<String, ConfigProperty> loadedConfig;
@@ -104,40 +108,69 @@ public Map<String, ConfigProperty> loadConfig() {
104108
return loadedConfig = result;
105109
}
106110

107-
private Collection<URI> getClassPathEntries(ClassLoader classLoader) {
111+
static Collection<URI> getClassPathEntries(ClassLoader classloader) {
108112
Collection<URI> entries = new LinkedHashSet<>();
109-
// Search parent first, since it's the order ClassLoader#loadClass() uses.
110-
ClassLoader parent = classLoader.getParent();
113+
ClassLoader parent = classloader.getParent();
111114
if (parent != null) {
112115
entries.addAll(getClassPathEntries(parent));
113116
}
114-
if (classLoader instanceof URLClassLoader) {
115-
URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
116-
for (URL entry : urlClassLoader.getURLs()) {
117+
for (URL url : getClassLoaderUrls(classloader)) {
118+
if (url.getProtocol().equals("file")) {
119+
entries.add(toFile(url).toURI());
120+
}
121+
}
122+
return new LinkedHashSet<>(entries);
123+
}
124+
125+
private static File toFile(URL url) {
126+
if (!url.getProtocol().equals("file")) {
127+
throw new IllegalArgumentException("Unsupported protocol in url: " + url);
128+
}
129+
try {
130+
return new File(url.toURI());
131+
} catch (URISyntaxException e) {
132+
return new File(url.getPath());
133+
}
134+
}
135+
136+
private static Collection<URL> getClassLoaderUrls(ClassLoader classloader) {
137+
if (classloader instanceof URLClassLoader) {
138+
return Arrays.stream(((URLClassLoader) classloader).getURLs()).collect(Collectors.toSet());
139+
}
140+
if (classloader.equals(ClassLoader.getSystemClassLoader())) {
141+
return parseJavaClassPath();
142+
}
143+
return Collections.emptySet();
144+
}
145+
146+
private static Collection<URL> parseJavaClassPath() {
147+
Collection<URL> urls = new LinkedHashSet<>();
148+
for (String entry : CLASSPATH.split(PATH_SEPARATOR)) {
149+
try {
117150
try {
118-
entries.add(entry.toURI());
119-
} catch (URISyntaxException e) {
120-
throw ThrowableUtil.propagate(e);
151+
urls.add(new File(entry).toURI().toURL());
152+
} catch (SecurityException e) {
153+
urls.add(new URL("file", null, new File(entry).getAbsolutePath()));
121154
}
155+
} catch (MalformedURLException ex) {
156+
throw ThrowableUtil.propagate(ex);
122157
}
123158
}
124-
return new LinkedHashSet<>(entries);
159+
return new LinkedHashSet<>(urls);
125160
}
126161

127162
private void scanDirectory(
128163
File directory, String prefix, Set<File> ancestors, Collection<Path> collector)
129164
throws IOException {
130165
File canonical = directory.getCanonicalFile();
131166
if (ancestors.contains(canonical)) {
132-
// A cycle in the filesystem, for example due to a symbolic link.
133167
return;
134168
}
135169
File[] files = directory.listFiles();
136170
if (files == null) {
137171
return;
138172
}
139-
HashSet<File> objects = new HashSet<>();
140-
objects.addAll(ancestors);
173+
Set<File> objects = new LinkedHashSet<>(ancestors);
141174
objects.add(canonical);
142175
Set<File> newAncestors = Collections.unmodifiableSet(objects);
143176
for (File f : files) {

0 commit comments

Comments
 (0)