cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5479] Updating Spring class path scanner to support laoding interfaces only and accepting a custom loader
Date Thu, 12 Jun 2014 12:29:06 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 8447da0da -> 3d635a798


[CXF-5479] Updating Spring class path scanner to support laoding interfaces only and accepting
a custom loader


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

Branch: refs/heads/master
Commit: 3d635a7989366b61ce55231c82c8625aa75b8524
Parents: 8447da0
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Thu Jun 12 13:28:43 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Thu Jun 12 13:28:43 2014 +0100

----------------------------------------------------------------------
 .../cxf/common/util/ClasspathScanner.java       | 24 +++++++--
 .../cxf/common/util/SpringClasspathScanner.java | 53 ++++++++++++++------
 .../maven_plugin/javatowadl/Java2WADLMojo.java  |  7 ++-
 3 files changed, 63 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/3d635a79/core/src/main/java/org/apache/cxf/common/util/ClasspathScanner.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/util/ClasspathScanner.java b/core/src/main/java/org/apache/cxf/common/util/ClasspathScanner.java
index 6d6b95b..870e749 100644
--- a/core/src/main/java/org/apache/cxf/common/util/ClasspathScanner.java
+++ b/core/src/main/java/org/apache/cxf/common/util/ClasspathScanner.java
@@ -92,12 +92,21 @@ public class ClasspathScanner {
     public static Map< Class< ? extends Annotation >, Collection< Class< ?
> > > findClasses(
         Collection< String > basePackages, List<Class< ? extends Annotation >
> annotations) 
         throws IOException, ClassNotFoundException {
-        return HELPER.findClassesInternal(basePackages, annotations);
+        return findClasses(basePackages, annotations, null);
         
     }
+
+    public static Map< Class< ? extends Annotation >, Collection< Class< ?
> > > findClasses(
+        Collection< String > basePackages, 
+        List<Class< ? extends Annotation > > annotations,
+        ClassLoader loader) throws IOException, ClassNotFoundException {
+        return HELPER.findClassesInternal(basePackages, annotations, loader);
+    }
     
     protected Map< Class< ? extends Annotation >, Collection< Class< ? >
> > findClassesInternal(
-        Collection< String > basePackages, List<Class< ? extends Annotation >
> annotations) 
+        Collection< String > basePackages, 
+        List<Class< ? extends Annotation > > annotations,
+        ClassLoader loader) 
         throws IOException, ClassNotFoundException {
         return Collections.emptyMap();
     }
