You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/08/11 09:10:01 UTC

[1/2] camel git commit: CAMEL-10235: Add fluentTemplate to XML DSL

Repository: camel
Updated Branches:
  refs/heads/master 940fde21b -> eb8fcf9ab


CAMEL-10235: Add fluentTemplate to XML DSL


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

Branch: refs/heads/master
Commit: 26639590c2f0d4c4e23327be2649b1c3df772f37
Parents: 940fde2
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Aug 11 10:57:54 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Aug 11 10:57:54 2016 +0200

----------------------------------------------------------------------
 ...tCamelFluentProducerTemplateFactoryBean.java | 105 +++++++++++++++++++
 ...bstractCamelProducerTemplateFactoryBean.java |   5 +
 .../camel/spring/CamelContextFactoryBean.java   |  23 ++--
 .../CamelFluentProducerTemplateFactoryBean.java |  56 ++++++++++
 .../spring/handler/CamelNamespaceHandler.java   |  35 ++++++-
 .../FluentProducerTemplateAutoRegisterTest.java |  45 ++++++++
 .../produce/FluentProduceTemplateTest.java      |  42 ++++++++
 ...ProducerTemplateAutoRegisterTest-context.xml |  28 +++++
 .../FluentProduceTemplateTest-context.xml       |  37 +++++++
 9 files changed, 363 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/26639590/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelFluentProducerTemplateFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelFluentProducerTemplateFactoryBean.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelFluentProducerTemplateFactoryBean.java
