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 classloader) {
138 this.classloader = classloader;
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 ioe) {
210 LOGGER.warn("could not read entries", ioe);
211 } catch (final URISyntaxException e) {
212 LOGGER.warn("could not read entries", e);
213 }
214 }
215 }
216
217 String extractPath(final URL url) throws UnsupportedEncodingException, URISyntaxException {
218 String urlPath = url.getPath();
219
220
221
222 if (urlPath.startsWith("jar:")) {
223 urlPath = urlPath.substring(4);
224 }
225
226 if (urlPath.startsWith("file:")) {
227 urlPath = urlPath.substring(5);
228 }
229
230 if (urlPath.indexOf('!') > 0) {
231 urlPath = urlPath.substring(0, urlPath.indexOf('!'));
232 }
233
234
235
236 final String protocol = url.getProtocol();
237 final List<String> neverDecode = Arrays.asList(VFSZIP, BUNDLE_RESOURCE);
238 if (neverDecode.contains(protocol)) {
239 return urlPath;
240 }
241 final String cleanPath = new URI(urlPath).getPath();
242 if (new File(cleanPath).exists()) {
243
244 return cleanPath;
245 }
246 return URLDecoder.decode(urlPath, StandardCharsets.UTF_8.name());
247 }
248
249 private void loadImplementationsInBundle(final Test test, final String packageName) {
250 final BundleWiring wiring = FrameworkUtil.getBundle(ResolverUtil.class).adapt(BundleWiring.class);
251 final Collection<String> list = wiring.listResources(packageName, "*.class",
252 BundleWiring.LISTRESOURCES_RECURSE);
253 for (final String name : list) {
254 addIfMatching(test, name);
255 }
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 private void loadImplementationsInDirectory(final Test test, final String parent, final File location) {
274 final File[] files = location.listFiles();
275 if (files == null) {
276 return;
277 }
278
279 StringBuilder builder;
280 for (final File file : files) {
281 builder = new StringBuilder();
282 builder.append(parent).append('/').append(file.getName());
283 final String packageOrClass = parent == null ? file.getName() : builder.toString();
284
285 if (file.isDirectory()) {
286 loadImplementationsInDirectory(test, packageOrClass, file);
287 } else if (isTestApplicable(test, file.getName())) {
288 addIfMatching(test, packageOrClass);
289 }
290 }
291 }
292
293 private boolean isTestApplicable(final Test test, final String path) {
294 return test.doesMatchResource() || path.endsWith(".class") && test.doesMatchClass();
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308 private void loadImplementationsInJar(final Test test, final String parent, final File jarFile) {
309 @SuppressWarnings("resource")
310 JarInputStream jarStream = null;
311 try {
312 jarStream = new JarInputStream(new FileInputStream(jarFile));
313 loadImplementationsInJar(test, parent, jarFile.getPath(), jarStream);
314 } catch (final FileNotFoundException ex) {
315 LOGGER.error("Could not search jar file '" + jarFile + "' for classes matching criteria: " + test
316 + " file not found", ex);
317 } catch (final IOException ioe) {
318 LOGGER.error("Could not search jar file '" + jarFile + "' for classes matching criteria: " + test
319 + " due to an IOException", ioe);
320 } finally {
321 close(jarStream, jarFile);
322 }
323 }
324
325
326
327
328
329 private void close(final JarInputStream jarStream, final Object source) {
330 if (jarStream != null) {
331 try {
332 jarStream.close();
333 } catch (final IOException e) {
334 LOGGER.error("Error closing JAR file stream for {}", source, e);
335 }
336 }
337 }
338
339
340
341
342
343
344
345
346
347
348
349
350 private void loadImplementationsInJar(final Test test, final String parent, final String path,
351 final JarInputStream stream) {
352
353 try {
354 JarEntry entry;
355
356 while ((entry = stream.getNextJarEntry()) != null) {
357 final String name = entry.getName();
358 if (!entry.isDirectory() && name.startsWith(parent) && isTestApplicable(test, name)) {
359 addIfMatching(test, name);
360 }
361 }
362 } catch (final IOException ioe) {
363 LOGGER.error("Could not search jar file '" + path + "' for classes matching criteria: " + test
364 + " due to an IOException", ioe);
365 }
366 }
367
368
369
370
371
372
373
374
375
376
377 protected void addIfMatching(final Test test, final String fqn) {
378 try {
379 final ClassLoader loader = getClassLoader();
380 if (test.doesMatchClass()) {
381 final String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.');
382 if (LOGGER.isDebugEnabled()) {
383 LOGGER.debug("Checking to see if class " + externalName + " matches criteria [" + test + ']');
384 }
385
386 final Class<?> type = loader.loadClass(externalName);
387 if (test.matches(type)) {
388 classMatches.add(type);
389 }
390 }
391 if (test.doesMatchResource()) {
392 URL url = loader.getResource(fqn);
393 if (url == null) {
394 url = loader.getResource(fqn.substring(1));
395 }
396 if (url != null && test.matches(url.toURI())) {
397 resourceMatches.add(url.toURI());
398 }
399 }
400 } catch (final Throwable t) {
401 LOGGER.warn("Could not examine class '" + fqn, t);
402 }
403 }
404
405
406
407
408
409 public interface Test {
410
411
412
413
414
415
416
417
418 boolean matches(Class<?> type);
419
420
421
422
423
424
425
426
427 boolean matches(URI resource);
428
429 boolean doesMatchClass();
430
431 boolean doesMatchResource();
432 }
433
434 }