You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@synapse.apache.org by as...@apache.org on 2007/09/26 15:17:04 UTC

svn commit: r579650 [2/2] - in /webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse: ./ config/xml/ mediators/base/ mediators/builtin/ mediators/db/ mediators/eip/ mediators/eip/splitter/ mediators/ext/ mediators/filters/ media...

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/eip/EIPUtils.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/eip/EIPUtils.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/eip/EIPUtils.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/eip/EIPUtils.java Wed Sep 26 06:16:59 2007
@@ -61,25 +61,26 @@
      * @param expression - AXIOMXPath expression describing the elements
      * @return List of OMElements in the envelope matching the expression
      */
-    public static List getElements(SOAPEnvelope envelope, AXIOMXPath expression) {
-        try {
-            Object o = expression.evaluate(envelope);
-            if (o instanceof OMNode) {
-                List list = new ArrayList();
-                list.add(o);
-                return list;
-            } else if (o instanceof List) {
-                return (List) o;
-            } else {
-                handleException("The evaluation of the XPath expression "
-                        + expression + " must result in an OMNode");
-            }
-        } catch (JaxenException e) {
-            handleException("Error evaluating XPath " + expression + " on message");
-        }
-
-        return null;
-    }
+// asankha - 26 sep - pending review
+//    public static List getElements(SOAPEnvelope envelope, AXIOMXPath expression) {
+//        try {
+//            Object o = expression.evaluate(envelope);
+//            if (o instanceof OMNode) {
+//                List list = new ArrayList();
+//                list.add(o);
+//                return list;
+//            } else if (o instanceof List) {
+//                return (List) o;
+//            } else {
+//                handleException("The evaluation of the XPath expression "
+//                        + expression + " must result in an OMNode");
+//            }
+//        } catch (JaxenException e) {
+//            handleException("Error evaluating XPath " + expression + " on message");
+//        }
+//
+//        return null;
+//    }
 
     /**
      * This static util method will be used to create a new MessageContext by passing the
@@ -90,7 +91,7 @@
      * @return MessageContext created from the paased arguments
      */
     public static MessageContext createNewMessageContext(
-            MessageContext synCtx, SOAPEnvelope envelope) {
+            MessageContext synCtx, SOAPEnvelope envelope) throws AxisFault {
 
         // create the message context and then copy the transportIn/Out from the original message
         MessageContext newCtx = synCtx.getEnvironment().createMessageContext();
@@ -111,7 +112,7 @@
         newAxis2MC.setProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO,
                 axis2MC.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO));
 
-        try {
+        //try {
             newCtx.setEnvelope(envelope);
 
             // copy all the properties to the newCtx
@@ -133,9 +134,10 @@
             newCtx.setSoapAction(synCtx.getSoapAction());
             newCtx.setWSAAction(synCtx.getWSAAction());
 
-        } catch (AxisFault axisFault) {
-            handleException("Unable to split the message" + axisFault.getMessage(), axisFault);
-        }
+// asankha - 26 sep - pending review
+//         } catch (AxisFault axisFault) {
+//            handleException("Unable to split the message" + axisFault.getMessage(), axisFault);
+//        }
 
         return newCtx;
     }
@@ -148,25 +150,26 @@
      * @param enricher - SOAPEnvelope from which the enriching element will be extracted
      * @param expression - AXIOMXPath describing the enriching element
      */
-    public static void enrichEnvelope(SOAPEnvelope envelope,
-                                      SOAPEnvelope enricher, AXIOMXPath expression) {
-        OMElement enrichingElement;
-        Object o = getElements(envelope, expression).get(0);
-        if (o instanceof OMElement && ((OMElement) o).getParent() instanceof OMElement) {
-            enrichingElement = (OMElement) ((OMElement) o).getParent();
-        } else {
-            enrichingElement = envelope.getBody();
-        }
-        
-        Iterator itr = getElements(enricher, expression).iterator();
-        while (itr.hasNext()) {
-            o = itr.next();
-            if (o != null && o instanceof OMElement) {
-                enrichingElement.addChild((OMElement) o);
-            }
-        }
-        
-    }
+// asankha - 26 - sep - pending review
+//     public static void enrichEnvelope(SOAPEnvelope envelope,
+//                                      SOAPEnvelope enricher, AXIOMXPath expression) {
+//        OMElement enrichingElement;
+//        Object o = getElements(envelope, expression).get(0);
+//        if (o instanceof OMElement && ((OMElement) o).getParent() instanceof OMElement) {
+//            enrichingElement = (OMElement) ((OMElement) o).getParent();
+//        } else {
+//            enrichingElement = envelope.getBody();
+//        }
+//
+//        Iterator itr = getElements(enricher, expression).iterator();
+//        while (itr.hasNext()) {
+//            o = itr.next();
+//            if (o != null && o instanceof OMElement) {
+//                enrichingElement.addChild((OMElement) o);
+//            }
+//        }
+//
+//    }
 
     /**
      * This static util method will be used to clone the SOAPEnvelope passed to the method
@@ -195,30 +198,5 @@
         }
         
         return envelope;
-    }
-
-    /**
-     * Private method to handle exceptions
-     *
-     * @param message - String message to be logged and to be put as the exception message
-     */
-    private static void handleException(String message) {
-        if (log.isDebugEnabled()) {
-            log.debug(message);
-        }
-        throw new SynapseException(message);
-    }
-
-    /**
-     * Private method to handle exceptions
-     *
-     * @param message - String message to be logged and to be put as the exception message
-     * @param e - Cause Exception for this exception
-     */
-    private static void handleException(String message, Exception e) {
-        if (log.isDebugEnabled()) {
-            log.debug(message);
-        }
-        throw new SynapseException(message, e);
     }
 }

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/eip/splitter/CloneMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/eip/splitter/CloneMediator.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/eip/splitter/CloneMediator.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/eip/splitter/CloneMediator.java Wed Sep 26 06:16:59 2007
@@ -25,6 +25,7 @@
 import org.apache.synapse.mediators.AbstractMediator;
 import org.apache.synapse.mediators.eip.EIPUtils;
 import org.apache.synapse.mediators.eip.Target;
