You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by ve...@apache.org on 2008/06/27 19:33:38 UTC

svn commit: r672337 - in /synapse/trunk/java/modules/core/src/main/java/org/apache/synapse: config/xml/ mediators/ mediators/transform/

Author: veithen
Date: Fri Jun 27 10:33:38 2008
New Revision: 672337

URL: http://svn.apache.org/viewvc?rev=672337&view=rev
Log:
XSLTMediator:
* Allow attributes to be set on the TransformerFactory similarly to what we have for features. This is for completeness and will also be useful for SYNAPSE-378.
* Eliminated some duplicate code along the way.

Modified:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorFactory.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorSerializer.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorPropertyFactory.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ValidateMediatorFactory.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XSLTMediatorFactory.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XSLTMediatorSerializer.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/MediatorProperty.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorFactory.java?rev=672337&r1=672336&r2=672337&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorFactory.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorFactory.java Fri Jun 27 10:33:38 2008
@@ -19,6 +19,10 @@
 
 package org.apache.synapse.config.xml;
 
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
 import org.apache.synapse.Mediator;
 import org.apache.synapse.SynapseException;
 import org.apache.axiom.om.OMElement;
@@ -84,6 +88,35 @@
         }
     }
 
+    /**
+     * Collect the <tt>name</tt> and <tt>value</tt> attributes from the children
+     * with a given QName.
+     *  
+     * @return
+     */
+    protected Map<String,String> collectNameValuePairs(OMElement elem, QName childElementName) {
+        Map<String,String> result = new LinkedHashMap<String,String>();
+        for (Iterator it = elem.getChildrenWithName(childElementName); it.hasNext(); ) {
+            OMElement child = (OMElement)it.next();
+            OMAttribute attName = child.getAttribute(ATT_NAME);
+            OMAttribute attValue = child.getAttribute(ATT_VALUE);
+            if (attName != null && attValue != null) {
+                String name = attName.getAttributeValue().trim();
+                String value = attValue.getAttributeValue().trim();
+                if (result.containsKey(attName)) {
+                    handleException("Duplicate " + childElementName.getLocalPart()
+                            + " with name " + name);
+                } else {
+                    result.put(name, value);
+                }
+            } else {
+                handleException("Both of the name and value attributes are required for a "
+                        + childElementName.getLocalPart());
+            }
+        }
+        return result;
+    }
+
     protected void handleException(String message, Exception e) {
         LogFactory.getLog(this.getClass()).error(message, e);
         throw new SynapseException(message, e);

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorSerializer.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorSerializer.java?rev=672337&r1=672336&r2=672337&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorSerializer.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/AbstractMediatorSerializer.java Fri Jun 27 10:33:38 2008
@@ -79,10 +79,11 @@
     }
 
     protected void serializeMediatorProperties(OMElement parent,
-        Collection<MediatorProperty> props) {
+                                               Collection<MediatorProperty> props,
+                                               QName childElementName) {
 
         for (MediatorProperty mp : props) {
-            OMElement prop = fac.createOMElement("property", synNS, parent);
+            OMElement prop = fac.createOMElement(childElementName, parent);
             if (mp.getName() != null) {
                 prop.addAttribute(fac.createOMAttribute("name", nullNS, mp.getName()));
             } else {
@@ -100,6 +101,12 @@
             }
         }
     }
