svn commit: r803055 - in /camel/trunk/components/camel-spring/src: main/java/org/apache/camel/spring/handler/ test/java/org/apache/camel/spring/config/ test/resources/org/apache/camel/spring/config/

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

svn commit: r803055 - in /camel/trunk/components/camel-spring/src: main/java/org/apache/camel/spring/handler/ test/java/org/apache/camel/spring/config/ test/resources/org/apache/camel/spring/config/

davsclaus-2
Author: davsclaus
Date: Tue Aug 11 10:19:44 2009
New Revision: 803055

URL: http://svn.apache.org/viewvc?rev=803055&view=rev
Log:
CAMEL-1899: Special care when using multiple camel contexts as we cannot then auto regsiter default templates as there would be an id clash.

Added:
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTwoCamelContextsTest.java
      - copied, changed from r803028, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTest.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest.java   (with props)
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTwoCamelContextsTest-context.xml
      - copied, changed from r803028, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTest-context.xml
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest-context.xml   (with props)
Modified:
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java

Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java?rev=803055&r1=803054&r2=803055&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java (original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java Tue Aug 11 10:19:44 2009
@@ -16,10 +16,8 @@
  */
 package org.apache.camel.spring.handler;
 
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -66,6 +64,7 @@
     protected Binder<Node> binder;
     private JAXBContext jaxbContext;
     private Map<String, BeanDefinitionParser> parserMap = new HashMap<String, BeanDefinitionParser>();
+    private Map<String, BeanDefinition> autoRegisterMap = new HashMap<String, BeanDefinition>();
 
     public ModelFileGenerator createModelFileGenerator() throws JAXBException {
         return new ModelFileGenerator(getJaxbContext());
@@ -107,10 +106,6 @@
         }
         registerParser("camelContext", new CamelContextBeanDefinitionParser(cl));
     }
-    
-    private void addBeanDefinitionParser(String elementName, Class<?> type) {
-        addBeanDefinitionParser(elementName, type, true);
-    }
 
     private void addBeanDefinitionParser(String elementName, Class<?> type, boolean register) {
         BeanDefinitionParser parser = new BeanDefinitionParser(type);
@@ -224,7 +219,6 @@
 
             boolean createdBeanPostProcessor = false;
             NodeList list = element.getChildNodes();
-            List beans = new ArrayList();
             int size = list.getLength();
             for (int i = 0; i < size; i++) {
                 Node child = list.item(i);
@@ -348,10 +342,9 @@
             templateElement.setAttribute("id", id);
             BeanDefinitionParser parser = parserMap.get("template");
             BeanDefinition definition = parser.parse(templateElement, parserContext);
-            parserContext.registerComponent(new BeanComponentDefinition(definition, id));
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Registered a default ProducerTemplate with id: " + id);
-            }
+
+            // auto register it
+            autoRegisterBeanDefinition(id, definition, parserContext, contextId);
         }
 
         if (!consumerTemplate) {
@@ -361,12 +354,40 @@
             templateElement.setAttribute("id", id);
             BeanDefinitionParser parser = parserMap.get("consumerTemplate");
             BeanDefinition definition = parser.parse(templateElement, parserContext);
+
+            // auto register it
+            autoRegisterBeanDefinition(id, definition, parserContext, contextId);
+        }
+
+    }
+
+    private void autoRegisterBeanDefinition(String id, BeanDefinition definition, ParserContext parserContext, String contextId) {
+        // it is a bit cumbersome to work with the spring bean definition parser
+        // as we kinda need to eagerly register the bean definition on the parser context
+        // and then later we might find out that we should not have done that in case we have multiple camel contexts
+        // that would have a id clash by auto regsitering the same bean definition with the same id such as a producer template
+
+        // see if we have already auto registered this id
+        BeanDefinition existing = autoRegisterMap.get(id);
+        if (existing == null) {
+            // no then add it to the map and register it
+            autoRegisterMap.put(id, definition);
             parserContext.registerComponent(new BeanComponentDefinition(definition, id));
             if (LOG.isDebugEnabled()) {
-                LOG.debug("Registered a default ConsumerTemplate with id: " + id);
+                LOG.debug("Registered default: " + definition.getBeanClassName() + " with id: " + id + " on camel context: " + contextId);
+            }
+        } else {
+            // ups we have already registered it before with same id, but on another camel context
+            // this is not good so we need to remove all traces of this auto registering.
+            // end user must manually add the needed XML elements and provide unique ids access all camel context himself.
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Unregistered default: " + definition.getBeanClassName() + " with id: " + id
+                    + " as we have multiple camel contexts and they must use unique ids."
+                    + " You must define the defintion in the XML file manually to avoid id clashes when using multiple camel contexts");
             }
-        }
 
