camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r762641 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/spi/ camel-core/src/main/resources/org/apache/camel/model/ components/came...
Date Tue, 07 Apr 2009 07:01:09 GMT
Author: davsclaus
Date: Tue Apr  7 07:01:08 2009
New Revision: 762641

URL: http://svn.apache.org/viewvc?rev=762641&view=rev
Log:
CAMEL-1475: Introduced transacted DSL keyword to more clearly state a route is transacted.
Also Camel can autolookup the spring transaction policy, hence to make the Spring XML configuration
a bit easier.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
  (with props)
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TransactedPolicy.java   (with
props)
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceTransactedTest.java
  (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/PolicyDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Registry.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RouteContext.java
    camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringTransactionPolicy.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java?rev=762641&r1=762640&r2=762641&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java Tue
Apr  7 07:01:08 2009
@@ -128,6 +128,10 @@
         return getCamelContext().getRegistry().lookup(name, type);
     }
 
+    public <T> Map<String, T> lookupByType(Class<T> type) {
+        return getCamelContext().getRegistry().lookupByType(type);
+    }
+
     @SuppressWarnings("unchecked")
     public void commit() {
         // now lets turn all of the event driven consumer processors into a

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java?rev=762641&r1=762640&r2=762641&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java Tue Apr 
7 07:01:08 2009
@@ -16,8 +16,9 @@
  */
 package org.apache.camel.impl;
 
+import java.util.Collections;
 import java.util.Hashtable;
-
+import java.util.Map;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NameNotFoundException;
@@ -56,6 +57,12 @@
         }
     }
 
