camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r762421 - in /camel/trunk/components/camel-spring/src: main/java/org/apache/camel/spring/ test/java/org/apache/camel/spring/interceptor/ test/resources/org/apache/camel/spring/interceptor/
Date Mon, 06 Apr 2009 17:16:15 GMT
Author: davsclaus
Date: Mon Apr  6 17:16:14 2009
New Revision: 762421

URL: http://svn.apache.org/viewvc?rev=762421&view=rev
Log:
CAMEL-1519: Fixed setting transaction policies with Spring DSL in a single tag just as its
done in the Java DSL.

Added:
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceTransactedTest.java
  (with props)
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSource.xml
      - copied, changed from r762043, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/transactionalClientDataSource.xml
Modified:
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.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=762421&r1=762420&r2=762421&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
Mon Apr  6 17:16:14 2009
@@ -41,6 +41,7 @@
 import org.apache.camel.model.RouteBuilderDefinition;
 import org.apache.camel.model.RouteContainer;
 import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.model.PolicyDefinition;
 import org.apache.camel.model.config.PropertiesDefinition;
 import org.apache.camel.model.dataformat.DataFormatsDefinition;
 import org.apache.camel.processor.interceptor.Debugger;
@@ -214,44 +215,14 @@
             }
         }
 
-        // setup the intercepts
+        // do special preparation for some concepts such as interceptors and policies
         for (RouteDefinition route : routes) {
-
             if (onExceptions != null) {
                 route.getOutputs().addAll(onExceptions);
-            }    
-            
-            for (InterceptDefinition intercept : intercepts) {
-                List<ProcessorDefinition<?>> outputs = new ArrayList<ProcessorDefinition<?>>();
-                List<ProcessorDefinition<?>> exceptionHandlers = new ArrayList<ProcessorDefinition<?>>();
-                for (ProcessorDefinition output : route.getOutputs()) {
-                    if (output instanceof OnExceptionDefinition) {
-                        exceptionHandlers.add(output);
-                    } else {
-                        outputs.add(output);
-                    }
-                }
-
-                // clearing the outputs
-                route.clearOutput();
-
-                // add exception handlers as top children
-                route.getOutputs().addAll(exceptionHandlers);
-                
-                // add the interceptor but we must do some pre configuration beforehand
-                intercept.afterPropertiesSet();
-                InterceptDefinition proxy = intercept.createProxy();
-                route.addOutput(proxy);
-                route.pushBlock(proxy.getProceed());
-
-                // if there is a proceed in the interceptor proxy then we should add
-                // the current outputs to out route so we will proceed and continue to route
to them
-                ProceedDefinition proceed = ProcessorDefinitionHelper.findFirstTypeInOutputs(proxy.getOutputs(),
ProceedDefinition.class);
-                if (proceed != null) {
-                    proceed.getOutputs().addAll(outputs);
-                }
             }
 
+            initInterceptors(route);
+            initPolicies(route);
         }
 
         if (dataFormats != null) {
@@ -271,6 +242,58 @@
         installRoutes();
     }
 
