Author: supun
Date: Tue Jan 18 11:22:31 2011
New Revision: 1060312

URL: http://svn.apache.org/viewvc?rev=1060312&view=rev
Log:
addplying patch SYNAPSE-716 and adding mediator property serialization and 
creation with scope

Added:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorFactory.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorSerializer.java
Modified:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.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/MediatorPropertySerializer.java

Added: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorFactory.java?rev=1060312&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorFactory.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorFactory.java
 Tue Jan 18 11:22:31 2011
@@ -0,0 +1,185 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.synapse.config.xml;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.synapse.Mediator;
+import org.jaxen.JaxenException;
+
+import javax.xml.namespace.QName;
+
+import org.apache.synapse.mediators.elementary.EnrichMediator;
+import org.apache.synapse.mediators.elementary.Source;
+import org.apache.synapse.mediators.elementary.Target;
+
+import java.util.Properties;
+
+
+public class EnrichMediatorFactory extends AbstractMediatorFactory {
+    private static final QName XML_Q = new 
QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "enrich");
+    private static final QName ATT_PROPERTY = new QName("property");
+    private static final QName ATT_XPATH = new QName("xpath");
+    private static final QName ATT_TYPE = new QName("type");
+    private static final QName ATT_CLONE = new QName("clone");
+    private static final QName ATT_ACTION = new QName("action");
+
+    public static final QName SOURCE_Q = new 
QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "source");
+    public static final QName TARGET_Q = new 
QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "target");
+
+    public static final String CUSTOM = "custom";
+    public static final String PROPERTY = "property";
+    public static final String ENVELOPE = "envelope";
+    public static final String BODY = "body";
+    public static final String INLINE = "inline";
+
+    @Override
+    protected Mediator createSpecificMediator(OMElement elem, Properties 
properties) {
+        if (!XML_Q.equals(elem.getQName())) {
+            handleException("Unable to create the enrich mediator. " +
+                    "Unexpected element as the enrich mediator configuration");
+        }
+
+        EnrichMediator enrich = new EnrichMediator();
+
+        OMElement sourceEle = elem.getFirstChildWithName(SOURCE_Q);
+        if (sourceEle == null) {
+            handleException("source element is mandatory");
+        }
+        Source source = new Source();
+        enrich.setSource(source);
+
+        OMElement targetEle = elem.getFirstChildWithName(TARGET_Q);
+        if (targetEle == null) {
+            handleException("target element is mandatory");
+        }
+        Target target = new Target();
+        enrich.setTarget(target);
+
+        populateSource(source, sourceEle);
+        populateTarget(target, targetEle);
+
+        return enrich;
+    }
+
+    private void populateSource(Source source, OMElement sourceEle) {
+
+        // type attribue
+        OMAttribute typeAttr = sourceEle.getAttribute(ATT_TYPE);
+        if (typeAttr != null && typeAttr.getAttributeValue() != null) {
+            
source.setSourceType(convertTypeToInit(typeAttr.getAttributeValue()));
+        }
+
+        OMAttribute cloneAttr = sourceEle.getAttribute(ATT_CLONE);
+        if (cloneAttr != null && cloneAttr.getAttributeValue() != null) {
+            
source.setClone(Boolean.parseBoolean(cloneAttr.getAttributeValue()));
+        }
+
+        if (source.getSourceType() == EnrichMediator.CUSTOM) {
+            OMAttribute xpathAttr = sourceEle.getAttribute(ATT_XPATH);
+            if (xpathAttr != null && xpathAttr.getAttributeValue() != null) {
+                try {
+                    
source.setXpath(SynapseXPathFactory.getSynapseXPath(sourceEle, ATT_XPATH));
+                } catch (JaxenException e) {
+                    handleException("Invalid XPath expression: " + xpathAttr);
+                }
+            } else {
+                handleException("xpath attribute is required for CUSTOM type");
+            }
+        } else if (source.getSourceType() == EnrichMediator.PROPERTY) {
+            OMAttribute propertyAttr = sourceEle.getAttribute(ATT_PROPERTY);
+            if (propertyAttr != null && propertyAttr.getAttributeValue() != 
null) {
+                source.setProperty(propertyAttr.getAttributeValue());
+            } else {
+                handleException("xpath attribute is required for CUSTOM type");
+            }
+        } else if (source.getSourceType() == EnrichMediator.INLINE) {
+            OMElement ele = null;
+            if (sourceEle.getFirstElement() != null) {
+                ele = sourceEle.getFirstElement().cloneOMElement();
+            }
+
+            if (ele != null) {
+                source.setInlineElement(ele);
+            } else if (sourceEle.getAttributeValue(ATT_KEY) != null) {
+                source.setInlineKey(sourceEle.getAttributeValue(ATT_KEY));
+            } else {
+                handleException("XML element is required for INLINE type");
+            }
+        }
+    }
+
+    private void populateTarget(Target target, OMElement sourceEle) {
+        // type attribue
+        OMAttribute typeAttr = sourceEle.getAttribute(ATT_TYPE);
+        if (typeAttr != null && typeAttr.getAttributeValue() != null) {
+            int type = convertTypeToInit(typeAttr.getAttributeValue());
+            if (type >= 0) {
+                target.setTargetType(type);
+            } else {
+                handleException("Un-expected type : " + 
typeAttr.getAttributeValue());
+            }
+        }
+
+        OMAttribute actionAttr = sourceEle.getAttribute(ATT_ACTION);
+        if (actionAttr != null && actionAttr.getAttributeValue() != null) {
+            target.setAction(actionAttr.getAttributeValue());
+        }
+
+        if (target.getTargetType() == EnrichMediator.CUSTOM) {
+            OMAttribute xpathAttr = sourceEle.getAttribute(ATT_XPATH);
+            if (xpathAttr != null && xpathAttr.getAttributeValue() != null) {
+                try {
+                    
target.setXpath(SynapseXPathFactory.getSynapseXPath(sourceEle, ATT_XPATH));
+                } catch (JaxenException e) {
+                    handleException("Invalid XPath expression: " + xpathAttr);
+                }
+            } else {
+                handleException("xpath attribute is required for CUSTOM type");
+            }
+        } else if (target.getTargetType() == EnrichMediator.PROPERTY) {
+            OMAttribute propertyAttr = sourceEle.getAttribute(ATT_PROPERTY);
+            if (propertyAttr != null && propertyAttr.getAttributeValue() != 
null) {
+                target.setProperty(propertyAttr.getAttributeValue());
+            } else {
+                handleException("xpath attribute is required for CUSTOM type");
+            }
+        }
+    }
+
+    private int convertTypeToInit(String type) {
+        if (type.equals(ENVELOPE)) {
+            return EnrichMediator.ENVELOPE;
+        } else if (type.equals(BODY)) {
+            return EnrichMediator.BODY;
+        } else if (type.equals(PROPERTY)) {
+            return EnrichMediator.PROPERTY;
+        } else if (type.equals(CUSTOM)) {
+            return EnrichMediator.CUSTOM;
+        } else if (type.equals(INLINE)) {
+            return EnrichMediator.INLINE;
+        }
+        return -1;
+    }
+
+    public QName getTagQName() {
+        return XML_Q;
+    }
+
+}

