You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/01/12 15:01:18 UTC

svn commit: r733758 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/impl/ main/java/org/apache/camel/model/ main/java/org/apache/camel/processor/interceptor/ main/java/org/apache/camel/spi/ test/java/org/apache/camel/processor/int...

Author: davsclaus
Date: Mon Jan 12 06:01:12 2009
New Revision: 733758

URL: http://svn.apache.org/viewvc?rev=733758&view=rev
Log:
CAMEL-639: Tracer now shows from node. Added TraceableUnitOfWork

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TraceableUnitOfWork.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OtherwiseType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceEventMessage.java
    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
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorDestinationTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java?rev=733758&r1=733757&r2=733758&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java Mon Jan 12 06:01:12 2009
@@ -17,13 +17,15 @@
 package org.apache.camel.impl;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
-import java.util.concurrent.CountDownLatch;
 
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.Exchange;
 import org.apache.camel.Service;
+import org.apache.camel.model.ProcessorType;
 import org.apache.camel.spi.Synchronization;
+import org.apache.camel.spi.TraceableUnitOfWork;
 import org.apache.camel.spi.UnitOfWork;
 import org.apache.camel.util.UuidGenerator;
 
@@ -32,17 +34,19 @@
  *
  * @version $Revision$
  */
-public class DefaultUnitOfWork implements UnitOfWork, Service {
+public class DefaultUnitOfWork implements TraceableUnitOfWork, Service {
     private static final UuidGenerator DEFAULT_ID_GENERATOR = new UuidGenerator();
 
     private String id;
     private List<Synchronization> synchronizations;
     private List<AsyncCallback> asyncCallbacks;
+    private List<ProcessorType> routeList;
 
     public DefaultUnitOfWork() {
     }
 
     public void start() throws Exception {
+        id = null;
     }
 
     public void stop() throws Exception {
@@ -53,6 +57,9 @@
         if (asyncCallbacks != null) {
             asyncCallbacks.clear();
         }
+        if (routeList != null) {
+            routeList.clear();
+        }
     }
 
     public synchronized void addSynchronization(Synchronization synchronization) {
@@ -92,6 +99,24 @@
         return id;
     }
 
+    public synchronized void addInterceptedNode(ProcessorType node) {
+        if (routeList == null) {
+            routeList = new ArrayList<ProcessorType>();
+        }
+        routeList.add(node);
+    }
+
+    public synchronized ProcessorType getLastInterceptedNode() {
+        if (routeList == null) {
+            return null;
+        }
+        return routeList.get(routeList.size() - 1);
+    }
+
+    public List<ProcessorType> getInterceptedNodes() {
+        return Collections.unmodifiableList(routeList);
+    }
+
     /**
      * Register some asynchronous processing step
      */

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OtherwiseType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OtherwiseType.java?rev=733758&r1=733757&r2=733758&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OtherwiseType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OtherwiseType.java Mon Jan 12 06:01:12 2009
@@ -16,12 +16,14 @@
  */
 package org.apache.camel.model;
 
+import java.util.List;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlRootElement;
 
 import org.apache.camel.Processor;
 import org.apache.camel.spi.RouteContext;
+import org.apache.camel.util.CollectionStringBuffer;
 
 /**
  * Represents an XML &lt;otherwise/&gt; element
@@ -49,6 +51,11 @@
 
     @Override
     public String getLabel() {
-        return "otherwise";
+        CollectionStringBuffer buffer = new CollectionStringBuffer();
+        List<ProcessorType> list = getOutputs();
+        for (ProcessorType type : list) {
+            buffer.append(type.getLabel());
+        }
+        return buffer.toString();
     }
 }

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=733758&r1=733757&r2=733758&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 Mon Jan 12 06:01:12 2009
@@ -122,7 +122,7 @@
     public Processor wrapProcessorInInterceptors(ProcessorType processorType, Processor target) throws Exception {
         String id = processorType.idOrCreate();
         if (logExchanges) {
-            TraceInterceptor  traceInterceptor = new TraceInterceptor(processorType, target, tracer);
+            TraceInterceptor traceInterceptor = new TraceInterceptor(processorType, target, tracer);
             target = traceInterceptor;
         }
         DebugInterceptor interceptor = new DebugInterceptor(processorType, target, createExchangeList(), createExceptionsList());

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java?rev=733758&r1=733757&r2=733758&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java Mon Jan 12 06:01:12 2009
@@ -19,6 +19,7 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.model.ProcessorType;
+import org.apache.camel.spi.TraceableUnitOfWork;
 import org.apache.camel.spi.UnitOfWork;
 import org.apache.camel.util.MessageHelper;
 
@@ -213,15 +214,15 @@
      * <br/>or
      * <br/><tt>ID-mojo/39713-1225468755256/2-0 -> transform(body)</tt>
      */
-    protected String extractBreadCrumb(TraceInterceptor interceptor, ProcessorType node, Exchange exchange) {
+    protected String extractBreadCrumb(TraceInterceptor interceptor, ProcessorType currentNode, Exchange exchange) {
         String id = "";
-        String nodeMsg = "";
         String result;
         
         if (!showBreadCrumb && !showExchangeId && !showShortExchangeId && !showNode) {
             return "";
         }
-        
+
+        // compute breadcrumb id
         if (showBreadCrumb) {
             id = getBreadCrumbID(exchange).toString();
         } else if (showExchangeId || showShortExchangeId) {
@@ -232,22 +233,38 @@
             }
         }
 
+        // compute from and to
+        String from = "";
+        if (showNode && exchange.getUnitOfWork() instanceof TraceableUnitOfWork) {
+            TraceableUnitOfWork tuow = (TraceableUnitOfWork) exchange.getUnitOfWork();
+            ProcessorType prev = tuow.getLastInterceptedNode();
+            if (prev != null) {
+                from = getNodeMessage(prev);
+            } else if (exchange.getFromEndpoint() != null) {
+                from = exchange.getFromEndpoint().getEndpointUri();
+            }
+        }
+        String to = "";
         if (showNode) {
-            nodeMsg = getNodeMessage(node);
+            to = getNodeMessage(currentNode);
         }
 
-        if (interceptor.shouldTraceOutExchanges() && exchange.getOut(false) != null) {
-            result = nodeMsg.trim() + " -> " + id.trim();
+        // assemble result with and without the to/from
+        if (showNode) {
+            result = id.trim() + " >>> " + from.toString() + " --> " + to.trim();
+            if (interceptor.shouldTraceOutExchanges() && exchange.getOut(false) != null) {
+                result += " (OUT) ";
+            }
         } else {
-            result = id.trim() + " -> " + nodeMsg.trim();
+            result = id;
         }
 
         if (breadCrumbLength > 0) {
             // we want to ensure text coming after this is aligned for readability
-            return String.format("%1$-" + breadCrumbLength + "." + breadCrumbLength + "s", result).trim();
+            return String.format("%1$-" + breadCrumbLength + "." + breadCrumbLength + "s", result.trim());
         } else {
             return result.trim();
         }
-
     }
+
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceEventMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceEventMessage.java?rev=733758&r1=733757&r2=733758&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceEventMessage.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceEventMessage.java Mon Jan 12 06:01:12 2009
@@ -21,6 +21,7 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.model.ProcessorType;
+import org.apache.camel.spi.TraceableUnitOfWork;
 import org.apache.camel.util.MessageHelper;
 
 /**
@@ -31,7 +32,8 @@
 public final class TraceEventMessage implements Serializable {
 
     private String fromEndpointUri;
-    private String node;
+    private String previousNode;
+    private String toNode;
     private String exchangeId;
     private String shortExchangeId;
     private String exchangePattern;
@@ -47,10 +49,10 @@
      * Creates a {@link TraceEventMessage} based on the given node it was traced while processing
      * the current {@link Exchange}
      *
-     * @param node  the node where this trace is intercepted
+     * @param toNode the node where this trace is intercepted
      * @param exchange the current {@link Exchange}
      */
-    public TraceEventMessage(final ProcessorType node, final Exchange exchange) {
+    public TraceEventMessage(final ProcessorType toNode, final Exchange exchange) {
         Message in = exchange.getIn();
 
         // false because we don't want to introduce side effects
@@ -58,7 +60,8 @@
 
         // need to use defensive copies to avoid Exchange altering after the point of interception
         this.fromEndpointUri = exchange.getFromEndpoint() != null ? exchange.getFromEndpoint().getEndpointUri() : null;
-        this.node = extractNode(node);
+        this.previousNode = extractPreviousNode(exchange);
+        this.toNode = extractNode(toNode);
         this.exchangeId = exchange.getExchangeId();
         this.shortExchangeId = extractShortExchangeId(exchange);
         this.exchangePattern = exchange.getPattern().toString();
@@ -81,21 +84,51 @@
         return exchange.getExchangeId().substring(exchange.getExchangeId().indexOf("/") + 1);
     }
 
+    private String extractPreviousNode(Exchange exchange) {
+        if (exchange.getUnitOfWork() instanceof TraceableUnitOfWork) {
+            TraceableUnitOfWork tuow = (TraceableUnitOfWork) exchange.getUnitOfWork();
+            ProcessorType last = tuow.getLastInterceptedNode();
+            return last != null ? extractNode(last) : null;
+        }
+        return null;
+    }
+
     // Properties
     //---------------------------------------------------------------
 
+    /**
+     * Uri of the endpoint that started the {@link Exchange} currently being traced.
+     */
     public String getFromEndpointUri() {
         return fromEndpointUri;
     }
 
-    public String getNode() {
-        return node;
+    /**
+     * Gets the previous node.
+     * <p/>
+     * Will return <tt>null</tt> if this is the first node, then you can use the from endpoint uri
+     * instread to indicate the start
+     */
+    public String getPreviousNode() {
+        return previousNode;
+    }
+
+    /**
+     * Gets the current node that just have been intercepted and processed
+     * <p/>
+     * Is never null.
+     */
+    public String getToNode() {
+        return toNode;
     }
 
     public String getExchangeId() {
         return exchangeId;
     }
 
+    /**
+     * Gets the exchange id without the leading hostname
+     */
     public String getShortExchangeId() {
         return shortExchangeId;
     }
@@ -134,6 +167,6 @@
 
     @Override
     public String toString() {
-        return "TraceEventMessage[" + exchangeId + "] for node: " + node;
+        return "TraceEventMessage[" + exchangeId + "] to node: " + toNode;
     }
 }

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=733758&r1=733757&r2=733758&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 Mon Jan 12 06:01:12 2009
@@ -26,6 +26,7 @@
 import org.apache.camel.processor.DelegateProcessor;
 import org.apache.camel.processor.Logger;
 import org.apache.camel.spi.InterceptStrategy;
+import org.apache.camel.spi.TraceableUnitOfWork;
 import org.apache.camel.util.ServiceHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -86,19 +87,28 @@
             return;
         }
 
+        boolean shouldLog = shouldLogNode(node) && shouldLogExchange(exchange);
+
         // okay this is a regular exchange being routed we might need to log and trace
         try {
+
             // before
-            if (shouldLogNode(node) && shouldLogExchange(exchange)) {
+            if (shouldLog) {
                 logExchange(exchange);
                 traceExchange(exchange);
+
+                // if traceable then register this as the previous node, now it has been logged
+                if (exchange.getUnitOfWork() instanceof TraceableUnitOfWork) {
+                    TraceableUnitOfWork tuow = (TraceableUnitOfWork) exchange.getUnitOfWork();
+                    tuow.addInterceptedNode(node);
+                }
             }
 
             // process the exchange
             super.proceed(exchange);
 
             // after (trace out)
-            if (tracer.isTraceOutExchanges() && shouldLogNode(node) && shouldLogExchange(exchange)) {
+            if (shouldLog && tracer.isTraceOutExchanges()) {
                 logExchange(exchange);
                 traceExchange(exchange);
             }

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=733758&r1=733757&r2=733758&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 Mon Jan 12 06:01:12 2009
@@ -66,13 +66,20 @@
         // 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, this);
+        return new TraceInterceptor(processorType, target, formatter, this);
     }
 
     public TraceFormatter getFormatter() {
         return formatter;
     }
 
+    public DefaultTraceFormatter getDefaultTraceFormatter() {
+        if (formatter instanceof DefaultTraceFormatter) {
+            return (DefaultTraceFormatter) formatter;
+        }
+        return null;
+    }
+
     public void setFormatter(TraceFormatter formatter) {
         this.formatter = formatter;
     }

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TraceableUnitOfWork.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TraceableUnitOfWork.java?rev=733758&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TraceableUnitOfWork.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/TraceableUnitOfWork.java Mon Jan 12 06:01:12 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.spi;
+
+import java.util.List;
+
+import org.apache.camel.model.ProcessorType;
+
+/**
+ * A Unit of work that is also traceable with the
+ * {@link org.apache.camel.processor.interceptor.TraceInterceptor} so we can trace the excact
+ * route path a given {@link org.apache.camel.Exchange} has been processed.
+ *
+ * @version $Revision$
+ */
+public interface TraceableUnitOfWork extends UnitOfWork {
+
+    /**
+     * Adds the given node that was intercepted
+     *
+     * @param node the node
+     */
+    void addInterceptedNode(ProcessorType node);
+
+    /**
+     * Gets the last intercepted node, is <tt>null</tt> if no last exists.
+     */
+    ProcessorType getLastInterceptedNode();
+
+    /**
+     * Gets the current list of intercepted nodes, representing the route path the
+     * current {@link org.apache.camel.Exchange} has taken.
+     */
+    List<ProcessorType> getInterceptedNodes();
+
+}

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorDestinationTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorDestinationTest.java?rev=733758&r1=733757&r2=733758&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorDestinationTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorDestinationTest.java Mon Jan 12 06:01:12 2009
@@ -152,8 +152,8 @@
             // here we can transform the message how we like want it
             TraceEventMessage msg = exchange.getIn().getBody(TraceEventMessage.class);
 
-            // we want to store it as a CSV with fromEndpoint;node;exchangeId;body
-            String s = msg.getFromEndpointUri() + ";" + msg.getNode() + ";" + msg.getExchangeId() + ";" + msg.getBody();
+            // we want to store it as a CSV with from;to;exchangeId;body
+            String s = msg.getFromEndpointUri() + ";" + msg.getToNode() + ";" + msg.getExchangeId() + ";" + msg.getBody();
 
             // so we replace the IN body with our CSV string
             exchange.getIn().setBody(s);