Author: jstrachan
Date: Wed May 21 07:54:14 2008
New Revision: 658722

URL: http://svn.apache.org/viewvc?rev=658722&view=rev
Log:
added test case to show the Debugger in action via the Spring Main for 
https://issues.apache.org/activemq/browse/CAMEL-531

Added:
    
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/debug/
    
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/debug/DebugTest.java
   (with props)
    
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/debug/
    
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/debug/applicationContext.xml
   (with props)
Modified:
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DebugInterceptor.java
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java
    
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DebugInterceptor.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DebugInterceptor.java?rev=658722&r1=658721&r2=658722&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DebugInterceptor.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DebugInterceptor.java
 Wed May 21 07:54:14 2008
@@ -24,22 +24,22 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Predicate;
 import org.apache.camel.Message;
+import org.apache.camel.processor.DelegateProcessor;
 
 /**
  * An interceptor for debugging and tracing routes
  * 
  * @version $Revision: 1.1 $
  */
-public class DebugInterceptor implements Processor {
+public class DebugInterceptor extends DelegateProcessor {
     private final ProcessorType node;
-    private final Processor target;
     private final List<Exchange> exchanges;
     private Predicate traceFilter;
     private Breakpoint breakpoint = new Breakpoint();
 
     public DebugInterceptor(ProcessorType node, Processor target, 
List<Exchange> exchanges) {
+        super(target);
         this.node = node;
-        this.target = target;
         this.exchanges = exchanges;
     }
 
@@ -51,17 +51,13 @@
     public void process(Exchange exchange) throws Exception {
         checkForBreakpoint(exchange);        
         addTraceExchange(exchange);
-        target.process(exchange);
+        super.proceed(exchange);
     }
 
     public ProcessorType getNode() {
         return node;
     }
 
-    public Processor getTarget() {
-        return target;
-    }
-
     public List<Exchange> getExchanges() {
         return exchanges;
     }

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java?rev=658722&r1=658721&r2=658722&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java
 Wed May 21 07:54:14 2008
@@ -24,6 +24,8 @@
 
 import org.apache.camel.Processor;
 import org.apache.camel.Exchange;
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.model.ProcessorType;
 import org.apache.camel.spi.InterceptStrategy;
 import org.apache.commons.logging.Log;
@@ -40,6 +42,25 @@
     private int exchangeBufferSize = -1;
     private Map<String, DebugInterceptor> interceptors = new HashMap<String, 
DebugInterceptor>();
 
+    /**
+     * A helper method to return the debugger instance for a given [EMAIL 
PROTECTED] CamelContext} if one is enabled
+     *
+     * @param context the camel context the debugger is connected to
+     * @return the debugger or null if none can be found
+     */
+    public static Debugger getDebugger(CamelContext context) {
+        if (context instanceof DefaultCamelContext) {
+            DefaultCamelContext defaultCamelContext = (DefaultCamelContext) 
context;
+            List<InterceptStrategy> list = 
defaultCamelContext.getInterceptStrategies();
+            for (InterceptStrategy interceptStrategy : list) {
+               if (interceptStrategy instanceof Debugger) {
+                   return (Debugger) interceptStrategy;
+               }
+            }
+        }
+        return null;
+    }
+
     public DebugInterceptor getInterceptor(String id) {
         return interceptors.get(id);
     }

Modified: 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java?rev=658722&r1=658721&r2=658722&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
 (original)
+++ 
activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
 Wed May 21 07:54:14 2008
@@ -28,6 +28,8 @@
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.CamelTemplate;
+import org.apache.camel.processor.interceptor.Debugger;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.ServiceSupport;
 import org.apache.camel.util.ObjectHelper;
@@ -60,6 +62,7 @@
     private List<SpringCamelContext> camelContexts = new 
ArrayList<SpringCamelContext>();
     private AbstractApplicationContext parentApplicationContext;
     private String parentApplicationContextUri;
+    private CamelTemplate camelTemplate;
 
     public Main() {
         addOption(new Option("h", "help", "Displays the help screen") {
@@ -77,7 +80,7 @@
         });
         addOption(new Option("d", "debug", "Enables the debugger") {
             protected void doProcess(String arg, LinkedList<String> 
remainingArgs) {
-                setDebug(true);
+                enableDebugging();
             }
         });
         addOption(new ParameterOption("o", "outdir",
@@ -196,7 +199,6 @@
         options.add(option);
     }
 
-
     public abstract class Option {
         private String abbreviation;
         private String fullName;
@@ -356,15 +358,53 @@
         return debug;
     }
 
-    public void setDebug(boolean debug) {
-        this.debug = debug;
-        if (debug) {
-            
setParentApplicationContextUri("/META-INF/services/org/apache/camel/spring/debug.xml");
+    public void enableDebugging() {
+        this.debug = true;
+        
setParentApplicationContextUri("/META-INF/services/org/apache/camel/spring/debug.xml");
+    }
+
+    /**
+     * Returns the currently active debugger if one is enabled
+     *
+     * @return the current debugger or null if none is active
+     * @see #enableDebugging()
+     */
+    public Debugger getDebugger() {
+        for (SpringCamelContext camelContext : camelContexts) {
+            Debugger debugger = Debugger.getDebugger(camelContext);
+            if (debugger != null) {
+                return debugger;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Returns a [EMAIL PROTECTED] CamelTemplate} from the Spring [EMAIL 
PROTECTED] ApplicationContext} instances
+     * or lazily creates a new one dynamically
+     *
+     * @return
+     */
+    public CamelTemplate getCamelTemplate() {
+        if (camelTemplate == null) {
+            camelTemplate = findOrCreateCamelTemplate();
         }
+        return camelTemplate;
     }
 
     // Implementation methods
     // 
-------------------------------------------------------------------------
+    protected CamelTemplate findOrCreateCamelTemplate() {
+        String[] names = 
getApplicationContext().getBeanNamesForType(CamelTemplate.class);
+        if (names != null && names.length > 0) {
+            return (CamelTemplate) getApplicationContext().getBean(names[0], 
CamelTemplate.class);
+        }
+        for (SpringCamelContext camelContext : camelContexts) {
+            return new CamelTemplate(camelContext);
+        }
+        throw new IllegalArgumentException("No CamelContexts are available so 
cannot create a CamelTemplate!");
+    }
+
     protected void doStart() throws Exception {
         LOG.info("Apache Camel " + getVersion() + " starting");
         if (applicationContext == null) {

Added: 
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/debug/DebugTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/debug/DebugTest.java?rev=658722&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/debug/DebugTest.java
 (added)
+++ 
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/debug/DebugTest.java
 Wed May 21 07:54:14 2008
@@ -0,0 +1,80 @@
+/**
+ *
+ * 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.debug;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+import org.apache.camel.processor.interceptor.DebugInterceptor;
+import org.apache.camel.processor.interceptor.Debugger;
+import org.apache.camel.spring.Main;
+import org.apache.camel.CamelTemplate;
+import org.apache.camel.Exchange;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class DebugTest extends TestCase {
+    private static final transient Log LOG = 
LogFactory.getLog(DebugTest.class);
+
+    protected Main main;
+    protected Debugger debugger;
+    protected Object expectedBody = "<hello id='abc'>world!</hello>";
+
+    public void testDebugger() throws Exception {
+        // START SNIPPET: example
+        // lets run the camel route in debug mode
+        main = new Main();
+        main.enableDebugging();
+        
main.setApplicationContextUri("org/apache/camel/spring/debug/applicationContext.xml");
+        main.start();
+
+        // now lets test we have a debugger available
+        debugger = main.getDebugger();
+        // END SNIPPET: example
+
+        assertNotNull("should have a debugger!", debugger);
+
+        DebugInterceptor f1 = assertHasInterceptor("f1");
+        DebugInterceptor o1 = assertHasInterceptor("o1");
+        DebugInterceptor o2 = assertHasInterceptor("o2");
+
+        // now lets send a message
+        CamelTemplate template = main.getCamelTemplate();
+        template.sendBody("direct:a", expectedBody);
+
+        List<Exchange> o1Messages = o1.getExchanges();
+        assertEquals("Expected messages at o1", 1, o1Messages.size());
+        LOG.info("o1 received message: " + o1Messages.get(0));
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        if (main != null) {
+            main.stop();
+        }
+    }
+
+    protected DebugInterceptor assertHasInterceptor(String id) {
+        DebugInterceptor interceptor = debugger.getInterceptor(id);
+        assertNotNull("Should have an interceptor for id: " + id, interceptor);
+        return interceptor;
+    }
+}

Propchange: 
activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/debug/DebugTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/debug/applicationContext.xml
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/debug/applicationContext.xml?rev=658722&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/debug/applicationContext.xml
 (added)
+++ 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/debug/applicationContext.xml
 Wed May 21 07:54:14 2008
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
+       http://activemq.apache.org/camel/schema/spring 
http://activemq.apache.org/camel/schema/spring/camel-spring.xsd";>
+
+  <camelContext xmlns="http://activemq.apache.org/camel/schema/spring";>
+    <route>
+      <from uri="direct:a"/>
+      <filter id="f1">
+        <xpath>/hello/@id = 'abc'</xpath>
+        <to uri="mock:b" id="o1"/>
+      </filter>
+      <to uri="mock:c" id="o2"/>
+    </route>
+  </camelContext>
+
+</beans>
+

Propchange: 
activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/debug/applicationContext.xml
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to