activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r519925 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/builder/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/util/ test/java/org/apache/camel/
Date Mon, 19 Mar 2007 12:22:21 GMT
Author: jstrachan
Date: Mon Mar 19 05:22:20 2007
New Revision: 519925

URL: http://svn.apache.org/viewvc?view=rev&rev=519925
Log:
improved the test case and fixed the predicate based routes

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/FilterProcessor.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicates.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConfiguredDestinationBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpoint.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/FilterProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/FilterProcessor.java?view=diff&rev=519925&r1=519924&r2=519925
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/FilterProcessor.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/FilterProcessor.java Mon
Mar 19 05:22:20 2007
@@ -39,4 +39,12 @@
     public String toString() {
         return "if (" + predicate + ") " + processor;
     }
+
+    public Predicate<E> getPredicate() {
+        return predicate;
+    }
+
+    public Processor<E> getProcessor() {
+        return processor;
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicates.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicates.java?view=diff&rev=519925&r1=519924&r2=519925
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicates.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicates.java Mon Mar
19 05:22:20 2007
@@ -16,25 +16,20 @@
  */
 package org.apache.camel;
 
+import org.apache.camel.util.ObjectHelper;
+
 /**
  * A helper class for working with predicates
  *
  * @version $Revision$
  */
 public class Predicates {
-
-    public static void notNull(Object value, String name) {
-        if (value == null) {
-            throw new NullPointerException("No " + name + " specified");
-        }
-    }
-
     /**
      * A helper method to combine multiple predicates by a logical AND
      */
     public static <E> Predicate<E> and(final Predicate<E> left, final Predicate<E>
right) {
-        notNull(left, "left");
-        notNull(right, "right");
+        ObjectHelper.notNull(left, "left");
+        ObjectHelper.notNull(right, "right");
         return new Predicate<E>() {
             public boolean evaluate(E exchange) {
                 return left.evaluate(exchange) && right.evaluate(exchange);
@@ -46,8 +41,8 @@
      * A helper method to combine multiple predicates by a logical OR
      */
     public static <E> Predicate<E> or(final Predicate<E> left, final Predicate<E>
right) {
-        notNull(left, "left");
-        notNull(right, "right");
+        ObjectHelper.notNull(left, "left");
+        ObjectHelper.notNull(right, "right");
         return new Predicate<E>() {
             public boolean evaluate(E exchange) {
                 return left.evaluate(exchange) || right.evaluate(exchange);

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConfiguredDestinationBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConfiguredDestinationBuilder.java?view=diff&rev=519925&r1=519924&r2=519925
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConfiguredDestinationBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ConfiguredDestinationBuilder.java
Mon Mar 19 05:22:20 2007
@@ -34,7 +34,7 @@
     }
 
     @Override
-    public Processor<E> createProcessor() {
-        return new SendProcessor<E>(destination);
+    public void createProcessors() {
+        getParent().addProcessor(new SendProcessor<E>(destination));
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java?view=diff&rev=519925&r1=519924&r2=519925
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
Mon Mar 19 05:22:20 2007
@@ -17,30 +17,34 @@
 package org.apache.camel.builder;
 
 import org.apache.camel.Endpoint;
-import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.Exchange;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
-import org.apache.camel.builder.ProcessorBuilder;
 
-import java.util.List;
 import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
  * @version $Revision$
  */
 public class DestinationBuilder<E extends Exchange> implements ProcessorBuilder<E>
{
+    private DestinationBuilder<E> parent;
     private RouteBuilder<E> builder;
     private Endpoint<E> from;
+    private List<Processor<E>> processors = new ArrayList<Processor<E>>();
     private List<ProcessorBuilder<E>> processBuilders = new ArrayList<ProcessorBuilder<E>>();
 
-    public DestinationBuilder(DestinationBuilder<E> parent) {
-        this(parent.getBuilder(), parent.getFrom());
-    }
-
     public DestinationBuilder(RouteBuilder builder, Endpoint<E> from) {
         this.builder = builder;
         this.from = from;
+        this.parent = this;
+    }
+
+    public DestinationBuilder(DestinationBuilder<E> parent) {
+        this.parent = parent;
+        this.builder = parent.getBuilder();
+        this.from = parent.getFrom();
     }
 
     /**
@@ -50,6 +54,10 @@
         return getBuilder().endpoint(uri);
     }
 
+    public DestinationBuilder<E> getParent() {
+        return parent;
+    }
+
     /**
      * Sends the exchange to the given endpoint URI
      */
@@ -62,7 +70,7 @@
      */
     public ProcessorBuilder<E> to(Endpoint<E> endpoint) {
         ConfiguredDestinationBuilder<E> answer = new ConfiguredDestinationBuilder<E>(this,
endpoint);
-        builder.addRoute(answer);
+        parent.addProcessBuilder(answer);
         return answer;
     }
 
@@ -95,5 +103,19 @@
 
     public Processor<E> createProcessor() {
         throw new UndefinedDestinationException();
+    }
+
+    public void addProcessor(Processor<E> processor) {
+        processors.add(processor);
+    }
+
+    public void createProcessors() {
+        for (ProcessorBuilder<E> processBuilder : processBuilders) {
+            processBuilder.createProcessors();
+        }
+    }
+
+    public List<Processor<E>> getProcessors() {
+        return processors;
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java?view=diff&rev=519925&r1=519924&r2=519925
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
Mon Mar 19 05:22:20 2007
@@ -19,6 +19,8 @@
 import org.apache.camel.Predicate;
 import org.apache.camel.Exchange;
 import org.apache.camel.Predicates;
+import org.apache.camel.Processor;
+import org.apache.camel.FilterProcessor;
 
 /**
  * @version $Revision$
@@ -49,5 +51,9 @@
 
     public Predicate<E> getPredicate() {
         return predicate;
+    }
+
+    public void addProcessor(Processor<E> processor) {
+        getParent().addProcessor(new FilterProcessor(predicate, processor));
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java?view=diff&rev=519925&r1=519924&r2=519925
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java
Mon Mar 19 05:22:20 2007
@@ -16,17 +16,11 @@
  */
 package org.apache.camel.builder;
 
-import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
-import org.apache.camel.Predicate;
-import org.apache.camel.Processor;
-import org.apache.camel.Predicates;
-import org.apache.camel.builder.DestinationBuilder;
 
 /**
  * @version $Revision$
  */
 public interface ProcessorBuilder<E extends Exchange> {
-
-    public Processor<E> createProcessor();
+    public void createProcessors();
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java?view=diff&rev=519925&r1=519924&r2=519925
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
Mon Mar 19 05:22:20 2007
@@ -20,12 +20,14 @@
 import org.apache.camel.EndpointResolver;
 import org.apache.camel.Exchange;
 import org.apache.camel.Predicate;
-import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.Processor;
 import org.apache.camel.impl.DefaultEndpointResolver;
-import org.apache.camel.builder.DestinationBuilder;
+import org.apache.camel.util.ObjectHelper;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
@@ -34,10 +36,10 @@
  * @version $Revision$
  */
 public abstract class RouteBuilder<E extends Exchange> {
-
     private EndpointResolver<E> endpointResolver;
     private List<DestinationBuilder<E>> destinationBuilders = new ArrayList<DestinationBuilder<E>>();
     private AtomicBoolean initalized = new AtomicBoolean(false);
+    private Map<Endpoint<E>, List<Processor<E>>> routeMap = new HashMap<Endpoint<E>,
List<Processor<E>>>();
 
     /**
      * Called on initialisation to to build the required destinationBuilders
@@ -56,14 +58,11 @@
     }
 
     public DestinationBuilder<E> from(Endpoint<E> endpoint) {
-        return new DestinationBuilder<E>(this, endpoint);
-    }
-
-    public void addRoute(DestinationBuilder<E> destinationBuilder) {
-        destinationBuilders.add(destinationBuilder);
+        DestinationBuilder<E> answer = new DestinationBuilder<E>(this, endpoint);
+        destinationBuilders.add(answer);
+        return answer;
     }
 
-
     // Helper methods
     //-----------------------------------------------------------------------
     public Predicate<E> headerEquals(final String header, final Object value) {
@@ -71,6 +70,11 @@
             public boolean evaluate(E exchange) {
                 return ObjectHelper.equals(value, exchange.getHeader(header));
             }
+
+            @Override
+            public String toString() {
+                return "header[" + header + "] == " + value;
+            }
         };
     }
 
@@ -78,9 +82,17 @@
     //-----------------------------------------------------------------------
 
     /**
+     * Returns the routing map from inbound endpoints to processors
+     */
+    public Map<Endpoint<E>, List<Processor<E>>> getRouteMap() {
+        checkInitialized();
+        return routeMap;
+    }
+
+    /**
      * Returns the destinationBuilders which have been created
      */
-    public List<DestinationBuilder<E>> getRoutes() {
+    public List<DestinationBuilder<E>> getDestinationBuilders() {
         checkInitialized();
         return destinationBuilders;
     }
@@ -105,7 +117,16 @@
     protected void checkInitialized() {
         if (initalized.compareAndSet(false, true)) {
             configure();
+            populateRouteMap(routeMap);
         }
     }
 
+    protected void populateRouteMap(Map<Endpoint<E>, List<Processor<E>>>
routeMap) {
+        for (DestinationBuilder<E> destinationBuilder : destinationBuilders) {
+            Endpoint<E> from = destinationBuilder.getFrom();
+            destinationBuilder.createProcessors();
+            List<Processor<E>> processors = destinationBuilder.getProcessors();
+            routeMap.put(from, processors);
+        }
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpoint.java?view=diff&rev=519925&r1=519924&r2=519925
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpoint.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpoint.java
Mon Mar 19 05:22:20 2007
@@ -19,6 +19,7 @@
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangeConverter;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * @version $Revision$
@@ -30,6 +31,24 @@
     protected DefaultEndpoint(String endpointUri, ExchangeConverter exchangeConverter) {
         this.endpointUri = endpointUri;
         this.exchangeConverter = exchangeConverter;
+    }
+
+    public int hashCode() {
+        return endpointUri.hashCode() * 37 + 1;
+    }
+
+    @Override
+    public boolean equals(Object object) {
+        if (object instanceof DefaultEndpoint) {
+            DefaultEndpoint that = (DefaultEndpoint) object;
+            return ObjectHelper.equals(this.endpointUri, that.endpointUri);
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return "Endpoint[" + endpointUri  + "]";
     }
 
     public String getEndpointUri() {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?view=diff&rev=519925&r1=519924&r2=519925
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
Mon Mar 19 05:22:20 2007
@@ -26,4 +26,10 @@
         }
         return a != null && b != null && a.equals(b);
     }
+
+    public static void notNull(Object value, String name) {
+        if (value == null) {
+            throw new NullPointerException("No " + name + " specified");
+        }
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java?view=diff&rev=519925&r1=519924&r2=519925
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java Mon
Mar 19 05:22:20 2007
@@ -21,6 +21,8 @@
 import org.apache.camel.builder.RouteBuilder;
 
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * @version $Revision$
@@ -28,19 +30,27 @@
 public class RouteBuilderTest extends TestCase {
 
     public void testSimpleRoute() throws Exception {
-        RouteBuilder builder = new RouteBuilder() {
+        RouteBuilder<Exchange> builder = new RouteBuilder<Exchange>() {
             public void configure() {
                 from("seda://a").to("seda://b");
             }
         };
 
-        List<DestinationBuilder> destinationBuilders = builder.getRoutes();
-        assertEquals("Number or destinationBuilders created", 1, destinationBuilders.size());
-        DestinationBuilder destinationBuilder = destinationBuilders.get(0);
-        Processor processor = destinationBuilder.createProcessor();
-        assertTrue("Processor should be a SendProcessor but was: " + processor + " with type:
" + processor.getClass().getName(), processor instanceof SendProcessor);
-        SendProcessor sendProcessor = (SendProcessor) processor;
-        assertEquals("Endpoint URI", "seda://b", sendProcessor.getDestination().getEndpointUri());
+        Map<Endpoint<Exchange>, List<Processor<Exchange>>> routeMap
= builder.getRouteMap();
+        Set<Map.Entry<Endpoint<Exchange>, List<Processor<Exchange>>>>
routes = routeMap.entrySet();
+        assertEquals("Number routes created", 1, routes.size());
+        for (Map.Entry<Endpoint<Exchange>, List<Processor<Exchange>>>
route : routes) {
+            Endpoint<Exchange> key = route.getKey();
+            assertEquals("From endpoint", "seda://a", key.getEndpointUri());
+            List<Processor<Exchange>> processors = route.getValue();
+
+            assertEquals("Number of processors", 1, processors.size());
+            Processor processor = processors.get(0);
+
+            assertTrue("Processor should be a SendProcessor but was: " + processor + " with
type: " + processor.getClass().getName(), processor instanceof SendProcessor);
+            SendProcessor sendProcessor = (SendProcessor) processor;
+            assertEquals("Endpoint URI", "seda://b", sendProcessor.getDestination().getEndpointUri());
+        }
     }
 
 
@@ -50,6 +60,26 @@
                 from("seda://a").filter(headerEquals("foo", "bar")).to("seda://b");
             }
         };
+
+        Map<Endpoint<Exchange>, List<Processor<Exchange>>> routeMap
= builder.getRouteMap();
+        Set<Map.Entry<Endpoint<Exchange>, List<Processor<Exchange>>>>
routes = routeMap.entrySet();
+        assertEquals("Number routes created", 1, routes.size());
+        for (Map.Entry<Endpoint<Exchange>, List<Processor<Exchange>>>
route : routes) {
+            Endpoint<Exchange> key = route.getKey();
+            assertEquals("From endpoint", "seda://a", key.getEndpointUri());
+            List<Processor<Exchange>> processors = route.getValue();
+
+            assertEquals("Number of processors", 1, processors.size());
+            Processor processor = processors.get(0);
+
+            assertTrue("Processor should be a FilterProcessor but was: " + processor + "
with type: " + processor.getClass().getName(), processor instanceof FilterProcessor);
+            FilterProcessor filterProcessor = (FilterProcessor) processor;
+        
+            SendProcessor sendProcessor = (SendProcessor) filterProcessor.getProcessor();
+            assertEquals("Endpoint URI", "seda://b", sendProcessor.getDestination().getEndpointUri());
+        }
+
+        System.out.println("Created map: " + routeMap);
     }
 
     public void testSimpleRouteWithChoice() throws Exception {



Mime
View raw message