+import org.apache.axis2.AxisFault;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -83,7 +84,12 @@
     private MessageContext getClonedMessageContext(MessageContext synCtx,
         int messageSequence, int messageCount) {
 
-        MessageContext newCtx = EIPUtils.createNewMessageContext(synCtx, synCtx.getEnvelope());
+        MessageContext newCtx = null;
+        try {
+            newCtx = EIPUtils.createNewMessageContext(synCtx, synCtx.getEnvelope());
+        } catch (AxisFault axisFault) {
+            handleException("Error creating a new message context", axisFault, synCtx);
+        }
 
         // Sets the property MESSAGE_SEQUENCE to the MC for aggragation purposes 
         newCtx.setProperty(EIPUtils.MESSAGE_SEQUENCE, String.valueOf(messageSequence)

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/ClassMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/ClassMediator.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/ClassMediator.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/ClassMediator.java Wed Sep 26 06:16:59 2007
@@ -28,18 +28,26 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
 
 /**
- * The class mediator delegates the mediation to a new instance of a specified
- * class. The specified class must implement the Mediator interface
+ * The class mediator delegates the mediation to a single instance of a specified
+ * class. The specified class must implement the Mediator interface and optionally
+ * may implement the ManagedLifecycle interface. At initialization time, a single
+ * instance of the class is instantiated using a public no argument constructor, and
+ * any one-time properties (parameter constants specified through the Synapse config)
+ * are set on the instance. If each request needs synchronization, the user must
+ * implement it within the specified class.
  * 
  * @see Mediator
  */
 public class ClassMediator extends AbstractMediator implements ManagedLifecycle {
 
-	private Mediator mediator = null;
-
-    private List properties = new ArrayList();
+    /** The reference to the actual class that implments the Mediator interface */
+    private Mediator mediator = null;
+    /** A list of simple properties that would be set on the class before being used */
+    private Map properties = new HashMap();
 
     /**
 	 * Don't use a new instance... do one instance of the object per instance of
@@ -51,42 +59,39 @@
 	 */
 	public boolean mediate(MessageContext synCtx) {
 
-		if (log.isDebugEnabled()) {
-			log.debug("Class mediator <" + mediator.getClass()
-					+ ">:: mediate()");
-		}
-		boolean shouldTrace = shouldTrace(synCtx.getTracingState());
-		if (shouldTrace) {
-			trace.trace("Start : Class mediator");
-		}
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : Class mediator");
 
-		try {
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx);
+            }
+        }
+
+        if (traceOrDebugOn) {
+			traceOrDebug(traceOn, "invoking : " + mediator.getClass() + ".mediate()");
+		}
 
-			if (mediator == null) {
-				if (log.isDebugEnabled()) {
-					log.debug("The instance of the specified mediator is null");
-				}
-				return true;
-			}
-			if (shouldTrace) {
-				trace.trace("Executing an instance of the specified class : "
-						+ mediator.getClass());
-			}
-			return mediator.mediate(synCtx);
+        boolean result = true;
+        try {
+			result = mediator.mediate(synCtx);
         } catch (Exception e) {
             // throw Synapse Exception for any exception in class meditor
             // so that the fault handler will be invoked
             throw new SynapseException("Error occured in the mediation of the class mediator", e);
-        } finally {
-			if (shouldTrace) {
-				trace.trace("End : Class mediator");
-			}
-		}
-	}
+        }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "End : Class mediator");
+        }
+        return result;
+    }
 
 	public void destroy() {
         if (log.isDebugEnabled()) {
-            log.debug("Class Mediator::destroy()");
+            log.debug("Destroying class mediator instance for : " + mediator.getClass());
         }
         if (mediator instanceof ManagedLifecycle) {
 			((ManagedLifecycle) mediator).destroy();
@@ -95,18 +100,15 @@
 
 	public void init(SynapseEnvironment se) {
         if (log.isDebugEnabled()) {
-            log.debug("Class Mediator::init()");
+            log.debug("Initializing class mediator instance for : " + mediator.getClass());
         }
         if (mediator == null) {
-            if (log.isDebugEnabled()) {
-                log.debug("init called before mediator set");
-            }
+            log.warn("init() called before mediator reference set");
             return;
 		}
 
 		if (mediator instanceof ManagedLifecycle) {
 			((ManagedLifecycle) mediator).init(se);
-
 		}
 	}
 
@@ -118,11 +120,11 @@
 		return mediator;
 	}
 
-    public void addProperty(OMElement property) {
-        properties.add(property);
+    public void addProperty(String name, Object value) {
+        properties.put(name, value);
     }
 
-    public List getProperties() {
+    public Map getProperties() {
         return this.properties;
     }
 }

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/POJOCommandMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/POJOCommandMediator.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/POJOCommandMediator.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/ext/POJOCommandMediator.java Wed Sep 26 06:16:59 2007
@@ -21,14 +21,10 @@
 
 import org.apache.synapse.mediators.AbstractMediator;
 import org.apache.synapse.*;
-import org.apache.synapse.config.xml.PropertyHelper;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.axiom.om.OMElement;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Iterator;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+
+import java.util.*;
 import java.lang.reflect.Method;
 import java.lang.reflect.InvocationTargetException;
 
@@ -47,69 +43,169 @@
      */
     private Class command = null;
 
-    private Object commandObject = null;
-
     /**
-     * This will hold the properties of the relevent Command object as OMElements for the
-     * use of serialization
+     * 'static' properties whose values are constant and does not depend
+     * on the current message (i.e. and XPath over it)
+     */
+    private Map staticProps = new HashMap();
+    /**
+     * 'dynamic' properties whose values are dynamically evaluated before each
+     * invocation of the command, by evaluating an XPath against the current message
      */
-    private List properties = new ArrayList();
+    private Map dynamicProps = new HashMap();
 
     /**
-     * Implements the mediate method of the Mediator interface. This method will instantiate all
-     * the properties and call the execute method of the Command object.
+     * Implements the mediate method of the Mediator interface. This method will instantiate
+     * a new instance of the POJO class, set all specified properties from the current runtime
+     * state (and message context) and call the execute method of the Command object.
      *
      * @param synCtx - Synapse MessageContext to be mediated
      * @return boolean true since this will not stop exection chain
      */
     public boolean mediate(MessageContext synCtx) {
 
-        // instantiate the command object
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : POJOCommand mediator");
+
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx);
+            }
+        }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Creating a new instance of POJO class : " + command.getClass());
+        }
+
+        Object commandObject = null;
         try {
+            // instantiate a new command object each time
             commandObject = command.newInstance();
+        } catch (Exception e) {
+            handleException("Error creating an instance of the POJO command class : " +
+                command.getClass(), e, synCtx);
+        }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Instance created, setting static and dynamic properties");
+        }
+
+        // then set the static/constant properties first
+        for (Iterator iter = staticProps.keySet().iterator(); iter.hasNext(); ) {
+            String name = (String) iter.next();
+            setInstanceProperty(name, (String) staticProps.get(name), commandObject, synCtx);
+        }
+
+        // now set the any dynamic properties evaluating XPath's on the current message
+        for (Iterator iter = dynamicProps.keySet().iterator(); iter.hasNext(); ) {
+
+            String name = (String) iter.next();
+            AXIOMXPath xpath = (AXIOMXPath) dynamicProps.get(name);
+            String value = Axis2MessageContext.getStringValue(xpath, synCtx);
+
+            setInstanceProperty(name, value, commandObject, synCtx);
+        }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "POJO initialized successfully, invoking the execute() method");
+        }
+
+        // then call the execute method if the Command interface is implemented
+        if (commandObject instanceof Command) {
+            try {
+                ((Command) commandObject).execute();
+            } catch (Exception e) {
+                handleException("Error invoking POJO command class : "
+                    + command.getClass(), e, synCtx);
+            }
+
+        } else {
+
+            Method exeMethod = null;
+            try {
+                exeMethod = command.getMethod("execute", new Class[]{});
+                exeMethod.invoke(commandObject, new Object[]{});
+            } catch (NoSuchMethodException e) {
+                handleException("Cannot locate an execute() method on POJO class : " +
+                    command.getClass(), e, synCtx);
+            } catch (Exception e) {
+                handleException("Error invoking the execute() method on POJO class : " +
+                    command.getClass(), e, synCtx);
+            }
+        }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "End : POJOCommand mediator");
+        }
+        return true;
+    }
+
+    /**
+     * Find and invoke the setter method with the name of form setXXX passing in the value given
+     * on the POJO object
+     * @param name name of the setter field
+     * @param value value to be set
+     * @param obj POJO instance
+     * @param synCtx current message
+     */
+    private void setInstanceProperty(String name, String value, Object obj, MessageContext synCtx) {
+
+        String mName = "set" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
+        Method method = null;
+
+        try {
+            Method[] methods = obj.getClass().getMethods();
+            boolean invoked = false;
 
-            // then set the properties
-            Iterator itr = properties.iterator();
-            while (itr.hasNext()) {
-                Object property = itr.next();
-                if (property instanceof OMElement) {
-                    if (PropertyHelper.isStaticProperty((OMElement) property)) {
-                        PropertyHelper.setStaticProperty((OMElement) property, commandObject);
+            for (int i=0; i<methods.length; i++) {
+                if (mName.equals(methods[i].getName())) {
+                    Class[] params = methods[i].getParameterTypes();
+                    if (params.length != 1) {
+                        handleException("Did not find a setter method named : " + mName +
+                            "() that takes a single String, int, long, float, double " +
+                            "or boolean parameter", synCtx);
                     } else {
-                        PropertyHelper.setDynamicProperty((OMElement) property, commandObject, synCtx);
+                        if (params[0].equals(String.class)) {
+                            method = obj.getClass().getMethod(mName, new Class[]{String.class});
+                            method.invoke(obj, new String[]{value});
+                        } else if (params[0].equals(int.class)) {
+                            method = obj.getClass().getMethod(mName, new Class[]{int.class});
+                            method.invoke(obj, new Integer[]{new Integer(value)});
+                        } else if (params[0].equals(long.class)) {
+                            method = obj.getClass().getMethod(mName, new Class[]{long.class});
+                            method.invoke(obj, new Long[]{new Long(value)});
+                        } else if (params[0].equals(float.class)) {
+                            method = obj.getClass().getMethod(mName, new Class[]{float.class});
+                            method.invoke(obj, new Float[]{new Float(value)});
+                        } else if (params[0].equals(double.class)) {
+                            method = obj.getClass().getMethod(mName, new Class[]{double.class});
+                            method.invoke(obj, new Double[]{new Double(value)});
+                        } else if (params[0].equals(boolean.class)) {
+                            method = obj.getClass().getMethod(mName, new Class[]{boolean.class});
+                            method.invoke(obj, new Boolean[]{new Boolean(value)});
+                        } else {
+                            handleException("Did not find a setter method named : " + mName +
+                                "() that takes a single String, int, long, float, double " +
+                                "or boolean parameter", synCtx);
+                        }
                     }
+                    invoked = true;
                 }
             }
 
-            // then call the execute method if the Command interface is implemented
-            if (commandObject instanceof Command) {
-                ((Command) commandObject).execute();
-            } else {
-                // use the reflection to find the execute method
-                try {
-                    Method exeMethod = command.getMethod("execute", new Class[]{});
-                    try {
-                        exeMethod.invoke(commandObject, new Object[]{});
-                    } catch (IllegalAccessException e) {
-                        handleException("Unable to invoke the execute() method", e);
-                    } catch (InvocationTargetException e) {
-                        handleException("Unable to invoke the execute() method", e);
-                    }
-                } catch (NoSuchMethodException e) {
-                    // nothing to do in here (Command has no implementation)
-                }
+            if (!invoked) {
+                handleException("Did not find a setter method named : " + mName +
+                    "() that takes a single String, int, long, float, double " +
+                    "or boolean parameter", synCtx);
             }
-        } catch (InstantiationException e) {
-            handleException("Unable to instantiate the Command object", e);
-        } catch (IllegalAccessException e) {
-            handleException("Unable to instantiate the Command object", e);
+
         } catch (Exception e) {
-            // convert any exception to SynException so that the fault handler will handle it
-            handleException("Error occured in the mediation of the command mediator", e);
+            handleException("Error invoking setter method named : " + mName +
+                "() that takes a single String, int, long, float, double " +
+                "or boolean parameter", e, synCtx);
         }
-
-        // continue the mediator execution
-        return true;
     }
 
     public Class getCommand() {
@@ -120,25 +216,19 @@
         this.command = command;
     }
 
-    public void addProperty(OMElement property) {
-        this.properties.add(property);
+    public void addStaticProperty(String name, String value) {
+        this.staticProps.put(name, value);
     }
 
-    public List getProperties() {
-        return this.properties;
+    public void addDynamicProperty(String name, Object value) {
+        this.dynamicProps.put(name, value);
     }
 
-    private void handleException(String message, Throwable e) {
-        if (log.isDebugEnabled()) {
-            log.debug(message + e.getMessage());
-        }
-        throw new SynapseException(message, e);
+    public Map getStaticProps() {
+        return this.staticProps;
     }
 
-    private void handleException(String message) {
-        if (log.isDebugEnabled()) {
-            log.debug(message);
-        }
-        throw new SynapseException(message);
+    public Map getDynamicProps() {
+        return this.dynamicProps;
     }
 }

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/FilterMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/FilterMediator.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/FilterMediator.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/FilterMediator.java Wed Sep 26 06:16:59 2007
@@ -49,30 +49,42 @@
      */
     public boolean mediate(MessageContext synCtx) {
 
-        if (log.isDebugEnabled()) {
-            log.debug("Filter mediator mediate()");
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : Filter mediator");
+
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx);
+            }
         }
-        boolean shouldTrace = shouldTrace(synCtx.getTracingState());
-        try {
-            if (shouldTrace) {
-                trace.trace("Start : Filter mediator ");
-            }
-            if (test(synCtx)) {
-                if (log.isDebugEnabled()) {
-                    log.debug("Filter condition satisfied.. executing child mediators");
-                }
-                return super.mediate(synCtx);
-            } else {
-                if (log.isDebugEnabled()) {
-                    log.debug("Filter condition failed.. will skip executing child mediators");
-                }
-                return true;
+
+        boolean result = false;
+        if (test(synCtx)) {
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, (xpath == null ?
+                    "Source : " + source + " against : " + regex.pattern() + " matches" :
+                    "XPath expression : "  + xpath + " evaluates to true") +
+                    " - executing child mediators");
             }
-        } finally {
-            if (shouldTrace) {
-                trace.trace("End : Filter mediator ");
+            result = super.mediate(synCtx);
+
+        } else {
+
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, (xpath == null ?
+                    "Source : " + source + " against : " + regex.pattern() + " does not match" :
+                    "XPath expression : "  + xpath + " evaluates to false") +
+                    " - skipping child mediators");
             }
+            result = true;
         }
+
+        if (traceOrDebugOn) {
+            trace.trace("End : Filter mediator ");
+        }
+        return result;
     }
 
     /**
@@ -85,50 +97,37 @@
      * @return true if evaluation of the XPath/Regex results in true
      */
     public boolean test(MessageContext synCtx) {
-        try {
-            if (xpath != null) {
-                if (log.isDebugEnabled()) {
-                    log.debug("Evaluating XPath expression : " + xpath);
-                }
-                if (shouldTrace(synCtx.getTracingState())) {
-                    trace.trace("XPath expression : " + xpath + " evaluates to : " +
-                            xpath.booleanValueOf(synCtx.getEnvelope()));
-                }
+
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (xpath != null) {
+            try {
                 return xpath.booleanValueOf(synCtx.getEnvelope());
+            } catch (JaxenException e) {
+                handleException("Error evaluating XPath expression : " + xpath, e, synCtx);
+            }
 
-            } else if (source != null && regex != null) {
-                if (log.isDebugEnabled()) {
-                    log.debug("Evaluating regular expression : " + regex.pattern() +
-                            " against source : " + source);
-                }
-                String sourceString = Axis2MessageContext.getStringValue(source, synCtx);
-                if (sourceString == null) {
-                    if (log.isDebugEnabled()) {
-                        log.debug("Source String has been evaluated to Null");
-                    }
-                    return false;
+        } else if (source != null && regex != null) {
+            String sourceString = Axis2MessageContext.getStringValue(source, synCtx);
+            if (sourceString == null) {
+                if (traceOrDebugOn) {
+                    traceOrDebug(traceOn, "Source String : " + source + " evaluates to null");
                 }
-                if (shouldTrace(synCtx.getTracingState())) {
-                    trace.trace("Regular expression : " + regex.pattern() + " and Source " +
-                            sourceString + " matches : " + regex.matcher(sourceString).matches());
-                }
-                Matcher matcher = regex.matcher(sourceString);
-                if (matcher == null) {
-                    if (log.isDebugEnabled()) {
-                        log.debug("Can not find a Regex Pattren Matcher");
-                    }
-                    return false;
+                return false;
+            }
+            Matcher matcher = regex.matcher(sourceString);
+            if (matcher == null) {
+                if (traceOrDebugOn) {
+                    traceOrDebug(traceOn, "Regex pattren matcher for : " + regex.pattern() +
+                        "against source : " + sourceString + " is null");
                 }
-                return matcher.matches();
-            } else {
-                log.error("Invalid configuration specified");
                 return false;
             }
-
-        } catch (JaxenException e) {
-            log.error("XPath error : " + e.getMessage());
-            return false;
+            return matcher.matches();
         }
+
+        return false; // never executes
     }
 
 

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/InMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/InMediator.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/InMediator.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/InMediator.java Wed Sep 26 06:16:59 2007
@@ -41,30 +41,35 @@
      */
     public boolean mediate(MessageContext synCtx) {
 
-        if (log.isDebugEnabled()) {
-            log.debug("In mediator mediate()");
-        }
-        boolean shouldTrace = shouldTrace(synCtx.getTracingState());
-        try {
-            if (shouldTrace) {
-                trace.trace("Start : In mediator");
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : In mediator");
+
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx);
             }
-            if (test(synCtx)) {
-                if (log.isDebugEnabled()) {
-                    log.debug("Current message is incoming.. executing child mediators");
-                }
-                return super.mediate(synCtx);
-            } else {
-                if (log.isDebugEnabled()) {
-                    log.debug("Current message is not incoming.. skipping child mediators");
-                }
-                return true;
+        }
+
+        boolean result = true;
+        if (test(synCtx)) {
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Current message is incoming - executing child mediators");
             }
-        } finally {
-            if (shouldTrace) {
-                trace.trace("End : In mediator");
+            result = super.mediate(synCtx);
+
+        } else {
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Current message is a response - skipping child mediators");
             }
         }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "End : In mediator");
+        }
+
+        return result;
     }
 
     /**

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/OutMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/OutMediator.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/OutMediator.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/OutMediator.java Wed Sep 26 06:16:59 2007
@@ -41,30 +41,35 @@
      */
     public boolean mediate(MessageContext synCtx) {
         
-        if (log.isDebugEnabled()) {
-            log.debug("Out mediator mediate()");
-        }
-        boolean shouldTrace = shouldTrace(synCtx.getTracingState());
-        try {
-            if (shouldTrace) {
-                trace.trace("Start : Out mediator");
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : Out mediator");
+
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx);
             }
-            if (test(synCtx)) {
-                if(log.isDebugEnabled()) {
-                    log.debug("Current message is outgoing.. executing child mediators");
-                }
-                return super.mediate(synCtx);
-            } else {
-                if(log.isDebugEnabled()) {
-                    log.debug("Current message is not outgoing.. skipping child mediators");
-                }
-                return true;
+        }
+
+        boolean result = true;
+        if (test(synCtx)) {
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Current message is outgoing - executing child mediators");
             }
-        } finally {
-            if (shouldTrace) {
-                trace.trace("End : Out mediator");
+            result = super.mediate(synCtx);
+
+        } else {
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Current message is a request - skipping child mediators");
             }
         }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "End : Out mediator");
+        }
+
+        return result;
     }
 
     /**

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/SwitchMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/SwitchMediator.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/SwitchMediator.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/filters/SwitchMediator.java Wed Sep 26 06:16:59 2007
@@ -52,21 +52,24 @@
      */
     public boolean mediate(MessageContext synCtx) {
 
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : Switch mediator");
+
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx);
+            }
+        }
+
         int parentsEffectiveTraceState = synCtx.getTracingState();
         // if I have been explicitly asked to enable or disable tracing, set it to the message
         // to pass it on; else, do nothing -> i.e. let the parents state flow
         setEffectiveTraceState(synCtx);
         int myEffectiveTraceState = synCtx.getTracingState();
 