new file mode 100644
index 0000000..9db2409
--- /dev/null
+++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelFluentProducerTemplateFactoryBean.java
@@ -0,0 +1,105 @@
+/**
+ * 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.core.xml;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlTransient;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.FluentProducerTemplate;
+import org.apache.camel.builder.DefaultFluentProducerTemplate;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.util.ServiceHelper;
+
+/**
+ * A factory for creating a new {@link org.apache.camel.FluentProducerTemplate}
+ * instance with a minimum of XML
+ *
+ * @version
+ */
+@XmlAccessorType(XmlAccessType.FIELD)
+public abstract class AbstractCamelFluentProducerTemplateFactoryBean extends AbstractCamelFactoryBean<FluentProducerTemplate> {
+    @XmlTransient
+    private FluentProducerTemplate template;
+    @XmlAttribute @Metadata(description = "Sets the default endpoint URI used by default for sending message exchanges")
+    private String defaultEndpoint;
+    @XmlAttribute @Metadata(description = "Sets a custom maximum cache size to use in the backing cache pools.")
+    private Integer maximumCacheSize;
+
+    public FluentProducerTemplate getObject() throws Exception {
+        CamelContext context = getCamelContext();
+        if (defaultEndpoint != null) {
+            Endpoint endpoint = context.getEndpoint(defaultEndpoint);
+            if (endpoint == null) {
+                throw new IllegalArgumentException("No endpoint found for URI: " + defaultEndpoint);
+            } else {
+                template = new DefaultFluentProducerTemplate(context);
+                template.setDefaultEndpoint(endpoint);
+            }
+        } else {
+            template = new DefaultFluentProducerTemplate(context);
+        }
+
+        // set custom cache size if provided
+        if (maximumCacheSize != null) {
+            template.setMaximumCacheSize(maximumCacheSize);
+        }
+
+        // must start it so its ready to use
+        ServiceHelper.startService(template);
+        return template;
+    }
+
+    public Class<DefaultFluentProducerTemplate> getObjectType() {
+        return DefaultFluentProducerTemplate.class;
+    }
+
+    public void destroy() throws Exception {
+        ServiceHelper.stopService(template);
+    }
+
+    // Properties
+    // -------------------------------------------------------------------------
+
+    public String getDefaultEndpoint() {
+        return defaultEndpoint;
+    }
+
+    /**
+     * Sets the default endpoint URI used by default for sending message exchanges
+     */
+    public void setDefaultEndpoint(String defaultEndpoint) {
+        this.defaultEndpoint = defaultEndpoint;
+    }
+
+    public Integer getMaximumCacheSize() {
+        return maximumCacheSize;
+    }
+
+    /**
+     * Sets a custom maximum cache size to use in the backing cache pools.
+     *
+     * @param maximumCacheSize the custom maximum cache size
+     */
+    public void setMaximumCacheSize(Integer maximumCacheSize) {
+        this.maximumCacheSize = maximumCacheSize;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/26639590/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelProducerTemplateFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelProducerTemplateFactoryBean.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelProducerTemplateFactoryBean.java
index 8690be0..6ec1685 100644
--- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelProducerTemplateFactoryBean.java
+++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelProducerTemplateFactoryBean.java
@@ -92,6 +92,11 @@ public abstract class AbstractCamelProducerTemplateFactoryBean extends AbstractC
         return maximumCacheSize;
     }
 
+    /**
+     * Sets a custom maximum cache size to use in the backing cache pools.
+     *
+     * @param maximumCacheSize the custom maximum cache size
+     */
     public void setMaximumCacheSize(Integer maximumCacheSize) {
         this.maximumCacheSize = maximumCacheSize;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/26639590/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
index 16233d4..1d13c11 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
@@ -153,17 +153,18 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Spr
     @XmlElement(name = "jmxAgent", type = CamelJMXAgentDefinition.class)
     private CamelJMXAgentDefinition camelJMXAgent;
     @XmlElements({
-            @XmlElement(name = "hystrixConfiguration", type = HystrixConfigurationDefinition.class, required = false),
-            @XmlElement(name = "consulConfiguration", type = ConsulConfigurationDefinition.class, required = false),
-            @XmlElement(name = "dnsConfiguration", type = DnsConfigurationDefinition.class, required = false),
-            @XmlElement(name = "etcdConfiguration", type = EtcdConfigurationDefinition.class, required = false),
-            @XmlElement(name = "kubernetesConfiguration", type = KubernetesConfigurationDefinition.class, required = false),
-            @XmlElement(name = "ribbonConfiguration", type = RibbonConfigurationDefinition.class, required = false),
-            @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class, required = false),
-            @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class, required = false),
-            @XmlElement(name = "proxy", type = CamelProxyFactoryDefinition.class, required = false),
-            @XmlElement(name = "export", type = CamelServiceExporterDefinition.class, required = false),
-            @XmlElement(name = "errorHandler", type = ErrorHandlerDefinition.class, required = false)})
+            @XmlElement(name = "hystrixConfiguration", type = HystrixConfigurationDefinition.class),
+            @XmlElement(name = "consulConfiguration", type = ConsulConfigurationDefinition.class),
+            @XmlElement(name = "dnsConfiguration", type = DnsConfigurationDefinition.class),
+            @XmlElement(name = "etcdConfiguration", type = EtcdConfigurationDefinition.class),
+            @XmlElement(name = "kubernetesConfiguration", type = KubernetesConfigurationDefinition.class),
+            @XmlElement(name = "ribbonConfiguration", type = RibbonConfigurationDefinition.class),
+            @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class),
+            @XmlElement(name = "fluentTemplate", type = CamelFluentProducerTemplateFactoryBean.class),
+            @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class),
+            @XmlElement(name = "proxy", type = CamelProxyFactoryDefinition.class),
+            @XmlElement(name = "export", type = CamelServiceExporterDefinition.class),
+            @XmlElement(name = "errorHandler", type = ErrorHandlerDefinition.class)})
     private List<?> beans;
     @XmlElement(name = "routeBuilder")
     private List<RouteBuilderDefinition> builderRefs = new ArrayList<RouteBuilderDefinition>();

