Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 892913e71 -> 6c9dab7e7


[CXF-6121] Extending JAX-RS Spring parser to support an annotation based 
filtering of the resources, modified patch from Thorsten Hoeger applied


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

Branch: refs/heads/3.0.x-fixes
Commit: 6c9dab7e7120d4e880af8e6e8fcacb153487366e
Parents: 892913e
Author: Sergey Beryozkin <sberyoz...@talend.com>
Authored: Wed Dec 3 12:17:57 2014 +0000
Committer: Sergey Beryozkin <sberyoz...@talend.com>
Committed: Wed Dec 3 12:21:42 2014 +0000

----------------------------------------------------------------------
 .../JAXRSServerFactoryBeanDefinitionParser.java | 56 ++++++++++++++++----
 .../jaxrs/src/main/resources/schemas/jaxrs.xsd  |  1 +
 2 files changed, 48 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/6c9dab7e/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java
----------------------------------------------------------------------
diff --git 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java
 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java
index 4ab0137..f786c28 100644
--- 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java
+++ 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.lang.annotation.Annotation;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
@@ -32,6 +33,7 @@ import javax.xml.namespace.QName;
 import org.w3c.dom.Element;
 
 import org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor;
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.util.ClasspathScanner;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.configuration.spring.AbstractBeanDefinitionParser;
@@ -78,6 +80,8 @@ public class JAXRSServerFactoryBeanDefinitionParser extends 
AbstractBeanDefiniti
             bean.addPropertyValue(name, q);
         } else if ("basePackages".equals(name)) {            
             bean.addPropertyValue("basePackages", 
ClasspathScanner.parsePackages(val));
+        } else if ("serviceAnnotation".equals(name)) {
+            bean.addPropertyValue("serviceAnnotation", val);
         } else {
             mapToProperty(bean, name, val);
         }
@@ -151,6 +155,7 @@ public class JAXRSServerFactoryBeanDefinitionParser extends 
AbstractBeanDefiniti
         
         private List<SpringResourceFactory> tempFactories;
         private List<String> basePackages;
+        private String serviceAnnotation;
         private ApplicationContext context;
         public SpringJAXRSServerFactoryBean() {
             super();
@@ -171,6 +176,10 @@ public class JAXRSServerFactoryBeanDefinitionParser 
extends AbstractBeanDefiniti
             this.basePackages = basePackages;
         }
         
+        public void setServiceAnnotation(String serviceAnnotation) {
+            this.serviceAnnotation = serviceAnnotation;
+        }
+        
         public void setTempResourceProviders(List<SpringResourceFactory> 
providers) {
             tempFactories = providers;
         }
@@ -189,30 +198,59 @@ public class JAXRSServerFactoryBeanDefinitionParser 
extends AbstractBeanDefiniti
                 tempFactories.clear();
                 super.setResourceProviders(factories);
             }
-            
+            Class<? extends Annotation> serviceAnnotationClass = 
loadServiceAnnotationClass();
             try {
                 if (basePackages != null) {
                     @SuppressWarnings("unchecked")
                     final Map< Class< ? extends Annotation >, Collection< 
Class< ? > > > classes = 
                         ClasspathScanner.findClasses(basePackages, 
Provider.class, Path.class);
                                               
-                    
this.setProviders(createBeans(classes.get(Provider.class)));
-                    this.setServiceBeans(createBeans(classes.get(Path.class)));
+                    
this.setServiceBeans(createBeansFromDiscoveredClasses(classes.get(Path.class),
+                                                                          
serviceAnnotationClass));
+                    
this.setProviders(createBeansFromDiscoveredClasses(classes.get(Provider.class),
+                                                                       
serviceAnnotationClass));
+                } catch (IOException ex) {
+                    throw new BeanDefinitionStoreException("I/O failure during 
classpath scanning", ex);
+                } catch (ClassNotFoundException ex) {
+                    throw new BeanCreationException("Failed to create bean 
from classfile", ex);
                 }
-            } catch (IOException ex) {
-                throw new BeanDefinitionStoreException("I/O failure during 
classpath scanning", ex);
-            } catch (ClassNotFoundException ex) {
-                throw new BeanCreationException("Failed to create bean from 
classfile", ex);
+            } else if (serviceAnnotationClass != null) {
+                List<Object> services = new LinkedList<Object>();
+                List<Object> providers = new LinkedList<Object>();
+                for (Object obj : 
ctx.getBeansWithAnnotation(serviceAnnotationClass).values()) {
+                    Class<?> cls = obj.getClass();
+                    if (cls.getAnnotation(Path.class) != null) {
+                        services.add(obj);
+                    } else if (cls.getAnnotation(Provider.class) != null) {
+                        providers.add(obj);
+                    } 
+                }
+                this.setServiceBeans(services);
+                this.setProviders(providers);
             }
-            
             if (bus == null) {
                 setBus(BusWiringBeanFactoryPostProcessor.addDefaultBus(ctx));
             }
         }        
-        private List<Object> createBeans(Collection<Class<?>> classes) {
+        @SuppressWarnings("unchecked")
+        private Class<? extends Annotation> loadServiceAnnotationClass() {
+            if (serviceAnnotation != null) {
+                try {
+                    return (Class<? extends 
Annotation>)ClassLoaderUtils.loadClass(serviceAnnotation, this.getClass());
+                } catch (Exception ex) {
+                    throw new RuntimeException(ex);
+                }
+            } 
+            return null;
+        }
+        private List<Object> 
createBeansFromDiscoveredClasses(Collection<Class<?>> classes, 
+                                                              Class<? extends 
Annotation> serviceClassAnnotation) {
             AutowireCapableBeanFactory beanFactory = 
context.getAutowireCapableBeanFactory();
             final List< Object > providers = new ArrayList< Object >();
             for (final Class< ? > clazz: classes) {
+                if (serviceClassAnnotation != null && 
clazz.getAnnotation(serviceClassAnnotation) == null) {
+                    continue;
+                }
                 Object bean = null;
                 try {
                     bean = beanFactory.createBean(clazz, 
AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);

http://git-wip-us.apache.org/repos/asf/cxf/blob/6c9dab7e/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd 
b/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd
index 932dbe9..ef408ad 100644
--- a/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd
+++ b/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd
@@ -69,6 +69,7 @@
           <xsd:attribute name="docLocation" type="xsd:string"/>
           <xsd:attribute name="publishedEndpointUrl" type="xsd:string"/>
           <xsd:attribute name="basePackages" type="xsd:string"/>
+          <xsd:attribute name="serviceAnnotation" type="xsd:string"/>
         </xsd:extension>
       </xsd:complexContent>
     </xsd:complexType>

Reply via email to