+    @SuppressWarnings("unchecked")
+    public <T> Map<String, T> lookupByType(Class<T> type) {
+        // not implemented so we return an empty map
+        return Collections.EMPTY_MAP;
+    }
+
     public void bind(String s, Object o) {
         try {
             getContext().bind(s, o);

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=762641&r1=762640&r2=762641&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 07:01:08 2009
@@ -16,6 +16,7 @@
  */
 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;
@@ -25,6 +26,7 @@
 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;
 
 /**
@@ -35,10 +37,14 @@
 @XmlRootElement(name = "policy")
 @XmlAccessorType(XmlAccessType.FIELD)
 public class PolicyDefinition extends OutputDefinition<ProcessorDefinition> {
+    @XmlTransient
+    public static final String PROPAGATION_REQUIRED = "PROPAGATION_REQUIRED";
     @XmlAttribute(required = true)
     private String ref;
     @XmlTransient
     private Policy policy;
+    @XmlTransient
+    protected Class<? extends Policy> type;
 
     public PolicyDefinition() {
     }
@@ -77,6 +83,22 @@
     }
 
     /**
+     * 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
@@ -98,7 +120,30 @@
 
     protected Policy resolvePolicy(RouteContext routeContext) {
         if (policy == null) {
-            policy = routeContext.lookup(getRef(), Policy.class);
+            // 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;
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java?rev=762641&r1=762640&r2=762641&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java Tue
Apr  7 07:01:08 2009
@@ -60,6 +60,7 @@
 import org.apache.camel.spi.InterceptStrategy;
 import org.apache.camel.spi.Policy;
 import org.apache.camel.spi.RouteContext;
+import org.apache.camel.spi.TransactedPolicy;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -1380,6 +1381,32 @@
     }
 
     /**
+     * Marks this route as transacted and uses the default transacted policy found in the
registry.
+     *
+     * @return the policy builder to configure
+     */
+    public PolicyDefinition transacted() {
+        PolicyDefinition answer = new PolicyDefinition();
+        answer.setType(TransactedPolicy.class);
+        addOutput(answer);
+        return answer;
+    }
+
+    /**
+     * Marks this route as transacted.
+     *
+     * @param ref  reference to lookup a transacted policy in the registry
+     * @return the policy builder to configure
+     */
+    public PolicyDefinition transacted(String ref) {
+        PolicyDefinition answer = new PolicyDefinition();
+        answer.setType(TransactedPolicy.class);
+        answer.setRef(ref);
+        addOutput(answer);
+        return answer;
+    }
+
+    /**
      * Forces handling of faults as exceptions
      *
      * @return the current builder with the fault handler configured

Added: 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=762641&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
Tue Apr  7 07:01:08 2009
@@ -0,0 +1,55 @@
+/**
+ * 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.model;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
+
+import org.apache.camel.spi.Policy;
+import org.apache.camel.spi.TransactedPolicy;
+
+/**
+ * Represents an XML &lt;transacted/&gt; element
+ *
+ * @version $Revision$
+ */
+@XmlRootElement(name = "transacted")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class TransactedDefinition extends PolicyDefinition {
+    @XmlTransient
+    protected Class<? extends Policy> type = TransactedPolicy.class;
+
+    public TransactedDefinition() {
+    }
+
+    public TransactedDefinition(Policy policy) {
+        super(policy);
+    }
+
+    @Override
+    public String toString() {
+        return "Transacted[" + description() + "]";
+    }
+
+    @Override
+    public String getShortName() {
+        return "transacted";
+    }
+
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/model/TransactedDefinition.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Registry.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Registry.java?rev=762641&r1=762640&r2=762641&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Registry.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/Registry.java Tue Apr  7 07:01:08
2009
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.spi;
 
+import java.util.Map;
+
 /**
  * Represents a service registry which may be implemented via a Spring ApplicationContext,
  * via JNDI, a simple Map or the OSGI Service Registry
@@ -41,4 +43,16 @@
      * @return the service from the registry or null if it could not be found
      */
     Object lookup(String name);
+
+    /**
+     * Looks up services in the registry by their type.
+     * <p/>
+     * <b>Note:</b> Not all registry implementations support this feature,
+     * such as the {@link org.apache.camel.impl.JndiRegistry}.
+     *
+     * @param type  the type of the registered services
+     * @return the types found, with their id as the key. Returns an empty Map if none found.
+     */
+    <T> Map<String, T> lookupByType(Class<T> type);
+
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RouteContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RouteContext.java?rev=762641&r1=762640&r2=762641&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RouteContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RouteContext.java Tue Apr  7
07:01:08 2009
@@ -17,6 +17,7 @@
 package org.apache.camel.spi;
 
 import java.util.List;
+import java.util.Map;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
@@ -98,6 +99,14 @@
     <T> T lookup(String name, Class<T> type);
 
     /**
+     * lookup objects by type
+     *
+     * @param type the expected type
+     * @return the found objects with the name as the key in the map. Returns an empty map
if none found.
+     */
+    <T> Map<String, T> lookupByType(Class<T> type);
+
+    /**
      * Lets complete the route creation, creating a single event driven route
      * for the current from endpoint with any processors required
      */

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TransactedPolicy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TransactedPolicy.java?rev=762641&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TransactedPolicy.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TransactedPolicy.java Tue Apr
 7 07:01:08 2009
@@ -0,0 +1,25 @@
+/**
+ * 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.spi;
+
+/**
+ * A marker for defining the policy to be used for applying transaction to routes.
+ *
+ * @version $Revision$
+ */
+public interface TransactedPolicy extends Policy {
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TransactedPolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TransactedPolicy.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index?rev=762641&r1=762640&r2=762641&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index (original)
+++ camel/trunk/camel-core/src/main/resources/org/apache/camel/model/jaxb.index Tue Apr  7
07:01:08 2009
@@ -66,6 +66,7 @@
 ThrottleDefinition
 ThrowFaultDefinition
 ToDefinition
+TransactedDefinition
 TransformDefinition
 TryDefinition
 UnmarshalDefinition

Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java?rev=762641&r1=762640&r2=762641&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java
(original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/ApplicationContextRegistry.java
Tue Apr  7 07:01:08 2009
@@ -16,8 +16,9 @@
  */
 package org.apache.camel.spring.spi;
 
-import org.apache.camel.spi.Registry;
+import java.util.Map;
 
+import org.apache.camel.spi.Registry;
 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
 import org.springframework.context.ApplicationContext;
 
@@ -50,4 +51,9 @@
             return null;
         }
     }
+
+    @SuppressWarnings("unchecked")
+    public <T> Map<String, T> lookupByType(Class<T> type) {
+        return applicationContext.getBeansOfType(type);
+    }
 }

Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringTransactionPolicy.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringTransactionPolicy.java?rev=762641&r1=762640&r2=762641&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringTransactionPolicy.java
(original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringTransactionPolicy.java
Tue Apr  7 07:01:08 2009
@@ -19,8 +19,8 @@
 import org.apache.camel.Processor;
 import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.builder.ErrorHandlerBuilderRef;
-import org.apache.camel.spi.Policy;
 import org.apache.camel.spi.RouteContext;
+import org.apache.camel.spi.TransactedPolicy;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -32,7 +32,7 @@
  *
  * @version $Revision$
  */
-public class SpringTransactionPolicy implements Policy {
+public class SpringTransactionPolicy implements TransactedPolicy {
     private static final transient Log LOG = LogFactory.getLog(SpringTransactionPolicy.class);
     private TransactionTemplate template;
     private String propagationBehaviorName;
@@ -76,10 +76,12 @@
             // no transaction error handler builder configure so create a temporary one as
we got all
             // the needed information form the configured builder anyway this allow us to
use transacted
             // routes anway even though the error handler is not transactional, eg ease of
configuration
-            if (builder != null && LOG.isDebugEnabled()) {
-                LOG.debug("The ErrorHandlerBuilder configured is not a TransactionErrorHandlerBuilder:
" + builder);
-            } else {
-                LOG.debug("No ErrorHandlerBuilder configured, will use default TransactionErrorHandlerBuilder
settings");
+            if (LOG.isDebugEnabled()) {
+                if (builder != null) {
+                    LOG.debug("The ErrorHandlerBuilder configured is not a TransactionErrorHandlerBuilder:
" + builder);
+                } else {
+                    LOG.debug("No ErrorHandlerBuilder configured, will use default TransactionErrorHandlerBuilder
settings");
+                }
             }
             TransactionErrorHandlerBuilder txBuilder = new TransactionErrorHandlerBuilder();
             txBuilder.setTransactionTemplate(getTransactionTemplate());

Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceTransactedTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceTransactedTest.java?rev=762641&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceTransactedTest.java
(added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/TransactionalClientDataSourceTransactedTest.java
Tue Apr  7 07:01:08 2009
@@ -0,0 +1,50 @@
+/**
+ * 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 org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spring.SpringRouteBuilder;
+
+/**
+ * Easier transaction configuration as we do not have to setup a transaction error handler
+ */
+public class TransactionalClientDataSourceTransactedTest extends TransactionalClientDataSourceTest
{
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new SpringRouteBuilder() {
+            public void configure() throws Exception {
+                // START SNIPPET: e1
+                from("direct:okay")
+                    // marks this route as transacted, and we dont pass in any parameters
so we
+                    // will auto lookup and use the Policy defined in the spring XML file
+                    .transacted()
+                    .setBody(constant("Tiger in Action")).beanRef("bookService")
+                    .setBody(constant("Elephant in Action")).beanRef("bookService");
+
+                // marks this route as transacted that will use the single policy defined
in the registry
+                from("direct:fail")
+                    // marks this route as transacted, and we dont pass in any parameters
so we
+                    // will auto lookup and use the Policy defined in the spring XML file
+                    .transacted()
+                    .setBody(constant("Tiger in Action")).beanRef("bookService")
+                    .setBody(constant("Donkey in Action")).beanRef("bookService");
+                // END SNIPPET: e1
+            }
+        };
+    }
+
+}

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

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



Mime
View raw message