Author: janstey
Date: Fri Oct 31 11:03:54 2008
New Revision: 709512
URL: http://svn.apache.org/viewvc?rev=709512&view=rev
Log:
CAMEL-1039 - Adding support to trace out messages.
Added:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceInterceptorWithOutBodyTraceTest.java
(with props)
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceFormatter.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/aggregator/PredicateAggregatorCollectionTest.java
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceFormatter.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceFormatter.java?rev=709512&r1=709511&r2=709512&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceFormatter.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceFormatter.java
Fri Oct 31 11:03:54 2008
@@ -33,20 +33,22 @@
private boolean showExchangePattern = true;
private boolean showProperties = true;
private boolean showHeaders = true;
- private boolean showBody = true;
+ private boolean showBody = true;
private boolean showBodyType = true;
+ private boolean showOutBody = false;
+ private boolean showOutBodyType = false;
private boolean showException = true;
public Object format(TraceInterceptor interceptor, Exchange exchange) {
Message in = exchange.getIn();
+
+ // false because we don't want to introduce side effects
+ Message out = exchange.getOut(false);
+
Throwable exception = exchange.getException();
StringBuilder sb = new StringBuilder();
- if (showBreadCrumb || showExchangeId) {
- sb.append(getBreadCrumbID(exchange)).append(" ");
- }
- if (showNode) {
- sb.append("-> ").append(getNodeMessage(interceptor)).append(" ");
- }
+ sb.append(getExchangeAndNode(interceptor, exchange));
+
if (showExchangePattern) {
sb.append(", Pattern:").append(exchange.getPattern()).append(" ");
}
@@ -64,6 +66,12 @@
if (showBody) {
sb.append(", Body:").append(getBodyAsString(in)).append(" ");
}
+ if (showOutBodyType && out != null) {
+ sb.append(",
OutBodyType:").append(getBodyTypeAsString(out)).append(" ");
+ }
+ if (showOutBody && out != null) {
+ sb.append(", OutBody:").append(getBodyAsString(out)).append(" ");
+ }
if (showException && exception != null) {
sb.append(", Exception:").append(exception);
}
@@ -87,6 +95,22 @@
this.showBodyType = showBodyType;
}
+ public void setShowOutBody(boolean showOutBody) {
+ this.showOutBody = showOutBody;
+ }
+
+ public boolean isShowOutBody() {
+ return showOutBody;
+ }
+
+ public void setShowOutBodyType(boolean showOutBodyType) {
+ this.showOutBodyType = showOutBodyType;
+ }
+
+ public boolean isShowOutBodyType() {
+ return showOutBodyType;
+ }
+
public boolean isShowBreadCrumb() {
return showBreadCrumb;
}
@@ -151,6 +175,10 @@
}
protected Object getBodyAsString(Message in) {
+ if (in == null) {
+ return null;
+ }
+
StreamCache newBody = null;
try {
newBody = in.getBody(StreamCache.class);
@@ -176,6 +204,9 @@
}
protected Object getBodyTypeAsString(Message message) {
+ if (message == null) {
+ return null;
+ }
String answer = ObjectHelper.classCanonicalName(message.getBody());
if (answer != null && answer.startsWith("java.lang.")) {
return answer.substring(10);
@@ -187,5 +218,36 @@
String message = interceptor.getNode().getShortName() + "(" +
interceptor.getNode().getLabel() + ")";
return String.format("%1$-25s", message);
}
-
+
+ /**
+ * Returns the exchange id and node, ordered based on whether this was a
trace of
+ * an exchange coming out of or into a processing step. For example,
+ *
+ * transform(body) -> ID-mojo/39713-1225468755256/2-0
+ *
+ * Or
+ *
+ * ID-mojo/39713-1225468755256/2-0 -> transform(body)
+ *
+ */
+ protected String getExchangeAndNode(TraceInterceptor interceptor, Exchange
exchange) {
+ String id = "";
+ String node = "";
+ String result;
+
+ if (showBreadCrumb || showExchangeId) {
+ id = getBreadCrumbID(exchange).toString();
+ }
+ if (showNode) {
+ node = getNodeMessage(interceptor);
+ }
+ if (interceptor.shouldTraceOutExchanges() && exchange.getOut(false) !=
null) {
+ result = node.trim() + " -> " + id;
+ } else {
+ result = id.trim() + " -> " + node;
+ }
+
+ // we want to ensure text coming after this is aligned for readability
+ return String.format("%1$-60s", result);
+ }
}
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=709512&r1=709511&r2=709512&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 Oct 31 11:03:54 2008
@@ -82,6 +82,9 @@
logExchange(exchange);
}
super.proceed(exchange);
+ if (tracer.isTraceOutExchanges() && shouldLogNode(node) &&
shouldLogExchange(exchange)) {
+ logExchange(exchange);
+ }
} catch (Exception e) {
if (shouldLogException(exchange)) {
logException(exchange, e);
@@ -135,7 +138,13 @@
return tracer.isTraceExceptions();
}
-
+ /**
+ * Returns whether exchanges coming out of processors should be traced
+ */
+ public boolean shouldTraceOutExchanges() {
+ return tracer.isTraceOutExchanges();
+ }
+
/**
* Returns true if the given node should be logged in the trace list
*/
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=709512&r1=709511&r2=709512&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 Oct 31 11:03:54 2008
@@ -41,7 +41,8 @@
private Predicate<Exchange> traceFilter;
private boolean traceInterceptors;
private boolean traceExceptions = true;
-
+ private boolean traceOutExchanges = false;
+
/**
* A helper method to return the Tracer instance for a given [EMAIL
PROTECTED] CamelContext} if one is enabled
*
@@ -89,7 +90,7 @@
}
/**
- * Sets wether interceptors should be traced or not
+ * Sets whether interceptors should be traced or not
*/
public void setTraceInterceptors(boolean traceInterceptors) {
this.traceInterceptors = traceInterceptors;
@@ -111,7 +112,7 @@
}
/**
- * Sets the logging level to ouput tracing. Will default use <tt>INFO</tt>
level.
+ * Sets the logging level to output tracing. Will use <tt>INFO</tt> level
by default.
*/
public void setLogLevel(LoggingLevel logLevel) {
this.logLevel = logLevel;
@@ -122,7 +123,7 @@
}
/**
- * Sets wether thrown exceptions should be traced
+ * Sets whether thrown exceptions should be traced
*/
public void setTraceExceptions(boolean traceExceptions) {
this.traceExceptions = traceExceptions;
@@ -139,4 +140,15 @@
public void setLogName(String logName) {
this.logName = logName;
}
+
+ /**
+ * Sets whether exchanges coming out of processors should be traced
+ */
+ public void setTraceOutExchanges(boolean traceOutExchanges) {
+ this.traceOutExchanges = traceOutExchanges;
+ }
+
+ public boolean isTraceOutExchanges() {
+ return traceOutExchanges;
+ }
}
\ No newline at end of file
Added:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceInterceptorWithOutBodyTraceTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceInterceptorWithOutBodyTraceTest.java?rev=709512&view=auto
==============================================================================
---
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceInterceptorWithOutBodyTraceTest.java
(added)
+++
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceInterceptorWithOutBodyTraceTest.java
Fri Oct 31 11:03:54 2008
@@ -0,0 +1,49 @@
+/**
+ * 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;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.processor.interceptor.Tracer;
+import org.apache.camel.util.ExchangeHelper;
+
+public class TraceInterceptorWithOutBodyTraceTest extends TraceInterceptorTest
{
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ // START SNIPPET: tracingOutExchanges
+ Tracer tracer = new Tracer();
+ tracer.setTraceOutExchanges(true);
+ tracer.getFormatter().setShowOutBody(true);
+ tracer.getFormatter().setShowOutBodyType(true);
+
+ getContext().addInterceptStrategy(tracer);
+ // START SNIPPET: tracingOutExchanges
+
+ from("direct:start").
+ transform().body().
+ to("mock:a").
+ to("mock:b");
+ }
+ };
+ }
+
+}
Propchange:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceInterceptorWithOutBodyTraceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/PredicateAggregatorCollectionTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/PredicateAggregatorCollectionTest.java?rev=709512&r1=709511&r2=709512&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/PredicateAggregatorCollectionTest.java
(original)
+++
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/PredicateAggregatorCollectionTest.java
Fri Oct 31 11:03:54 2008
@@ -70,7 +70,7 @@
// our route is aggregating from the direct queue and sending
the response to the mock
from("direct:start")
- // we use the collection based aggregator we already have
configued
+ // we use the collection based aggregator we already have
configured
.aggregator(ag)
// wait for 0.5 seconds to aggregate
.batchTimeout(500L)