cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [07/20] cxf git commit: [CXF-6869] Adding an option to scan JAXRS resources without them being marked as Spring Components
Date Sun, 03 Jul 2016 22:10:04 GMT
[CXF-6869] Adding an option to scan JAXRS resources without them being marked as Spring Components


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

Branch: refs/heads/master-jaxrs-2.1
Commit: 654f8ba90fdb0da8a7ea0fbbbbd2d0a8bd556a35
Parents: 1a62188
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Fri Jul 1 10:58:41 2016 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Fri Jul 1 10:58:41 2016 +0100

----------------------------------------------------------------------
 .../autoconfigure/CxfAutoConfiguration.java     | 15 +++-
 .../spring/AbstractJaxrsClassesScanServer.java  | 72 ++++++++++++++++++++
 .../spring/SpringJaxrsClassesScanServer.java    | 32 +++++++++
 3 files changed, 116 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/654f8ba9/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
----------------------------------------------------------------------
diff --git a/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
b/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
index 2c48cfc..4460c7e 100644
--- a/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
+++ b/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
@@ -23,12 +23,13 @@ import java.util.Map;
 import org.apache.cxf.bus.spring.SpringBus;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.spring.SpringComponentScanServer;
+import org.apache.cxf.jaxrs.spring.SpringJaxrsClassesScanServer;
 import org.apache.cxf.transport.servlet.CXFServlet;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
 import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
 import org.springframework.boot.context.embedded.ServletRegistrationBean;
@@ -77,9 +78,17 @@ public class CxfAutoConfiguration {
     
     @Configuration
     @ConditionalOnClass(JAXRSServerFactoryBean.class)
-    @ConditionalOnProperty(prefix = "cxf", name = "jaxrs.component-scan", havingValue = "true")
+    @ConditionalOnExpression("'${cxf.jaxrs.component-scan}'=='true' && '${cxf.jaxrs.classes-scan}'!='true'")
     @Import(SpringComponentScanServer.class)
-    protected static class JaxRsConfiguration {
+    protected static class JaxRsComponentConfiguration {
+     
+    }
+    
+    @Configuration
+    @ConditionalOnClass(JAXRSServerFactoryBean.class)
+    @ConditionalOnExpression("'${cxf.jaxrs.classes-scan}'=='true' && '${cxf.jaxrs.component-scan}'!='true'")
+    @Import(SpringJaxrsClassesScanServer.class)
+    protected static class JaxRsClassesConfiguration {
      
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/654f8ba9/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractJaxrsClassesScanServer.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractJaxrsClassesScanServer.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractJaxrsClassesScanServer.java
new file mode 100644
index 0000000..bfe7625
--- /dev/null
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractJaxrsClassesScanServer.java
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.spring;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+import javax.ws.rs.Path;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.cxf.common.util.ClasspathScanner;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.service.factory.ServiceConstructionException;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
+public abstract class AbstractJaxrsClassesScanServer extends AbstractSpringConfigurationFactory
{
+    @Value("${cxf.jaxrs.classes-scan-packages}")
+    private String basePackages;
+    
+    protected AbstractJaxrsClassesScanServer() {
+        
+    }
+    protected void setJaxrsResources(JAXRSServerFactoryBean factory) {
+        try {
+            final Map< Class< ? extends Annotation >, Collection< Class< ?
> > > classes =
+                ClasspathScanner.findClasses(basePackages, Provider.class, Path.class);
+                                      
+            List<Object> jaxrsServices = createBeansFromDiscoveredClasses(classes.get(Path.class));
+            List<Object> jaxrsProviders = createBeansFromDiscoveredClasses(classes.get(Provider.class));
+            
+            factory.setServiceBeans(jaxrsServices);
+            factory.setProviders(jaxrsProviders);
+        } catch (Exception ex) {
+            throw new ServiceConstructionException(ex);
+        }
+        
+    }
+        
+    protected List<Object> createBeansFromDiscoveredClasses(Collection<Class<?>>
classes) {
+        AutowireCapableBeanFactory beanFactory = super.applicationContext.getAutowireCapableBeanFactory();
+        final List< Object > providers = new ArrayList< Object >();
+        for (final Class< ? > clazz: classes) {
+            Object bean = null;
+            try {
+                bean = beanFactory.createBean(clazz, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE,
true);
+            } catch (Exception ex) {
+                bean = beanFactory.createBean(clazz);
+            }
+            providers.add(bean);
+        }
+        return providers;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/654f8ba9/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringJaxrsClassesScanServer.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringJaxrsClassesScanServer.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringJaxrsClassesScanServer.java
new file mode 100644
index 0000000..6e80b8f
--- /dev/null
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringJaxrsClassesScanServer.java
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.spring;
+
+import org.apache.cxf.endpoint.Server;
+import org.springframework.context.annotation.Bean;
+
+
+public class SpringJaxrsClassesScanServer extends AbstractJaxrsClassesScanServer {
+
+    @Bean
+    public Server jaxRsServer() {
+        return super.createJaxRsServer();
+    }
+    
+}


Mime
View raw message