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 11:52:04 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 40cb28fbc -> 5afec44d7


[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/5afec44d
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/5afec44d
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/5afec44d

Branch: refs/heads/master
Commit: 5afec44d741580762fb5d6f4f4fac7d34446ae95
Parents: 40cb28f
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 12:51:47 2017 +0100

----------------------------------------------------------------------
 .../AbstractSpringComponentScanServer.java      | 133 +++++++++++++------
 .../AbstractSpringConfigurationFactory.java     |   7 +-
 2 files changed, 100 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/5afec44d/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 d5a11dc..65c665b 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;
@@ -49,7 +54,8 @@ import org.springframework.context.annotation.FilterType;
 
 @ComponentScan(
     includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,
-                                           value = {Path.class,
+                                           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;
     }
+    @Override
+    protected Server createJaxRsServer() {
+            
+        JAXRSServerFactoryBean factoryBean = null;
+        
+        String[] beanNames = applicationContext.getBeanNamesForAnnotation(ApplicationPath.class);
+
+        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) {
-        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;    
+        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,35 @@ 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 +261,6 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo
         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,4 +280,11 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo
         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/5afec44d/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 2c675d4..1ba53ef 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,7 +45,7 @@ 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);
 
@@ -55,6 +56,10 @@ public abstract class AbstractSpringConfigurationFactory
         finalizeFactorySetup(factory);
         return factory.create();
     }
+    
+    protected Bus getBus() {
+        return applicationContext.getBean(SpringBus.class);
+    }
 
     @Override
     public void setApplicationContext(ApplicationContext ac) throws BeansException {


Mime
View raw message