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] Optional support for the auto-discovery of root resources
Date Mon, 09 Jun 2014 15:42:33 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 9e51e56a7 -> 1900e1f4a


[CXF-5479] Optional support for the auto-discovery of root resources


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

Branch: refs/heads/master
Commit: 1900e1f4ace53bc112c16f36d846f25905daab4c
Parents: 9e51e56
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Mon Jun 9 16:42:06 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Mon Jun 9 16:42:06 2014 +0100

----------------------------------------------------------------------
 .../maven_plugin/javatowadl/Java2WADLMojo.java  | 49 +++++++++++++++-----
 1 file changed, 37 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/1900e1f4/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 f2881bd..4b0065c 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
@@ -23,13 +23,19 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.lang.annotation.Annotation;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
+import java.util.Map;
+
+import javax.ws.rs.Path;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
+import org.apache.cxf.common.util.ClasspathScanner;
 import org.apache.cxf.helpers.FileUtils;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.wadl.DocumentationProvider;
@@ -97,6 +103,10 @@ public class Java2WADLMojo extends AbstractMojo {
      */
     private List<String> classResourceNames;
     
+    /**
+     * @parameter
+     */
+    private String basePackages;
     
     /**
      * @parameter expression="${project}"
@@ -170,8 +180,8 @@ public class Java2WADLMojo extends AbstractMojo {
     private String outputFileExtension;
     
     public void execute() throws MojoExecutionException {
-        
-        getResourcesList();
+        List<Class<?>> resourceClasses = loadResourceClasses();
+        initClassResourceInfoList(resourceClasses);
         WadlGenerator wadlGenerator = new WadlGenerator(getBus());
         DocumentationProvider documentationProvider = null;
         if (docProvider != null) {
@@ -188,7 +198,7 @@ public class Java2WADLMojo extends AbstractMojo {
         
         StringBuilder sbMain = wadlGenerator.generateWADL(getBaseURI(), classResourceInfos,
useJson, null, null);
         getLog().debug("the wadl is =====> \n" + sbMain.toString());
-        generateWadl(sbMain.toString());
+        generateWadl(resourceClasses, sbMain.toString());
     }
     
     private void setExtraProperties(WadlGenerator wg) {
@@ -207,7 +217,7 @@ public class Java2WADLMojo extends AbstractMojo {
         }
     }
     
-    private void generateWadl(String wadl) throws MojoExecutionException {
+    private void generateWadl(List<Class<?>> resourceClasses, String wadl) throws
MojoExecutionException {
      
         if (outputFile == null && project != null) {
             // Put the wadl in target/generated/wadl
@@ -215,10 +225,8 @@ public class Java2WADLMojo extends AbstractMojo {
             String name = null;
             if (outputFileName != null) {
                 name = outputFileName;
-            } else if (classResourceNames.size() == 1) {
-                String className = classResourceNames.get(0);
-                int i = className.lastIndexOf('.');
-                name = className.substring(i + 1);
+            } else if (resourceClasses.size() == 1) {
+                name = resourceClasses.get(0).getSimpleName();
             } else {
                 name = "application";
             }
@@ -288,15 +296,32 @@ public class Java2WADLMojo extends AbstractMojo {
             throw new MojoExecutionException(e.getMessage(), e);
         }
     }
-    
-    private void getResourcesList() throws MojoExecutionException {
+    private List<Class<?>> loadResourceClasses() throws MojoExecutionException
{
+        List<Class<?>> resourceClasses = new ArrayList<Class<?>>(classResourceNames.size());
         for (String className : classResourceNames) {
-            Class<?> beanClass = null;
             try {
-                beanClass = getClassLoader().loadClass(className);
+                resourceClasses.add(getClassLoader().loadClass(className));
             } catch (Exception e) {
                 throw new MojoExecutionException(e.getMessage(), e);
             } 
+        }
+        if (resourceClasses.isEmpty() && basePackages != null) {
+            try {
+                @SuppressWarnings("unchecked")
+                final Map< Class< ? extends Annotation >, Collection< Class<
? > > > discoveredClasses = 
+                    ClasspathScanner.findClasses(ClasspathScanner.parsePackages(basePackages),
Path.class);
+                if (discoveredClasses.containsKey(Path.class)) {
+                    resourceClasses.addAll(discoveredClasses.get(Path.class));
+                }
+            } catch (Exception ex) {
+                // ignore
+            }
+        }
+        return resourceClasses;
+    }
+    
+    private void initClassResourceInfoList(List<Class<?>> resourceClasses) throws
MojoExecutionException {
+        for (Class<?> beanClass : resourceClasses) {
             ClassResourceInfo cri = getCreatedFromModel(beanClass);
             if (cri != null) {
                 if (!InjectionUtils.isConcreteClass(cri.getServiceClass())) {


Mime
View raw message