Author: ningjiang
Date: Fri Aug  1 04:19:50 2008
New Revision: 681654

URL: http://svn.apache.org/viewvc?rev=681654&view=rev
Log:
CAMEL-776 support to configure the trace dynamically

Added:
    
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorTest.java
   (with props)
Modified:
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceInterceptor.java
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceInterceptor.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceInterceptor.java?rev=681654&r1=681653&r2=681654&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceInterceptor.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceInterceptor.java
 Fri Aug  1 04:19:50 2008
@@ -33,17 +33,27 @@
  */
 public class TraceInterceptor extends DelegateProcessor implements 
ExchangeFormatter {
     private final ProcessorType node;
-    private Predicate traceFilter;
+    private Predicate<Exchange> traceFilter;
     private boolean traceExceptions = true;
     private Logger logger = new 
Logger(LogFactory.getLog(TraceInterceptor.class), this);
     private TraceFormatter formatter;
+    private Tracer tracer;
 
-    public TraceInterceptor(ProcessorType node, Processor target, 
TraceFormatter formatter) {
+    public TraceInterceptor(ProcessorType node, Processor target, 
TraceFormatter formatter, Tracer tracer) {
         super(target);
+        this.tracer = tracer;
         this.node = node;
         this.formatter = formatter;
     }
 
+    public TraceInterceptor(ProcessorType node, Processor target, 
TraceFormatter formatter) {
+        this(node, target, formatter, null);
+    }
+
+    public TraceInterceptor(ProcessorType node, Processor target, Tracer 
tracer) {
+        this(node, target, null, tracer);
+    }
+
     @Override
     public String toString() {
         return "TraceInterceptor[" + node + "]";
@@ -65,7 +75,14 @@
     }
 
     public Object format(Exchange exchange) {
-        return formatter.format(this, exchange);
+        TraceFormatter traceFormatter = null;
+        if (formatter != null) {
+            traceFormatter = formatter;
+        } else {
+            assert tracer != null;
+            traceFormatter = tracer.getFormatter();
+        }
+        return traceFormatter.format(this, exchange);
     }
 
     // Properties
@@ -133,7 +150,8 @@
      * Returns true if the given exchange should be logged in the trace list
      */
     protected boolean shouldLogExchange(Exchange exchange) {
-        return traceFilter == null || traceFilter.matches(exchange);
+        return (tracer == null || tracer.isEnabled())
+            && (traceFilter == null || traceFilter.matches(exchange));
     }
 
 }
\ No newline at end of file

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java?rev=681654&r1=681653&r2=681654&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
 Fri Aug  1 04:19:50 2008
@@ -16,7 +16,11 @@
  */
 package org.apache.camel.processor.interceptor;
 
+import java.util.List;
+
+import org.apache.camel.CamelContext;
 import org.apache.camel.Processor;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.model.ProcessorType;
 import org.apache.camel.spi.InterceptStrategy;
 
@@ -28,12 +32,31 @@
 public class Tracer implements InterceptStrategy {
 
     private TraceFormatter formatter = new TraceFormatter();
+    private boolean isEnabled = true;
+    /**
+     * A helper method to return the Tracer 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 Tracer getDebugger(CamelContext context) {
+        if (context instanceof DefaultCamelContext) {
+            DefaultCamelContext defaultCamelContext = (DefaultCamelContext) 
context;
+            List<InterceptStrategy> list = 
defaultCamelContext.getInterceptStrategies();
+            for (InterceptStrategy interceptStrategy : list) {
+                if (interceptStrategy instanceof Tracer) {
+                    return (Tracer)interceptStrategy;
+                }
+            }
+        }
+        return null;
+    }
 
     public Processor wrapProcessorInInterceptors(ProcessorType processorType, 
Processor target) throws Exception {
         // Force the creation of an id, otherwise the id is not available when 
the trace formatter is
         // outputting trace information
         String id = processorType.idOrCreate();
-        return new TraceInterceptor(processorType, target, formatter);
+        return new TraceInterceptor(processorType, target, this);
     }
 
     public TraceFormatter getFormatter() {
@@ -43,4 +66,12 @@
     public void setFormatter(TraceFormatter formatter) {
         this.formatter = formatter;
     }
+
+    public void setEnabled(boolean enabled) {
+        isEnabled = enabled;
+    }
+
+    public boolean isEnabled() {
+        return isEnabled;
+    }
 }
\ No newline at end of file

Added: 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorTest.java?rev=681654&view=auto
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorTest.java
 (added)
+++ 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorTest.java
 Fri Aug  1 04:19:50 2008
@@ -0,0 +1,64 @@
+/**
+ * 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.processor.interceptor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.easymock.classextension.EasyMock;
+
+public class TraceInterceptorTest extends ContextTestSupport {
+    TraceFormatter formatter;
+    Tracer tracer;
+    @Override
+    protected void setUp() throws Exception {
+        formatter = EasyMock.createMock(TraceFormatter.class);
+        tracer = new Tracer();
+        super.setUp();
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                tracer.setFormatter(formatter);
+                getContext().addInterceptStrategy(tracer);
+                from("direct:a").to("mock:a");
+            }
+        };
+    }
+
+    public void testTracerInterceptor() throws Exception {
+        EasyMock.reset(formatter);
+        formatter.format(EasyMock.isA(TraceInterceptor.class), 
EasyMock.isA(Exchange.class));
+        EasyMock.expectLastCall().andReturn("Test");
+        EasyMock.replay(formatter);
+        template.sendBody("direct:a", "<hello>world!</hello>");
+        EasyMock.verify(formatter);
+    }
+
+    public void testTracerDisabledInterceptor() throws Exception {
+        tracer.setEnabled(false);
+        try {
+            testTracerInterceptor();
+            fail("The tracer should not work");
+        } catch (Throwable ex) {
+            assertTrue("ex should AssertionError", ex instanceof 
AssertionError);
+        }
+    }
+
+}

Propchange: 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to