Skip to content

Commit 0c1a244

Browse files
committed
Merge branch 'feature/advanced-system-props' into feature/146/predicate-shortcuts
# Conflicts: # config/src/main/java/io/scalecube/config/source/ClassPathConfigSource.java
2 parents 92a58c9 + f16438b commit 0c1a244

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
@@ -4,6 +4,7 @@
44
import io.scalecube.config.utils.ThrowableUtil;
55
import java.io.File;
66
import java.io.IOException;
7+
import java.net.MalformedURLException;
78
import java.net.URI;
89
import java.net.URISyntaxException;
910
import java.net.URL;
@@ -12,10 +13,10 @@
1213
import java.nio.file.FileSystems;
1314
import java.nio.file.Path;
1415
import java.util.ArrayList;
16+
import java.util.Arrays;
1517
import java.util.Collection;
1618
import java.util.Collections;
1719
import java.util.Enumeration;
18-
import java.util.HashSet;
1920
import java.util.LinkedHashSet;
2021
import java.util.List;
2122
import java.util.Map;
@@ -30,6 +31,9 @@
3031
import java.util.stream.Collectors;
3132

3233
public final class ClassPathConfigSource extends FilteredPathConfigSource {
34+
private static final String CLASSPATH = System.getProperty("java.class.path");
35+
private static final String PATH_SEPARATOR = System.getProperty("path.separator");
36+
3337
private Map<String, ConfigProperty> loadedConfig;
3438

3539
/**
@@ -119,40 +123,69 @@ public Map<String, ConfigProperty> loadConfig() {
119123
return loadedConfig = result;
120124
}
121125

122-
private Collection<URI> getClassPathEntries(ClassLoader classLoader) {
126+
static Collection<URI> getClassPathEntries(ClassLoader classloader) {
123127
Collection<URI> entries = new LinkedHashSet<>();
124-
// Search parent first, since it's the order ClassLoader#loadClass() uses.
125-
ClassLoader parent = classLoader.getParent();
128+
ClassLoader parent = classloader.getParent();
126129
if (parent != null) {
127130
entries.addAll(getClassPathEntries(parent));
128131
}
129-
if (classLoader instanceof URLClassLoader) {
130-
URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
131-
for (URL entry : urlClassLoader.getURLs()) {
132+
for (URL url : getClassLoaderUrls(classloader)) {
133+
if (url.getProtocol().equals("file")) {
134+
entries.add(toFile(url).toURI());
135+
}
136+
}
137+
return new LinkedHashSet<>(entries);
138+
}
139+
140+
private static File toFile(URL url) {
141+
if (!url.getProtocol().equals("file")) {
142+
throw new IllegalArgumentException("Unsupported protocol in url: " + url);
143+
}
144+
try {
145+
return new File(url.toURI());
146+
} catch (URISyntaxException e) {
147+
return new File(url.getPath());
148+
}
149+
}
150+
151+
private static Collection<URL> getClassLoaderUrls(ClassLoader classloader) {
152+
if (classloader instanceof URLClassLoader) {
153+
return Arrays.stream(((URLClassLoader) classloader).getURLs()).collect(Collectors.toSet());
154+
}
155+
if (classloader.equals(ClassLoader.getSystemClassLoader())) {
156+
return parseJavaClassPath();
157+
}
158+
return Collections.emptySet();
159+
}
160+
161+
private static Collection<URL> parseJavaClassPath() {
162+
Collection<URL> urls = new LinkedHashSet<>();
163+
for (String entry : CLASSPATH.split(PATH_SEPARATOR)) {
164+
try {
132165
try {
133-
entries.add(entry.toURI());
134-
} catch (URISyntaxException e) {
135-
throw ThrowableUtil.propagate(e);
166+
urls.add(new File(entry).toURI().toURL());
167+
} catch (SecurityException e) {
168+
urls.add(new URL("file", null, new File(entry).getAbsolutePath()));
136169
}
170+
} catch (MalformedURLException ex) {
171+
throw ThrowableUtil.propagate(ex);
137172
}
138173
}
139-
return new LinkedHashSet<>(entries);
174+
return new LinkedHashSet<>(urls);
140175
}
141176

142177
private void scanDirectory(
143178
File directory, String prefix, Set<File> ancestors, Collection<Path> collector)
144179
throws IOException {
145180
File canonical = directory.getCanonicalFile();
146181
if (ancestors.contains(canonical)) {
147-
// A cycle in the filesystem, for example due to a symbolic link.
148182
return;
149183
}
150184
File[] files = directory.listFiles();
151185
if (files == null) {
152186
return;
153187
}
154-
HashSet<File> objects = new HashSet<>();
155-
objects.addAll(ancestors);
188+
Set<File> objects = new LinkedHashSet<>(ancestors);
156189
objects.add(canonical);
157190
Set<File> newAncestors = Collections.unmodifiableSet(objects);
158191
for (File f : files) {

0 commit comments

Comments
 (0)