You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2009/08/03 16:13:10 UTC

svn commit: r800394 - in /camel/branches/camel-1.x: ./ components/camel-spring/src/main/java/org/apache/camel/spring/ components/camel-spring/src/main/java/org/apache/camel/spring/handler/ components/camel-spring/src/test/java/org/apache/camel/spring/ ...

Author: ningjiang
Date: Mon Aug  3 14:13:10 2009
New Revision: 800394

URL: http://svn.apache.org/viewvc?rev=800394&view=rev
Log:
Merged revisions 800340 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r800340 | ningjiang | 2009-08-03 20:56:31 +0800 (Mon, 03 Aug 2009) | 1 line
  
  CAMEL-1873 fixed the ambiguity of more than one camel context in the same spring application context
........

Modified:
    camel/branches/camel-1.x/   (props changed)
    camel/branches/camel-1.x/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
    camel/branches/camel-1.x/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
    camel/branches/camel-1.x/components/camel-spring/src/test/java/org/apache/camel/spring/CamelContextAwareTest.java
    camel/branches/camel-1.x/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextAwareBean.xml
    camel/branches/camel-1.x/components/camel-spring/src/test/resources/org/apache/camel/spring/remoting/spring-with-exporter-namespace.xml

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Aug  3 14:13:10 2009
@@ -1 +1 @@
-/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909,760937,761194,761536,761583,761607,762047,762633,762650,762935,763095,763484,763551,765154,765686,765729,765743,765824,766016,766289,766584,766588,766590,766602,766673,767403,767824,768342,769239,769346,769368,769434,770172,770906,771303,773193,773446,773781,774192,774383,774658-774659,776198,776289,776504,776975,778102,778689-778701,779121,779143,779489,781314-781349,781775,781
 923,781974,781993,782557,782594,782681,782886,782918-782923,783204,783248,783363,783639,783704,785564,785584,785599,787206,787581,787598,787605,787928,788393,789121,789703,790560,790936,791379,791476,791767,792038,792381,792398,792899,793359,793518,793547-793555,793862,793935,794237,794368,794550,794967,795790,798027,798052,798063,798489,798864,799252
+/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909,760937,761194,761536,761583,761607,762047,762633,762650,762935,763095,763484,763551,765154,765686,765729,765743,765824,766016,766289,766584,766588,766590,766602,766673,767403,767824,768342,769239,769346,769368,769434,770172,770906,771303,773193,773446,773781,774192,774383,774658-774659,776198,776289,776504,776975,778102,778689-778701,779121,779143,779489,781314-781349,781775,781
 923,781974,781993,782557,782594,782681,782886,782918-782923,783204,783248,783363,783639,783704,785564,785584,785599,787206,787581,787598,787605,787928,788393,789121,789703,790560,790936,791379,791476,791767,792038,792381,792398,792899,793359,793518,793547-793555,793862,793935,794237,794368,794550,794967,795790,798027,798052,798063,798489,798864,799252,800340

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-1.x/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java?rev=800394&r1=800393&r2=800394&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java (original)
+++ camel/branches/camel-1.x/components/camel-spring/src/main/java/org/apache/camel/spring/CamelBeanPostProcessor.java Mon Aug  3 14:13:10 2009
@@ -53,7 +53,7 @@
  * {@link org.apache.camel.RecipientList} for creating <a href="http://activemq.apache.org/camel/recipientlist-annotation.html">a Recipient List router via annotations</a>.
  * <p>
  * If you use the &lt;camelContext&gt; element in your <a href="http://activemq.apache.org/camel/spring.html">Spring XML</a> 
- * then one of these bean post processors is implicity installed and configured for you. So you should never have to
+ * then one of these bean post processors is implicitly installed and configured for you. So you should never have to
  * explicitly create or configure one of these instances.
  *
  * @version $Revision$
@@ -89,7 +89,7 @@
         }
         injectFields(bean);
         injectMethods(bean);