+            parserContext.getRegistry().removeBeanDefinition(id);
+        }
     }
 
     private void registerEndpoint(Element childElement, ParserContext parserContext, String contextId) {

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTwoCamelContextsTest.java (from r803028, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTwoCamelContextsTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTwoCamelContextsTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTest.java&r1=803028&r2=803055&rev=803055&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTest.java (original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTwoCamelContextsTest.java Tue Aug 11 10:19:44 2009
@@ -16,10 +16,12 @@
  */
 package org.apache.camel.spring.config;
 
+import javax.annotation.Resource;
+
 import org.apache.camel.CamelContext;
+import org.apache.camel.ConsumerTemplate;
 import org.apache.camel.ProducerTemplate;
 import org.junit.Test;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;
 
@@ -27,19 +29,35 @@
  * @version $Revision$
  */
 @ContextConfiguration
-public class ProducerTemplateAutoRegisterTest extends AbstractJUnit38SpringContextTests {
+public class ProducerTemplateAutoRegisterTwoCamelContextsTest extends AbstractJUnit38SpringContextTests {
 
-    @Autowired
-    private ProducerTemplate template;
+    @Resource(name = "camel1")
+    private CamelContext context1;
 
-    @Autowired
-    private CamelContext context;
+    @Resource(name = "camel2")
+    private CamelContext context2;
 
     @Test
-    public void testHasTemplate() {
-        assertNotNull("Should have injected a producer template", template);
+    public void testHasNoTemplateCamel1() {
+        ProducerTemplate lookup = context1.getRegistry().lookup("template", ProducerTemplate.class);
+        assertNull("Should NOT lookup producer template", lookup);
+    }
+
+    @Test
+    public void testHasNoTemplateCamel2() {
+        ProducerTemplate lookup = context2.getRegistry().lookup("template", ProducerTemplate.class);
+        assertNull("Should NOT lookup producer template", lookup);
+    }
 
-        ProducerTemplate lookup = context.getRegistry().lookup("template", ProducerTemplate.class);
-        assertNotNull("Should lookup producer template", lookup);
+    @Test
+    public void testHasNoConsumerTemplateCamel1() {
+        ConsumerTemplate lookup = context1.getRegistry().lookup("consumerTemplate", ConsumerTemplate.class);
+        assertNull("Should NOT lookup consumer template", lookup);
+    }
+
+    @Test
+    public void testHasNoConsumerTemplateCamel2() {
+        ConsumerTemplate lookup = context2.getRegistry().lookup("consumerTemplate", ConsumerTemplate.class);
+        assertNull("Should NOT lookup consumer template", lookup);
     }
-}
+}
\ No newline at end of file

Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest.java?rev=803055&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest.java Tue Aug 11 10:19:44 2009
@@ -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.spring.config;
+
+import javax.annotation.Resource;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.impl.DefaultProducerTemplate;
+import org.junit.Test;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;
+
+/**
+ * @version $Revision$
+ */
+@ContextConfiguration
+public class ProducerTemplateMixedAutoRegisterTwoCamelContextsTest extends AbstractJUnit38SpringContextTests {
+
+    @Resource(name = "camel1")
+    private CamelContext context1;
+
+    @Resource(name = "camel2")
+    private CamelContext context2;
+
+    @Test
+    public void testHasTemplateCamel1() {
+        DefaultProducerTemplate lookup = context1.getRegistry().lookup("template1", DefaultProducerTemplate.class);
+        assertNotNull("Should lookup producer template", lookup);
+        assertEquals("camel1", lookup.getContext().getName());
+    }
+
+    @Test
+    public void testHasTemplateCamel2() {
+        DefaultProducerTemplate lookup = context1.getRegistry().lookup("template2", DefaultProducerTemplate.class);
+        assertNotNull("Should lookup producer template", lookup);
+        assertEquals("camel2", lookup.getContext().getName());
+    }
+
+    @Test
+    public void testHasNoConsumerTemplateCamel1() {
+        ConsumerTemplate lookup = context1.getRegistry().lookup("consumerTemplate", ConsumerTemplate.class);
+        assertNull("Should NOT lookup consumer template", lookup);
+    }
+
+    @Test
+    public void testHasNoConsumerTemplateCamel2() {
+        ConsumerTemplate lookup = context2.getRegistry().lookup("consumerTemplate", ConsumerTemplate.class);
+        assertNull("Should NOT lookup consumer template", lookup);
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTwoCamelContextsTest-context.xml (from r803028, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTest-context.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTwoCamelContextsTest-context.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTwoCamelContextsTest-context.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTest-context.xml&r1=803028&r2=803055&rev=803055&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTest-context.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateAutoRegisterTwoCamelContextsTest-context.xml Tue Aug 11 10:19:44 2009
@@ -22,7 +22,10 @@
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <camelContext xmlns="http://camel.apache.org/schema/spring">
+    <camelContext id="camel1" xmlns="http://camel.apache.org/schema/spring">
+    </camelContext>
+
+    <camelContext id="camel2" xmlns="http://camel.apache.org/schema/spring">
     </camelContext>
 
 </beans>

Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest-context.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest-context.xml?rev=803055&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest-context.xml (added)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest-context.xml Tue Aug 11 10:19:44 2009
@@ -0,0 +1,33 @@
+<?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-2.5.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+    <camelContext id="camel1" xmlns="http://camel.apache.org/schema/spring">
+        <template id="template1"/>
+    </camelContext>
+
+    <camelContext id="camel2" xmlns="http://camel.apache.org/schema/spring">
+        <template id="template2"/>
+    </camelContext>
+
+</beans>

Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest-context.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest-context.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/ProducerTemplateMixedAutoRegisterTwoCamelContextsTest-context.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml