1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.config.plugins.util;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.net.URLDecoder;
28 import java.nio.charset.StandardCharsets;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Enumeration;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35 import java.util.jar.JarEntry;
36 import java.util.jar.JarInputStream;
37
38 import org.apache.logging.log4j.Logger;
39 import org.apache.logging.log4j.core.util.Loader;
40 import org.apache.logging.log4j.status.StatusLogger;
41 import org.osgi.framework.FrameworkUtil;
42 import org.osgi.framework.wiring.BundleWiring;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public class ResolverUtil {
82
83 private static final Logger LOGGER = StatusLogger.getLogger();
84
85 private static final String VFSZIP = "vfszip";
86
87 private static final String VFS = "vfs";
88
89 private static final String BUNDLE_RESOURCE = "bundleresource";
90
91
92 private final Set<Class<?>> classMatches = new HashSet<>();
93
94
95 private final Set<URI> resourceMatches = new HashSet<>();
96
97
98
99
100
101 private ClassLoader classloader;
102
103
104
105
106
107
108
109 public Set<Class<?>> getClasses() {
110 return classMatches;
111 }
112
113
114
115
116
117
118 public Set<URI> getResources() {
119 return resourceMatches;
120 }
121
122
123
124
125
126
127
128 public ClassLoader getClassLoader() {
129 return classloader != null ? classloader : (classloader = Loader.getClassLoader(ResolverUtil.class, null));
130 }
131
132
133
134
135
136
137
138
139 public void setClassLoader(final ClassLoader aClassloader) {
140 this.classloader = aClassloader;
141 }
142
143
144
145
146
147
148
149
150
151
152 public void find(final Test test, final String... packageNames) {
153 if (packageNames == null) {
154 return;
155 }
156
157 for (final String pkg : packageNames) {
158 findInPackage(test, pkg);
159 }
160 }
161
162
163
164
165
166
167
168
169
170
171
172 public void findInPackage(final Test test, String packageName) {
173 packageName = packageName.replace('.', '/');
174 final ClassLoader loader = getClassLoader();
175 Enumeration<URL> urls;
176
177 try {
178 urls = loader.getResources(packageName);
179 } catch (final IOException ioe) {
180 LOGGER.warn("Could not read package: {}", packageName, ioe);
181 return;
182 }
183
184 while (urls.hasMoreElements()) {
185 try {
186 final URL url = urls.nextElement();
187 final String urlPath = extractPath(url);
188
189 LOGGER.info("Scanning for classes in '{}' matching criteria {}", urlPath , test);
190
191 if (VFSZIP.equals(url.getProtocol())) {
192 final String path = urlPath.substring(0, urlPath.length() - packageName.length() - 2);
193 final URL newURL = new URL(url.getProtocol(), url.getHost(), path);
194 @SuppressWarnings("resource")
195 final JarInputStream stream = new JarInputStream(newURL.openStream());
196 try {
197 loadImplementationsInJar(test, packageName, path, stream);
198 } finally {
199 close(stream, newURL);
200 }
201 } else if (VFS.equals(url.getProtocol())) {
202 final String containerPath = urlPath.substring(1,
203 urlPath.length() - packageName.length() - 2);
204 final File containerFile = new File(containerPath);
205 if (containerFile.isDirectory()) {
206 loadImplementationsInDirectory(test, packageName, new File(containerFile, packageName));
207 } else {
208 loadImplementationsInJar(test, packageName, containerFile);
209 }
210 } else if (BUNDLE_RESOURCE.equals(url.getProtocol())) {
211 loadImplementationsInBundle(test, packageName);
212 } else {
213 final File file = new File(urlPath);
214 if (file.isDirectory()) {
215 loadImplementationsInDirectory(test, packageName, file);
216 } else {
217 loadImplementationsInJar(test, packageName, file);
218 }
219 }
220 } catch (final IOException | URISyntaxException ioe) {
221 LOGGER.warn("Could not read entries", ioe);
222 }
223 }
224 }
225
226 String extractPath(final URL url) throws UnsupportedEncodingException, URISyntaxException {
227 String urlPath = url.getPath();
228
229
230
231 if (urlPath.startsWith("jar:")) {
232 urlPath = urlPath.substring(4);
233 }
234
235 if (urlPath.startsWith("file:")) {
236 urlPath = urlPath.substring(5);
237 }
238
239 final int bangIndex = urlPath.indexOf('!');
240 if (bangIndex > 0) {
241 urlPath = urlPath.substring(0, bangIndex);
242 }
243
244
245
246 final String protocol = url.getProtocol();
247 final List<String> neverDecode = Arrays.asList(VFS, VFSZIP, BUNDLE_RESOURCE);
248 if (neverDecode.contains(protocol)) {
249 return urlPath;
250 }
251 final String cleanPath = new URI(urlPath).getPath();
252 if (new File(cleanPath).exists()) {
253
254 return cleanPath;
255 }
256 return URLDecoder.decode(urlPath, StandardCharsets.UTF_8.name());
257 }
258
259 private void loadImplementationsInBundle(final Test test, final String packageName) {
260 final BundleWiring wiring = FrameworkUtil.getBundle(ResolverUtil.class).adapt(BundleWiring.class);
261 final Collection<String> list = wiring.listResources(packageName, "*.class",
262 BundleWiring.LISTRESOURCES_RECURSE);
263 for (final String name : list) {
264 addIfMatching(test, name);
265 }
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 private void loadImplementationsInDirectory(final Test test, final String parent, final File location) {
284 final File[] files = location.listFiles();
285 if (files == null) {
286 return;
287 }
288
289 StringBuilder builder;
290 for (final File file : files) {
291 builder = new StringBuilder();
292 builder.append(parent).append('/').append(file.getName());
293 final String packageOrClass = parent == null ? file.getName() : builder.toString();
294
295 if (file.isDirectory()) {
296 loadImplementationsInDirectory(test, packageOrClass, file);
297 } else if (isTestApplicable(test, file.getName())) {
298 addIfMatching(test, packageOrClass);
299 }
300 }
301 }
302
303 private boolean isTestApplicable(final Test test, final String path) {
304 return test.doesMatchResource() || path.endsWith(".class") && test.doesMatchClass();
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318 private void loadImplementationsInJar(final Test test, final String parent, final File jarFile) {
319 JarInputStream jarStream = null;
320 try {
321 jarStream = new JarInputStream(new FileInputStream(jarFile));
322 loadImplementationsInJar(test, parent, jarFile.getPath(), jarStream);
323 } catch (final IOException ex) {
324 LOGGER.error("Could not search JAR file '{}' for classes matching criteria {}, file not found", jarFile,
325 test, ex);
326 } finally {
327 close(jarStream, jarFile);
328 }
329 }
330
331
332
333
334
335 private void close(final JarInputStream jarStream, final Object source) {
336 if (jarStream != null) {
337 try {
338 jarStream.close();
339 } catch (final IOException e) {
340 LOGGER.error("Error closing JAR file stream for {}", source, e);
341 }
342 }
343 }
344
345
346
347
348
349
350
351
352
353
354
355
356 private void loadImplementationsInJar(final Test test, final String parent, final String path,
357 final JarInputStream stream) {
358
359 try {
360 JarEntry entry;
361
362 while ((entry = stream.getNextJarEntry()) != null) {
363 final String name = entry.getName();
364 if (!entry.isDirectory() && name.startsWith(parent) && isTestApplicable(test, name)) {
365 addIfMatching(test, name);
366 }
367 }
368 } catch (final IOException ioe) {
369 LOGGER.error("Could not search JAR file '{}' for classes matching criteria {} due to an IOException", path,
370 test, ioe);
371 }
372 }
373
374
375
376
377
378
379
380
381
382
383 protected void addIfMatching(final Test test, final String fqn) {
384 try {
385 final ClassLoader loader = getClassLoader();
386 if (test.doesMatchClass()) {
387 final String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.');
388 if (LOGGER.isDebugEnabled()) {
389 LOGGER.debug("Checking to see if class {} matches criteria {}", externalName, test);
390 }
391
392 final Class<?> type = loader.loadClass(externalName);
393 if (test.matches(type)) {
394 classMatches.add(type);
395 }
396 }
397 if (test.doesMatchResource()) {
398 URL url = loader.getResource(fqn);
399 if (url == null) {
400 url = loader.getResource(fqn.substring(1));
401 }
402 if (url != null && test.matches(url.toURI())) {
403 resourceMatches.add(url.toURI());
404 }
405 }
406 } catch (final Throwable t) {
407 LOGGER.warn("Could not examine class {}", fqn, t);
408 }
409 }
410
411
412
413
414
415 public interface Test {
416
417
418
419
420
421
422
423
424 boolean matches(Class<?> type);
425
426
427
428
429
430
431
432
433 boolean matches(URI resource);
434
435 boolean doesMatchClass();
436
437 boolean doesMatchResource();
438 }
439
440 }