001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.config.plugins; 018 019import java.io.File; 020import java.io.FileInputStream; 021import java.io.FileNotFoundException; 022import java.io.IOException; 023import java.io.UnsupportedEncodingException; 024import java.lang.annotation.Annotation; 025import java.net.URI; 026import java.net.URL; 027import java.net.URLDecoder; 028import java.util.Arrays; 029import java.util.Collection; 030import java.util.Enumeration; 031import java.util.HashSet; 032import java.util.List; 033import java.util.Set; 034import java.util.jar.JarEntry; 035import java.util.jar.JarInputStream; 036 037import org.apache.logging.log4j.Logger; 038import org.apache.logging.log4j.core.helpers.Charsets; 039import org.apache.logging.log4j.core.helpers.Loader; 040import org.apache.logging.log4j.status.StatusLogger; 041import org.osgi.framework.FrameworkUtil; 042import org.osgi.framework.wiring.BundleWiring; 043 044/** 045 * <p>ResolverUtil is used to locate classes that are available in the/a class path and meet 046 * arbitrary conditions. The two most common conditions are that a class implements/extends 047 * another class, or that is it annotated with a specific annotation. However, through the use 048 * of the {@link Test} class it is possible to search using arbitrary conditions.</p> 049 * 050 * <p>A ClassLoader is used to locate all locations (directories and jar files) in the class 051 * path that contain classes within certain packages, and then to load those classes and 052 * check them. By default the ClassLoader returned by 053 * {@code Thread.currentThread().getContextClassLoader()} is used, but this can be overridden 054 * by calling {@link #setClassLoader(ClassLoader)} prior to invoking any of the {@code find()} 055 * methods.</p> 056 * 057 * <p>General searches are initiated by calling the 058 * {@link #find(ResolverUtil.Test, String...)} method and supplying 059 * a package name and a Test instance. This will cause the named package <b>and all sub-packages</b> 060 * to be scanned for classes that meet the test. There are also utility methods for the common 061 * use cases of scanning multiple packages for extensions of particular classes, or classes 062 * annotated with a specific annotation.</p> 063 * 064 * <p>The standard usage pattern for the ResolverUtil class is as follows:</p> 065 * 066 *<pre> 067 *ResolverUtil<ActionBean> resolver = new ResolverUtil<ActionBean>(); 068 *resolver.findImplementation(ActionBean.class, pkg1, pkg2); 069 *resolver.find(new CustomTest(), pkg1); 070 *resolver.find(new CustomTest(), pkg2); 071 *Collection<ActionBean> beans = resolver.getClasses(); 072 *</pre> 073 * 074 * <p>This class was copied and modified from Stripes - http://stripes.mc4j.org/confluence/display/stripes/Home 075 * </p> 076 */ 077public class ResolverUtil { 078 /** An instance of Log to use for logging in this class. */ 079 private static final Logger LOGGER = StatusLogger.getLogger(); 080 081 private static final String VFSZIP = "vfszip"; 082 083 private static final String BUNDLE_RESOURCE = "bundleresource"; 084 085 /** The set of matches being accumulated. */ 086 private final Set<Class<?>> classMatches = new HashSet<Class<?>>(); 087 088 /** The set of matches being accumulated. */ 089 private final Set<URI> resourceMatches = new HashSet<URI>(); 090 091 /** 092 * The ClassLoader to use when looking for classes. If null then the ClassLoader returned 093 * by Thread.currentThread().getContextClassLoader() will be used. 094 */ 095 private ClassLoader classloader; 096 097 /** 098 * Provides access to the classes discovered so far. If no calls have been made to 099 * any of the {@code find()} methods, this set will be empty. 100 * 101 * @return the set of classes that have been discovered. 102 */ 103 public Set<Class<?>> getClasses() { 104 return classMatches; 105 } 106 107 /** 108 * Returns the matching resources. 109 * @return A Set of URIs that match the criteria. 110 */ 111 public Set<URI> getResources() { 112 return resourceMatches; 113 } 114 115 116 /** 117 * Returns the classloader that will be used for scanning for classes. If no explicit 118 * ClassLoader has been set by the calling, the context class loader will be used. 119 * 120 * @return the ClassLoader that will be used to scan for classes 121 */ 122 public ClassLoader getClassLoader() { 123 return classloader != null ? classloader : (classloader = Loader.getClassLoader(ResolverUtil.class, null)); 124 } 125 126 /** 127 * Sets an explicit ClassLoader that should be used when scanning for classes. If none 128 * is set then the context classloader will be used. 129 * 130 * @param classloader a ClassLoader to use when scanning for classes 131 */ 132 public void setClassLoader(final ClassLoader classloader) { this.classloader = classloader; } 133 134 /** 135 * Attempts to discover classes that are assignable to the type provided. In the case 136 * that an interface is provided this method will collect implementations. In the case 137 * of a non-interface class, subclasses will be collected. Accumulated classes can be 138 * accessed by calling {@link #getClasses()}. 139 * 140 * @param parent the class of interface to find subclasses or implementations of 141 * @param packageNames one or more package names to scan (including subpackages) for classes 142 */ 143 public void findImplementations(final Class<?> parent, final String... packageNames) { 144 if (packageNames == null) { 145 return; 146 } 147 148 final Test test = new IsA(parent); 149 for (final String pkg : packageNames) { 150 findInPackage(test, pkg); 151 } 152 } 153 154 /** 155 * Attempts to discover classes who's name ends with the provided suffix. Accumulated classes can be 156 * accessed by calling {@link #getClasses()}. 157 * 158 * @param suffix The class name suffix to match 159 * @param packageNames one or more package names to scan (including subpackages) for classes 160 */ 161 public void findSuffix(final String suffix, final String... packageNames) { 162 if (packageNames == null) { 163 return; 164 } 165 166 final Test test = new NameEndsWith(suffix); 167 for (final String pkg : packageNames) { 168 findInPackage(test, pkg); 169 } 170 } 171 172 /** 173 * Attempts to discover classes that are annotated with to the annotation. Accumulated 174 * classes can be accessed by calling {@link #getClasses()}. 175 * 176 * @param annotation the annotation that should be present on matching classes 177 * @param packageNames one or more package names to scan (including subpackages) for classes 178 */ 179 public void findAnnotated(final Class<? extends Annotation> annotation, final String... packageNames) { 180 if (packageNames == null) { 181 return; 182 } 183 184 final Test test = new AnnotatedWith(annotation); 185 for (final String pkg : packageNames) { 186 findInPackage(test, pkg); 187 } 188 } 189 190 public void findNamedResource(final String name, final String... pathNames) { 191 if (pathNames == null) { 192 return; 193 } 194 195 final Test test = new NameIs(name); 196 for (final String pkg : pathNames) { 197 findInPackage(test, pkg); 198 } 199 } 200 201 /** 202 * Attempts to discover classes that pass the test. Accumulated 203 * classes can be accessed by calling {@link #getClasses()}. 204 * 205 * @param test the test to determine matching classes 206 * @param packageNames one or more package names to scan (including subpackages) for classes 207 */ 208 public void find(final Test test, final String... packageNames) { 209 if (packageNames == null) { 210 return; 211 } 212 213 for (final String pkg : packageNames) { 214 findInPackage(test, pkg); 215 } 216 } 217 218 /** 219 * Scans for classes starting at the package provided and descending into subpackages. 220 * Each class is offered up to the Test as it is discovered, and if the Test returns 221 * true the class is retained. Accumulated classes can be fetched by calling 222 * {@link #getClasses()}. 223 * 224 * @param test an instance of {@link Test} that will be used to filter classes 225 * @param packageName the name of the package from which to start scanning for 226 * classes, e.g. {@code net.sourceforge.stripes} 227 */ 228 public void findInPackage(final Test test, String packageName) { 229 packageName = packageName.replace('.', '/'); 230 final ClassLoader loader = getClassLoader(); 231 Enumeration<URL> urls; 232 233 try { 234 urls = loader.getResources(packageName); 235 } catch (final IOException ioe) { 236 LOGGER.warn("Could not read package: " + packageName, ioe); 237 return; 238 } 239 240 while (urls.hasMoreElements()) { 241 try { 242 final URL url = urls.nextElement(); 243 String urlPath = extractPath(url); 244 245 LOGGER.info("Scanning for classes in [" + urlPath + "] matching criteria: " + test); 246 // Check for a jar in a war in JBoss 247 if (VFSZIP.equals(url.getProtocol())) { 248 final String path = urlPath.substring(0, urlPath.length() - packageName.length() - 2); 249 final URL newURL = new URL(url.getProtocol(), url.getHost(), path); 250 @SuppressWarnings("resource") 251 final JarInputStream stream = new JarInputStream(newURL.openStream()); 252 try { 253 loadImplementationsInJar(test, packageName, path, stream); 254 } finally { 255 close(stream, newURL); 256 } 257 } else if (BUNDLE_RESOURCE.equals(url.getProtocol())) { 258 loadImplementationsInBundle(test, packageName); 259 } else { 260 final File file = new File(urlPath); 261 if (file.isDirectory()) { 262 loadImplementationsInDirectory(test, packageName, file); 263 } else { 264 loadImplementationsInJar(test, packageName, file); 265 } 266 } 267 } catch (final IOException ioe) { 268 LOGGER.warn("could not read entries", ioe); 269 } 270 } 271 } 272 273 String extractPath(final URL url) throws UnsupportedEncodingException { 274 String urlPath = url.getPath(); // same as getFile but without the Query portion 275 //System.out.println(url.getProtocol() + "->" + urlPath); 276 277 // I would be surprised if URL.getPath() ever starts with "jar:" but no harm in checking 278 if (urlPath.startsWith("jar:")) { 279 urlPath = urlPath.substring(4); 280 } 281 // For jar: URLs, the path part starts with "file:" 282 if (urlPath.startsWith("file:")) { 283 urlPath = urlPath.substring(5); 284 } 285 // If it was in a JAR, grab the path to the jar 286 if (urlPath.indexOf('!') > 0) { 287 urlPath = urlPath.substring(0, urlPath.indexOf('!')); 288 } 289 290 // LOG4J2-445 291 // Finally, decide whether to URL-decode the file name or not... 292 final String protocol = url.getProtocol(); 293 final List<String> neverDecode = Arrays.asList(VFSZIP, BUNDLE_RESOURCE); 294 if (neverDecode.contains(protocol)) { 295 return urlPath; 296 } 297 if (new File(urlPath).exists()) { 298 // if URL-encoded file exists, don't decode it 299 return urlPath; 300 } 301 urlPath = URLDecoder.decode(urlPath, Charsets.UTF_8.name()); 302 return urlPath; 303 } 304 305 private void loadImplementationsInBundle(final Test test, final String packageName) { 306 //Do not remove the cast on the next line as removing it will cause a compile error on Java 7. 307 final BundleWiring wiring = (BundleWiring) FrameworkUtil.getBundle( 308 ResolverUtil.class).adapt(BundleWiring.class); 309 final Collection<String> list = wiring.listResources(packageName, "*.class", 310 BundleWiring.LISTRESOURCES_RECURSE); 311 for (final String name : list) { 312 addIfMatching(test, name); 313 } 314 } 315 316 317 /** 318 * Finds matches in a physical directory on a filesystem. Examines all 319 * files within a directory - if the File object is not a directory, and ends with <i>.class</i> 320 * the file is loaded and tested to see if it is acceptable according to the Test. Operates 321 * recursively to find classes within a folder structure matching the package structure. 322 * 323 * @param test a Test used to filter the classes that are discovered 324 * @param parent the package name up to this directory in the package hierarchy. E.g. if 325 * /classes is in the classpath and we wish to examine files in /classes/org/apache then 326 * the values of <i>parent</i> would be <i>org/apache</i> 327 * @param location a File object representing a directory 328 */ 329 private void loadImplementationsInDirectory(final Test test, final String parent, final File location) { 330 final File[] files = location.listFiles(); 331 if (files == null) { 332 return; 333 } 334 335 StringBuilder builder; 336 for (final File file : files) { 337 builder = new StringBuilder(); 338 builder.append(parent).append("/").append(file.getName()); 339 final String packageOrClass = parent == null ? file.getName() : builder.toString(); 340 341 if (file.isDirectory()) { 342 loadImplementationsInDirectory(test, packageOrClass, file); 343 } else if (isTestApplicable(test, file.getName())) { 344 addIfMatching(test, packageOrClass); 345 } 346 } 347 } 348 349 private boolean isTestApplicable(final Test test, final String path) { 350 return test.doesMatchResource() || path.endsWith(".class") && test.doesMatchClass(); 351 } 352 353 /** 354 * Finds matching classes within a jar files that contains a folder structure 355 * matching the package structure. If the File is not a JarFile or does not exist a warning 356 * will be logged, but no error will be raised. 357 * 358 * @param test a Test used to filter the classes that are discovered 359 * @param parent the parent package under which classes must be in order to be considered 360 * @param jarFile the jar file to be examined for classes 361 */ 362 private void loadImplementationsInJar(final Test test, final String parent, final File jarFile) { 363 @SuppressWarnings("resource") 364 JarInputStream jarStream = null; 365 try { 366 jarStream = new JarInputStream(new FileInputStream(jarFile)); 367 loadImplementationsInJar(test, parent, jarFile.getPath(), jarStream); 368 } catch (final FileNotFoundException ex) { 369 LOGGER.error("Could not search jar file '" + jarFile + "' for classes matching criteria: " + test 370 + " file not found"); 371 } catch (final IOException ioe) { 372 LOGGER.error("Could not search jar file '" + jarFile + "' for classes matching criteria: " + test 373 + " due to an IOException", ioe); 374 } finally { 375 close(jarStream, jarFile); 376 } 377 } 378 379 /** 380 * @param jarStream 381 * @param source 382 */ 383 private void close(JarInputStream jarStream, final Object source) { 384 if (jarStream != null) { 385 try { 386 jarStream.close(); 387 } catch (IOException e) { 388 LOGGER.error("Error closing JAR file stream for {}", source, e); 389 } 390 } 391 } 392 393 /** 394 * Finds matching classes within a jar files that contains a folder structure 395 * matching the package structure. If the File is not a JarFile or does not exist a warning 396 * will be logged, but no error will be raised. 397 * 398 * @param test a Test used to filter the classes that are discovered 399 * @param parent the parent package under which classes must be in order to be considered 400 * @param stream The jar InputStream 401 */ 402 private void loadImplementationsInJar(final Test test, final String parent, final String path, 403 final JarInputStream stream) { 404 405 try { 406 JarEntry entry; 407 408 while ((entry = stream.getNextJarEntry()) != null) { 409 final String name = entry.getName(); 410 if (!entry.isDirectory() && name.startsWith(parent) && isTestApplicable(test, name)) { 411 addIfMatching(test, name); 412 } 413 } 414 } catch (final IOException ioe) { 415 LOGGER.error("Could not search jar file '" + path + "' for classes matching criteria: " + 416 test + " due to an IOException", ioe); 417 } 418 } 419 420 /** 421 * Add the class designated by the fully qualified class name provided to the set of 422 * resolved classes if and only if it is approved by the Test supplied. 423 * 424 * @param test the test used to determine if the class matches 425 * @param fqn the fully qualified name of a class 426 */ 427 protected void addIfMatching(final Test test, final String fqn) { 428 try { 429 final ClassLoader loader = getClassLoader(); 430 if (test.doesMatchClass()) { 431 final String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.'); 432 if (LOGGER.isDebugEnabled()) { 433 LOGGER.debug("Checking to see if class " + externalName + " matches criteria [" + test + "]"); 434 } 435 436 final Class<?> type = loader.loadClass(externalName); 437 if (test.matches(type)) { 438 classMatches.add(type); 439 } 440 } 441 if (test.doesMatchResource()) { 442 URL url = loader.getResource(fqn); 443 if (url == null) { 444 url = loader.getResource(fqn.substring(1)); 445 } 446 if (url != null && test.matches(url.toURI())) { 447 resourceMatches.add(url.toURI()); 448 } 449 } 450 } catch (final Throwable t) { 451 LOGGER.warn("Could not examine class '" + fqn + "' due to a " + 452 t.getClass().getName() + " with message: " + t.getMessage()); 453 } 454 } 455 456 /** 457 * A simple interface that specifies how to test classes to determine if they 458 * are to be included in the results produced by the ResolverUtil. 459 */ 460 public interface Test { 461 /** 462 * Will be called repeatedly with candidate classes. Must return True if a class 463 * is to be included in the results, false otherwise. 464 * @param type The Class to match against. 465 * @return true if the Class matches. 466 */ 467 boolean matches(Class<?> type); 468 469 /** 470 * Test for a resource. 471 * @param resource The URI to the resource. 472 * @return true if the resource matches. 473 */ 474 boolean matches(URI resource); 475 476 boolean doesMatchClass(); 477 478 boolean doesMatchResource(); 479 } 480 481 /** 482 * Test against a Class. 483 */ 484 public abstract static class ClassTest implements Test { 485 @Override 486 public boolean matches(final URI resource) { 487 throw new UnsupportedOperationException(); 488 } 489 490 @Override 491 public boolean doesMatchClass() { 492 return true; 493 } 494 495 @Override 496 public boolean doesMatchResource() { 497 return false; 498 } 499 } 500 501 /** 502 * Test against a resource. 503 */ 504 public abstract static class ResourceTest implements Test { 505 @Override 506 public boolean matches(final Class<?> cls) { 507 throw new UnsupportedOperationException(); 508 } 509 510 @Override 511 public boolean doesMatchClass() { 512 return false; 513 } 514 515 @Override 516 public boolean doesMatchResource() { 517 return true; 518 } 519 } 520 521 /** 522 * A Test that checks to see if each class is assignable to the provided class. Note 523 * that this test will match the parent type itself if it is presented for matching. 524 */ 525 public static class IsA extends ClassTest { 526 private final Class<?> parent; 527 528 /** 529 * Constructs an IsA test using the supplied Class as the parent class/interface. 530 * @param parentType The parent class to check for. 531 */ 532 public IsA(final Class<?> parentType) { this.parent = parentType; } 533 534 /** 535 * Returns true if type is assignable to the parent type supplied in the constructor. 536 * @param type The Class to check. 537 * @return true if the Class matches. 538 */ 539 @Override 540 public boolean matches(final Class<?> type) { 541 return type != null && parent.isAssignableFrom(type); 542 } 543 544 @Override 545 public String toString() { 546 return "is assignable to " + parent.getSimpleName(); 547 } 548 } 549 550 /** 551 * A Test that checks to see if each class name ends with the provided suffix. 552 */ 553 public static class NameEndsWith extends ClassTest { 554 private final String suffix; 555 556 /** 557 * Constructs a NameEndsWith test using the supplied suffix. 558 * @param suffix the String suffix to check for. 559 */ 560 public NameEndsWith(final String suffix) { this.suffix = suffix; } 561 562 /** 563 * Returns true if type name ends with the suffix supplied in the constructor. 564 * @param type The Class to check. 565 * @return true if the Class matches. 566 */ 567 @Override 568 public boolean matches(final Class<?> type) { 569 return type != null && type.getName().endsWith(suffix); 570 } 571 572 @Override 573 public String toString() { 574 return "ends with the suffix " + suffix; 575 } 576 } 577 578 /** 579 * A Test that checks to see if each class is annotated with a specific annotation. If it 580 * is, then the test returns true, otherwise false. 581 */ 582 public static class AnnotatedWith extends ClassTest { 583 private final Class<? extends Annotation> annotation; 584 585 /** 586 * Constructs an AnnotatedWith test for the specified annotation type. 587 * @param annotation The annotation to check for. 588 */ 589 public AnnotatedWith(final Class<? extends Annotation> annotation) { 590 this.annotation = annotation; 591 } 592 593 /** 594 * Returns true if the type is annotated with the class provided to the constructor. 595 * @param type the Class to match against. 596 * @return true if the Classes match. 597 */ 598 @Override 599 public boolean matches(final Class<?> type) { 600 return type != null && type.isAnnotationPresent(annotation); 601 } 602 603 @Override 604 public String toString() { 605 return "annotated with @" + annotation.getSimpleName(); 606 } 607 } 608 609 /** 610 * A Test that checks to see if the class name matches. 611 */ 612 public static class NameIs extends ResourceTest { 613 private final String name; 614 615 public NameIs(final String name) { this.name = "/" + name; } 616 617 @Override 618 public boolean matches(final URI resource) { 619 return resource.getPath().endsWith(name); 620 } 621 622 @Override public String toString() { 623 return "named " + name; 624 } 625 } 626}