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-7001] Updating AbstractSpringComponentScanner to discover JAXRS Applications, modified patch from Dennis Kieselhorst applied, This closes #264
Date Fri, 21 Apr 2017 12:02:46 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes 73dcc6d88 -> 5e4a0c99b


[CXF-7001] Updating AbstractSpringComponentScanner to discover JAXRS Applications, modified
patch from Dennis Kieselhorst applied, This closes #264


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

Branch: refs/heads/3.1.x-fixes
Commit: 5e4a0c99bfe9945f0298476979504666fa8a8217
Parents: 73dcc6d
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Fri Apr 21 12:51:47 2017 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Fri Apr 21 13:02:24 2017 +0100

----------------------------------------------------------------------
 .../AbstractSpringComponentScanServer.java      | 137 +++++++++++++------
 .../AbstractSpringConfigurationFactory.java     |   9 +-
 2 files changed, 104 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/5e4a0c99/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 9099519..33cef1e 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
@@ -28,7 +28,9 @@ import java.util.Map;
 import java.util.Set;
 import java.util.logging.Logger;
 
+import javax.ws.rs.ApplicationPath;
 import javax.ws.rs.Path;
+import javax.ws.rs.core.Application;
 import javax.ws.rs.ext.Provider;
 
 import org.apache.cxf.annotations.Provider.Scope;
@@ -37,10 +39,13 @@ import org.apache.cxf.common.util.ClassHelper;
 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.endpoint.Server;
 import org.apache.cxf.feature.Feature;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.interceptor.Interceptor;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
+import org.apache.cxf.jaxrs.utils.ResourceUtils;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.service.factory.ServiceConstructionException;
 import org.springframework.beans.factory.annotation.Value;
@@ -48,8 +53,9 @@ import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.FilterType;
 
 @ComponentScan(
-    includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, 
-                                           value = {Path.class, 
+    includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,
+                                           value = {ApplicationPath.class,
+                                                    Path.class,
                                                     Provider.class,
                                                     org.apache.cxf.annotations.Provider.class})
 )
@@ -76,27 +82,72 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo
     protected AbstractSpringComponentScanServer(Class<? extends Annotation> serviceAnnotation)
{
         this.serviceAnnotation = serviceAnnotation;
     }
