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