|
4 | 4 | import io.scalecube.config.utils.ThrowableUtil; |
5 | 5 | import java.io.File; |
6 | 6 | import java.io.IOException; |
| 7 | +import java.net.MalformedURLException; |
7 | 8 | import java.net.URI; |
8 | 9 | import java.net.URISyntaxException; |
9 | 10 | import java.net.URL; |
|
12 | 13 | import java.nio.file.FileSystems; |
13 | 14 | import java.nio.file.Path; |
14 | 15 | import java.util.ArrayList; |
| 16 | +import java.util.Arrays; |
15 | 17 | import java.util.Collection; |
16 | 18 | import java.util.Collections; |
17 | 19 | import java.util.Enumeration; |
18 | | -import java.util.HashSet; |
19 | 20 | import java.util.LinkedHashSet; |
20 | 21 | import java.util.List; |
21 | 22 | import java.util.Map; |
|
30 | 31 | import java.util.stream.Collectors; |
31 | 32 |
|
32 | 33 | 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 | + |
33 | 37 | private Map<String, ConfigProperty> loadedConfig; |
34 | 38 |
|
35 | 39 | /** |
@@ -119,40 +123,69 @@ public Map<String, ConfigProperty> loadConfig() { |
119 | 123 | return loadedConfig = result; |
120 | 124 | } |
121 | 125 |
|
122 | | - private Collection<URI> getClassPathEntries(ClassLoader classLoader) { |
| 126 | + static Collection<URI> getClassPathEntries(ClassLoader classloader) { |
123 | 127 | 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(); |
126 | 129 | if (parent != null) { |
127 | 130 | entries.addAll(getClassPathEntries(parent)); |
128 | 131 | } |
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 { |
132 | 165 | 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())); |
136 | 169 | } |
| 170 | + } catch (MalformedURLException ex) { |
| 171 | + throw ThrowableUtil.propagate(ex); |
137 | 172 | } |
138 | 173 | } |
139 | | - return new LinkedHashSet<>(entries); |
| 174 | + return new LinkedHashSet<>(urls); |
140 | 175 | } |
141 | 176 |
|
142 | 177 | private void scanDirectory( |
143 | 178 | File directory, String prefix, Set<File> ancestors, Collection<Path> collector) |
144 | 179 | throws IOException { |
145 | 180 | File canonical = directory.getCanonicalFile(); |
146 | 181 | if (ancestors.contains(canonical)) { |
147 | | - // A cycle in the filesystem, for example due to a symbolic link. |
148 | 182 | return; |
149 | 183 | } |
150 | 184 | File[] files = directory.listFiles(); |
151 | 185 | if (files == null) { |
152 | 186 | return; |
153 | 187 | } |
154 | | - HashSet<File> objects = new HashSet<>(); |
155 | | - objects.addAll(ancestors); |
| 188 | + Set<File> objects = new LinkedHashSet<>(ancestors); |
156 | 189 | objects.add(canonical); |
157 | 190 | Set<File> newAncestors = Collections.unmodifiableSet(objects); |
158 | 191 | for (File f : files) { |
|
0 commit comments