Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id C3CBA200B40 for ; Fri, 1 Jul 2016 11:59:01 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id C2537160A61; Fri, 1 Jul 2016 09:59:01 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E4BCD160A5D for ; Fri, 1 Jul 2016 11:59:00 +0200 (CEST) Received: (qmail 34104 invoked by uid 500); 1 Jul 2016 09:59:00 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 34095 invoked by uid 99); 1 Jul 2016 09:59:00 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 01 Jul 2016 09:59:00 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id F3C6CDFDC1; Fri, 1 Jul 2016 09:58:59 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sergeyb@apache.org To: commits@cxf.apache.org Message-Id: <1db2e59444dd4dd996a3ff7c6750d2ee@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: cxf git commit: [CXF-6869] Adding an option to scan JAXRS resources without them being marked as Spring Components Date: Fri, 1 Jul 2016 09:58:59 +0000 (UTC) archived-at: Fri, 01 Jul 2016 09:59:01 -0000 Repository: cxf Updated Branches: refs/heads/master 1a621886f -> 654f8ba90 [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 Commit: 654f8ba90fdb0da8a7ea0fbbbbd2d0a8bd556a35 Parents: 1a62188 Author: Sergey Beryozkin Authored: Fri Jul 1 10:58:41 2016 +0100 Committer: Sergey Beryozkin 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 jaxrsServices = createBeansFromDiscoveredClasses(classes.get(Path.class)); + List jaxrsProviders = createBeansFromDiscoveredClasses(classes.get(Provider.class)); + + factory.setServiceBeans(jaxrsServices); + factory.setProviders(jaxrsProviders); + } catch (Exception ex) { + throw new ServiceConstructionException(ex); + } + + } + + protected List createBeansFromDiscoveredClasses(Collection> 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(); + } + +}