+    
+    protected void serializeMediatorProperties(OMElement parent,
+            Collection<MediatorProperty> props) {
+        
+        serializeMediatorProperties(parent, props, PROP_Q);
+    }
 
     protected void serializeProperties(OMElement parent, Collection<MediatorProperty> props) {
         serializeMediatorProperties(parent, props);

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorPropertyFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorPropertyFactory.java?rev=672337&r1=672336&r2=672337&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorPropertyFactory.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorPropertyFactory.java Fri Jun 27 10:33:38 2008
@@ -27,7 +27,6 @@
 import org.apache.synapse.mediators.MediatorProperty;
 import org.jaxen.JaxenException;
 
-import javax.xml.namespace.QName;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -50,8 +49,7 @@
 
         List<MediatorProperty> propertyList = new ArrayList<MediatorProperty>();
 
-        Iterator iter = elem.getChildrenWithName(
-            new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "property"));
+        Iterator iter = elem.getChildrenWithName(MediatorProperty.PROPERTY_Q);
 
         while (iter.hasNext()) {
 

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ValidateMediatorFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ValidateMediatorFactory.java?rev=672337&r1=672336&r2=672337&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ValidateMediatorFactory.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/ValidateMediatorFactory.java Fri Jun 27 10:33:38 2008
@@ -30,6 +30,7 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 /**
  * Factory for {@link ValidateMediator} instances.
@@ -109,31 +110,22 @@
         // set its common attributes such as tracing etc
         processTraceState(validateMediator,elem);
         // set the features
-        Iterator iter = elem.getChildrenWithName(FEATURE_Q);
-        while (iter.hasNext()) {
-            OMElement featureElem = (OMElement) iter.next();
-            OMAttribute attName = featureElem.getAttribute(ATT_NAME);
-            OMAttribute attValue = featureElem.getAttribute(ATT_VALUE);
-            if (attName != null && attValue != null) {
-                String name = attName.getAttributeValue();
-                String value = attValue.getAttributeValue();
-                if (name != null && value != null) {
-                    try {
-                        if ("true".equals(value.trim())) {
-                            validateMediator.addFeature(name.trim(), true);
-                        } else if ("false".equals(value.trim())) {
-                            validateMediator.addFeature(name.trim(), false);
-                        } else {
-                            handleException("The feature must have value true or false");
-                        }
-                    } catch (SAXException e) {
-                        handleException("Error setting validation feature : " + name + " to : " + value, e);
-                    }
-                } else {
-                    handleException("The valid values for both of the name and value are need");
-                }
+        for (Map.Entry<String,String> entry : collectNameValuePairs(elem, FEATURE_Q).entrySet()) {
+            String value = entry.getValue();
+            boolean isFeatureEnabled;
+            if ("true".equals(value)) {
+                isFeatureEnabled = true;
+            } else if ("false".equals(value)) {
+                isFeatureEnabled = false;
             } else {
-                handleException("Both of the name and value attribute are required for a feature");
+                handleException("The feature must have value true or false");
+                break;
+            }
+            try {
+                validateMediator.addFeature(entry.getKey(), isFeatureEnabled);
+            } catch (SAXException e) {
+                handleException("Error setting validation feature : " + entry.getKey()
+                        + " to : " + value, e);
             }
         }
         return validateMediator;

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XSLTMediatorFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XSLTMediatorFactory.java?rev=672337&r1=672336&r2=672337&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XSLTMediatorFactory.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XSLTMediatorFactory.java Fri Jun 27 10:33:38 2008
@@ -26,7 +26,7 @@
 import org.jaxen.JaxenException;
 
 import javax.xml.namespace.QName;
-import java.util.Iterator;
+import java.util.Map;
 
 /**
  * Factory for {@link XSLTMediator} instances.
@@ -41,7 +41,10 @@
  */
 public class XSLTMediatorFactory extends AbstractMediatorFactory {
 
-    private static final QName TAG_NAME    = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "xslt");
+    private static final QName TAG_NAME
+                = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "xslt");
+    private static final QName ATTRIBUTE_Q
+                = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "attribute");
 
     public QName getTagQName() {
         return TAG_NAME;
@@ -80,30 +83,21 @@
         // set its common attributes such as tracing etc
         processTraceState(transformMediator, elem);
         // set the features 
-        Iterator iter = elem.getChildrenWithName(FEATURE_Q);
-        while (iter.hasNext()) {
-            OMElement featureElem = (OMElement) iter.next();
-            OMAttribute attName = featureElem.getAttribute(ATT_NAME);
-            OMAttribute attValue = featureElem.getAttribute(ATT_VALUE);
-            if (attName != null && attValue != null) {
-                String name = attName.getAttributeValue();
-                String value = attValue.getAttributeValue();
-                if (name != null && value != null) {
-                    if ("true".equals(value.trim())) {
-                        transformMediator.addFeature(name.trim(),
-                                true);
-                    } else if ("false".equals(value.trim())) {
-                        transformMediator.addFeature(name.trim(),
-                                false);
-                    } else {
-                        handleException("The feature must have value true or false");
-                    }
-                } else {
-                    handleException("The valid values for both of the name and value are need");
-                }
+        for (Map.Entry<String,String> entry : collectNameValuePairs(elem, FEATURE_Q).entrySet()) {
+            String value = entry.getValue();
+            boolean isFeatureEnabled;
+            if ("true".equals(value)) {
+                isFeatureEnabled = true;
+            } else if ("false".equals(value)) {
+                isFeatureEnabled = false;
             } else {
-                handleException("Both of the name and value attribute are required for a feature");
+                handleException("The feature must have value true or false");
+                break;
             }
+            transformMediator.addFeature(entry.getKey(), isFeatureEnabled);
+        }
+        for (Map.Entry<String,String> entry : collectNameValuePairs(elem, ATTRIBUTE_Q).entrySet()) {
+            transformMediator.addAttribute(entry.getKey(), entry.getValue());
         }
         transformMediator.addAllProperties(
             MediatorPropertyFactory.getMediatorProperties(elem));

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XSLTMediatorSerializer.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XSLTMediatorSerializer.java?rev=672337&r1=672336&r2=672337&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XSLTMediatorSerializer.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/XSLTMediatorSerializer.java Fri Jun 27 10:33:38 2008
@@ -27,12 +27,16 @@
 
 import java.util.List;
 
+import javax.xml.namespace.QName;
+
 /**
  * Serializer for {@link XSLTMediator} instances.
  * 
  * @see XSLTMediatorFactory
  */
 public class XSLTMediatorSerializer extends AbstractMediatorSerializer {
+    private static final QName ATTRIBUTE_Q
+                = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "attribute");
 
     public OMElement serializeMediator(OMElement parent, Mediator m) {
 
@@ -76,6 +80,7 @@
                 }
             }
         }
+        serializeMediatorProperties(xslt, mediator.getAttributes(), ATTRIBUTE_Q);
         if (parent != null) {
             parent.addChild(xslt);
         }

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/MediatorProperty.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/MediatorProperty.java?rev=672337&r1=672336&r2=672337&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/MediatorProperty.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/MediatorProperty.java Fri Jun 27 10:33:38 2008
@@ -32,6 +32,8 @@
  */
 public class MediatorProperty {
 
+    // TODO: these constants are related to a specific configuration language
+    //       and should be moved to a class in the related package
     public static final QName PROPERTY_Q  = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "property");
     public static final QName ATT_NAME_Q  = new QName(XMLConfigConstants.NULL_NAMESPACE, "name");
     public static final QName ATT_VALUE_Q = new QName(XMLConfigConstants.NULL_NAMESPACE, "value");

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java?rev=672337&r1=672336&r2=672337&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java Fri Jun 27 10:33:38 2008
@@ -129,9 +129,15 @@
     private List<MediatorProperty> properties = new ArrayList<MediatorProperty>();
 
     /**
-     * Any features which should be set to the TransformerFactory by explicitly
+     * Any features which should be set to the TransformerFactory explicitly
      */
-    private List<MediatorProperty> explicitFeatures = new ArrayList<MediatorProperty>();
+    private List<MediatorProperty> transformerFactoryFeatures = new ArrayList<MediatorProperty>();
+
+    /**
+     * Any attributes which should be set to the TransformerFactory explicitly
+     */
+    private List<MediatorProperty> transformerFactoryAttributes
+                = new ArrayList<MediatorProperty>();
 
     /**
      * The Template instance used to create a Transformer object. This is  thread-safe
@@ -423,11 +429,16 @@
     }
     
     /**
-     * to add a feature which need to set to the TransformerFactory
-     * @param  featureName The name of the feature
-     * @param isFeatureEnable should this feature enable?
+     * Add a feature to be set on the {@link TransformerFactory} used by this mediator instance.
+     * This method can also be used to enable some Synapse specific optimizations and
+     * enhancements as described in the documentation of this class.
+     * 
+     * @param featureName The name of the feature
+     * @param isFeatureEnable the desired state of the feature
+     * 
+     * @see TransformerFactory#setFeature(String, boolean)
+     * @see XSLTMediator
      */
-    
     public void addFeature(String featureName, boolean isFeatureEnable) {
         try {
             MediatorProperty mp = new MediatorProperty();
@@ -437,7 +448,7 @@
             } else {
                 mp.setValue("false");
             }
-            explicitFeatures.add(mp);
+            transformerFactoryFeatures.add(mp);
             if (USE_DOM_SOURCE_AND_RESULTS.equals(featureName)) {
                 useDOMSourceAndResults = isFeatureEnable;
             } else {
@@ -451,6 +462,31 @@
     }
 
     /**
+     * Add an attribute to be set on the {@link TransformerFactory} used by this mediator instance.
+     * This method can also be used to enable some Synapse specific optimizations and
+     * enhancements as described in the documentation of this class.
+     * 
+     * @param name The name of the feature
+     * @param value should this feature enable?
+     * 
+     * @see TransformerFactory#setAttribute(String, Object)
+     * @see XSLTMediator
+     */
+    public void addAttribute(String name, String value) {
+        MediatorProperty mp = new MediatorProperty();
+        mp.setName(name);
+        mp.setValue(value);
+        transformerFactoryAttributes.add(mp);
+        try {
+            transFact.setAttribute(name, value);
+        } catch (IllegalArgumentException e) {
+            String msg = "Error occured when setting attribute to the TransformerFactory";
+            log.error(msg, e);
+            throw new SynapseException(msg, e);
+        }
+    }
+
+    /**
      * 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 tempData the encoded text payload
@@ -468,11 +504,17 @@
     }
 
     /**
-     *
-     * @return Returns the features explicitly  set to the TransformerFactory through this mediator
+     * @return Return the features explicitly set to the TransformerFactory through this mediator.
      */
     public List<MediatorProperty> getFeatures(){
-        return explicitFeatures;
+        return transformerFactoryFeatures;
+    }
+
+    /**
+     * @return Return the attributes explicitly set to the TransformerFactory through this mediator.
+     */
+    public List<MediatorProperty> getAttributes(){
+        return transformerFactoryAttributes;
     }
 
     public void addAllProperties(List<MediatorProperty> list) {