Author: davsclaus
Date: Sat Apr  3 08:47:09 2010
New Revision: 930493

URL: http://svn.apache.org/viewvc?rev=930493&view=rev
Log:
CAMEL-2610: Added depends-on to CamelContext XML tag.

Added:
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyDependsOnBean.java
   (with props)
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyDependsOnRouteBuilder.java
   (with props)
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringCamelContextDependsOnTest.java
   (with props)
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.java
   (with props)
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringCamelContextDependsOnTest.xml
      - copied, changed from r930484, 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CamelPropertiesTest.xml
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.xml
   (with props)
Modified:
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
    
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/CamelContextFactoryBean.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java?rev=930493&r1=930492&r2=930493&view=diff
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
 (original)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
 Sat Apr  3 08:47:09 2010
@@ -108,6 +108,8 @@ import static org.apache.camel.util.Obje
 public class CamelContextFactoryBean extends IdentifiedType implements 
RouteContainer, FactoryBean, InitializingBean, DisposableBean, 
ApplicationContextAware, ApplicationListener {
     private static final Log LOG = 
LogFactory.getLog(CamelContextFactoryBean.class);
 
+    @XmlAttribute(name = "depends-on", required = false)
+    private String dependsOn;
     @XmlAttribute(required = false)
     private String trace;
     @XmlAttribute(required = false)
@@ -954,6 +956,14 @@ public class CamelContextFactoryBean ext
         this.threadPoolProfiles = threadPoolProfiles;
     }
 
+    public String getDependsOn() {
+        return dependsOn;
+    }
+
+    public void setDependsOn(String dependsOn) {
+        this.dependsOn = dependsOn;
+    }
+
     // Implementation methods
     // 
-------------------------------------------------------------------------
 

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=930493&r1=930492&r2=930493&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
 Sat Apr  3 08:47:09 2010
@@ -209,7 +209,7 @@ public class CamelNamespaceHandler exten
             }
 
             // now lets parse the routes with JAXB
-            Binder<Node> binder = null;
+            Binder<Node> binder;
             try {
                 binder = getJaxbContext().createBinder();
             } catch (JAXBException e) {
@@ -237,6 +237,8 @@ public class CamelNamespaceHandler exten
                 builder.addPropertyValue("camelPropertyPlaceholder", 
factoryBean.getCamelPropertyPlaceholder());
                 builder.addPropertyValue("camelJMXAgent", 
factoryBean.getCamelJMXAgent());
                 builder.addPropertyValue("threadPoolProfiles", 
factoryBean.getThreadPoolProfiles());
+                // add any depends-on
+                addDependsOn(factoryBean, builder);
             }
 
             boolean createdBeanPostProcessor = false;
@@ -293,6 +295,25 @@ public class CamelNamespaceHandler exten
 
     }
 
+    protected void addDependsOn(CamelContextFactoryBean factoryBean, 
BeanDefinitionBuilder builder) {
+        String dependsOn = factoryBean.getDependsOn();
+        if (ObjectHelper.isNotEmpty(dependsOn)) {
+            // comma, whitespace and semi colon is valid separators in Spring 
depends-on
+            String[] depends = dependsOn.split(",|;|\\s");
+            if (depends == null) {
+                throw new IllegalArgumentException("Cannot separate 
depends-on, was: " + dependsOn);
+            } else {
+                for (String depend : depends) {
+                    depend = depend.trim();
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Adding dependsOn " + depend + " to 
CamelContext(" + factoryBean.getId() + ")");
+                    }
+                    builder.addDependsOn(depend);
+                }
+            }
+        }
+    }
+
     protected void injectNamespaces(Element element, Binder<Node> binder) {
         NodeList list = element.getChildNodes();
         Namespaces namespaces = null;

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyDependsOnBean.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyDependsOnBean.java?rev=930493&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyDependsOnBean.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyDependsOnBean.java
 Sat Apr  3 08:47:09 2010
@@ -0,0 +1,40 @@
+/**
+ * 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;
+
+/**
+ * @version $Revision$
+ */
+public class MyDependsOnBean {
+
+    private long time;
+    private String endpointName;
+
+    public String getEndpointName() {
+        return endpointName;
+    }
+
+    public void setEndpointName(String endpointName) {
+        // time when we was invoked by Spring
+        this.time = System.nanoTime();
+        this.endpointName = endpointName;
+    }
+
+    public long getTime() {
+        return time;
+    }
+}

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

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

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyDependsOnRouteBuilder.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyDependsOnRouteBuilder.java?rev=930493&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyDependsOnRouteBuilder.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/MyDependsOnRouteBuilder.java
 Sat Apr  3 08:47:09 2010
@@ -0,0 +1,41 @@
+/**
+ * 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.spring.SpringRouteBuilder;
+
+/**
+ * @version $Revision$
+ */
+public class MyDependsOnRouteBuilder extends SpringRouteBuilder {
+
+    private long time;
+
+    @Override
+    public void configure() throws Exception {
+        // time when we was invoked by Spring
+        this.time = System.nanoTime();
+
+        MyDependsOnBean bean = lookup("myDependsOnBean", 
MyDependsOnBean.class);
+
+        from("direct:start").to(bean.getEndpointName());
+    }
+
+    public long getTime() {
+        return time;
+    }
+}

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

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

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringCamelContextDependsOnTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringCamelContextDependsOnTest.java?rev=930493&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringCamelContextDependsOnTest.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringCamelContextDependsOnTest.java
 Sat Apr  3 08:47:09 2010
@@ -0,0 +1,46 @@
+/**
+ * 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.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision$
+ */
+public class SpringCamelContextDependsOnTest extends SpringTestSupport {
+
+    @Override
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new 
ClassPathXmlApplicationContext("org/apache/camel/spring/config/SpringCamelContextDependsOnTest.xml");
+    }
+
+    public void testDependsOn() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        long time1 = context.getRegistry().lookup("myDependsOnBean", 
MyDependsOnBean.class).getTime();
+        long time2 = context.getRegistry().lookup("myRouteBuilder", 
MyDependsOnRouteBuilder.class).getTime();
+
+        assertTrue("myDependsOnBean should be created before myRouteBuilder", 
time1 < time2);
+    }
+
+}

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

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

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.java?rev=930493&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.java
 Sat Apr  3 08:47:09 2010
