CAMEL-8317: XML DSL - Allow to use bean property style to configure endpoint 
options


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/75bcae1a
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/75bcae1a
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/75bcae1a

Branch: refs/heads/master
Commit: 75bcae1a0202529f88c1e572231f104ebf8fb775
Parents: ffd8bea
Author: Claus Ibsen <[email protected]>
Authored: Thu Feb 5 13:37:13 2015 +0100
Committer: Claus Ibsen <[email protected]>
Committed: Thu Feb 5 15:51:24 2015 +0100

----------------------------------------------------------------------
 .../handler/CamelNamespaceHandler.java          | 61 +++++++++++++++++---
 .../test/blueprint/EndpointPropertyTest.java    | 52 +++++++++++++++++
 .../test/blueprint/EndpointPropertyTest.xml     | 49 ++++++++++++++++
 3 files changed, 153 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/75bcae1a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
----------------------------------------------------------------------
diff --git 
a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
 
b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
index 8d0a789..85e26f0 100644
--- 
a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
+++ 
b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
@@ -28,18 +28,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.Callable;
-
 import javax.xml.bind.Binder;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-
 import org.apache.aries.blueprint.BeanProcessor;
 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
 import org.apache.aries.blueprint.ComponentDefinitionRegistryProcessor;
@@ -52,11 +44,13 @@ import 
org.apache.aries.blueprint.mutable.MutableRefMetadata;
 import org.apache.aries.blueprint.mutable.MutableReferenceMetadata;
 import org.apache.camel.BeanInject;
 import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Produce;
 import org.apache.camel.PropertyInject;
 import org.apache.camel.blueprint.BlueprintCamelContext;
 import org.apache.camel.blueprint.CamelContextFactoryBean;
+import org.apache.camel.blueprint.CamelEndpointFactoryBean;
 import org.apache.camel.blueprint.CamelRestContextFactoryBean;
 import org.apache.camel.blueprint.CamelRouteContextFactoryBean;
 import org.apache.camel.builder.xml.Namespaces;
@@ -97,7 +91,6 @@ import 
org.apache.camel.util.blueprint.SecureRandomParametersFactoryBean;
 import org.apache.camel.util.jsse.KeyStoreParameters;
 import org.apache.camel.util.jsse.SSLContextParameters;
 import org.apache.camel.util.jsse.SecureRandomParameters;
-
 import org.osgi.framework.Bundle;
 import org.osgi.service.blueprint.container.BlueprintContainer;
 import org.osgi.service.blueprint.container.ComponentDefinitionException;
@@ -107,6 +100,11 @@ import org.osgi.service.blueprint.reflect.Metadata;
 import org.osgi.service.blueprint.reflect.RefMetadata;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 import static 
org.osgi.service.blueprint.reflect.ComponentMetadata.ACTIVATION_LAZY;
 import static 
