drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject [2/7] drill git commit: DRILL-2815: Augment PathScanner logging; clean up code.
Date Tue, 04 Aug 2015 22:11:22 GMT
DRILL-2815: Augment PathScanner logging; clean up code.

Added some DEBUG- and TRACE-level log calls.
Augmented/edited existing log call.

Misc. code hygiene:
- Added documentation of forResources(...).
- Renamed to clarify a number of local names.
- Made logger private.
- Added "final".
- Fixed indentation; wrapped some long lines; etc.

Conflicts:
	common/src/main/java/org/apache/drill/common/util/PathScanner.java


Project: http://git-wip-us.apache.org/repos/asf/drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/a0b37710
Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/a0b37710
Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/a0b37710

Branch: refs/heads/master
Commit: a0b37710c857acbf2e592787f7c8d5c6308037a9
Parents: 357d020
Author: dbarclay <dbarclay@maprtech.com>
Authored: Mon Jul 13 21:02:40 2015 -0700
Committer: Jason Altekruse <altekrusejason@gmail.com>
Committed: Tue Aug 4 08:49:01 2015 -0700

----------------------------------------------------------------------
 .../apache/drill/common/util/PathScanner.java   | 112 ++++++++++++-------
 1 file changed, 73 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/a0b37710/common/src/main/java/org/apache/drill/common/util/PathScanner.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/drill/common/util/PathScanner.java b/common/src/main/java/org/apache/drill/common/util/PathScanner.java
index c322643..9832ae9 100644
--- a/common/src/main/java/org/apache/drill/common/util/PathScanner.java
+++ b/common/src/main/java/org/apache/drill/common/util/PathScanner.java
@@ -37,12 +37,14 @@ import org.reflections.scanners.SubTypesScanner;
 import org.reflections.scanners.TypeAnnotationsScanner;
 import org.reflections.util.ClasspathHelper;
 import org.reflections.util.ConfigurationBuilder;
