cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6789] Setting Swagger feature resource package to the shared package if multiple service classes are available
Date Wed, 24 Feb 2016 14:08:32 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes 5823ebb3a -> e4bfc1647


[CXF-6789] Setting Swagger feature resource package to the shared package if multiple service
classes are available


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

Branch: refs/heads/3.1.x-fixes
Commit: e4bfc1647e12b39b25bc363bdb9fe8beb2372a3b
Parents: 5823ebb
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Wed Feb 24 14:07:22 2016 +0000
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Wed Feb 24 14:08:19 2016 +0000

----------------------------------------------------------------------
 .../apache/cxf/common/util/PackageUtils.java    | 28 ++++++++++++
 .../cxf/common/util/PackageUtilsTest.java       | 46 +++++++++++++++++++-
 .../jaxrs/swagger/AbstractSwaggerFeature.java   | 26 ++++++++---
 3 files changed, 93 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/e4bfc164/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java b/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
index ada4e1b..b743320 100644
--- a/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
@@ -20,6 +20,7 @@
 package org.apache.cxf.common.util;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.StringTokenizer;
 
@@ -49,6 +50,33 @@ public final class PackageUtils {
         return getPackageName(className);
     }
     
+    public static String getSharedPackageName(List<Class<?>> classes) {
+        List<String> currentParts = null;
+        for (Class<?> cls : classes) {
+            List<String> parts = StringUtils.getParts(getPackageName(cls), "\\.");
+            if (currentParts == null) {
+                currentParts = parts;
+            } else {
+                List<String> subList = Collections.emptyList();
+                for (int i = parts.size() - 1; i > 0; i--) {
+                    subList = parts.subList(0, i + 1);
+                    if (currentParts.equals(subList)) {
+                        break;
+                    }
+                }
+                currentParts.retainAll(subList);
+            }
+        }
+        StringBuilder sb = new StringBuilder();
+        for (String part : currentParts) {
+            if (sb.length() > 0) {
+                sb.append(".");
+            }
+            sb.append(part);
+        }
+        return sb.toString();
+    }
+    
     public static String parsePackageName(String namespace, String defaultPackageName) {
         String packageName = (defaultPackageName != null && defaultPackageName.trim().length()
> 0)
             ? defaultPackageName : null;

http://git-wip-us.apache.org/repos/asf/cxf/blob/e4bfc164/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java b/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
index f8d49a0..644e529 100644
--- a/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
+++ b/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
@@ -20,19 +20,61 @@
 package org.apache.cxf.common.util;
 
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.Collections;
+
 import org.junit.Assert;
 import org.junit.Test;
 
 public class PackageUtilsTest extends Assert {
     @Test
-    public void testgetPackageName() throws Exception {
+    public void testGetClassPackageName() throws Exception {
         String packageName = PackageUtils.getPackageName(this.getClass());       
         assertEquals("Should get same packageName", this.getClass().getPackage().getName(),
packageName);
     }
     
     @Test
-    public void testGetPackageName() throws Exception {
+    public void testGetEmptyPackageName() throws Exception {
         String className = "HelloWorld";
         assertEquals("Should return empty string", "", PackageUtils.getPackageName(className));
     }
+    
+    @Test
+    public void testSharedPackageNameSingleClass() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Collections.singletonList(this.getClass()));       
+        assertEquals(this.getClass().getPackage().getName(), packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesInSamePackage() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(Integer.class, Number.class));       
+        assertEquals("java.lang", packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesInDiffPackages() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(Integer.class, this.getClass()));       
+        assertEquals("", packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesCommonRoot() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(Integer.class, Annotation.class));       
+        assertEquals("java.lang", packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesCommonRoot2() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(Annotation.class, Integer.class));       
+        assertEquals("java.lang", packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesCommonRoot3() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(Annotation.class, Array.class));       
+        assertEquals("java.lang", packageName);
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/e4bfc164/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
index 386855d..7408c6d 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
@@ -18,11 +18,16 @@
  */
 package org.apache.cxf.jaxrs.swagger;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
 import org.apache.cxf.Bus;
+import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.feature.AbstractFeature;
 import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean;
-import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 
 public abstract class AbstractSwaggerFeature extends AbstractFeature {
 
@@ -72,13 +77,24 @@ public abstract class AbstractSwaggerFeature extends AbstractFeature {
     protected abstract void setBasePathByAddress(String address);
 
     private void calculateDefaultResourcePackage(Server server) {
+        if (!StringUtils.isEmpty(getResourcePackage())) {
+            return;
+        }
         JAXRSServiceFactoryBean serviceFactoryBean = 
             (JAXRSServiceFactoryBean)server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
-        AbstractResourceInfo resourceInfo = serviceFactoryBean.getClassResourceInfo().get(0);
+        List<ClassResourceInfo> resourceInfos = serviceFactoryBean.getClassResourceInfo();
         
-        if ((resourceInfo != null) 
-            && (getResourcePackage() == null || getResourcePackage().length() ==
0)) {
-            setResourcePackage(resourceInfo.getServiceClass().getPackage().getName());
+        if (resourceInfos.size() == 1) {
+            setResourcePackage(resourceInfos.get(0).getServiceClass().getPackage().getName());
+        } else {
+            List<Class<?>> serviceClasses = new ArrayList<Class<?>>(resourceInfos.size());
+            for (ClassResourceInfo cri : resourceInfos) {
+                serviceClasses.add(cri.getServiceClass());
+            }
+            String sharedPackage = PackageUtils.getSharedPackageName(serviceClasses);
+            if (!StringUtils.isEmpty(getResourcePackage())) {
+                setResourcePackage(sharedPackage);
+            }
         }
     }
     


Mime
View raw message