org.osgi.service.blueprint.reflect.ServiceReferenceMetadata.AVAILABILITY_MANDATORY;
@@ -124,6 +122,7 @@ public class CamelNamespaceHandler implements 
NamespaceHandler {
     private static final String CAMEL_CONTEXT = "camelContext";
     private static final String ROUTE_CONTEXT = "routeContext";
     private static final String REST_CONTEXT = "restContext";
+    private static final String ENDPOINT = "endpoint";
     private static final String KEY_STORE_PARAMETERS = "keyStoreParameters";
     private static final String SECURE_RANDOM_PARAMETERS = 
"secureRandomParameters";
     private static final String SSL_CONTEXT_PARAMETERS = 
"sslContextParameters";
@@ -191,6 +190,9 @@ public class CamelNamespaceHandler implements 
NamespaceHandler {
             if (element.getLocalName().equals(REST_CONTEXT)) {
                 return parseRestContextNode(element, context);
             }
+            if (element.getLocalName().equals(ENDPOINT)) {
+                return parseEndpointNode(element, context);
+            }
             if (element.getLocalName().equals(KEY_STORE_PARAMETERS)) {
                 return parseKeyStoreParametersNode(element, context);
             }
@@ -409,6 +411,47 @@ public class CamelNamespaceHandler implements 
NamespaceHandler {
         return ctx;
     }
 
+    private Metadata parseEndpointNode(Element element, ParserContext context) 
{
+        LOG.trace("Parsing Endpoint {}", element);
+        // now parse the rests with JAXB
+        Binder<Node> binder;
+        try {
+            binder = getJaxbContext().createBinder();
+        } catch (JAXBException e) {
+            throw new ComponentDefinitionException("Failed to create the JAXB 
binder : " + e, e);
+        }
+        Object value = parseUsingJaxb(element, context, binder);
+        if (!(value instanceof CamelEndpointFactoryBean)) {
+            throw new ComponentDefinitionException("Expected an instance of " 
+ CamelEndpointFactoryBean.class);
+        }
+
+        CamelEndpointFactoryBean rcfb = (CamelEndpointFactoryBean) value;
+        String id = rcfb.getId();
+
+        MutablePassThroughMetadata factory = 
context.createMetadata(MutablePassThroughMetadata.class);
+        factory.setId(".camelBlueprint.passThrough." + id);
+        factory.setObject(new PassThroughCallable<Object>(rcfb));
+
+        MutableBeanMetadata factory2 = 
context.createMetadata(MutableBeanMetadata.class);
+        factory2.setId(".camelBlueprint.factory." + id);
+        factory2.setFactoryComponent(factory);
+        factory2.setFactoryMethod("call");
+        factory2.setInitMethod("afterPropertiesSet");
+        factory2.setDestroyMethod("destroy");
+        factory2.addProperty("blueprintContainer", createRef(context, 
"blueprintContainer"));
+
+        MutableBeanMetadata ctx = 
context.createMetadata(MutableBeanMetadata.class);
+        ctx.setId(id);
+        ctx.setRuntimeClass(Endpoint.class);
+        ctx.setFactoryComponent(factory2);
+        ctx.setFactoryMethod("getObject");
+        // must be lazy as we want CamelContext to be activated first
+        ctx.setActivation(ACTIVATION_LAZY);
+
+        LOG.trace("Parsing endpoint done, returning {}", element, ctx);
+        return ctx;
+    }
+
     private Metadata parseKeyStoreParametersNode(Element element, 
ParserContext context) {
         LOG.trace("Parsing KeyStoreParameters {}", element);
         // now parse the key store parameters with JAXB

http://git-wip-us.apache.org/repos/asf/camel/blob/75bcae1a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/EndpointPropertyTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/EndpointPropertyTest.java
 
b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/EndpointPropertyTest.java
new file mode 100644
index 0000000..d8ad7ec
--- /dev/null
+++ 
b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/EndpointPropertyTest.java
@@ -0,0 +1,52 @@
+/**
+ * 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.camel.test.blueprint;
+
+import org.apache.camel.blueprint.BlueprintCamelContext;
+import org.apache.camel.component.seda.SedaEndpoint;
+import org.junit.Test;
+
+public class EndpointPropertyTest extends CamelBlueprintTestSupport {
+
+    @Override
+    protected String getBlueprintDescriptor() {
+        return "org/apache/camel/test/blueprint/EndpointPropertyTest.xml";
+    }
+
+    @Test
+    public void testEndpointProperty() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(2);
+        template.sendBody("ref:foo", "Hello World");
+        template.sendBody("ref:bar", "Bye World");
+        assertMockEndpointsSatisfied();
+
+        BlueprintCamelContext blue = 
context().adapt(BlueprintCamelContext.class);
+
+        SedaEndpoint foo = (SedaEndpoint) 
blue.getBlueprintContainer().getComponentInstance("foo");
+        assertNotNull(foo);
+        assertEquals(100, foo.getSize());
+        assertEquals(5000, foo.getPollTimeout());
+        assertEquals(true, foo.isBlockWhenFull());
+        
assertEquals("seda://foo?blockWhenFull=true&pollTimeout=5000&size=100", 
foo.getEndpointUri());
+
+        SedaEndpoint bar = (SedaEndpoint) 
blue.getBlueprintContainer().getComponentInstance("bar");
+        assertNotNull(bar);
+        assertEquals(200, bar.getSize());
+        assertEquals("seda://bar?size=200", bar.getEndpointUri());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/75bcae1a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/EndpointPropertyTest.xml
----------------------------------------------------------------------
diff --git 
a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/EndpointPropertyTest.xml
 
b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/EndpointPropertyTest.xml
new file mode 100644
index 0000000..d9075cb
--- /dev/null
+++ 
b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/EndpointPropertyTest.xml
@@ -0,0 +1,49 @@
+<?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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0";
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+           xsi:schemaLocation="
+             http://www.osgi.org/xmlns/blueprint/v1.0.0 
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd";>
+
+  <!-- outside camelContext -->
+  <endpoint id="bar" uri="seda:bar" camelContextId="myCamel" 
xmlns="http://camel.apache.org/schema/blueprint";>
+    <property key="size" value="200"/>
+  </endpoint>
+
+  <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/blueprint";>
+
+    <!-- inside camelContext -->
+    <endpoint id="foo" uri="seda:foo">
+      <property key="size" value="100"/>
+      <property key="pollTimeout" value="5000"/>
+      <property key="blockWhenFull" value="true"/>
+    </endpoint>
+
+    <route>
+      <from uri="ref:foo"/>
+      <to uri="mock:result"/>
+    </route>
+
+    <route>
+      <from uri="ref:bar"/>
+      <to uri="mock:result"/>
+    </route>
+
+  </camelContext>
+
+</blueprint>
\ No newline at end of file

Reply via email to