-        boolean traceOn = isTraceOn(synCtx);
-        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
-
-        if (traceOn && trace.isTraceEnabled()) {
-            trace.trace("Evaluating XPath : " + source + " against message " + synCtx);
-        }
-
         String sourceText = Axis2MessageContext.getStringValue(source, synCtx);
-
         if (traceOrDebugOn) {
             traceOrDebug(traceOn, "XPath : " + source + " evaluates to : " + sourceText);
         }
@@ -87,7 +90,7 @@
                     if (swCase != null) {
                         if (swCase.matches(sourceText)) {
                             if (traceOrDebugOn) {
-                                traceOrDebug(traceOn, "Run matching case : " + swCase.getRegex());
+                                traceOrDebug(traceOn, "Matching case found : " + swCase.getRegex());
                             }
                             return swCase.mediate(synCtx);
                         }
@@ -97,18 +100,22 @@
                 if (defaultCase != null) {
                     // if any of the switch cases did not match
                     if (traceOrDebugOn) {
-                        traceOrDebug(traceOn, "None of the switch cases matched. Executing default");
+                        traceOrDebug(traceOn, "None of the switch cases matched - executing default");
                     }
                     return defaultCase.mediate(synCtx);
                 } else {
                     if (traceOrDebugOn) {
-                        traceOrDebug(traceOn, "None of the switch cases matched. No default case");
+                        traceOrDebug(traceOn, "None of the switch cases matched - no default case");
                     }
                 }
             }
 
         } finally {
             synCtx.setTracingState(parentsEffectiveTraceState);
+        }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "End : Switch mediator");
         }
         return true;
     }

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/FaultMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/FaultMediator.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/FaultMediator.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/FaultMediator.java Wed Sep 26 06:16:59 2007
@@ -44,6 +44,9 @@
  *
  * This class exposes methods to set SOAP 1.1 and 1.2 fault elements and uses
  * these as required.