Added: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorSerializer.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorSerializer.java?rev=1060312&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorSerializer.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/EnrichMediatorSerializer.java
 Tue Jan 18 11:22:31 2011
@@ -0,0 +1,118 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.synapse.config.xml;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.synapse.Mediator;
+
+
+import org.apache.synapse.mediators.elementary.EnrichMediator;
+import org.apache.synapse.mediators.elementary.Source;
+import org.apache.synapse.mediators.elementary.Target;
+
+
+public class EnrichMediatorSerializer extends AbstractMediatorSerializer {
+
+    @Override
+    protected OMElement serializeSpecificMediator(Mediator m) {
+        assert m != null : "mediator cannot be null";
+        assert m instanceof EnrichMediator : "mediator should be of type 
EnrichMediator";
+
+        EnrichMediator mediator = (EnrichMediator) m;
+
+        OMElement enrichEle = fac.createOMElement("enrich", synNS);
+
+        OMElement sourceEle = serializeSource(mediator.getSource());
+        OMElement targetEle = serializeTarget(mediator.getTarget());
+
+        enrichEle.addChild(sourceEle);
+        enrichEle.addChild(targetEle);
+
+        return enrichEle;
+    }
+
+    private OMElement serializeSource(Source source) {
+        OMElement sourceEle = fac.createOMElement("source", synNS);
+
+        if (source.getSourceType() != EnrichMediator.CUSTOM) {
+            sourceEle.addAttribute(fac.createOMAttribute("type", nullNS,
+                    intTypeToString(source.getSourceType())));
+        }
+
+        if (source.isClone()) {
+            sourceEle.addAttribute(fac.createOMAttribute("clone", nullNS,
+                    Boolean.toString(source.isClone())));
+        }
+
+        if (source.getSourceType() == EnrichMediator.PROPERTY) {
+            sourceEle.addAttribute(fac.createOMAttribute("property", nullNS, 
source.getProperty()));
+        } else if (source.getSourceType() == EnrichMediator.CUSTOM) {
+            SynapseXPathSerializer.serializeXPath(source.getXpath(), 
sourceEle, "xpath");
+        } else if (source.getSourceType() == EnrichMediator.INLINE) {
+            if (source.getInlineElement() != null) {
+                sourceEle.addChild(source.getInlineElement().cloneOMElement());
+            } else if (source.getInlineKey() != null) {
+                sourceEle.addAttribute("key", source.getInlineKey(), null);
+            }
+        }
+        return sourceEle;
+    }
+
+    private OMElement serializeTarget(Target target) {
+        OMElement targetEle = fac.createOMElement("target", synNS);
+
+        if (target.getTargetType() != EnrichMediator.CUSTOM) {
+            targetEle.addAttribute(fac.createOMAttribute("type", nullNS,
+                    intTypeToString(target.getTargetType())));
+        }
+
+        if (!target.getAction().equals(Target.ACTION_REPLACE)) {
+            targetEle.addAttribute(fac.createOMAttribute("action", nullNS,
+                    target.getAction()));
+        }
+
+        if (target.getTargetType() == EnrichMediator.PROPERTY) {
+            targetEle.addAttribute(fac.createOMAttribute("property", nullNS, 
target.getProperty()));
+        } else if (target.getTargetType() == EnrichMediator.CUSTOM) {
+            SynapseXPathSerializer.serializeXPath(target.getXpath(), 
targetEle, "xpath");
+        }
+
+        return targetEle;
+    }
+
+
+    private String intTypeToString(int type) {
+        if (type == EnrichMediator.CUSTOM) {
+            return EnrichMediatorFactory.CUSTOM;
+        } else if (type == EnrichMediator.BODY) {
+            return EnrichMediatorFactory.BODY;
+        } else if (type == EnrichMediator.ENVELOPE) {
+            return EnrichMediatorFactory.ENVELOPE;
+        } else if (type == EnrichMediator.PROPERTY) {
+            return EnrichMediatorFactory.PROPERTY;
+        } else if (type == EnrichMediator.INLINE) {
+            return EnrichMediatorFactory.INLINE;
+        }
+        return null;
+    }
+
+    public String getMediatorClassName() {
+        return EnrichMediator.class.getName();
+    }
+}

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java?rev=1060312&r1=1060311&r2=1060312&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorFactoryFinder.java
 Tue Jan 18 11:22:31 2011