@@ -0,0 +1,47 @@
+/**
+ * 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.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision$
+ */
+public class SpringCamelContextNoDependsOnTest extends SpringTestSupport {
+
+    @Override
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new 
ClassPathXmlApplicationContext("org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.xml");
+    }
+
+    public void testNoDependsOn() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        // in this example CamelContext is created first, then route builder, 
and then the depends on bean is last
+        long time1 = context.getRegistry().lookup("myDependsOnBean", 
MyDependsOnBean.class).getTime();
+        long time2 = context.getRegistry().lookup("myRouteBuilder", 
MyDependsOnRouteBuilder.class).getTime();
+
+        assertTrue("myDependsOnBean should NOT be created before 
myRouteBuilder", time1 > time2);
+    }
+
+}
\ No newline at end of file

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

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

Copied: 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringCamelContextDependsOnTest.xml
 (from r930484, 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CamelPropertiesTest.xml)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringCamelContextDependsOnTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringCamelContextDependsOnTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CamelPropertiesTest.xml&r1=930484&r2=930493&rev=930493&view=diff
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CamelPropertiesTest.xml
 (original)
+++ 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringCamelContextDependsOnTest.xml
 Sat Apr  3 08:47:09 2010
@@ -22,15 +22,19 @@
        http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring";>
-        <properties>
-            <property key="foo" value="123"/>
-            <property key="bar" value="cheese"/>
-        </properties>
-        <route>
-            <from uri="direct:start"/>
-            <to uri="mock:result"/>
-        </route>
+    <!-- START SNIPPET: e1 -->
+    <bean id="myRouteBuilder" 
class="org.apache.camel.spring.config.MyDependsOnRouteBuilder"/>
+
+    <!-- this bean must be initialized first -->
+    <bean id="myDependsOnBean" 
class="org.apache.camel.spring.config.MyDependsOnBean">
+        <property name="endpointName" value="mock:result"/>
+    </bean>
+
+    <!-- and therefore we use depends-on here to tell Spring that bean must be 
created first -->
+    <!-- you can use comma, semi color or whitespace if you need to define 
multiple beans -->
+    <camelContext id="myCamel" depends-on="myDependsOnBean" 
xmlns="http://camel.apache.org/schema/spring";>
+        <routeBuilder ref="myRouteBuilder"/>
     </camelContext>
+    <!-- END SNIPPET: e1 -->
 
 </beans>

Added: 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.xml?rev=930493&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.xml
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringCamelContextNoDependsOnTest.xml
 Sat Apr  3 08:47:09 2010
@@ -0,0 +1,35 @@
+<?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
+    ">
+
+    <bean id="myRouteBuilder" 
class="org.apache.camel.spring.config.MyDependsOnRouteBuilder"/>
+
+    <bean id="myDependsOnBean" 
class="org.apache.camel.spring.config.MyDependsOnBean">
+        <property name="endpointName" value="mock:result"/>
+    </bean>
+
+    <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring";>
+        <routeBuilder ref="myRouteBuilder"/>
+    </camelContext>
+
+</beans>

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

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

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


Reply via email to