-        if (bean instanceof CamelContextAware) {
+        if (bean instanceof CamelContextAware && canSetCamelContext(bean, beanName)) {
             CamelContextAware contextAware = (CamelContextAware)bean;
             if (camelContext == null) {
                 LOG.warn("No CamelContext defined yet so cannot inject into: " + bean);
@@ -166,6 +166,29 @@
         // all other beans can of course be processed
         return true;
     }
+    
+    
+    protected boolean canSetCamelContext(Object bean, String beanName) {
+        
+        try {
+            Method method = null;
+            try {
+                method = bean.getClass().getMethod("getCamelContext", new Class[]{});
+            } catch (NoSuchMethodException ex) {
+                method = bean.getClass().getMethod("getContext", new Class[]{});
+            }
+            CamelContext context = (CamelContext) method.invoke(bean, new Object[]{});
+            if (context != null) {
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("The camel context of " + beanName + " is set, so we skip inject the camel context of it.");
+                }
+                return false;
+            }
+        } catch (Exception e) {
+            // can't check the status of camelContext , so return true by default
+        }
+        return true;
+    }
 
     /**
      * A strategy method to allow implementations to perform some custom JBI

Modified: camel/branches/camel-1.x/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java?rev=800394&r1=800393&r2=800394&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java (original)
+++ camel/branches/camel-1.x/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java Mon Aug  3 14:13:10 2009
@@ -16,8 +16,10 @@
  */
 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;
 
@@ -69,17 +71,15 @@
     }
 
     public void init() {
-        // remoting
-        addBeanDefinitionParser("proxy", CamelProxyFactoryBean.class);
-        addBeanDefinitionParser("template", CamelTemplateFactoryBean.class);
-        addBeanDefinitionParser("export", CamelServiceExporter.class);
+        // These elements parser should be used inside the camel context
+        addBeanDefinitionParser("proxy", CamelProxyFactoryBean.class, false);
+        addBeanDefinitionParser("template", CamelTemplateFactoryBean.class, false);
+        addBeanDefinitionParser("export", CamelServiceExporter.class, false);
 
-        // jmx agent
+        // jmx agent cannot be used outside of the camel context
         addBeanDefinitionParser("jmxAgent", CamelJMXAgentType.class);
 
-        // TODO switch to use the above mechanism?
-        registerParser("endpoint", endpointParser);
-
+        
         boolean osgi = false;
         Class cl = CamelContextFactoryBean.class;
         try {
@@ -100,10 +100,16 @@
         }
         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);
-        registerParser(elementName, parser);
+        if (register) {
+            registerParser(elementName, parser);
+        }
         parserMap.put(elementName, parser);
     }
 
@@ -213,6 +219,7 @@
 
             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);
@@ -243,12 +250,19 @@
                                 if (localName.equals("jmxAgent")) {
                                     builder.addPropertyReference("camelJMXAgent", id);
                                 }
+                                // set the templates with the camel context 
+                                if (localName.equals("template") 
+                                    || localName.equals("proxy") || localName.equals("export")) {
+                                    // set the camel context 
+                                    definition.getPropertyValues().addPropertyValue("camelContext", new RuntimeBeanReference(contextId));
+                                }   
                             }
                         }
-
                     }
                 }
             }
