This is an automated email from the ASF dual-hosted git repository. ningjiang pushed a commit to branch camel-2.20.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-2.20.x by this push: new cc38f2a CAMEL-12541: Use configured bean instead of creating a new bean cc38f2a is described below commit cc38f2a6b5358a399906296a40870e1619ea4637 Author: Willian Antunes <willian.lima.antu...@gmail.com> AuthorDate: Sun May 27 13:20:53 2018 -0300 CAMEL-12541: Use configured bean instead of creating a new bean When the endpoint is created the bean is always available and is used for the consumer approach (newJAXRSServerFactoryBean). Now the same behavior is used for producer approach (newJAXRSClientFactoryBean). CAMEL-12541: In order to make CxfRsProducer cache works properly CAMEL-12541: Test object reference and no need for version check anymore Version check was removed as we already move to 2.6.x CAMEL-12541 Fixed the build error --- .../component/cxf/jaxrs/CxfRsSpringEndpoint.java | 16 +++++- .../cxf/jaxrs/CxfRsSpringEndpointTest.java | 61 +++++++++++++++++----- .../cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml | 57 -------------------- .../cxf/jaxrs/CxfRsSpringEndpointBeans.xml | 17 ++++-- 4 files changed, 76 insertions(+), 75 deletions(-) diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java index d10d6c5..94ebae2 100644 --- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java +++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpoint.java @@ -25,6 +25,7 @@ import org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean; import org.springframework.context.ApplicationContext; +import org.springframework.util.ReflectionUtils; public class CxfRsSpringEndpoint extends CxfRsEndpoint implements BeanIdAware { private AbstractJAXRSFactoryBean bean; @@ -69,7 +70,8 @@ public class CxfRsSpringEndpoint extends CxfRsEndpoint implements BeanIdAware { @Override protected JAXRSClientFactoryBean newJAXRSClientFactoryBean() { - return new SpringJAXRSClientFactoryBean(); + checkBeanType(bean, JAXRSClientFactoryBean.class); + return newInstanceWithCommonProperties(); } @Override @@ -86,4 +88,14 @@ public class CxfRsSpringEndpoint extends CxfRsEndpoint implements BeanIdAware { public void setBeanId(String id) { this.beanId = id; } -} + + private JAXRSClientFactoryBean newInstanceWithCommonProperties() { + SpringJAXRSClientFactoryBean cfb = new SpringJAXRSClientFactoryBean(); + + if (bean instanceof SpringJAXRSClientFactoryBean) { + ReflectionUtils.shallowCopyFieldState(bean, cfb); + } + + return cfb; + } +} \ No newline at end of file diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java index b5a4354..55bbc25 100644 --- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java @@ -22,18 +22,24 @@ import org.apache.camel.component.cxf.jaxrs.testbean.CustomerService; import org.apache.camel.component.cxf.spring.SpringJAXRSClientFactoryBean; import org.apache.camel.component.cxf.spring.SpringJAXRSServerFactoryBean; import org.apache.camel.test.spring.CamelSpringTestSupport; -import org.apache.cxf.version.Version; import org.junit.Test; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.context.support.AbstractXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CxfRsSpringEndpointTest extends CamelSpringTestSupport { - + + private static final String BEAN_SERVICE_ENDPOINT_NAME = "serviceEndpoint"; + private static final String BEAN_SERVICE_ADDRESS = "http://localhost/programmatically"; + private static final String BEAN_SERVICE_USERNAME = "BEAN_SERVICE_USERNAME"; + private static final String BEAN_SERVICE_PASSWORD = "BEAN_SERVICE_PASSWORD"; + @Test public void testCreateCxfRsServerFactoryBean() { CxfRsEndpoint endpoint = resolveMandatoryEndpoint("cxfrs://bean://rsServer", CxfRsEndpoint.class); SpringJAXRSServerFactoryBean sfb = (SpringJAXRSServerFactoryBean)endpoint.createJAXRSServerFactoryBean(); - + assertEquals("Get a wrong provider size", 1, sfb.getProviders().size()); assertEquals("Get a wrong beanId", sfb.getBeanId(), "rsServer"); assertEquals("Get a wrong address", sfb.getAddress(), "http://localhost:9000/router"); @@ -42,33 +48,64 @@ public class CxfRsSpringEndpointTest extends CamelSpringTestSupport { assertEquals("Got the wrong loggingFeatureEnabled", true, sfb.isLoggingFeatureEnabled()); assertEquals("Got the wrong loggingSizeLimit", 200, sfb.getLoggingSizeLimit()); assertEquals("Got a wrong size of interceptors", 1, sfb.getInInterceptors().size()); - + Map<String, Object> endpointProps = sfb.getProperties(); // The beanId key is put by the AbstractCxfBeanDefinitionParser, so the size is 2 assertEquals("Single endpoint property is expected", 2, endpointProps.size()); assertEquals("Wrong property value", "aValue", endpointProps.get("aKey")); } - + @Test public void testCreateCxfRsClientFactoryBean() { CxfRsEndpoint endpoint = resolveMandatoryEndpoint("cxfrs://bean://rsClient", CxfRsEndpoint.class); SpringJAXRSClientFactoryBean cfb = (SpringJAXRSClientFactoryBean)endpoint.createJAXRSClientFactoryBean(); assertEquals("Get a wrong beanId", cfb.getBeanId(), "rsClient"); - assertEquals("Get a wrong address", cfb.getAddress(), "http://localhost:9002/helloworld"); + assertEquals("Get a wrong address", cfb.getAddress(), "http://localhost:9002/helloworld"); assertTrue("Get a wrong resource class instance", cfb.create() instanceof CustomerService); assertEquals("Got the wrong loggingFeatureEnabled", false, cfb.isLoggingFeatureEnabled()); assertEquals("Got the wrong loggingSizeLimit", 0, cfb.getLoggingSizeLimit()); assertEquals("Got a wrong size of interceptors", 1, cfb.getInInterceptors().size()); } - + + @Test + public void testCreateCxfRsClientFactoryBeanProgrammatically() { + + CxfRsEndpoint endpoint = resolveMandatoryEndpoint("cxfrs://bean://" + BEAN_SERVICE_ENDPOINT_NAME, CxfRsEndpoint.class); + SpringJAXRSClientFactoryBean cfb = (SpringJAXRSClientFactoryBean)endpoint.createJAXRSClientFactoryBean(); + + assertNotSame("Got the same object but must be different", super.applicationContext.getBean(BEAN_SERVICE_ENDPOINT_NAME), cfb); + assertEquals("Got the wrong address", BEAN_SERVICE_ADDRESS, cfb.getAddress()); + assertNotNull("Service class must not be null", cfb.getServiceClass()); + assertEquals("Got the wrong ServiceClass", CustomerService.class, cfb.getServiceClass()); + assertEquals("Got the wrong username", BEAN_SERVICE_USERNAME, cfb.getUsername()); + assertEquals("Got the wrong password", BEAN_SERVICE_PASSWORD, cfb.getPassword()); + } + + public static SpringJAXRSClientFactoryBean serviceEndpoint() { + + SpringJAXRSClientFactoryBean clientFactoryBean = new SpringJAXRSClientFactoryBean(); + clientFactoryBean.setAddress(BEAN_SERVICE_ADDRESS); + clientFactoryBean.setServiceClass(CustomerService.class); + clientFactoryBean.setUsername(BEAN_SERVICE_USERNAME); + clientFactoryBean.setPassword(BEAN_SERVICE_PASSWORD); + + return clientFactoryBean; + } + @Override protected AbstractXmlApplicationContext createApplicationContext() { - String version = Version.getCurrentVersion(); - if (version.contains("2.5") || version.contains("2.4")) { - return new ClassPathXmlApplicationContext(new String("org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml")); - } - return new ClassPathXmlApplicationContext(new String("org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml")); + + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String("org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml")); + emulateBeanRegistrationProgrammatically(applicationContext); + + return applicationContext; } + private void emulateBeanRegistrationProgrammatically(ClassPathXmlApplicationContext applicationContext) { + + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory(); + BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(CxfRsSpringEndpointTest.class.getName()).setFactoryMethod("serviceEndpoint"); + beanFactory.registerBeanDefinition(BEAN_SERVICE_ENDPOINT_NAME, definitionBuilder.getBeanDefinition()); + } } diff --git a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml deleted file mode 100644 index 8b0ca34..0000000 --- a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans-2.6.xml +++ /dev/null @@ -1,57 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - - 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. - ---> -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:cxf="http://camel.apache.org/schema/cxf" - xsi:schemaLocation=" - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd - http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> - - - <cxf:rsServer id="rsServer" address="http://localhost:9000/router" - serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" - loggingFeatureEnabled="true" loggingSizeLimit="200"> - <cxf:properties> - <entry key="aKey" value="aValue"/> - </cxf:properties> - <cxf:providers> - <ref bean="jsonProvider"/> - </cxf:providers> - <cxf:inInterceptors> - <bean class="org.apache.camel.component.cxf.jaxrs.TestInInterceptor"/> - </cxf:inInterceptors> - </cxf:rsServer> - - <cxf:rsClient id="rsClient" address="http://localhost:9002/helloworld" - serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"> - <cxf:inInterceptors> - <bean class="org.apache.camel.component.cxf.jaxrs.TestInInterceptor"/> - </cxf:inInterceptors> - </cxf:rsClient> - - <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider"/> - - <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> - </camelContext> - - - -</beans> diff --git a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml index fcd0e82..8b0ca34 100644 --- a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml +++ b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointBeans.xml @@ -23,22 +23,31 @@ xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd - http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd - "> + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <cxf:rsServer id="rsServer" address="http://localhost:9000/router" serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" loggingFeatureEnabled="true" loggingSizeLimit="200"> + <cxf:properties> + <entry key="aKey" value="aValue"/> + </cxf:properties> <cxf:providers> <ref bean="jsonProvider"/> </cxf:providers> + <cxf:inInterceptors> + <bean class="org.apache.camel.component.cxf.jaxrs.TestInInterceptor"/> + </cxf:inInterceptors> </cxf:rsServer> <cxf:rsClient id="rsClient" address="http://localhost:9002/helloworld" - serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"/> + serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService"> + <cxf:inInterceptors> + <bean class="org.apache.camel.component.cxf.jaxrs.TestInInterceptor"/> + </cxf:inInterceptors> + </cxf:rsClient> - <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider"/> + <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider"/> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> </camelContext> -- To stop receiving notification emails like this one, please contact ningji...@apache.org.