@@ -78,7 +78,8 @@ public class MediatorFactoryFinder imple
         EnqueueMediatorFactory.class,
         ConditionalRouterMediatorFactory.class,
         SamplingThrottleMediatorFactory.class,
-        URLRewriteMediatorFactory.class
+        URLRewriteMediatorFactory.class,
+        EnrichMediatorFactory.class    
     };
 
     private final static MediatorFactoryFinder instance  = new 
MediatorFactoryFinder();

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=1060312&r1=1060311&r2=1060312&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
 Tue Jan 18 11:22:31 2011
@@ -57,6 +57,7 @@ public class MediatorPropertyFactory {
             OMAttribute attName  = 
propEle.getAttribute(MediatorProperty.ATT_NAME_Q);
             OMAttribute attValue = 
propEle.getAttribute(MediatorProperty.ATT_VALUE_Q);
             OMAttribute attExpr  = 
propEle.getAttribute(MediatorProperty.ATT_EXPR_Q);
+            OMAttribute attScope = 
propEle.getAttribute(MediatorProperty.ATT_SCOPE_Q);
 
             MediatorProperty prop = new MediatorProperty();
 
@@ -113,6 +114,23 @@ public class MediatorPropertyFactory {
                 throw new SynapseException(msg);
             }
 
+            if (attScope != null) {
+                String valueStr = attScope.getAttributeValue();
+                if (!XMLConfigConstants.SCOPE_AXIS2.equals(valueStr) &&
+                        !XMLConfigConstants.SCOPE_TRANSPORT.equals(valueStr) &&
+                        !XMLConfigConstants.SCOPE_DEFAULT.equals(valueStr) &&
+                        !XMLConfigConstants.SCOPE_CLIENT.equals(valueStr)) {
+                    String msg = "Only '" + XMLConfigConstants.SCOPE_AXIS2 + 
"' or '" +
+                            XMLConfigConstants.SCOPE_TRANSPORT + "' or '" +
+                            XMLConfigConstants.SCOPE_CLIENT +
+                            "' values are allowed for attribute scope for a 
property" +
+                            ", Unsupported scope " + valueStr;
+                    log.error(msg);
+                    throw new SynapseException(msg);
+                }
+                prop.setScope(valueStr);
+            }
+
             propertyList.add(prop);
         }
 

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorPropertySerializer.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorPropertySerializer.java?rev=1060312&r1=1060311&r2=1060312&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorPropertySerializer.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorPropertySerializer.java
 Tue Jan 18 11:22:31 2011
@@ -4,6 +4,7 @@ import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMFactory;
 import org.apache.axiom.om.OMAbstractFactory;
 import org.apache.axiom.om.OMNamespace;
+import org.apache.synapse.Mediator;
 import org.apache.synapse.mediators.MediatorProperty;
 import org.apache.synapse.SynapseConstants;
 import org.apache.synapse.SynapseException;
@@ -107,5 +108,8 @@ public class MediatorPropertySerializer 
             throw new SynapseException(msg);
         }
 
+        if (mp.getScope() != null && 
!XMLConfigConstants.SCOPE_DEFAULT.equals(mp.getScope())) {
+            prop.addAttribute(fac.createOMAttribute("scope", nullNS, 
mp.getScope()));
+        }
     }
 }


Reply via email to