http://git-wip-us.apache.org/repos/asf/camel/blob/26639590/components/camel-spring/src/main/java/org/apache/camel/spring/CamelFluentProducerTemplateFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelFluentProducerTemplateFactoryBean.java b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelFluentProducerTemplateFactoryBean.java
new file mode 100644
index 0000000..fecbb30
--- /dev/null
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelFluentProducerTemplateFactoryBean.java
@@ -0,0 +1,56 @@
+/**
+ * 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.spring;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.FluentProducerTemplate;
+import org.apache.camel.core.xml.AbstractCamelFluentProducerTemplateFactoryBean;
+import org.apache.camel.spring.util.CamelContextResolverHelper;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+/**
+ * Configures a {@link org.apache.camel.FluentProducerTemplate}
+ * 
+ * @version 
+ */
+@XmlRootElement(name = "fluentTemplate")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class CamelFluentProducerTemplateFactoryBean extends AbstractCamelFluentProducerTemplateFactoryBean implements FactoryBean<FluentProducerTemplate>, InitializingBean, DisposableBean, ApplicationContextAware {
+
+    @XmlTransient
+    private ApplicationContext applicationContext;
+
+    @Override
+    protected CamelContext getCamelContextWithId(String camelContextId) {
+        return CamelContextResolverHelper.getCamelContextWithId(applicationContext, camelContextId);
+    }
+
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        this.applicationContext = applicationContext;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/26639590/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
index 917a3f3..945c2e9 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
@@ -50,6 +50,7 @@ import org.apache.camel.spring.CamelBeanPostProcessor;
 import org.apache.camel.spring.CamelConsumerTemplateFactoryBean;
 import org.apache.camel.spring.CamelContextFactoryBean;
 import org.apache.camel.spring.CamelEndpointFactoryBean;
+import org.apache.camel.spring.CamelFluentProducerTemplateFactoryBean;
 import org.apache.camel.spring.CamelProducerTemplateFactoryBean;
 import org.apache.camel.spring.CamelRedeliveryPolicyFactoryBean;
 import org.apache.camel.spring.CamelRestContextFactoryBean;
@@ -142,6 +143,7 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport {
 
         addBeanDefinitionParser("proxy", CamelProxyFactoryBean.class, true, false);
         addBeanDefinitionParser("template", CamelProducerTemplateFactoryBean.class, true, false);
+        addBeanDefinitionParser("fluentTemplate", CamelFluentProducerTemplateFactoryBean.class, true, false);
         addBeanDefinitionParser("consumerTemplate", CamelConsumerTemplateFactoryBean.class, true, false);
         addBeanDefinitionParser("export", CamelServiceExporter.class, true, false);
         addBeanDefinitionParser("threadPool", CamelThreadPoolFactoryBean.class, true, true);
@@ -429,7 +431,7 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport {
                             if (ObjectHelper.isNotEmpty(id)) {
                                 parserContext.registerComponent(new BeanComponentDefinition(definition, id));
                                 // set the templates with the camel context
-                                if (localName.equals("template") || localName.equals("consumerTemplate")
+                                if (localName.equals("template") || localName.equals("fluentTemplate") || localName.equals("consumerTemplate")
                                         || localName.equals("proxy") || localName.equals("export")) {
                                     // set the camel context
                                     definition.getPropertyValues().addPropertyValue("camelContext", new RuntimeBeanReference(contextId));
@@ -554,10 +556,11 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport {
     }
 
     /**
-     * Used for auto registering producer and consumer templates if not already defined in XML.
+     * Used for auto registering producer, fluent producer and consumer templates if not already defined in XML.
      */
     protected void registerTemplates(Element element, ParserContext parserContext, String contextId) {
         boolean template = false;
+        boolean fluentTemplate = false;
         boolean consumerTemplate = false;
 
         NodeList list = element.getChildNodes();
@@ -569,6 +572,8 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport {
                 String localName = childElement.getLocalName();
                 if ("template".equals(localName)) {
                     template = true;
+                } else if ("fluentTemplate".equals(localName)) {
+                    fluentTemplate = true;
                 } else if ("consumerTemplate".equals(localName)) {
                     consumerTemplate = true;
                 }
@@ -601,6 +606,32 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport {
             }
         }
 
+        if (!fluentTemplate) {
+            // either we have not used fluentTemplate before or we have auto registered it already and therefore we
+            // need it to allow to do it so it can remove the existing auto registered as there is now a clash id
+            // since we have multiple camel contexts
+            boolean existing = autoRegisterMap.get("fluentTemplate") != null;
+            boolean inUse = false;
+            try {
+                inUse = parserContext.getRegistry().isBeanNameInUse("fluentTemplate");
+            } catch (BeanCreationException e) {
+                // Spring Eclipse Tooling may throw an exception when you edit the Spring XML online in Eclipse
+                // when the isBeanNameInUse method is invoked, so ignore this and continue (CAMEL-2739)
+                LOG.debug("Error checking isBeanNameInUse(fluentTemplate). This exception will be ignored", e);
+            }
+            if (!inUse || existing) {
+                String id = "fluentTemplate";
+                // auto create a fluentTemplate
+                Element templateElement = element.getOwnerDocument().createElement("fluentTemplate");
+                templateElement.setAttribute("id", id);
+                BeanDefinitionParser parser = parserMap.get("fluentTemplate");
+                BeanDefinition definition = parser.parse(templateElement, parserContext);
+
+                // auto register it
+                autoRegisterBeanDefinition(id, definition, parserContext, contextId);
+            }
+        }
+
         if (!consumerTemplate) {
             // either we have not used template before or we have auto registered it already and therefore we
             // need it to allow to do it so it can remove the existing auto registered as there is now a clash id

http://git-wip-us.apache.org/repos/asf/camel/blob/26639590/components/camel-spring/src/test/java/org/apache/camel/spring/config/FluentProducerTemplateAutoRegisterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/config/FluentProducerTemplateAutoRegisterTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/config/FluentProducerTemplateAutoRegisterTest.java
new file mode 100644
index 0000000..f04bf21
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/config/FluentProducerTemplateAutoRegisterTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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.spring.config;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.FluentProducerTemplate;
+import org.apache.camel.spring.SpringRunWithTestSupport;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+
+/**
+ * @version 
+ */
+@ContextConfiguration
+public class FluentProducerTemplateAutoRegisterTest extends SpringRunWithTestSupport {
+
+    @Autowired
+    private FluentProducerTemplate template;
+
+    @Autowired
+    private CamelContext context;
+
+    @Test
+    public void testHasFluentTemplate() {
+        assertNotNull("Should have injected a fluent producer template", template);
+
+        FluentProducerTemplate lookup = context.getRegistry().lookupByNameAndType("fluentTemplate", FluentProducerTemplate.class);
+        assertNotNull("Should lookup fluent producer template", lookup);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/26639590/components/camel-spring/src/test/java/org/apache/camel/spring/produce/FluentProduceTemplateTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/produce/FluentProduceTemplateTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/produce/FluentProduceTemplateTest.java
new file mode 100644
index 0000000..836fe62
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/produce/FluentProduceTemplateTest.java
@@ -0,0 +1,42 @@
+/**
+ * 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.spring.produce;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.FluentProducerTemplate;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spring.SpringRunWithTestSupport;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+
+@ContextConfiguration
+public class FluentProduceTemplateTest extends SpringRunWithTestSupport {
+    @Autowired
+    protected FluentProducerTemplate producer;
+    
+    @EndpointInject(uri = "mock:result")
+    protected MockEndpoint result;
+
+    @Test
+    public void testProducerTemplate() throws Exception {
+        result.expectedBodiesReceived("hello");
+        // lets send a message
+        producer.withBody("hello").to("direct:start").send();
+        result.assertIsSatisfied();
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/26639590/components/camel-spring/src/test/resources/org/apache/camel/spring/config/FluentProducerTemplateAutoRegisterTest-context.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/config/FluentProducerTemplateAutoRegisterTest-context.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/config/FluentProducerTemplateAutoRegisterTest-context.xml
new file mode 100644
index 0000000..6f3e86c
--- /dev/null
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/config/FluentProducerTemplateAutoRegisterTest-context.xml
@@ -0,0 +1,28 @@
+<?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"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+    <camelContext xmlns="http://camel.apache.org/schema/spring">
+    </camelContext>
+
+</beans>

http://git-wip-us.apache.org/repos/asf/camel/blob/26639590/components/camel-spring/src/test/resources/org/apache/camel/spring/produce/FluentProduceTemplateTest-context.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/produce/FluentProduceTemplateTest-context.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/produce/FluentProduceTemplateTest-context.xml
new file mode 100644
index 0000000..9d5c191
--- /dev/null
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/produce/FluentProduceTemplateTest-context.xml
@@ -0,0 +1,37 @@
+<?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.
+-->
+<!-- START SNIPPET: example -->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+  <camelContext xmlns="http://camel.apache.org/schema/spring">
+  	<fluentTemplate id="camelTemplate" />
+  	<route>
+  	   <from uri="direct:start"/>
+  	   <to uri="mock:result"/>
+  	</route>
+  </camelContext>
+
+</beans>
+<!-- END SNIPPET: example -->


[2/2] camel git commit: CAMEL-10235: Add fluentTemplate to XML DSL

Posted by da...@apache.org.
CAMEL-10235: Add fluentTemplate to XML DSL


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

Branch: refs/heads/master
Commit: eb8fcf9abce22241f950291d283f75f167b5dfd8
Parents: 2663959
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Aug 11 11:09:49 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Aug 11 11:09:49 2016 +0200

----------------------------------------------------------------------
 .../blueprint/CamelContextFactoryBean.java      | 113 ++++++++++---------
 .../CamelFluentProducerTemplateFactoryBean.java |  66 +++++++++++
 .../org/apache/camel/blueprint/jaxb.index       |   1 +
 .../org/apache/camel/spring/jaxb.index          |   1 +
 4 files changed, 125 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/eb8fcf9a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
index be8a6af..4961bee 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
@@ -86,109 +86,110 @@ import org.slf4j.LoggerFactory;
 public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<BlueprintCamelContext> {
     private static final Logger LOG = LoggerFactory.getLogger(CamelContextFactoryBean.class);
 
-    @XmlAttribute(name = "depends-on", required = false)
+    @XmlAttribute(name = "depends-on")
     private String dependsOn;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String trace;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String messageHistory;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String logExhaustedMessageBody;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String streamCache = "false";
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String delayer;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String handleFault;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String errorHandlerRef;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String autoStartup = "true";
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String useMDCLogging;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String useBreadcrumb;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String allowUseOriginalMessage;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String runtimeEndpointRegistryEnabled;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String managementNamePattern;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private String threadNamePattern;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private Boolean useBlueprintPropertyResolver;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private ShutdownRoute shutdownRoute;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private ShutdownRunningTask shutdownRunningTask;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     @Deprecated
     private Boolean lazyLoadTypeConverters;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private Boolean typeConverterStatisticsEnabled;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private TypeConverterExists typeConverterExists;
-    @XmlAttribute(required = false)
+    @XmlAttribute
     private LoggingLevel typeConverterExistsLoggingLevel;
-    @XmlElement(name = "properties", required = false)
+    @XmlElement(name = "properties")
     private PropertiesDefinition properties;
-    @XmlElement(name = "propertyPlaceholder", type = CamelPropertyPlaceholderDefinition.class, required = false)
+    @XmlElement(name = "propertyPlaceholder", type = CamelPropertyPlaceholderDefinition.class)
     private CamelPropertyPlaceholderDefinition camelPropertyPlaceholder;
-    @XmlElement(name = "package", required = false)
+    @XmlElement(name = "package")
     private String[] packages = {};
-    @XmlElement(name = "packageScan", type = PackageScanDefinition.class, required = false)
+    @XmlElement(name = "packageScan", type = PackageScanDefinition.class)
     private PackageScanDefinition packageScan;
-    @XmlElement(name = "contextScan", type = ContextScanDefinition.class, required = false)
+    @XmlElement(name = "contextScan", type = ContextScanDefinition.class)
     private ContextScanDefinition contextScan;
-    @XmlElement(name = "jmxAgent", type = CamelJMXAgentDefinition.class, required = false)
+    @XmlElement(name = "jmxAgent", type = CamelJMXAgentDefinition.class)
     private CamelJMXAgentDefinition camelJMXAgent;
-    @XmlElement(name = "streamCaching", type = CamelStreamCachingStrategyDefinition.class, required = false)
+    @XmlElement(name = "streamCaching", type = CamelStreamCachingStrategyDefinition.class)
     private CamelStreamCachingStrategyDefinition camelStreamCachingStrategy;
     @XmlElements({
-        @XmlElement(name = "hystrixConfiguration", type = HystrixConfigurationDefinition.class, required = false),
-        @XmlElement(name = "kubernetesConfiguration", type = KubernetesConfigurationDefinition.class, required = false),
-        @XmlElement(name = "ribbonConfiguration", type = RibbonConfigurationDefinition.class, required = false),
-        @XmlElement(name = "consulConfiguration", type = ConsulConfigurationDefinition.class, required = false),
-        @XmlElement(name = "dnsConfiguration", type = DnsConfigurationDefinition.class, required = false),
-        @XmlElement(name = "etcdConfiguration", type = EtcdConfigurationDefinition.class, required = false),
-        @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class, required = false),
-        @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class, required = false),
-        @XmlElement(name = "proxy", type = CamelProxyFactoryBean.class, required = false),
-        @XmlElement(name = "export", type = CamelServiceExporterDefinition.class, required = false),
-        @XmlElement(name = "errorHandler", type = CamelErrorHandlerFactoryBean.class, required = false)})
+        @XmlElement(name = "hystrixConfiguration", type = HystrixConfigurationDefinition.class),
+        @XmlElement(name = "kubernetesConfiguration", type = KubernetesConfigurationDefinition.class),
+        @XmlElement(name = "ribbonConfiguration", type = RibbonConfigurationDefinition.class),
+        @XmlElement(name = "consulConfiguration", type = ConsulConfigurationDefinition.class),
+        @XmlElement(name = "dnsConfiguration", type = DnsConfigurationDefinition.class),
+        @XmlElement(name = "etcdConfiguration", type = EtcdConfigurationDefinition.class),
+        @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class),
+        @XmlElement(name = "fluentTemplate", type = CamelFluentProducerTemplateFactoryBean.class),
+        @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class),
+        @XmlElement(name = "proxy", type = CamelProxyFactoryBean.class),
+        @XmlElement(name = "export", type = CamelServiceExporterDefinition.class),
+        @XmlElement(name = "errorHandler", type = CamelErrorHandlerFactoryBean.class)})
     private List<?> beans;
-    @XmlElement(name = "routeBuilder", required = false)
+    @XmlElement(name = "routeBuilder")
     private List<RouteBuilderDefinition> builderRefs = new ArrayList<RouteBuilderDefinition>();
-    @XmlElement(name = "routeContextRef", required = false)
+    @XmlElement(name = "routeContextRef")
     private List<RouteContextRefDefinition> routeRefs = new ArrayList<RouteContextRefDefinition>();
-    @XmlElement(name = "restContextRef", required = false)
+    @XmlElement(name = "restContextRef")
     private List<RestContextRefDefinition> restRefs = new ArrayList<RestContextRefDefinition>();
-    @XmlElement(name = "threadPoolProfile", required = false)
+    @XmlElement(name = "threadPoolProfile")
     private List<ThreadPoolProfileDefinition> threadPoolProfiles;
-    @XmlElement(name = "threadPool", required = false)
+    @XmlElement(name = "threadPool")
     private List<CamelThreadPoolFactoryBean> threadPools;
-    @XmlElement(name = "endpoint", required = false)
+    @XmlElement(name = "endpoint")
     private List<CamelEndpointFactoryBean> endpoints;
-    @XmlElement(name = "dataFormats", required = false)
+    @XmlElement(name = "dataFormats")
     private DataFormatsDefinition dataFormats;
-    @XmlElement(name = "redeliveryPolicyProfile", required = false)
+    @XmlElement(name = "redeliveryPolicyProfile")
     private List<CamelRedeliveryPolicyFactoryBean> redeliveryPolicies;
-    @XmlElement(name = "onException", required = false)
+    @XmlElement(name = "onException")
     private List<OnExceptionDefinition> onExceptions = new ArrayList<OnExceptionDefinition>();
-    @XmlElement(name = "onCompletion", required = false)
+    @XmlElement(name = "onCompletion")
     private List<OnCompletionDefinition> onCompletions = new ArrayList<OnCompletionDefinition>();
-    @XmlElement(name = "intercept", required = false)
+    @XmlElement(name = "intercept")
     private List<InterceptDefinition> intercepts = new ArrayList<InterceptDefinition>();
-    @XmlElement(name = "interceptFrom", required = false)
+    @XmlElement(name = "interceptFrom")
     private List<InterceptFromDefinition> interceptFroms = new ArrayList<InterceptFromDefinition>();
-    @XmlElement(name = "interceptSendToEndpoint", required = false)
+    @XmlElement(name = "interceptSendToEndpoint")
     private List<InterceptSendToEndpointDefinition> interceptSendToEndpoints = new ArrayList<InterceptSendToEndpointDefinition>();
-    @XmlElement(name = "restConfiguration", required = false)
+    @XmlElement(name = "restConfiguration")
     private RestConfigurationDefinition restConfiguration;
-    @XmlElement(name = "rest", required = false)
+    @XmlElement(name = "rest")
     private List<RestDefinition> rests = new ArrayList<RestDefinition>();
-    @XmlElement(name = "route", required = false)
+    @XmlElement(name = "route")
     private List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
     @XmlTransient
     private BlueprintCamelContext context;

http://git-wip-us.apache.org/repos/asf/camel/blob/eb8fcf9a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelFluentProducerTemplateFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelFluentProducerTemplateFactoryBean.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelFluentProducerTemplateFactoryBean.java
new file mode 100644
index 0000000..9637a20
--- /dev/null
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelFluentProducerTemplateFactoryBean.java
@@ -0,0 +1,66 @@
+/**
+ * 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.blueprint;
+
+import java.util.Set;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.core.xml.AbstractCamelFluentProducerTemplateFactoryBean;
+import org.osgi.service.blueprint.container.BlueprintContainer;
+
+/**
+ * A factory for creating a new {@link org.apache.camel.FluentProducerTemplate}
+ * instance with a minimum of XML
+ *
+ * @version 
+ */
+@XmlRootElement(name = "fluentTemplate")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class CamelFluentProducerTemplateFactoryBean extends AbstractCamelFluentProducerTemplateFactoryBean {
+
+    @XmlTransient
+    private BlueprintContainer blueprintContainer;
+
+    public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
+        this.blueprintContainer = blueprintContainer;
+    }
+
+    @Override
+    protected CamelContext getCamelContextWithId(String camelContextId) {
+        if (blueprintContainer != null) {
+            return (CamelContext) blueprintContainer.getComponentInstance(camelContextId);
+        }
+        return null;
+    }
+
+    @Override
+    protected CamelContext discoverDefaultCamelContext() {
+        if (blueprintContainer != null) {
+            Set<String> ids = BlueprintCamelContextLookupHelper.lookupBlueprintCamelContext(blueprintContainer);
+            if (ids.size() == 1) {
+                // there is only 1 id for a BlueprintCamelContext so fallback and use this
+                return getCamelContextWithId(ids.iterator().next());
+            }
+        }
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/eb8fcf9a/components/camel-blueprint/src/main/resources/org/apache/camel/blueprint/jaxb.index
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/main/resources/org/apache/camel/blueprint/jaxb.index b/components/camel-blueprint/src/main/resources/org/apache/camel/blueprint/jaxb.index
index 2c1b625..54021b3 100644
--- a/components/camel-blueprint/src/main/resources/org/apache/camel/blueprint/jaxb.index
+++ b/components/camel-blueprint/src/main/resources/org/apache/camel/blueprint/jaxb.index
@@ -18,6 +18,7 @@ CamelConsumerTemplateFactoryBean
 CamelContextFactoryBean
 CamelEndpointFactoryBean
 CamelErrorHandlerFactoryBean
+CamelFluentProducerTemplateFactoryBean
 CamelProducerTemplateFactoryBean
 CamelThreadPoolFactoryBean
 CamelRedeliveryPolicyFactoryBean

http://git-wip-us.apache.org/repos/asf/camel/blob/eb8fcf9a/components/camel-spring/src/main/resources/org/apache/camel/spring/jaxb.index
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/main/resources/org/apache/camel/spring/jaxb.index b/components/camel-spring/src/main/resources/org/apache/camel/spring/jaxb.index
index 780a60e..feb4365 100644
--- a/components/camel-spring/src/main/resources/org/apache/camel/spring/jaxb.index
+++ b/components/camel-spring/src/main/resources/org/apache/camel/spring/jaxb.index
@@ -18,6 +18,7 @@ CamelBeanPostProcessor
 CamelConsumerTemplateFactoryBean
 CamelContextFactoryBean
 CamelEndpointFactoryBean
+CamelFluentProducerTemplateFactoryBean
 CamelProducerTemplateFactoryBean
 CamelRedeliveryPolicyFactoryBean
 CamelRestContextFactoryBean