You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by bs...@apache.org on 2008/04/16 06:30:43 UTC

svn commit: r648504 - /servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/TraceComponent.java

Author: bsnyder
Date: Tue Apr 15 21:30:42 2008
New Revision: 648504

URL: http://svn.apache.org/viewvc?rev=648504&view=rev
Log:
SM-1315 - Enhance logging inside TraceComponent.java to log NormalizedMassage properties

Modified:
    servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/TraceComponent.java

Modified: servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/TraceComponent.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/TraceComponent.java?rev=648504&r1=648503&r2=648504&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/TraceComponent.java (original)
+++ servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/TraceComponent.java Tue Apr 15 21:30:42 2008
@@ -19,6 +19,7 @@
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 
 import org.apache.commons.logging.Log;
@@ -54,8 +55,14 @@
         this.sourceTransformer = sourceTransformer;
     }
 
+    /** 
+     * Intercepts the {@link MessageExchange} to output the message and its 
+     * properties for debugging purposes. 
+     * 
+     * @param exchange A JBI {@link MessageExchange} between two endpoints
+     */
     public void onMessageExchange(MessageExchange exchange) throws MessagingException {
-        // lets dump the incoming message
+        // lets dump the incoming message  
         NormalizedMessage message = exchange.getMessage("in");
         if (message == null) {
             log.warn("Received null message from exchange: " + exchange);
@@ -66,7 +73,40 @@
             } catch (TransformerException e) {
                 log.error("Failed to turn message body into text: " + e, e);
             }
+            outputProperties(message);
         }
         done(exchange);
+    }
+    
+    /**
+     * Outputs the properties on the {@link NormalizedMessage}. Properties of 
+     * type {@link Source} are transformed to a {@link String} before 
+     * being output.
+     *
+     * @param message The {@link NormalizedMessage} to be processed
+     */
+    @SuppressWarnings("unchecked")
+    protected void outputProperties(NormalizedMessage message) {
+        // Loop over all the properties on the normalized message 
+        for (Object o : message.getPropertyNames()) {
+            // NormalizedMessage API does not use generics. This interface is
+            // written in Java older than 5.0. On the basis of other methods and
+            // the default implementation of this interface we can assume that
+            // only String keys are allowed.
+            String key = (String) o;
+            try {
+                Object contents = message.getProperty(key);
+                // Is this the only value type that we would like to treat
+                // differently? The default behavior is to invoke toString() on
+                // the object.
+                if (contents instanceof Source) {
+                    contents = getSourceTransformer().toString((Source) contents);
+                }
+
+                log.info("Value for property '" + key + "' is: " + contents);
+            } catch (TransformerException e) {
+                log.error("Failed to turn property '" + key + "' value into text: " + e, e);
+            }
+        }
     }
 }