+ *
+ * Directs the fault messages' "To" EPR to the "FaultTo" or the "ReplyTo" or to
+ * null of the original SOAP message
  */
 public class FaultMediator extends AbstractMediator {
 
@@ -72,40 +75,59 @@
     private String faultDetail = null;
 
     public boolean mediate(MessageContext synCtx) {
-        if (log.isDebugEnabled()) {
-            log.debug("Fault mediator mediate()");
-        }
-        boolean shouldTrace = shouldTrace(synCtx.getTracingState());
-        if(shouldTrace) {
-            trace.trace("Start : Fault mediator");
+
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : Fault mediator");
+
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx);
+            }
         }
+
         switch (soapVersion) {
             case SOAP11:
-                return makeSOAPFault(synCtx, SOAP11,shouldTrace);
+                return makeSOAPFault(synCtx, SOAP11, traceOrDebugOn, traceOn);
             case SOAP12:
-                return makeSOAPFault(synCtx, SOAP12,shouldTrace);
+                return makeSOAPFault(synCtx, SOAP12, traceOrDebugOn, traceOn);
+
             default : {
+                // determine from current message's SOAP envelope namespace
                 SOAPEnvelope envelop = synCtx.getEnvelope();
                 if (envelop != null) {
                     if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(
                         envelop.getNamespace().getNamespaceURI())) {
                         soapVersion = SOAP12;
-                        return makeSOAPFault(synCtx, SOAP12,shouldTrace);
+                        return makeSOAPFault(synCtx, SOAP12, traceOrDebugOn, traceOn);
                     } else {
                         soapVersion = SOAP11;
-                        return makeSOAPFault(synCtx, SOAP11,shouldTrace);
+                        return makeSOAPFault(synCtx, SOAP11, traceOrDebugOn, traceOn);
                     }
                 } else {
-                    return makeSOAPFault(synCtx, SOAP11,shouldTrace);
+                    // default to SOAP 11
+                    return makeSOAPFault(synCtx, SOAP11, traceOrDebugOn, traceOn);
                 }
             }
         }
     }
 
