activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r520163 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/builder/ test/java/org/apache/camel/
Date Mon, 19 Mar 2007 23:13:36 GMT
Author: chirino
Date: Mon Mar 19 16:13:35 2007
New Revision: 520163

URL: http://svn.apache.org/viewvc?view=rev&rev=520163
Log:
Added an InterceptorProcessor and a builder for it. 

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/InterceptorProcessor.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/InterceptorBuilder.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DestinationBuilder.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/RouteBuilderTest.java

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/InterceptorProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/InterceptorProcessor.java?view=auto&rev=520163
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/InterceptorProcessor.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/InterceptorProcessor.java
Mon Mar 19 16:13:35 2007
@@ -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;
+
+/**
+ * @version $Revision: 519941 $
+ */
+public class InterceptorProcessor<E> implements Processor<E> {
+
+    protected Processor<E> next;
+
+	public InterceptorProcessor() {
+    }
+
+    public void onExchange(E exchange) {
+       if( next != null ) {
+    	   next.onExchange(exchange);
+       }
+    }
+
+    @Override
+    public String toString() {
+        return "intercept(" + next + ")";
+    }
+
+	public Processor<E> getNext() {
+		return next;
+	}
+	public void setNext(Processor<E> next) {
+		this.next = next;
+	}
+}

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=520163&r1=520162&r2=520163
==============================================================================
--- 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 16:13:35 2007
@@ -16,15 +16,15 @@
  */
 package org.apache.camel.builder;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.camel.CompositeProcessor;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 
-import java.util.ArrayList;
-import java.util.List;
-
 /**
  * @version $Revision$
  */
@@ -172,4 +172,10 @@
     public List<Processor<E>> getProcessors() {
         return processors;
     }
+
+	public InterceptorBuilder<E> intercept() {
+		InterceptorBuilder<E> answer = new InterceptorBuilder<E>(this);
+        addProcessBuilder(answer);
+        return answer;
+	}
 }

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/InterceptorBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/InterceptorBuilder.java?view=auto&rev=520163
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/InterceptorBuilder.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/InterceptorBuilder.java
Mon Mar 19 16:13:35 2007
@@ -0,0 +1,75 @@
+/**
+ *
+ * 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.builder;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.InterceptorProcessor;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+
+/**
+ * @version $Revision: 519943 $
+ */
+public class InterceptorBuilder<E extends Exchange> implements ProcessorBuilder<E>
{
+    private final List<InterceptorProcessor<E>> intercepts = new ArrayList<InterceptorProcessor<E>>();
+	private final DestinationBuilder<E> parent;
+	private DestinationBuilder<E> target;
+
+	public InterceptorBuilder(DestinationBuilder<E> parent) {
+		this.parent = parent;
+	}
+
+	public InterceptorBuilder<E> add(InterceptorProcessor<E> intercept) {
+		intercepts.add(intercept);
+		return this;
+	}
+	
+    public DestinationBuilder<E> target() {
+        this.target = new DestinationBuilder<E>(parent);
+        return target;
+    }
+
+    public Processor<E> createProcessor() {
+    	
+    	// The target is required.
+    	if( target == null ) 
+    		throw new RuntimeCamelException("target provided.");
+    	
+    	// Interceptors are optional
+    	InterceptorProcessor<E> first=null;
+    	InterceptorProcessor<E> last=null;
+        for (InterceptorProcessor<E> p : intercepts) {
+            if( first == null ) {
+            	first = p;
+            }
+            if( last != null ) {
+            	last.setNext(p);
+            }
+            last = p;
+        }
+        
+        Processor<E> p = target.createProcessor();
+        if( last != null ) {
+        	last.setNext(p);
+        }
+        return first == null ? p : first;
+    }
+}

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=520163&r1=520162&r2=520163
==============================================================================
--- 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 16:13:35 2007
@@ -39,7 +39,7 @@
             return "MyProcessor";
         }
     };
-
+    
     public void testSimpleRoute() throws Exception {
         // START SNIPPET: e1
         RouteBuilder<Exchange> builder = new RouteBuilder<Exchange>() {
@@ -207,6 +207,46 @@
 
             assertSendTo(processors.get(0), "queue:tap");
             assertSendTo(processors.get(1), "queue:b");
+        }
+    }
+
+    public void testRouteWithInterceptor() throws Exception {
+    	
+        final InterceptorProcessor<Exchange> interceptor1 = new InterceptorProcessor<Exchange>()
{
+        };
+        final InterceptorProcessor<Exchange> interceptor2 = new InterceptorProcessor<Exchange>()
{    	
+        };
+
+        // START SNIPPET: e7
+        RouteBuilder<Exchange> builder = new RouteBuilder<Exchange>() {
+            public void configure() {
+                from("queue:a")
+                    .intercept()
+            		   .add(interceptor1)
+            		   .add(interceptor2)
+            		.target().to("queue:d");
+            }
+        };
+        // END SNIPPET: e7
+
+        Map<Endpoint<Exchange>, Processor<Exchange>> routeMap = builder.getRouteMap();
+        System.out.println("Created map: " + routeMap);
+
+        Set<Map.Entry<Endpoint<Exchange>, Processor<Exchange>>> routes
= routeMap.entrySet();
+        assertEquals("Number routes created", 1, routes.size());
+        for (Map.Entry<Endpoint<Exchange>, Processor<Exchange>> route :
routes) {
+            Endpoint<Exchange> key = route.getKey();
+            assertEquals("From endpoint", "queue:a", key.getEndpointUri());
+            Processor processor = route.getValue();
+
+            assertTrue("Processor should be a interceptor1 but was: " + processor + " with
type: " + processor.getClass().getName(), processor==interceptor1);
+            InterceptorProcessor<Exchange> p1 = (InterceptorProcessor<Exchange>)
processor;
+
+            processor = p1.getNext();
+            assertTrue("Processor should be a interceptor2 but was: " + processor + " with
type: " + processor.getClass().getName(), processor==interceptor2);
+            InterceptorProcessor<Exchange> p2 = (InterceptorProcessor<Exchange>)
processor;
+
+            assertSendTo(p2.getNext(), "queue:d");
         }
     }
 



Mime
View raw message