camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r762645 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/ components/camel-spring/src/main/java/org/apache/camel/spring/ components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/ components/camel-spring/sr...
Date Tue, 07 Apr 2009 08:06:20 GMT
Author: davsclaus
Date: Tue Apr  7 08:06:20 2009
New Revision: 762645

URL: http://svn.apache.org/viewvc?rev=762645&view=rev
Log:
CAMEL-1475: Easier transacted configuration of routes with Spring DSL.

Added:
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceUsingTransactedTest.java
  (contents, props changed)
      - copied, changed from r762421, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceTransactedTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSourceUsingTransacted.xml
      - copied, changed from r762421, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSource.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java?rev=762645&r1=762644&r2=762645&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java Tue
Apr  7 08:06:20 2009
@@ -37,10 +37,13 @@
 @XmlRootElement(name = "policy")
 @XmlAccessorType(XmlAccessType.FIELD)
 public class PolicyDefinition extends OutputDefinition<ProcessorDefinition> {
+
+    // TODO: Align this code with TransactedDefinition
+
     @XmlTransient
     public static final String PROPAGATION_REQUIRED = "PROPAGATION_REQUIRED";
     @XmlAttribute(required = true)
-    private String ref;
+    protected String ref;
     @XmlTransient
     private Policy policy;
     @XmlTransient

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java?rev=762645&r1=762644&r2=762645&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
Tue Apr  7 08:06:20 2009
@@ -16,13 +16,18 @@
  */
 package org.apache.camel.model;
 
+import java.util.Map;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
 
+import org.apache.camel.Processor;
 import org.apache.camel.spi.Policy;
+import org.apache.camel.spi.RouteContext;
 import org.apache.camel.spi.TransactedPolicy;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * Represents an XML &lt;transacted/&gt; element
@@ -31,7 +36,18 @@
  */
 @XmlRootElement(name = "transacted")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class TransactedDefinition extends PolicyDefinition {
+public class TransactedDefinition extends OutputDefinition<ProcessorDefinition> {
+
+    // TODO: Align this code with PolicyDefinition
+
+    // JAXB does not support changing the ref attribute from required to optional
+    // if we extend PolicyDefinition so we must make a copy of the class
+    @XmlTransient
+    public static final String PROPAGATION_REQUIRED = "PROPAGATION_REQUIRED";
+    @XmlAttribute
+    protected String ref;
+    @XmlTransient
+    private Policy policy;
     @XmlTransient
     protected Class<? extends Policy> type = TransactedPolicy.class;
 
@@ -39,7 +55,7 @@
     }
 
     public TransactedDefinition(Policy policy) {
-        super(policy);
+        this.policy = policy;
     }
 
     @Override
@@ -52,4 +68,96 @@
         return "transacted";
     }
 
+    @Override
+    public String getLabel() {
+        if (ref != null) {
+            return "ref: " + ref;
+        } else if (policy != null) {
+            return policy.toString();
+        } else {
+            return "";
+        }
+    }
+
+    public String getRef() {
+        return ref;
+    }
+
+    public void setRef(String ref) {
+        this.ref = ref;
+    }
+
+    /**
+     * Sets a policy type that this defition should scope within.
+     * <p/>
+     * Is used for convention over configuration situations where the policy
+     * should be automatic looked up in the registry and it should be based
+     * on this type. For instance a {@link org.apache.camel.spi.TransactedPolicy}
+     * can be set as type for easy transaction configuration.
+     * <p/>
+     * Will by default scope to the wide {@link Policy}
+     *
+     * @param type the policy type
+     */
+    public void setType(Class<? extends Policy> type) {
+        this.type = type;
+    }
+
+    /**
+     * Sets a reference to use for lookup the policy in the registry.
+     *
+     * @param ref the reference
+     * @return the builder
+     */
+    public TransactedDefinition ref(String ref) {
+        setRef(ref);
+        return this;
+    }
+
+    @Override
+    public Processor createProcessor(RouteContext routeContext) throws Exception {
+        Processor childProcessor = createOutputsProcessor(routeContext);
+
+        Policy policy = resolvePolicy(routeContext);
+        ObjectHelper.notNull(policy, "policy", this);
+        return policy.wrap(routeContext, childProcessor);
+    }
+
+    protected Policy resolvePolicy(RouteContext routeContext) {
+        if (policy == null) {
+            // try ref first
+            String ref = getRef();
+            if (ObjectHelper.isNotEmpty(ref)) {
+                policy = routeContext.lookup(ref, Policy.class);
+            }
+
+            // try to lookup by scoped type
+            if (policy == null && type != null) {
+                // try find by type, note that this method is not supported by all registry
+                Map types = routeContext.lookupByType(type);
+                if (types.size() == 1) {
+                    // only one policy defined so use it
+                    Object found = types.values().iterator().next();
+                    if (type.isInstance(found)) {
+                        return type.cast(found);
+                    }
+                }
+            }
+
+            // for transacted routing try the default REQUIRED name
+            if (policy == null && type == TransactedPolicy.class) {
+                // still not found try with the default name PROPAGATION_REQUIRED
+                policy = routeContext.lookup(PROPAGATION_REQUIRED, TransactedPolicy.class);
+            }
+        }
+        return policy;
+    }
+
+    protected String description() {
+        if (policy != null) {
+            return policy.toString();
+        } else {
+            return "ref: " + ref;
+        }
+    }
 }

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=762645&r1=762644&r2=762645&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
Tue Apr  7 08:06:20 2009
@@ -42,6 +42,7 @@
 import org.apache.camel.model.RouteContainer;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.PolicyDefinition;
