|
5 | 5 | import io.scalecube.config.utils.ThrowableUtil; |
6 | 6 | import java.io.File; |
7 | 7 | import java.io.IOException; |
| 8 | +import java.net.MalformedURLException; |
8 | 9 | import java.net.URI; |
9 | 10 | import java.net.URISyntaxException; |
10 | 11 | import java.net.URL; |
|
17 | 18 | import java.util.Collection; |
18 | 19 | import java.util.Collections; |
19 | 20 | import java.util.Enumeration; |
20 | | -import java.util.HashSet; |
21 | 21 | import java.util.LinkedHashSet; |
22 | 22 | import java.util.List; |
23 | 23 | import java.util.Map; |
|
27 | 27 | import java.util.function.Predicate; |
28 | 28 | import java.util.jar.JarEntry; |
29 | 29 | import java.util.jar.JarFile; |
| 30 | +import java.util.stream.Collectors; |
30 | 31 |
|
31 | 32 | 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 | + |
32 | 36 | private final ClassLoader classLoader; |
33 | 37 |
|
34 | 38 | private Map<String, ConfigProperty> loadedConfig; |
@@ -104,40 +108,69 @@ public Map<String, ConfigProperty> loadConfig() { |
104 | 108 | return loadedConfig = result; |
105 | 109 | } |
106 | 110 |
|
107 | | - private Collection<URI> getClassPathEntries(ClassLoader classLoader) { |
| 111 | + static Collection<URI> getClassPathEntries(ClassLoader classloader) { |
108 | 112 | 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(); |
111 | 114 | if (parent != null) { |
112 | 115 | entries.addAll(getClassPathEntries(parent)); |
113 | 116 | } |
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 { |
117 | 150 | 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())); |
121 | 154 | } |
| 155 | + } catch (MalformedURLException ex) { |
| 156 | + throw ThrowableUtil.propagate(ex); |
122 | 157 | } |
123 | 158 | } |
124 | | - return new LinkedHashSet<>(entries); |
| 159 | + return new LinkedHashSet<>(urls); |
125 | 160 | } |
126 | 161 |
|
127 | 162 | private void scanDirectory( |
128 | 163 | File directory, String prefix, Set<File> ancestors, Collection<Path> collector) |
129 | 164 | throws IOException { |
130 | 165 | File canonical = directory.getCanonicalFile(); |
131 | 166 | if (ancestors.contains(canonical)) { |
132 | | - // A cycle in the filesystem, for example due to a symbolic link. |
133 | 167 | return; |
134 | 168 | } |
135 | 169 | File[] files = directory.listFiles(); |
136 | 170 | if (files == null) { |
137 | 171 | return; |
138 | 172 | } |
139 | | - HashSet<File> objects = new HashSet<>(); |
140 | | - objects.addAll(ancestors); |
| 173 | + Set<File> objects = new LinkedHashSet<>(ancestors); |
141 | 174 | objects.add(canonical); |
142 | 175 | Set<File> newAncestors = Collections.unmodifiableSet(objects); |
143 | 176 | for (File f : files) { |
|
0 commit comments