-    private boolean makeSOAPFault(MessageContext synCtx, int soapVersion,boolean shouldTrace) {
-        if (log.isDebugEnabled()) {
-            log.debug("Creating a SOAP fault using SOAP " + (soapVersion == SOAP11 ? "1.1" : "1.2"));
+    /**
+     * Actual transformation of the current message into a fault message
+     * @param synCtx the current message context
+     * @param soapVersion SOAP version of the resulting fault desired
+     * @param traceOrDebugOn is trace or debug logging on?
+     * @param traceOn is tracing on?
+     * @return true, always
+     */
+    private boolean makeSOAPFault(MessageContext synCtx, int soapVersion,
+        boolean traceOrDebugOn, boolean traceOn) {
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Creating a SOAP " + (soapVersion == SOAP11 ? "1.1" : "1.2") + "fault");
         }
+
         // get the correct SOAP factory to be used
         SOAPFactory factory = (
             soapVersion == SOAP11 ? OMAbstractFactory.getSOAP11Factory() : OMAbstractFactory.getSOAP12Factory());
@@ -143,19 +165,24 @@
                 }
             }
         }
-        if (log.isDebugEnabled()) {
-            log.debug("The fault message as : " + fault);
+
+        if (traceOrDebugOn) {
+            String msg =
+                "Original SOAP Message : " + synCtx.getEnvelope().toString() +
+                "Fault Message created : " + faultEnvelope.toString();
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace(msg);
+            }
+            if (log.isTraceEnabled()) {
+                log.trace(msg);
+            }
         }
+
         // overwrite current message envelope with new fault envelope
         try {
-            if (shouldTrace) {
-                trace.trace("Original SOAP Message : " + synCtx.getEnvelope().toString());
-                trace.trace("Fault Message created : " + faultEnvelope.toString());
-            }
             synCtx.setEnvelope(faultEnvelope);
         } catch (AxisFault af) {
-            String msg = "Error replacing SOAP envelope with a fault envelope " + af.getMessage();
-            handleException(msg,af);
+            handleException("Error replacing current SOAP envelope with the fault envelope", af, synCtx);
         }
 
         if (synCtx.getFaultTo() != null) {
@@ -166,9 +193,10 @@
             synCtx.setTo(null);
         }
 
