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