+import org.apache.camel.model.TransactedDefinition;
 import org.apache.camel.model.config.PropertiesDefinition;
 import org.apache.camel.model.dataformat.DataFormatsDefinition;
 import org.apache.camel.processor.interceptor.Debugger;
@@ -278,19 +279,29 @@
     private void initPolicies(RouteDefinition route) {
         // setup the policies as JAXB yet again have not created a correct model for us
         List<ProcessorDefinition> types = route.getOutputs();
+        // we need to types as transacted cannot extend policy due JAXB limitations
         PolicyDefinition policy = null;
+        TransactedDefinition transacted = null;
         for (ProcessorDefinition type : types) {
             if (type instanceof PolicyDefinition) {
                 policy = (PolicyDefinition) type;
+            } else if (type instanceof TransactedDefinition) {
+                transacted = (TransactedDefinition) type;
             } else if (policy != null) {
                 // the outputs should be moved to the policy
                 policy.addOutput(type);
+            } else if (transacted != null) {
+                // the outputs should be moved to the transacted policy
+                transacted.addOutput(type);
             }
         }
         // did we find a policy if so replace it as the only output on the route
         if (policy != null) {
             route.clearOutput();
             route.addOutput(policy);
+        } else if (transacted != null) {
+            route.clearOutput();
+            route.addOutput(transacted);
         }
     }
 

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceUsingTransactedTest.java
(from r762421, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceTransactedTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceUsingTransactedTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceUsingTransactedTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceTransactedTest.java&r1=762421&r2=762645&rev=762645&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceTransactedTest.java
(original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceUsingTransactedTest.java
Tue Apr  7 08:06:20 2009
@@ -16,10 +16,6 @@
  */
 package org.apache.camel.spring.interceptor;
 
-import javax.sql.DataSource;
-
-import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.spring.SpringTestSupport;
 import org.springframework.context.support.AbstractXmlApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -27,50 +23,13 @@
 /**
  * Easier transaction configuration as we do not have to setup a transaction error handler
  */
-public class SpringTransactionalClientDataSourceTransactedTest extends SpringTestSupport
{
+public class SpringTransactionalClientDataSourceUsingTransactedTest extends SpringTransactionalClientDataSourceTransactedTest
{
 
     protected JdbcTemplate jdbc;
 
     protected AbstractXmlApplicationContext createApplicationContext() {
         return new ClassPathXmlApplicationContext(
-            "/org/apache/camel/spring/interceptor/springTransactionalClientDataSource.xml");
-    }
-
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-
-        // create database and insert dummy data
-        final DataSource ds = getMandatoryBean(DataSource.class, "dataSource");
-        jdbc = new JdbcTemplate(ds);
-        jdbc.execute("create table books (title varchar(50))");
-        jdbc.update("insert into books (title) values (?)", new Object[] {"Camel in Action"});
-    }
-
-    @Override
-    protected void tearDown() throws Exception {
-        super.tearDown();
-        jdbc.execute("drop table books");
-    }
-
-    public void testTransactionSuccess() throws Exception {
-        template.sendBody("direct:okay", "Hello World");
-
-        int count = jdbc.queryForInt("select count(*) from books");
-        assertEquals("Number of books", 3, count);
-    }
-
-    public void testTransactionRollback() throws Exception {
-        try {
-            template.sendBody("direct:fail", "Hello World");
-        } catch (RuntimeCamelException e) {
-            // expeced as we fail
-            assertTrue(e.getCause() instanceof IllegalArgumentException);
-            assertEquals("We don't have Donkeys, only Camels", e.getCause().getMessage());
-        }
-
-        int count = jdbc.queryForInt("select count(*) from books");
-        assertEquals("Number of books", 1, count);
+            "/org/apache/camel/spring/interceptor/springTransactionalClientDataSourceUsingTransacted.xml");
     }
 
 }
\ No newline at end of file

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

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

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSourceUsingTransacted.xml
(from r762421, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSource.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSourceUsingTransacted.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSourceUsingTransacted.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSource.xml&r1=762421&r2=762645&rev=762645&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSource.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSourceUsingTransacted.xml
Tue Apr  7 08:06:20 2009
@@ -23,14 +23,11 @@
 
     <import resource="transactionalClientDataSource.xml"/>
 
-    <bean id="errorHandler" class="org.apache.camel.spring.spi.TransactionErrorHandlerBuilder">
-        <property name="springTransactionPolicy" ref="PROPAGATION_REQUIRED"/>
-    </bean>
-
-    <camelContext errorHandlerRef="errorHandler" id="camel" xmlns="http://camel.apache.org/schema/spring">
+    <!-- START SNIPPET: e1 -->
+    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
         <route>
             <from uri="direct:okay"/>
-            <policy ref="PROPAGATION_REQUIRED"/>
+            <transacted/>
             <setBody>
                 <constant>Tiger in Action</constant>
             </setBody>
@@ -43,7 +40,7 @@
 
         <route>
             <from uri="direct:fail"/>
-            <policy ref="PROPAGATION_REQUIRED"/>
+            <transacted/>
             <setBody>
                 <constant>Tiger in Action</constant>
             </setBody>
@@ -54,5 +51,6 @@
             <bean ref="bookService"/>
         </route>
     </camelContext>
+    <!-- END SNIPPET: e1 -->
 
 </beans>



Mime
View raw message