-        if (shouldTrace) {
-            trace.trace("End : Fault mediator");
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "End : Fault mediator");
         }
+        
         return true;
     }
 
@@ -177,7 +205,7 @@
         QName fault_code = null;
 
         if (faultCodeValue == null && faultCodeExpr == null) {
-            handleException("A valid fault code QName value or expression is required");
+            handleException("A valid fault code QName value or expression is required", synCtx);
         } else if (faultCodeValue != null) {
             fault_code = faultCodeValue;
         } else {
@@ -201,7 +229,7 @@
         String reasonString = null;
 
         if (faultReasonValue == null && faultReasonExpr == null) {
-            handleException("A valid fault reason value or expression is required");
+            handleException("A valid fault reason value or expression is required", synCtx);
         } else if (faultReasonValue != null) {
             reasonString = faultReasonValue;
         } else {
@@ -246,11 +274,6 @@
         }
     }
 
-    private void handleException(String msg) {
-        log.error(msg);
-        throw new SynapseException(msg);
-    }
-
     public int getSoapVersion() {
         return soapVersion;
     }
@@ -282,16 +305,12 @@
 
             } else {
                 String msg = "Invalid Fault code value for a SOAP 1.2 fault : " + faultCodeValue;
-                handleException(msg);
+                log.error(msg);
+                throw new SynapseException(msg);
             }
         }
     }
 
-    private void handleException(String msg, Exception e) {
-        log.error(msg, e);
-        throw new SynapseException(msg, e);
-    }
-
     public AXIOMXPath getFaultCodeExpr() {
         return faultCodeExpr;
     }
@@ -322,7 +341,9 @@
 
     public void setFaultNode(URI faultNode) {
         if (soapVersion == SOAP11) {
-            handleException("A fault node does not apply to a SOAP 1.1 fault");
+            String msg = "A fault node does not apply to a SOAP 1.1 fault";
+            log.error(msg);
+            throw new SynapseException(msg);
         }
         this.faultNode = faultNode;
     }

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/HeaderMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/HeaderMediator.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/HeaderMediator.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/HeaderMediator.java Wed Sep 26 06:16:59 2007
@@ -66,30 +66,29 @@
      */
     public boolean mediate(MessageContext synCtx) {
 
-        if (log.isDebugEnabled()) {
-            log.debug("Header mediator <" + (action == ACTION_SET ? "Set" : "Remove") + "> :: mediate()");
-        }
-        boolean shouldTrace = shouldTrace(synCtx.getTracingState());
-        if (shouldTrace) {
-            trace.trace("Start : Header mediator, action = " +
-                    (action == ACTION_SET ? "set" : "remove"));
-        }
-        if (qName == null) {
-            if (log.isDebugEnabled()) {
-                log.debug("Hadear Name is null");
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : Header mediator");
+
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx);
             }
-            return true;
         }
+
         if (action == ACTION_SET) {
+
             String value = (getValue() != null ? getValue() :
                     Axis2MessageContext.getStringValue(expression, synCtx));
-            if (log.isDebugEnabled()) {
-                log.debug("Setting header : " + qName + " to : " + value);
-            }
-            if (shouldTrace) {
-                trace.trace("Set Header : " + qName + " to : " + value);
+
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Set SOAP header : " + qName + " to : " + value);
             }
+
             if (qName.getNamespaceURI() == null || "".equals(qName.getNamespaceURI())) {
+
+                // is this a "well known" Synapse header?
                 if (SynapseConstants.HEADER_TO.equals(qName.getLocalPart())) {
                     synCtx.setTo(new EndpointReference(value));
                 } else if (SynapseConstants.HEADER_FROM.equals(qName.getLocalPart())) {
@@ -108,15 +107,18 @@
             }
 
         } else {
+
             if (log.isDebugEnabled()) {
                 log.debug("Removing header : " + qName + " from current message");
             }
 
-            if (shouldTrace) {
-                trace.trace("Remove Header : " + qName);
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Removing SOAP Header : " + qName);
             }
 
             if (qName.getNamespaceURI() == null || "".equals(qName.getNamespaceURI())) {
+
+                // is this a "well known" Synapse header?
                 if (SynapseConstants.HEADER_TO.equals(qName.getLocalPart())) {
                     synCtx.setTo(null);
                 } else if (SynapseConstants.HEADER_FROM.equals(qName.getLocalPart())) {
@@ -133,23 +135,25 @@
                         SOAPHeader header = envelope.getHeader();
                         if (header != null) {
                             removeFromHeaderList(header.
-                                    getHeaderBlocksWithNSURI(""));
+                                getHeaderBlocksWithNSURI(""));
                         }
                     }
                 }
+
             } else {
                 SOAPEnvelope envelope = synCtx.getEnvelope();
                 if (envelope != null) {
                     SOAPHeader header = envelope.getHeader();
                     if (header != null) {
                         removeFromHeaderList(header.
-                                getHeaderBlocksWithNSURI(qName.getNamespaceURI()));
+                            getHeaderBlocksWithNSURI(qName.getNamespaceURI()));
                     }
                 }
             }
         }
