Author: supun
Date: Wed Jan 19 16:41:05 2011
New Revision: 1060858

URL: http://svn.apache.org/viewvc?rev=1060858&view=rev
Log:
adding dynamic registry key support

Added:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/KeyFactory.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/KeySerializer.java
Modified:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MediatorPropertySerializer.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.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

Added: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/KeyFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/KeyFactory.java?rev=1060858&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/KeyFactory.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/KeyFactory.java
 Wed Jan 19 16:41:05 2011
@@ -0,0 +1,126 @@
+/*
+ *  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.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.mediators.Key;
+import org.apache.synapse.util.xpath.SynapseXPath;
+import org.jaxen.JaxenException;
+
+import javax.xml.namespace.QName;
+
+/**
+ * Factory for {@link org.apache.synapse.mediators.Key} instances.
+ */
+public class KeyFactory {
+
+    private static final Log log = LogFactory.getLog(KeyFactory.class);
+
+    private static final QName ATT_KEY = new QName("key");
+
+    /**
+     * Create a key instance
+     *
+     * @param elem OMElement
+     * @return Key
+     */
+    public Key createKey(OMElement elem) {
+
+        Key key = null;
+        OMAttribute attXslt = elem.getAttribute(ATT_KEY);
+
+        if (attXslt != null) {
+            String attributeValue = attXslt.getAttributeValue();
+            if (isDynamicKey(attributeValue)) {
+                /** dynamic key */
+                SynapseXPath synXpath = createSynXpath(elem, attributeValue);
+                key = new Key(synXpath);
+            } else {
+                /** static key */
+                key = new Key(attributeValue);
+            }
+        } else {
+            handleException("The 'key' attribute is required for the XSLT 
mediator");
+        }
+        return key;
+    }
+
+
+    /**
+     * Validate the given key to identify whether it is static or dynamic key
+     * If the key is in the {} format then it is dynamic key(XPath)
+     * Otherwise just a static key
+     *
+     * @param keyValue string to validate as a key
+     * @return isDynamicKey representing key type
+     */
+    public boolean isDynamicKey(String keyValue) {
+        boolean dynamicKey = false;
+
+        final char startExpression = '{';
+        final char endExpression = '}';
+
+        char firstChar = keyValue.charAt(0);
+        char lastChar = keyValue.charAt(keyValue.length() - 1);
+
+        if (startExpression == firstChar && endExpression == lastChar) {
+            dynamicKey = true;
+        }
+        return dynamicKey;
+    }
+
+    /**
+     * Create synapse xpath expression
+     * {} type user input is used to create real xpath expression
+     *
+     * @param elem the element
+     * @param key xpath expression with {}
+     * @return SynapseXpath
+     */
+    public SynapseXPath createSynXpath(OMElement elem, String key) {
+        //derive XPath Expression from key
+        String xpathExpr = key.trim().substring(1, key.length() - 1);
+
+        SynapseXPath synapseXPath = null;
+
+        try {
+            synapseXPath = SynapseXPathFactory.getSynapseXPath(elem, 
xpathExpr);
+        } catch (JaxenException e) {
+            handleException("Can not create Synapse Xpath from given key");
+        }
+
+        return synapseXPath;
+    }
+
+    /**
+     * Handle exceptions
+     *
+     * @param msg error message
+     */
+    private void handleException(String msg) {
+        log.error(msg);
+        throw new SynapseException(msg);
+    }
+
+}

Added: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/KeySerializer.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/KeySerializer.java?rev=1060858&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/KeySerializer.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/KeySerializer.java
 Wed Jan 19 16:41:05 2011
@@ -0,0 +1,57 @@
+/*
+ *  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.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.synapse.mediators.Key;
+
+/**
+ * Serializer for {@link org.apache.synapse.mediators.Key} instances.
+ */
+public class KeySerializer {
+    protected static final OMFactory fac = OMAbstractFactory.getOMFactory();
+    protected static final OMNamespace nullNS
+            = fac.createOMNamespace(XMLConfigConstants.NULL_NAMESPACE, "");
+
+    /**
+     * Serialize the Key object to an OMElement representing the entry
+     *
+     * @param key  Key to serialize
+     * @param elem OMElement
+     * @return OMElement
+     */
+    public OMElement serializeKey(Key key, OMElement elem) {
+        if (key != null) {
+            if (key.getExpression() == null) {
+                //static key
+                elem.addAttribute(fac.createOMAttribute("key", nullNS, 
key.getKeyValue()));
+            } else {
+                //dynamic key
+                SynapseXPathSerializer.serializeXPath(key.getExpression(), "{" 
+
+                        key.getExpression().toString() + "}", elem, "key");
+            }
+        }
+        return elem;
+    }
+
+}

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=1060858&r1=1060857&r2=1060858&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
 Wed Jan 19 16:41:05 2011
@@ -4,7 +4,6 @@ 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;

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java?rev=1060858&r1=1060857&r2=1060858&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathFactory.java
 Wed Jan 19 16:41:05 2011
@@ -55,6 +55,19 @@ public class SynapseXPathFactory {
         return xpath;
     }
 
+    public static SynapseXPath getSynapseXPath(OMElement elem, String 
expression)
+        throws JaxenException {
+
+        if (expression == null) {
+            handleException("XPath expression cannot be null");
+        }
+
+        SynapseXPath xpath = new SynapseXPath(expression);
+        OMElementUtils.addNameSpaces(xpath, elem, log);
+
+        return xpath;
+    }
+
     private static void handleException(String message) {
         log.error(message);
         throw new SynapseException(message);

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.java?rev=1060858&r1=1060857&r2=1060858&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/SynapseXPathSerializer.java
 Wed Jan 19 16:41:05 2011
@@ -52,6 +52,26 @@ public class SynapseXPathSerializer {
         return elem;
     }
 
+    public static OMElement serializeXPath(SynapseXPath xpath, String 
expression,
+                                           OMElement elem, String attribName) {
+
+        OMNamespace nullNS = elem.getOMFactory()
+            .createOMNamespace(XMLConfigConstants.NULL_NAMESPACE, "");
+
+        if (xpath != null && expression != null) {
+
+            elem.addAttribute(elem.getOMFactory().createOMAttribute(
+                attribName, nullNS, expression));
+
+            serializeNamespaces(elem, xpath);
+
+        } else {
+            handleException("Couldn't find the xpath in the SynapseXPath");
+        }
+
+        return elem;
+    }
+
     private static void serializeNamespaces(OMElement elem, SynapseXPath 
xpath) {
 
         for (Object o : xpath.getNamespaces().keySet()) {

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=1060858&r1=1060857&r2=1060858&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
 Wed Jan 19 16:41:05 2011
@@ -23,6 +23,7 @@ import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMElement;
 import org.apache.synapse.Mediator;
 import org.apache.synapse.mediators.transform.XSLTMediator;
+import org.apache.synapse.mediators.Key;
 import org.jaxen.JaxenException;
 
 import javax.xml.namespace.QName;
@@ -62,7 +63,13 @@ public class XSLTMediatorFactory extends
         OMAttribute attTarget = elem.getAttribute(ATT_TARGET);
 
         if (attXslt != null) {
-            transformMediator.setXsltKey(attXslt.getAttributeValue());
+            // KeyFactory for creating dynamic or static Key
+            KeyFactory keyFac = new KeyFactory();
+            // create dynamic or static key based on OMElement
+            Key generatedKey = keyFac.createKey(elem);
+
+            // set generated key as the Key
+            transformMediator.setXsltKey(generatedKey);
         } else {
             handleException("The 'key' attribute is required for the XSLT 
mediator");
         }

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=1060858&r1=1060857&r2=1060858&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
 Wed Jan 19 16:41:05 2011
@@ -21,12 +21,11 @@ package org.apache.synapse.config.xml;
 
 import org.apache.axiom.om.OMElement;
 import org.apache.synapse.Mediator;
-import org.apache.synapse.mediators.transform.XSLTMediator;
 import org.apache.synapse.mediators.MediatorProperty;
-
-import java.util.List;
+import org.apache.synapse.mediators.transform.XSLTMediator;
 
 import javax.xml.namespace.QName;
+import java.util.List;
 
 /**
  * Serializer for {@link XSLTMediator} instances.
@@ -48,8 +47,9 @@ public class XSLTMediatorSerializer exte
         OMElement xslt = fac.createOMElement("xslt", synNS);
 
         if (mediator.getXsltKey() != null) {
-            xslt.addAttribute(fac.createOMAttribute(
-                "key", nullNS, mediator.getXsltKey()));
+            //xslt.addAttribute(fac.createOMAttribute("key", nullNS, 
mediator.getXsltKey()));
+            KeySerializer keySerializer =  new KeySerializer();
+            keySerializer.serializeKey(mediator.getXsltKey(), xslt);
         } else {
             handleException("Invalid XSLT mediator. XSLT registry key is 
required");
         }


Reply via email to