-    protected void setJaxrsResources(JAXRSServerFactoryBean factory) {
-        boolean checkJaxrsRoots = checkJaxrsRoots();
-        boolean checkJaxrsProviders = checkJaxrsProviders();
-        boolean checkCxfProviders = checkCxfProviders();
+    @Override
+    protected Server createJaxRsServer() {
+            
+        JAXRSServerFactoryBean factoryBean = null;
+        
+        String[] beanNames = applicationContext.getBeanNamesForAnnotation(ApplicationPath.class);
 
-        Set<String> componentScanPackagesSet = !StringUtils.isEmpty(componentScanPackages)

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

-                ? ClasspathScanner.parsePackages(componentScanBeans) : null;    
+        if (beanNames.length > 0) {
+            Set<String> componentScanPackagesSet = parseSetProperty(componentScanPackages);
+            Set<String> componentScanBeansSet = parseSetProperty(componentScanBeans);
+            
+            for (String beanName : beanNames) {
+                if (isComponentMatched(beanName, componentScanPackagesSet, componentScanBeansSet))
{
+                    Application app = applicationContext.getBean(beanName, Application.class);
+                    factoryBean = createFactoryBeanFromApplication(app);
+                    for (String cxfBeanName : applicationContext.getBeanNamesForAnnotation(
+                                                  org.apache.cxf.annotations.Provider.class))
{
+                        if (isComponentMatched(cxfBeanName, componentScanPackagesSet, componentScanBeansSet))
{
+                            addCxfProvider(getProviderBean(cxfBeanName));
+                        }
+                    }
+                    break;
+                }
+            }
+        }
+            
+        if (!StringUtils.isEmpty(classesScanPackages)) {
+            try {
+                final Map< Class< ? extends Annotation >, Collection< Class<
? > > > appClasses =
+                    ClasspathScanner.findClasses(classesScanPackages, ApplicationPath.class);
+                
+                List<Application> apps = CastUtils.cast(JAXRSServerFactoryBeanDefinitionParser
+                    .createBeansFromDiscoveredClasses(super.applicationContext, 
+                                                      appClasses.get(ApplicationPath.class),
null));
+                if (apps.size() > 0) {
+                    factoryBean = createFactoryBeanFromApplication(apps.get(0));
+                    final Map< Class< ? extends Annotation >, Collection< Class<
? > > > cxfClasses =
+                        ClasspathScanner.findClasses(classesScanPackages, org.apache.cxf.annotations.Provider.class);
+                    addCxfProvidersFromClasses(cxfClasses.get(org.apache.cxf.annotations.Provider.class));
+                }
+                
+            } catch (Exception ex) {
+                throw new ServiceConstructionException(ex);
+            }
+        }
+            
+        if (factoryBean != null) {
+            setFactoryCxfProviders(factoryBean);
+            return factoryBean.create();
+        }
+        
+        return super.createJaxRsServer();
+    }
+    
+    protected void setJaxrsResources(JAXRSServerFactoryBean factory) {
+        Set<String> componentScanPackagesSet = parseSetProperty(componentScanPackages);
+        Set<String> componentScanBeansSet = parseSetProperty(componentScanBeans);
             
         for (String beanName : applicationContext.getBeanDefinitionNames()) {
-            if (checkJaxrsRoots 
-                && isValidComponent(beanName, Path.class, componentScanPackagesSet,
componentScanBeansSet)) {
+            if (isValidComponent(beanName, Path.class, componentScanPackagesSet, componentScanBeansSet))
{
                 SpringResourceFactory resourceFactory = new SpringResourceFactory(beanName);
                 resourceFactory.setApplicationContext(applicationContext);
                 resourceProviders.add(resourceFactory);
-            } else if (checkJaxrsProviders 
-                && isValidComponent(beanName, Provider.class, componentScanPackagesSet,
componentScanBeansSet)) {
+            } else if (isValidComponent(beanName, Provider.class, componentScanPackagesSet,
componentScanBeansSet)) {
                 jaxrsProviders.add(getProviderBean(beanName));
-            } else if (checkCxfProviders 
-                && isValidComponent(beanName, org.apache.cxf.annotations.Provider.class,

+            } else if (isValidComponent(beanName, org.apache.cxf.annotations.Provider.class,

                                     componentScanPackagesSet, componentScanBeansSet)) {
                 addCxfProvider(getProviderBean(beanName));
             }
@@ -109,16 +160,9 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo
                                                  org.apache.cxf.annotations.Provider.class);
                                           
                 jaxrsProviders.addAll(JAXRSServerFactoryBeanDefinitionParser
-                    .createBeansFromDiscoveredClasses(super.applicationContext, classes.get(Provider.class),
null));
+                    .createBeansFromDiscoveredClasses(applicationContext, classes.get(Provider.class),
null));
                 warnIfDuplicatesAvailable(jaxrsProviders);
-                List<Object> cxfProviders = JAXRSServerFactoryBeanDefinitionParser
-                    .createBeansFromDiscoveredClasses(super.applicationContext, 
-                                                      classes.get(org.apache.cxf.annotations.Provider.class),

-                                                      null);
-                for (Object cxfProvider : cxfProviders) {
-                    addCxfProvider(cxfProvider);
-                }
-                warnIfDuplicatesAvailable(cxfFeatures);
+                addCxfProvidersFromClasses(classes.get(org.apache.cxf.annotations.Provider.class));
             } catch (Exception ex) {
                 throw new ServiceConstructionException(ex);
             }
@@ -126,19 +170,36 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo
         
         factory.setResourceProviders(getResourceProviders());
         factory.setProviders(getJaxrsProviders());
+        setFactoryCxfProviders(factory);
+
+    }
+
+    protected void setFactoryCxfProviders(JAXRSServerFactoryBean factory) {
         factory.setFeatures(getFeatures());
         factory.setInInterceptors(getInInterceptors());
         factory.setOutInterceptors(getOutInterceptors());
-        
     }
 
+    protected void addCxfProvidersFromClasses(Collection<Class<?>> classes) {
+        List<Object> cxfProviders = JAXRSServerFactoryBeanDefinitionParser
+            .createBeansFromDiscoveredClasses(applicationContext, classes, null);
+        for (Object cxfProvider : cxfProviders) {
+            addCxfProvider(cxfProvider);
+        }
+        warnIfDuplicatesAvailable(cxfFeatures);    
+    }
     protected boolean isValidComponent(String beanName, 
                                       Class<? extends Annotation> ann,
                                       Set<String> componentScanPackagesSet,
                                       Set<String> componentScanBeansSet) {
         return isAnnotationAvailable(beanName, ann)
             && nonProxyClass(beanName)
-            && matchesServiceAnnotation(beanName)
+            && isComponentMatched(beanName, componentScanPackagesSet, componentScanBeansSet);
+    }
+    protected boolean isComponentMatched(String beanName, 
+                                         Set<String> componentScanPackagesSet,
+                                         Set<String> componentScanBeansSet) {
+        return matchesServiceAnnotation(beanName)
             && matchesComponentPackage(beanName, componentScanPackagesSet)
             && matchesComponentName(beanName, componentScanBeansSet);
     }
@@ -201,18 +262,6 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo
     protected <A extends Annotation> boolean isAnnotationAvailable(String beanName,
Class<A> annClass) {
         return applicationContext.findAnnotationOnBean(beanName, annClass) != null;
     }
-    
-    protected boolean checkCxfProviders() {
-        return true;    
-    }
-    
-    protected boolean checkJaxrsProviders() {
-        return true;    
-    }
-    
-    protected boolean checkJaxrsRoots() {
-        return true;    
-    }
 
     protected List<ResourceProvider> getResourceProviders() {
         return resourceProviders;
@@ -232,5 +281,13 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo
     public List<Interceptor<? extends Message>> getOutInterceptors() {
         return cxfOutInterceptors;
     }
-    
+
+    protected JAXRSServerFactoryBean createFactoryBeanFromApplication(Application app) {
+        return ResourceUtils.createApplication(app, false, true, false, getBus());
+    }
+    protected static Set<String> parseSetProperty(String componentScanProp) {
+        return !StringUtils.isEmpty(componentScanProp) 
+            ? ClasspathScanner.parsePackages(componentScanProp) : null;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/5e4a0c99/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringConfigurationFactory.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringConfigurationFactory.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringConfigurationFactory.java
index a9c6cf5..177700f 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringConfigurationFactory.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringConfigurationFactory.java
@@ -21,6 +21,7 @@ package org.apache.cxf.jaxrs.spring;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.cxf.Bus;
 import org.apache.cxf.bus.spring.SpringBus;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.endpoint.Server;
@@ -44,8 +45,8 @@ public abstract class AbstractSpringConfigurationFactory
         JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
         factory.setAddress(getAddress());
         factory.setTransportId(getTransportId());
-        factory.setBus(applicationContext.getBean(SpringBus.class));
-        
+        factory.setBus(getBus());
+
         setJaxrsResources(factory);
         
         factory.setInInterceptors(getInInterceptors());
@@ -56,6 +57,10 @@ public abstract class AbstractSpringConfigurationFactory
         return factory.create();
     }
     
+    protected Bus getBus() {
+        return applicationContext.getBean(SpringBus.class);
+    }
+
     @Override
     public void setApplicationContext(ApplicationContext ac) throws BeansException {
         applicationContext = ac;


Mime
View raw message