+
+
             // lets inject the namespaces into any namespace aware POJOs
             injectNamespaces(element);
             if (!createdBeanPostProcessor) {

Modified: camel/branches/camel-1.x/components/camel-spring/src/test/java/org/apache/camel/spring/CamelContextAwareTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-spring/src/test/java/org/apache/camel/spring/CamelContextAwareTest.java?rev=800394&r1=800393&r2=800394&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-spring/src/test/java/org/apache/camel/spring/CamelContextAwareTest.java (original)
+++ camel/branches/camel-1.x/components/camel-spring/src/test/java/org/apache/camel/spring/CamelContextAwareTest.java Mon Aug  3 14:13:10 2009
@@ -16,8 +16,10 @@
  */
 package org.apache.camel.spring;
 
+import java.lang.reflect.Method;
 import java.util.Map;
 
+import org.apache.camel.impl.DefaultProducerTemplate;
 import org.springframework.context.support.AbstractXmlApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
@@ -25,22 +27,31 @@
  * @version $Revision$
  */
 public class CamelContextAwareTest extends SpringTestSupport {
-    protected CamelContextAwareBean bean;
+    protected CamelContextAwareBean bean1;
+
 
     public void testInjectionPoints() throws Exception {
-        assertNotNull("No CamelContext injected!", bean.getCamelContext());
-        Map<String, String> properties  = bean.getCamelContext().getProperties();
+        assertNotNull("No CamelContext injected!", bean1.getCamelContext());
+        Map<String, String> properties  = bean1.getCamelContext().getProperties();
         assertNotNull("the properties should not been null", properties);
         assertEquals("No properties injected", properties.size(), 1);
-        assertEquals("Should get the value of org.apache.camel.test", properties.get("org.apache.camel.test"), "this is a test");
+        assertEquals("Should get the value of org.apache.camel.test", properties.get("org.apache.camel.test"), "this is a test first");
+    }
+    
+    public void testCamelTemplates() throws Exception {
+        DefaultProducerTemplate producer1 = getMandatoryBean(DefaultProducerTemplate.class, "producer1");
+        assertEquals("Inject a wrong camel context", producer1.getContext().getName(), "camel1");
+        
+        DefaultProducerTemplate producer2 = getMandatoryBean(DefaultProducerTemplate.class, "producer2");
+        assertEquals("Inject a wrong camel context", producer2.getContext().getName(), "camel2");
     }
 
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-
-        bean = getMandatoryBean(CamelContextAwareBean.class, "bean");
+        bean1 = getMandatoryBean(CamelContextAwareBean.class, "bean1");
     }
+       
 
     protected AbstractXmlApplicationContext createApplicationContext() {
         return new ClassPathXmlApplicationContext("org/apache/camel/spring/camelContextAwareBean.xml");

Modified: camel/branches/camel-1.x/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextAwareBean.xml
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextAwareBean.xml?rev=800394&r1=800393&r2=800394&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextAwareBean.xml (original)
+++ camel/branches/camel-1.x/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextAwareBean.xml Mon Aug  3 14:13:10 2009
@@ -17,17 +17,25 @@
 -->
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:camel="http://activemq.apache.org/camel/schema/spring"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
     ">
 
-  <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
+  <camelContext id="camel1" xmlns="http://activemq.apache.org/camel/schema/spring">
     <properties>
-       <property key="org.apache.camel.test" value="this is a test"/>
+       <property key="org.apache.camel.test" value="this is a test first"/>
     </properties>
-    <beanPostProcessor/>
+    <camel:template id="producer1"/>
   </camelContext>
+  
+  <camel:camelContext id="camel2">
+    <camel:template id="producer2"/>
+  </camel:camelContext>
+
+  <bean id="bean1" class="org.apache.camel.spring.CamelContextAwareBean">
+     <property name="camelContext" ref="camel1" />
+  </bean>
 
-  <bean id="bean" class="org.apache.camel.spring.CamelContextAwareBean"/>
 </beans>

Modified: camel/branches/camel-1.x/components/camel-spring/src/test/resources/org/apache/camel/spring/remoting/spring-with-exporter-namespace.xml
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-spring/src/test/resources/org/apache/camel/spring/remoting/spring-with-exporter-namespace.xml?rev=800394&r1=800393&r2=800394&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-spring/src/test/resources/org/apache/camel/spring/remoting/spring-with-exporter-namespace.xml (original)
+++ camel/branches/camel-1.x/components/camel-spring/src/test/resources/org/apache/camel/spring/remoting/spring-with-exporter-namespace.xml Mon Aug  3 14:13:10 2009
@@ -23,21 +23,23 @@
        http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
     ">
 
-  <!-- START SNIPPET: proxy -->
-  <!--  Creates a proxy to the direct:say endpoint. -->
-  <camel:proxy id="sayProxy" serviceUrl="direct:say"
-                      serviceInterface="org.apache.camel.spring.remoting.ISay"/>
-  <!-- END SNIPPET: proxy -->
-
-  <!-- START SNIPPET: export -->
   <bean id="sayService" class="org.apache.camel.spring.remoting.SayService"/>
 
-  <camel:export id="say" uri="direct:sayImpl" serviceRef="sayService"
-                         serviceInterface="org.apache.camel.spring.remoting.ISay"/>
-  <!-- END SNIPPET: export -->
-
   <!-- START SNIPPET: example -->
   <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
+     <!-- START SNIPPET: proxy -->
+     <!--  Creates a proxy to the direct:say endpoint. -->
+     <camel:proxy id="sayProxy" serviceUrl="direct:say"
+                      serviceInterface="org.apache.camel.spring.remoting.ISay"/>
+     <!-- END SNIPPET: proxy -->
+     <!-- START SNIPPET: export --> 
+     <camel:export id="say" uri="direct:sayImpl" serviceRef="sayService"
+                         serviceInterface="org.apache.camel.spring.remoting.ISay"/>
+     <!--
+       The bean is defined out side of the camel  
+       <bean id="sayService" class="org.apache.camel.spring.remoting.SayService"/>
+     -->
+     <!-- END SNIPPET: export --> 
     <route>
       <from uri="direct:say"/>
       <to uri="direct:sayImpl"/>