+    private void initInterceptors(RouteDefinition route) {
+        for (InterceptDefinition intercept : intercepts) {
+            List<ProcessorDefinition<?>> outputs = new ArrayList<ProcessorDefinition<?>>();
+            List<ProcessorDefinition<?>> exceptionHandlers = new ArrayList<ProcessorDefinition<?>>();
+            for (ProcessorDefinition output : route.getOutputs()) {
+                if (output instanceof OnExceptionDefinition) {
+                    exceptionHandlers.add(output);
+                } else {
+                    outputs.add(output);
+                }
+            }
+
+            // clearing the outputs
+            route.clearOutput();
+
+            // add exception handlers as top children
+            route.getOutputs().addAll(exceptionHandlers);
+
+            // add the interceptor but we must do some pre configuration beforehand
+            intercept.afterPropertiesSet();
+            InterceptDefinition proxy = intercept.createProxy();
+            route.addOutput(proxy);
+            route.pushBlock(proxy.getProceed());
+
+            // if there is a proceed in the interceptor proxy then we should add
+            // the current outputs to out route so we will proceed and continue to route
to them
+            ProceedDefinition proceed = ProcessorDefinitionHelper.findFirstTypeInOutputs(proxy.getOutputs(),
ProceedDefinition.class);
+            if (proceed != null) {
+                proceed.getOutputs().addAll(outputs);
+            }
+        }
+    }
+
+    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();
+        PolicyDefinition policy = null;
+        for (ProcessorDefinition type : types) {
+            if (type instanceof PolicyDefinition) {
+                policy = (PolicyDefinition) type;
+            } else if (policy != null) {
+                // the outputs should be moved to the policy
+                policy.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);
+        }
+    }
+
     private void initJMXAgent() throws Exception {
         if (camelJMXAgent != null && camelJMXAgent.isDisabled()) {
             LOG.debug("JMXAgent disabled");

Added: 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/SpringTransactionalClientDataSourceTransactedTest.java?rev=762421&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceTransactedTest.java
(added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringTransactionalClientDataSourceTransactedTest.java
Mon Apr  6 17:16:14 2009
@@ -0,0 +1,76 @@
+/**
+ * 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.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;
+
+/**
+ * Easier transaction configuration as we do not have to setup a transaction error handler
+ */
+public class SpringTransactionalClientDataSourceTransactedTest extends SpringTestSupport
{
+
+    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);
+    }
+
+}
\ No newline at end of file

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

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

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSource.xml
(from r762043, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/transactionalClientDataSource.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSource.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSource.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/transactionalClientDataSource.xml&r1=762043&r2=762421&rev=762421&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/transactionalClientDataSource.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/springTransactionalClientDataSource.xml
Mon Apr  6 17:16:14 2009
@@ -21,29 +21,38 @@
          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">
 
-    <!-- START SNIPPET: e1 -->
-    <!-- datasource to the database -->
-    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
-        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
-        <property name="url" value="jdbc:hsqldb:mem:camel"/>
-        <property name="username" value="sa"/>
-        <property name="password" value=""/>
-    </bean>
+    <import resource="transactionalClientDataSource.xml"/>
 
-    <!-- spring transaction manager -->
-    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
-        <property name="dataSource" ref="dataSource"/>
+    <bean id="errorHandler" class="org.apache.camel.spring.spi.TransactionErrorHandlerBuilder">
+        <property name="springTransactionPolicy" ref="PROPAGATION_REQUIRED"/>
     </bean>
 
-    <!-- policy for required transaction used in our Camel routes -->
-    <bean id="PROPAGATION_REQUIRED" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
-        <property name="transactionManager" ref="txManager"/>
-    </bean>
+    <camelContext errorHandlerRef="errorHandler" id="camel" xmlns="http://camel.apache.org/schema/spring">
+        <route>
+            <from uri="direct:okay"/>
+            <policy ref="PROPAGATION_REQUIRED"/>
+            <setBody>
+                <constant>Tiger in Action</constant>
+            </setBody>
+            <bean ref="bookService"/>
+            <setBody>
+                <constant>Elephant in Action</constant>
+            </setBody>
+            <bean ref="bookService"/>
+        </route>
 
-    <!-- bean for book business logic -->
-    <bean id="bookService" class="org.apache.camel.spring.interceptor.BookService">
-        <property name="dataSource" ref="dataSource"/>
-    </bean>
-    <!-- END SNIPPET: e1 -->
+        <route>
+            <from uri="direct:fail"/>
+            <policy ref="PROPAGATION_REQUIRED"/>
+            <setBody>
+                <constant>Tiger in Action</constant>
+            </setBody>
+            <bean ref="bookService"/>
+            <setBody>
+                <constant>Donkey in Action</constant>
+            </setBody>
+            <bean ref="bookService"/>
+        </route>
+    </camelContext>
 
 </beans>



Mime
View raw message