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-7290] Adding cxf.jaxrs.component-scan-packages and cxf.jaxrs.component-scan-beans properties
Date Tue, 21 Mar 2017 16:14:34 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes e6ffe48b9 -> ae1d427f6


[CXF-7290] Adding cxf.jaxrs.component-scan-packages and cxf.jaxrs.component-scan-beans properties


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

Branch: refs/heads/3.1.x-fixes
Commit: ae1d427f61362cd56596f9fa2d1ff0000ee64861
Parents: e6ffe48
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Tue Mar 21 16:02:48 2017 +0000
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Tue Mar 21 16:14:15 2017 +0000

----------------------------------------------------------------------
 .../AbstractSpringComponentScanServer.java      | 49 ++++++++++++++++----
 1 file changed, 40 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/ae1d427f/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringComponentScanServer.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringComponentScanServer.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringComponentScanServer.java
index fb8ee4b..4afe05e 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringComponentScanServer.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringComponentScanServer.java
@@ -33,6 +33,7 @@ import javax.ws.rs.ext.Provider;
 import org.apache.cxf.annotations.Provider.Scope;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.ClasspathScanner;
+import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.feature.Feature;
 import org.apache.cxf.interceptor.Interceptor;
@@ -55,7 +56,11 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo
     private static final Logger LOG = LogUtils.getL7dLogger(AbstractSpringComponentScanServer.class);
     @Value("${cxf.jaxrs.classes-scan-packages:}")
     private String classesScanPackages;
-    
+    @Value("${cxf.jaxrs.component-scan-packages:}")
+    private String componentScanPackages;
+    @Value("${cxf.jaxrs.component-scan-beans:}")
+    private String componentScanBeans;
+
     private List<ResourceProvider> resourceProviders = new LinkedList<ResourceProvider>();
     private List<Object> jaxrsProviders = new LinkedList<Object>();
     private List<Feature> cxfFeatures = new LinkedList<Feature>();
@@ -73,18 +78,24 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo
         boolean checkJaxrsRoots = checkJaxrsRoots();
         boolean checkJaxrsProviders = checkJaxrsProviders();
         boolean checkCxfProviders = checkCxfProviders();
-        
+
+        Set<String> componentScanPackagesSet = !StringUtils.isEmpty(componentScanPackages)

+            ? ClasspathScanner.parsePackages(componentScanPackages) : null;
+        Set<String> componentScanBeansSet = !StringUtils.isEmpty(componentScanBeans)

+                ? ClasspathScanner.parsePackages(componentScanBeans) : null;    
+            
         for (String beanName : applicationContext.getBeanDefinitionNames()) {
-            if (checkJaxrsRoots && isAnnotationAvailable(beanName, Path.class)
-                && matchesServiceAnnotation(beanName)) {
+            if (checkJaxrsRoots 
+                && isValidComponent(beanName, Path.class, componentScanPackagesSet,
componentScanBeansSet)) {
                 SpringResourceFactory resourceFactory = new SpringResourceFactory(beanName);
                 resourceFactory.setApplicationContext(applicationContext);
                 resourceProviders.add(resourceFactory);
-            } else if (checkJaxrsProviders && isAnnotationAvailable(beanName, Provider.class)
-                && matchesServiceAnnotation(beanName)) {
+            } else if (checkJaxrsProviders 
+                && isValidComponent(beanName, Provider.class, componentScanPackagesSet,
componentScanBeansSet)) {
                 jaxrsProviders.add(getProviderBean(beanName));
-            } else if (checkCxfProviders && isAnnotationAvailable(beanName, 
-                org.apache.cxf.annotations.Provider.class) && matchesServiceAnnotation(beanName))
{
+            } else if (checkCxfProviders 
+                && isValidComponent(beanName, org.apache.cxf.annotations.Provider.class,

+                                    componentScanPackagesSet, componentScanBeansSet)) {
                 addCxfProvider(getProviderBean(beanName));
             }
         }
@@ -118,7 +129,27 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo
         factory.setOutInterceptors(getOutInterceptors());
         
     }
-    
+
+    protected boolean isValidComponent(String beanName, 
+                                      Class<? extends Annotation> ann,
+                                      Set<String> componentScanPackagesSet,
+                                      Set<String> componentScanBeansSet) {
+        return isAnnotationAvailable(beanName, ann)
+            && matchesServiceAnnotation(beanName)
+            && matchesComponentPackage(beanName, componentScanPackagesSet)
+            && matchesComponentName(beanName, componentScanBeansSet);
+    }
+    protected boolean matchesComponentName(String beanName, Set<String> componentScanBeansSet)
{
+        return componentScanBeansSet == null || componentScanBeansSet.contains(beanName);
+    }
+    protected boolean matchesComponentPackage(String beanName, Set<String> componentScanPackagesSet)
{
+        return componentScanPackagesSet == null 
+            || !applicationContext.isSingleton(beanName)
+            || componentScanPackagesSet.contains(
+                PackageUtils.getPackageName(applicationContext.getBean(beanName).getClass()));
+        
+    }
+
     private static void warnIfDuplicatesAvailable(List<? extends Object> providers)
{
         Set<String> classNames = new HashSet<String>();
         for (Object o : providers) {


Mime
View raw message