+import org.slf4j.Logger;
+import static org.slf4j.LoggerFactory.getLogger;
 
 import com.google.common.base.Stopwatch;
 import com.google.common.collect.Sets;
 
 public class PathScanner {
-  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(PathScanner.class);
+  private static final Logger logger = getLogger(PathScanner.class);
 
   private static final SubTypesScanner subTypeScanner = new SubTypesScanner();
   private static final TypeAnnotationsScanner annotationsScanner = new TypeAnnotationsScanner();
@@ -50,13 +52,15 @@ public class PathScanner {
   private static final Object SYNC = new Object();
   static volatile Reflections REFLECTIONS = null;
 
-  public static <A extends Annotation, T> Map<A, Class<? extends T>> scanForAnnotatedImplementations(Class<A>
annotationClass, Class<T> baseClass, final List<String> scanPackages) {
-    Collection<Class<? extends T>> providerClasses = scanForImplementations(baseClass,
scanPackages);
+  public static <A extends Annotation, T> Map<A, Class<? extends T>> scanForAnnotatedImplementations(
+      Class<A> annotationClass, Class<T> baseClass, final List<String>
scanPackages) {
+    final Collection<Class<? extends T>> providerClasses =
+        scanForImplementations(baseClass, scanPackages);
 
-    Map<A, Class<? extends T>> map = new HashMap<A, Class<? extends T>>();
+    final Map<A, Class<? extends T>> map = new HashMap<A, Class<? extends
T>>();
 
     for (Class<? extends T> c : providerClasses) {
-      A annotation = (A) c.getAnnotation(annotationClass);
+      final A annotation = (A) c.getAnnotation(annotationClass);
       if (annotation == null) {
         continue;
       }
@@ -68,25 +72,30 @@ public class PathScanner {
 
   private static Reflections getReflections() {
     if (REFLECTIONS == null) {
-      REFLECTIONS = new Reflections(new ConfigurationBuilder().setUrls(getMarkedPaths()).setScanners(subTypeScanner,
annotationsScanner, resourcesScanner));
+      REFLECTIONS =
+          new Reflections(
+              new ConfigurationBuilder()
+                  .setUrls(getMarkedPaths())
+                  .setScanners(subTypeScanner, annotationsScanner, resourcesScanner));
     }
     return REFLECTIONS;
   }
 
-  public static <T> Class<?>[] scanForImplementationsArr(Class<T> baseClass,
final List<String> scanPackages) {
+  public static <T> Class<?>[] scanForImplementationsArr(final Class<T>
baseClass,
+                                                         final List<String> scanPackages)
{
     Collection<Class<? extends T>> imps = scanForImplementations(baseClass, scanPackages);
     Class<?>[] arr = imps.toArray(new Class<?>[imps.size()]);
     return arr;
   }
 
-  public static <T> Set<Class<? extends T>> scanForImplementations(Class<T>
baseClass, final List<String> scanPackages) {
-    Stopwatch w = new Stopwatch().start();
+  public static <T> Set<Class<? extends T>> scanForImplementations(final
Class<T> baseClass,
+                                                                   final List<String>
scanPackages) {
+    final Stopwatch w = new Stopwatch().start();
     try {
       synchronized(SYNC) {
-
-        Set<Class<? extends T>> classes = getReflections().getSubTypesOf(baseClass);
+        final Set<Class<? extends T>> classes = getReflections().getSubTypesOf(baseClass);
         for (Iterator<Class<? extends T>> i = classes.iterator(); i.hasNext();)
{
-          Class<? extends T> c = i.next();
+          final Class<? extends T> c = i.next();
           assert baseClass.isAssignableFrom(c);
           if (Modifier.isAbstract(c.getModifiers())) {
             i.remove();
@@ -94,13 +103,16 @@ public class PathScanner {
         }
         return classes;
       }
-    } finally{
-      logger.debug("Classpath scanning took {}ms", w.elapsed(TimeUnit.MILLISECONDS));
+    } finally {
+      logger.debug("Implementations scanning took {} ms for {}.",
+                   w.elapsed(TimeUnit.MILLISECONDS),
+                   baseClass); // no .getName(), so it has "class "/"interface "
     }
   }
 
   private static Collection<URL> getMarkedPaths() {
-    Collection<URL> urls = forResource(CommonConstants.DRILL_JAR_MARKER_FILE_RESOURCE_PATHNAME,
true);
+    Collection<URL> urls =
+        forResource(CommonConstants.DRILL_JAR_MARKER_FILE_RESOURCE_PATHNAME, true);
     return urls;
   }
 
@@ -108,33 +120,55 @@ public class PathScanner {
     return forResource(CommonConstants.DRILL_JAR_MARKER_FILE_RESOURCE_PATHNAME, false);
   }
 
-  public static Set<URL> forResource(String name, boolean stripName, ClassLoader...
classLoaders) {
-    final Set<URL> result = Sets.newHashSet();
-
-    final ClassLoader[] loaders = ClasspathHelper.classLoaders(classLoaders);
-    final String resourceName = name;
-
-    for (ClassLoader classLoader : loaders) {
-        try {
-            final Enumeration<URL> urls = classLoader.getResources(resourceName);
-            while (urls.hasMoreElements()) {
-                final URL url = urls.nextElement();
-
-                int index = url.toExternalForm().lastIndexOf(resourceName);
-                if (index != -1 && stripName) {
-                    result.add(new URL(url.toExternalForm().substring(0, index)));
-                } else {
-                    result.add(url);
-                }
-            }
-        } catch (IOException e) {
-            if (Reflections.log != null) {
-                Reflections.log.error("error getting resources for package " + name, e);
-            }
+  /**
+   * Gets URLs of any classpath resources with given resource pathname.
+   *
+   * @param  resourcePathname  resource pathname of classpath resource instances
+   *           to scan for (relative to specified class loaders' classpath roots)
+   * @param  returnRootPathname  whether to collect classpath root portion of
+   *           URL for each resource instead of full URL of each resource
+   * @param  classLoaders  set of class loaders in which to look up resource;
+   *           none (empty array) to specify to use current thread's context
+   *           class loader and {@link Reflections}'s class loader
+   * @returns  ...; empty set if none
+   */
+  public static Set<URL> forResource(final String resourcePathname,
+                                     final boolean returnRootPathname,
+                                     final ClassLoader... classLoaders) {
+    logger.debug("Scanning classpath for resources with pathname \"{}\".",
+                 resourcePathname);
+    final Set<URL> resultUrlSet = Sets.newHashSet();
+
+    final ClassLoader[] netLoaders = ClasspathHelper.classLoaders(classLoaders);
+    for (ClassLoader classLoader : netLoaders) {
+      try {
+        final Enumeration<URL> resourceUrls =
+            classLoader.getResources(resourcePathname);
+        while (resourceUrls.hasMoreElements()) {
+          final URL resourceUrl = resourceUrls.nextElement();
+          logger.trace( "- found a(n) {} at {}.", resourcePathname, resourceUrl );
+
+          int index = resourceUrl.toExternalForm().lastIndexOf(resourcePathname);
+          if (index != -1 && returnRootPathname) {
+            final URL classpathRootUrl =
+                new URL(resourceUrl.toExternalForm().substring(0, index));
+            resultUrlSet.add(classpathRootUrl);
+            logger.debug( "- collected resource's classpath root URL {}.",
+                          classpathRootUrl );
+          } else {
+            resultUrlSet.add(resourceUrl);
+            logger.debug( "- collected resource URL {}.", resourceUrl );
+          }
         }
+      } catch (IOException e) {
+        if (Reflections.log != null) {
+          Reflections.log.error(
+              "Error scanning for resources named " + resourcePathname, e);
+        }
+      }
     }
 
-    return result;
+    return resultUrlSet;
   }
 
 }


Mime
View raw message