-        if (shouldTrace) {
-            trace.trace("End : Header mediator");
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "End : Header mediator");
         }
         return true;
     }

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java?rev=579650&r1=579649&r2=579650&view=diff
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java (original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java Wed Sep 26 06:16:59 2007
@@ -31,9 +31,6 @@
 import org.apache.axiom.om.xpath.AXIOMXPath;
 import org.apache.axiom.soap.SOAP11Constants;
 import org.apache.axiom.soap.SOAP12Constants;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.synapse.SynapseConstants;
 import org.apache.synapse.MessageContext;
 import org.apache.synapse.SynapseException;
 import org.apache.synapse.core.axis2.Axis2MessageContext;
@@ -80,7 +77,7 @@
      * The feature for which deciding swiching between DOM and Stream during the
      * transformation process
      */
-    public static final String FEATURE =
+    public static final String USE_DOM_SOURCE_AND_RESULTS =
         "http://ws.apache.org/ns/synapse/transform/feature/dom";
     /**
      * The resource key/name which refers to the XSLT to be used for the transformation
@@ -126,11 +123,11 @@
     /**
      *  Is it need to use DOMSource and DOMResult?
      */
-    private boolean isDOMRequired = true;
+    private boolean useDOMSourceAndResults = true;
 
     // todo - this is a hack to get the handler module case working - ruwan
-//    public static final String DEFAULT_XPATH = "//s11:Envelope/s11:Body/child::*[position()=1] | " +
-//            "//s12:Envelope/s12:Body/child::*[position()=1]";
+    //    public static final String DEFAULT_XPATH = "//s11:Envelope/s11:Body/child::*[position()=1] | " +
+    //            "//s12:Envelope/s12:Body/child::*[position()=1]";
     public static final String DEFAULT_XPATH = "s11:Body/child::*[position()=1] | " +
             "s12:Body/child::*[position()=1]";
 
@@ -151,7 +148,9 @@
             this.source.addNamespace("s11", SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
             this.source.addNamespace("s12", SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
         } catch (JaxenException e) {
-            handleException("Error creating source XPath expression", e);
+            String msg = "Error creating default source XPath expression : " + DEFAULT_XPATH;
+            log.error(msg, e);
+            throw new SynapseException(msg, e);
         }
     }
 
@@ -163,74 +162,82 @@
      * @return true always
      */
     public boolean mediate(MessageContext synCtx) {
-        try {
-            boolean shouldTrace = shouldTrace(synCtx.getTracingState());
-            if (shouldTrace) {
-                trace.trace("Start : XSLT mediator : Using style sheet with key : " + xsltKey +
-                    (sourceXPathString == null ? "" : " against source XPath : " + sourceXPathString));
-            }
-            if (log.isDebugEnabled()) {
-                log.debug("Performing XSLT using style sheet with key : " + xsltKey  +
-                    (sourceXPathString == null ? "" : " against source XPath : " + sourceXPathString));
-            }
 
-            performXLST(synCtx, shouldTrace);
+        boolean traceOn = isTraceOn(synCtx);
+        boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
 
-            if (shouldTrace) {
-                trace.trace("End : XSLT mediator");
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Start : XSLT mediator");
+
+            if (traceOn && trace.isTraceEnabled()) {
+                trace.trace("Message : " + synCtx);
             }
+        }
+
+        try {
+            performXLST(synCtx, traceOrDebugOn, traceOn);
             return true;
 
         } catch (Exception e) {
-            handleException("Unable to perform transformation. XSLT : " + xsltKey +
-                (sourceXPathString == null ? "" : " source XPath : " + sourceXPathString), e);
+            handleException("Unable to perform XSLT transformation using : " + xsltKey +
+                " against source XPath : " +
+                (sourceXPathString == null ? DEFAULT_XPATH : " source XPath : " +
+                 sourceXPathString), e, synCtx);
+
         }
+
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "End : XSLT mediator");
+        }
+
         return false;
     }
 
-    private void performXLST(MessageContext msgCtx, boolean shouldTrace) {
+    /**
+     * Perform actual XSLT transformation
+     * @param synCtx current message
+     * @param traceOrDebugOn is trace or debug on?
+     * @param traceOn is trace on?
+     */
+    private void performXLST(MessageContext synCtx, boolean traceOrDebugOn, boolean traceOn) {
 
         boolean reCreate = false;
-        OMNode sourceNode = getTransformSource(msgCtx);
-        if (shouldTrace) {
+        OMNode sourceNode = getTransformSource(synCtx);
+        ByteArrayOutputStream baosForTarget = null;
+
+        if (traceOrDebugOn) {
             trace.trace("Transformation source : " + sourceNode.toString());
         }
-        if (log.isDebugEnabled()) {
-            log.debug("Transformation source : " + sourceNode);
-        }
 
         Source transformSrc = null;
         Result transformTgt = null;
-        ByteArrayOutputStream baosForTarget = new ByteArrayOutputStream();
 
-        if (isDOMRequired) {
-            if (log.isDebugEnabled()) {
-                log.debug("Using a DOMSource for transformation");
-            }
-            if (shouldTrace) {
-                trace.debug("Using a DOMSource for transformation");
+        if (useDOMSourceAndResults) {
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Using a DOMSource for transformation");
             }
 
             // for fast transformations create a DOMSource - ** may not work always though **
             transformSrc = new DOMSource(
-                    ((Element) ElementHelper.importOMElement((OMElement) sourceNode,
-                            DOOMAbstractFactory.getOMFactory())).getOwnerDocument());
+                ((Element) ElementHelper.importOMElement((OMElement) sourceNode,
+                DOOMAbstractFactory.getOMFactory())).getOwnerDocument());
             DocumentBuilderFactoryImpl.setDOOMRequired(true);
+
             try {
-                transformTgt = new DOMResult(DocumentBuilderFactoryImpl.newInstance().
-                    newDocumentBuilder().newDocument());
+                transformTgt = new DOMResult(
+                    DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder().newDocument());
             } catch (ParserConfigurationException e) {
-                handleException("Error creating a DOMResult ", e);
+                handleException("Error creating a DOMResult for the transformation," +
+                    " Consider setting optimization feature : " + USE_DOM_SOURCE_AND_RESULTS +
+                    " off", e, synCtx);
             }
 
         } else {
-            if (log.isDebugEnabled()) {
-                log.debug("Using byte array serialization");
-            }
-            if (shouldTrace) {
-                trace.trace("Using byte array serialization");
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Using byte array serialization for transformation");
             }
 
+            baosForTarget = new ByteArrayOutputStream();
             try {
                 // create a byte array output stream and serialize the source node into it
                 ByteArrayOutputStream baosForSource = new ByteArrayOutputStream();
@@ -245,20 +252,19 @@
                 transformTgt = new StreamResult(baosForTarget);
 
             } catch (XMLStreamException e) {
-                handleException("Error gettting transform source " + e.getMessage(), e);
+                handleException("Error creating a StreamResult for the transformation", e, synCtx);
             }
         }
 
         if (transformTgt == null) {
-            log.error("Was unable to get a transformation target created");
-            if (shouldTrace) {
-                trace.trace("Was unable to get a transformation target created");
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Was unable to get a javax.xml.transform.Result created");
             }
             return;
         }
 
         // build transformer - if necessary
-        Entry dp = msgCtx.getConfiguration().getEntryDefinition(xsltKey);
+        Entry dp = synCtx.getConfiguration().getEntryDefinition(xsltKey);
 
         // if the xsltKey refers to a dynamic resource
         if (dp != null && dp.isDynamic()) {
@@ -271,10 +277,10 @@
             if (reCreate || cachedTemplates == null) {
                 try {
                     cachedTemplates = transFact.newTemplates(
-                        Util.getStreamSource(msgCtx.getEntry(xsltKey)));
+                        Util.getStreamSource(synCtx.getEntry(xsltKey)));
 
                 } catch (TransformerConfigurationException e) {
-                    handleException("Error creating XSLT transformer using : " + xsltKey, e);
+                    handleException("Error creating XSLT transformer using : " + xsltKey, e, synCtx);
                 }
             }
         }
@@ -291,57 +297,75 @@
                             transformer.setParameter(prop.getName(), prop.getValue());
                         } else {
                             transformer.setParameter(prop.getName(),
-                                Axis2MessageContext.getStringValue(prop.getExpression(), msgCtx));
+                                Axis2MessageContext.getStringValue(prop.getExpression(), synCtx));
                         }
                     }
                 }
             }
 
             transformer.transform(transformSrc, transformTgt);
