[camel] branch camel-2.23.x updated: CAMEL-12979 - support for service beans with custom lifecycle on CxfRsEndpoint, Thanks to Jens Kleine-Herzbruch

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch camel-2.23.x updated: CAMEL-12979 - support for service beans with custom lifecycle on CxfRsEndpoint, Thanks to Jens Kleine-Herzbruch

onders-2
This is an automated email from the ASF dual-hosted git repository.

onders pushed a commit to branch camel-2.23.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.23.x by this push:
     new b0b2c1e  CAMEL-12979 - support for service beans with custom lifecycle on CxfRsEndpoint, Thanks to Jens Kleine-Herzbruch
b0b2c1e is described below

commit b0b2c1e18812c773c91c3867cdeac8c6a2907f66
Author: onders <[hidden email]>
AuthorDate: Wed Dec 5 11:26:09 2018 +0300

    CAMEL-12979 - support for service beans with custom lifecycle on CxfRsEndpoint, Thanks to Jens Kleine-Herzbruch
---
 .../camel/component/cxf/jaxrs/CxfRsEndpoint.java   | 32 ++++++++++++++++++++++
 .../component/cxf/jaxrs/CxfRsConsumerTest.java     | 22 ++++++++++++++-
 2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
index e756da8..0aa2693 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
@@ -85,6 +85,8 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
     private String address;
     @UriParam
     private List<Class<?>> resourceClasses;
+    @UriParam(label = "consumer,advanced")
+    private List<Object> serviceBeans;
     @UriParam
     private String modelRef;
     @UriParam(label = "consumer", defaultValue = "Default")
@@ -253,6 +255,9 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
         if (getResourceClasses() != null) {
             sfb.setResourceClasses(getResourceClasses());
         }
+        if (serviceBeans != null && !serviceBeans.isEmpty()) {
+            sfb.setServiceBeans(serviceBeans);
+        }
 
         // setup the resource providers for interfaces
         List<ClassResourceInfo> cris = sfb.getServiceFactory().getClassResourceInfo();
@@ -439,6 +444,33 @@ public class CxfRsEndpoint extends DefaultEndpoint implements HeaderFilterStrate
         setResourceClasses(Arrays.asList(classes));
     }
 
+    public List<?> getServiceBeans() {
+        return serviceBeans;
+    }
+
+    public void addServiceBean(Object bean) {
+        if (serviceBeans == null) {
+            serviceBeans = new ArrayList<>();
+        }
+        serviceBeans.add(bean);
+    }
+
+    /**
+     * The service beans which you want to export as REST service. Multiple beans can be separated by comma.
+     */
+    public void setServiceBeans(List<?> beans) {
+        this.serviceBeans = new ArrayList<Object>(beans);
+    }
+
+    public void setServiceBeans(Object... beans) {
+        setServiceBeans(Arrays.asList(beans));
+    }
+
+    public void setServiceBeans(String beans) {
+        setServiceBeans(EndpointHelper.resolveReferenceListParameter(getCamelContext(), beans, Object.class));
+    }
+
+
     /**
      * The service publish address.
      */
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
index 7420732..895c02f 100644
--- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 
+import javax.naming.Context;
 import javax.servlet.ServletRequest;
 import javax.ws.rs.HttpMethod;
 import javax.ws.rs.WebApplicationException;
@@ -33,6 +34,7 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.LoggingLevel;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.RuntimeCamelException;
@@ -41,6 +43,7 @@ import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.cxf.CXFTestSupport;
 import org.apache.camel.component.cxf.common.message.CxfConstants;
 import org.apache.camel.component.cxf.jaxrs.testbean.Customer;
+import org.apache.camel.component.cxf.jaxrs.testbean.CustomerService;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpGet;
@@ -71,6 +74,17 @@ public class CxfRsConsumerTest extends CamelTestSupport {
             "cxfrs://http://localhost:" + CXT + "/rest5?"
             + "propagateContexts=true&"
             + "modelRef=classpath:/org/apache/camel/component/cxf/jaxrs/CustomerServiceDefaultHandlerModel.xml";
+    private static final String CXF_RS_ENDPOINT_URI6 =
+            "cxfrs://http://localhost:" + CXT + "/rest6?"
+            + "performInvocation=true&serviceBeans=#serviceBean";
+
+    @Override
+    protected Context createJndiContext() throws Exception {
+        Context ctx = super.createJndiContext();
+        ctx.bind("serviceBean", new CustomerService());
+        return ctx;
+    }
+
     protected RouteBuilder createRouteBuilder() throws Exception {
         final Processor testProcessor = new TestProcessor();
         final Processor testProcessor2 = new TestProcessor2();
@@ -83,6 +97,7 @@ public class CxfRsConsumerTest extends CamelTestSupport {
                 from(CXF_RS_ENDPOINT_URI3).process(testProcessor);
                 from(CXF_RS_ENDPOINT_URI4).process(testProcessor2);
                 from(CXF_RS_ENDPOINT_URI5).process(testProcessor3);
+                from(CXF_RS_ENDPOINT_URI6).log(LoggingLevel.OFF, "dummy");
             }
         };
     }
@@ -141,7 +156,12 @@ public class CxfRsConsumerTest extends CamelTestSupport {
             "The remoteAddress is 127.0.0.1");
         
     }
-    
+
+    @Test
+    public void testGetCustomerImplCustomLifecycle() throws Exception {
+        invokeGetCustomer("http://localhost:" + CXT + "/rest6/customerservice/customers/123",
+                "{\"Customer\":{\"id\":123,\"name\":\"John\"}}");
+    }
     
     @Test
     public void testGetWrongCustomer() throws Exception {