@@ -123,9 +132,14 @@ public class ClasspathScanner {
      */
     public static List<URL> findResources(Collection<String> basePackages, String
extension) 
         throws IOException {
-        return HELPER.findResourcesInternal(basePackages, extension);
+        return findResources(basePackages, extension, null);
     }
     
+    public static List<URL> findResources(Collection<String> basePackages, String
extension,
+                                          ClassLoader loader) 
+        throws IOException {
+        return HELPER.findResourcesInternal(basePackages, extension, loader);
+    }
     
     public static Set<String> parsePackages(final String packagesAsCsv) {        
         final String[] values = StringUtils.split(packagesAsCsv, ",");
@@ -144,7 +158,9 @@ public class ClasspathScanner {
         return basePackages;
     }
     
-    protected List<URL> findResourcesInternal(Collection<String> basePackages,
String extension) 
+    protected List<URL> findResourcesInternal(Collection<String> basePackages,

+                                              String extension,
+                                              ClassLoader loader) 
         throws IOException {
         return Collections.emptyList();
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/3d635a79/core/src/main/java/org/apache/cxf/common/util/SpringClasspathScanner.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/util/SpringClasspathScanner.java b/core/src/main/java/org/apache/cxf/common/util/SpringClasspathScanner.java
index 8daab5d..b026c66 100644
--- a/core/src/main/java/org/apache/cxf/common/util/SpringClasspathScanner.java
+++ b/core/src/main/java/org/apache/cxf/common/util/SpringClasspathScanner.java
@@ -46,10 +46,12 @@ class SpringClasspathScanner extends ClasspathScanner {
     
 
     protected Map< Class< ? extends Annotation >, Collection< Class< ? >
> > findClassesInternal(
-        Collection< String > basePackages, List<Class< ? extends Annotation >
> annotations) 
+        Collection< String > basePackages, 
+        List<Class< ? extends Annotation > > annotations,
+        ClassLoader loader) 
         throws IOException, ClassNotFoundException {
     
-        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
+        ResourcePatternResolver resolver = getResolver(loader);
         MetadataReaderFactory factory = new CachingMetadataReaderFactory(resolver);
         
         final Map< Class< ? extends Annotation >, Collection< Class< ? >
> > classes = 
@@ -89,8 +91,7 @@ class SpringClasspathScanner extends ClasspathScanner {
                     boolean concreteClass = !metadata.isInterface() && !metadata.isAbstract();
                     if (metadata.isAnnotated(annotation.getName())) {
                         if (concreteClass) {
-                            classes.get(annotation).add(
-                                ClassLoaderUtils.loadClass(metadata.getClassName(), getClass()));
+                            classes.get(annotation).add(loadClass(metadata.getClassName(),
loader));
                         } else {
                             matchingInterfaces.get(annotation).add(metadata.getClassName());
   
                         }
@@ -100,29 +101,39 @@ class SpringClasspathScanner extends ClasspathScanner {
                 }
             }
         }
-        for (Map.Entry<Class<? extends Annotation>, Collection<String>>
e1 : matchingInterfaces.entrySet()) {
-            for (Map.Entry<String, String[]> e2 : nonMatchingClasses.entrySet()) {
-                for (String intName : e2.getValue()) {
-                    if (e1.getValue().contains(intName)) {
-                        classes.get(e1.getKey()).add(ClassLoaderUtils.loadClass(e2.getKey(),
getClass()));
-                        break;
+        if (!nonMatchingClasses.isEmpty()) {
+            for (Map.Entry<Class<? extends Annotation>, Collection<String>>
e1 : matchingInterfaces.entrySet()) {
+                for (Map.Entry<String, String[]> e2 : nonMatchingClasses.entrySet())
{
+                    for (String intName : e2.getValue()) {
+                        if (e1.getValue().contains(intName)) {
+                            classes.get(e1.getKey()).add(loadClass(e2.getKey(), loader));
+                            break;
+                        }
                     }
                 }
             }
         }
         
+        for (Map.Entry<Class<? extends Annotation>, Collection<String>>
e : matchingInterfaces.entrySet()) {
+            if (classes.get(e.getKey()).isEmpty()) {
+                for (String intName : e.getValue()) {
+                    classes.get(e.getKey()).add(loadClass(intName, loader));
+                }
+            }
+        }
+        
         return classes;
     }
     
-    protected List<URL> findResourcesInternal(Collection<String> basePackages,
String extension) 
+    protected List<URL> findResourcesInternal(Collection<String> basePackages,

+                                              String extension,
+                                              ClassLoader loader) 
         throws IOException {
         final List<URL> resourceURLs = new ArrayList<URL>();
         if (basePackages == null || basePackages.isEmpty()) {
             return resourceURLs;
         }
-        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
-        
-        
+        ResourcePatternResolver resolver = getResolver(loader);
         
         for (final String basePackage: basePackages) {
             final boolean scanAllPackages = basePackage.equals(ALL_PACKAGES);
@@ -145,7 +156,10 @@ class SpringClasspathScanner extends ClasspathScanner {
         return resourceURLs;
     }
     
-    
+    private ResourcePatternResolver getResolver(ClassLoader loader) {
+        return loader != null 
+            ? new PathMatchingResourcePatternResolver(loader) : new PathMatchingResourcePatternResolver();
+    }
        
     private boolean shouldSkip(final String classname) {
         for (String packageToSkip: PACKAGES_TO_SKIP) {
@@ -156,4 +170,13 @@ class SpringClasspathScanner extends ClasspathScanner {
         
         return false;
     }
+    
+    private Class<?> loadClass(String className, ClassLoader loader) 
+        throws ClassNotFoundException {
+        if (loader == null) {
+            return ClassLoaderUtils.loadClass(className, getClass());
+        } else {
+            return loader.loadClass(className);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/3d635a79/maven-plugins/java2wadl-plugin/src/main/java/org/apache/cxf/maven_plugin/javatowadl/Java2WADLMojo.java
----------------------------------------------------------------------
diff --git a/maven-plugins/java2wadl-plugin/src/main/java/org/apache/cxf/maven_plugin/javatowadl/Java2WADLMojo.java
b/maven-plugins/java2wadl-plugin/src/main/java/org/apache/cxf/maven_plugin/javatowadl/Java2WADLMojo.java
index a52b349..58cc578 100644
--- a/maven-plugins/java2wadl-plugin/src/main/java/org/apache/cxf/maven_plugin/javatowadl/Java2WADLMojo.java
+++ b/maven-plugins/java2wadl-plugin/src/main/java/org/apache/cxf/maven_plugin/javatowadl/Java2WADLMojo.java
@@ -311,9 +311,12 @@ public class Java2WADLMojo extends AbstractMojo {
         }
         if (resourceClasses.isEmpty() && basePackages != null) {
             try {
-                @SuppressWarnings("unchecked")
+                List<Class<? extends Annotation>> anns = new ArrayList<Class<?
extends Annotation>>();
+                anns.add(Path.class);
                 final Map< Class< ? extends Annotation >, Collection< Class<
? > > > discoveredClasses = 
-                    ClasspathScanner.findClasses(ClasspathScanner.parsePackages(basePackages),
Path.class);
+                    ClasspathScanner.findClasses(ClasspathScanner.parsePackages(basePackages),

+                                                 anns,
+                                                 getClassLoader());
                 if (discoveredClasses.containsKey(Path.class)) {
                     resourceClasses.addAll(discoveredClasses.get(Path.class));
                 }


Mime
View raw message