-            debugLogAndTrace("Transformation completed. Processing result", shouldTrace);
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Transformation completed - processing result");
+            }
 
             // get the result OMElement
             OMElement result = null;
             if (transformTgt instanceof DOMResult) {
                 Node node = ((DOMResult) transformTgt).getNode();
                 if (node == null) {
-                    debugLogAndTrace("Transformation result was null", shouldTrace);
+                    if (traceOrDebugOn) {
+                        traceOrDebug(traceOn, ("Transformation result (DOMResult) was null"));
+                    }
                     return;
                 }
                 Node resultNode = node.getFirstChild();
                 if (resultNode == null) {
-                    debugLogAndTrace("Transformation result was empty", shouldTrace);
+                    if (traceOrDebugOn) {
+                        traceOrDebug(traceOn, ("Transformation result (DOMResult) was empty"));
+                    }
                     return;
                 }
 
-                result = ElementHelper.importOMElement((OMElement) resultNode,
-                        OMAbstractFactory.getOMFactory());
+                result = ElementHelper.importOMElement(
+                    (OMElement) resultNode, OMAbstractFactory.getOMFactory());
 
             } else {
+
                 try {
                     StAXOMBuilder builder = new StAXOMBuilder(
                         new ByteArrayInputStream(baosForTarget.toByteArray()));
                     result = builder.getDocumentElement();
+
                 } catch (XMLStreamException e) {
-                    handleException("Error building result from XSLT transformation", e);
+                    handleException(
+                        "Error building result element from XSLT transformation", e, synCtx);
+
                 } catch (Exception e) {
-                    result = handleNonXMLResult(baosForTarget.toString(), shouldTrace);
+                    result = handleNonXMLResult(baosForTarget.toString(), traceOrDebugOn, traceOn);
                 }
             }
 
             if (result == null) {
-                debugLogAndTrace("Transformation result is null", shouldTrace);
+                if (traceOrDebugOn) {
+                    traceOrDebug(traceOn, "Transformation result was null");
+                }
                 return;
+            } else {
+                if (traceOn && trace.isTraceEnabled()) {
+                    trace.trace("Transformation result : " + result.toString());
+                }
             }
 
-            debugLogAndTrace("Transformation result : " + result.toString(), shouldTrace);
+            if (traceOrDebugOn) {
+                traceOrDebug(traceOn, "Replace source node with result");
+            }
 
             // replace the sourceNode with the result.
             sourceNode.insertSiblingAfter(result);
             sourceNode.detach();
 
         } catch (TransformerException e) {
-            handleException("Error performing XSLT transformation " + xsltKey, e);
+            handleException("Error performing XSLT transformation using : " + xsltKey, e, synCtx);
         }
     }
 
@@ -362,24 +386,14 @@
                 return (OMNode) ((List) o).get(0);  // Always fetches *only* the first
             } else {
                 handleException("The evaluation of the XPath expression "
-                        + source + " must result in an OMNode");
+                        + source + " did not result in an OMNode", synCtx);
             }
         } catch (JaxenException e) {
-            handleException("Error evaluating XPath " + source + " on message");
+            handleException("Error evaluating XPath expression : " + source, e, synCtx);
         }
         return null;
     }
 
-    private void handleException(String msg, Exception e) {
-        log.error(msg, e);
-        throw new SynapseException(msg, e);
-    }
-
-    private void handleException(String msg) {
-        log.error(msg);
-        throw new SynapseException(msg);
-    }
-
     public AXIOMXPath getSource() {
         return source;
     }
@@ -416,31 +430,41 @@
                 mp.setValue("false");
             }
             explicitFeatures.add(mp);
-            if (FEATURE.equals(featureName)) {
-                isDOMRequired = isFeatureEnable;
+            if (USE_DOM_SOURCE_AND_RESULTS.equals(featureName)) {
+                useDOMSourceAndResults = isFeatureEnable;
             } else {
                 transFact.setFeature(featureName, isFeatureEnable);
             }
         } catch (TransformerConfigurationException e) {
-            handleException("Error occured when setting features to the TransformerFactory",e);
+            String msg = "Error occured when setting features to the TransformerFactory";
+            log.error(msg, e);
+            throw new SynapseException(msg, e);
         }
     }
 
-    private OMElement handleNonXMLResult(String textPayload, boolean shouldTrace) {
+    /**
+     * If the transformation results in a non-XML payload, use standard wrapper elements
+     * to wrap the text payload so that other mediators could still process the result
+     * @param textPayload the text payload to wrap
+     * @param traceOrDebugOn is tracing on debug logging on?
+     * @param traceOn is tracing on?
+     * @return an OMElement wrapping the text payload
+     */
+    private OMElement handleNonXMLResult(String textPayload, boolean traceOrDebugOn, boolean traceOn) {
 
         OMFactory fac = OMAbstractFactory.getOMFactory();
-        QName wrapperQName = null;
         OMElement wrapper = null;
 
-        debugLogAndTrace("Processing non SOAP/XML transformation result", shouldTrace);
+        if (traceOrDebugOn) {
+            traceOrDebug(traceOn, "Processing non SOAP/XML (text) transformation result");
+        }
+        if (traceOn && trace.isTraceEnabled()) {
+            trace.trace("Wrapping text transformation result : " + textPayload);
+        }
 
         if (textPayload != null) {
             OMTextImpl textData = (OMTextImpl) fac.createOMText(textPayload);
-
-            if (wrapperQName == null) {
-                wrapperQName = BaseConstants.DEFAULT_TEXT_WRAPPER;
-            }
-            wrapper = fac.createOMElement(wrapperQName, null);
+            wrapper = fac.createOMElement(BaseConstants.DEFAULT_TEXT_WRAPPER, null);
             wrapper.addChild(textData);
         }
 
@@ -465,15 +489,6 @@
 
     public void setSourceXPathString(String sourceXPathString) {
         this.sourceXPathString = sourceXPathString;
-    }
-
-    private void debugLogAndTrace(String msg, boolean shouldTrace) {
-        if (log.isDebugEnabled()) {
-            log.debug(msg);
-        }
-        if (shouldTrace) {
-            trace.trace(msg);
-        }
     }
 }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: synapse-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: synapse-dev-help@ws.apache.org