You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by su...@apache.org on 2011/03/15 09:48:48 UTC

svn commit: r1081684 - in /synapse/trunk/java/modules/core/src/main/java/org/apache/synapse: config/ config/xml/ config/xml/endpoints/ endpoints/

Author: supun
Date: Tue Mar 15 08:48:48 2011
New Revision: 1081684

URL: http://svn.apache.org/viewvc?rev=1081684&view=rev
Log:
improving the endpoint templates by introducing methods to multixml config builder

Added:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateFactory.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/Template.java
Modified:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MultiXMLConfigurationBuilder.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java?rev=1081684&r1=1081683&r2=1081684&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java Tue Mar 15 08:48:48 2011
@@ -1423,4 +1423,8 @@ public class SynapseConfiguration implem
     public Map<String, Template> getEndpointTemplates() {
         return endpointTemplates;
     }
+
+    public Template getEndpointTemplate(String name) {
+        return endpointTemplates.get(name);
+    }
 }

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MultiXMLConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MultiXMLConfigurationBuilder.java?rev=1081684&r1=1081683&r2=1081684&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MultiXMLConfigurationBuilder.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/MultiXMLConfigurationBuilder.java Tue Mar 15 08:48:48 2011
@@ -26,6 +26,7 @@ import org.apache.commons.logging.LogFac
 import org.apache.synapse.Mediator;
 import org.apache.synapse.Startup;
 import org.apache.synapse.SynapseConstants;
+import org.apache.synapse.endpoints.Template;
 import org.apache.synapse.mediators.template.TemplateMediator;
 import org.apache.synapse.SynapseException;
 import org.apache.synapse.message.store.MessageStore;
@@ -38,6 +39,7 @@ import org.apache.synapse.endpoints.Endp
 import org.apache.synapse.eventing.SynapseEventSource;
 import org.apache.synapse.mediators.base.SequenceMediator;
 
+import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
 import java.io.*;
 import java.util.Properties;
@@ -278,22 +280,43 @@ public class MultiXMLConfigurationBuilde
     private static void createTemplates(SynapseConfiguration synapseConfig, String rootDirPath,
                                         Properties properties) throws XMLStreamException {
 
-        File sequencesDir = new File(rootDirPath, TEMPLATES_DIR);
-        if (sequencesDir.exists()) {
+        File templatesDir = new File(rootDirPath, TEMPLATES_DIR);
+        if (templatesDir.exists()) {
             if (log.isDebugEnabled()) {
-                log.debug("Loading sequences from : " + sequencesDir.getPath());
+                log.debug("Loading template from : " + templatesDir.getPath());
             }
-            File[] sequences = sequencesDir.listFiles(filter);
+            File[] sequences = templatesDir.listFiles(filter);
             for (File file : sequences) {
                 try {
                     OMElement document = parseFile(file);
-                    Mediator seq = SynapseXMLConfigurationFactory.defineMediatorTemplate(
+                    SynapseXMLConfigurationFactory.defineTemplate(
                             synapseConfig, document, properties);
-                    if (seq != null && seq instanceof TemplateMediator) {
-                        TemplateMediator templateSeq = (TemplateMediator) seq;
-                        templateSeq.setFileName(file.getName());
-                        synapseConfig.getArtifactDeploymentStore().addArtifact(
-                                file.getAbsolutePath(), templateSeq.getName());
+                    OMElement element = document.getFirstChildWithName(
+                            new QName(SynapseConstants.SYNAPSE_NAMESPACE, "sequence"));
+                    if (element != null) {
+                        TemplateMediator mediator =
+                                (TemplateMediator) SynapseXMLConfigurationFactory.defineMediatorTemplate(
+                                        synapseConfig, document, properties);
+                        if (mediator != null) {
+                            mediator.setFileName(file.getName());
+                            synapseConfig.getArtifactDeploymentStore().addArtifact(
+                                    file.getAbsolutePath(), mediator.getName());
+                        }
+                        return;
+                    }
+
+                    element = document.getFirstChildWithName(
+                            new QName(SynapseConstants.SYNAPSE_NAMESPACE, "endpoint"));
+                    if (element != null) {
+                        Template endpointTemplate =
+                                SynapseXMLConfigurationFactory.defineEndpointTemplate(
+                                        synapseConfig, document, properties);
+                        if (endpointTemplate != null) {
+                            endpointTemplate.setFileName(file.getName());
+                            synapseConfig.getArtifactDeploymentStore().addArtifact(
+                                    file.getAbsolutePath(), endpointTemplate.getFileName());
+                        }
+                        return;
                     }
                 } catch (FileNotFoundException ignored) {}
             }

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateFactory.java?rev=1081684&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateFactory.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateFactory.java Tue Mar 15 08:48:48 2011
@@ -0,0 +1,87 @@
+/*
+ *  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.endpoints;
+
+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.SynapseConstants;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.xml.XMLConfigConstants;
+import org.apache.synapse.endpoints.Template;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+import java.util.Properties;
+
+public class TemplateFactory {
+    public static final Log log = LogFactory.getLog(TemplateFactory.class);
+
+    public Template createEndpointTemplate(OMElement element, Properties properties) {
+        Template template = new Template();
+
+        OMAttribute nameAttribute = element.getAttribute(
+                new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
+
+        if (nameAttribute != null) {
+            template.setName(nameAttribute.getAttributeValue());
+        } else {
+            handleException("Error loading the configuration from endpointTemplate, '" +
+                    "name' attribute missing");
+        }
+
+        Iterator paramItr = element.getChildrenWithName(
+                new QName(SynapseConstants.SYNAPSE_NAMESPACE, "parameter"));
+        while (paramItr.hasNext()) {
+            OMElement paramElement = (OMElement) paramItr.next();
+
+            OMAttribute paramName = paramElement.getAttribute(new QName("name"));
+            OMAttribute paramValue = paramElement.getAttribute(new QName("value"));
+
+            if (paramName == null) {
+                handleException("parameter name should be present");
+            }
+
+            if (paramValue == null) {
+                handleException("parameter value should be present");
+            }
+
+            assert paramName != null;
+            assert paramValue != null;
+
+            template.addParameter(paramName.getAttributeValue(), paramValue.getAttributeValue());
+        }
+
+        OMElement endpointElement = element.getFirstChildWithName(
+                new QName(SynapseConstants.SYNAPSE_NAMESPACE, "endpoint"));
+        if (endpointElement == null) {
+            handleException("endpoint element is required in an endpoint template");
+        }
+        template.setElement(endpointElement);
+
+        return template;
+    }
+
+    protected void handleException(String message) {
+        log.error(message);
+        throw new SynapseException(message);
+    }
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/Template.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/Template.java?rev=1081684&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/Template.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/Template.java Tue Mar 15 08:48:48 2011
@@ -0,0 +1,133 @@
+/*
+ *  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.endpoints;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.synapse.SynapseArtifact;
+import org.apache.synapse.config.xml.endpoints.EndpointFactory;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * A template with the endpoint information.
+ */
+public class Template implements SynapseArtifact {
+    private OMElement element = null;
+
+    private String name = null;
+
+    private Map<String, String> parameters = new HashMap<String, String>();
+
+    private String fileName = null;
+
+    private String description = null;
+
+    public Endpoint create(TemplateEndpoint templateEndpoint, Properties properties) {
+        // first go through all the elements and replace with the parameters
+        OMElement clonedElement = element.cloneOMElement();
+        replaceElement(templateEndpoint, clonedElement);
+
+        return EndpointFactory.getEndpointFromElement(clonedElement, false, properties);
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Map<String, String> getParameters() {
+        return parameters;
+    }
+
+    public void addParameter(String name, String value) {
+        parameters.put(name, value);
+    }
+
+    public void setElement(OMElement element) {
+        this.element = element;
+    }
+
+    public OMElement getElement() {
+        return element;
+    }
+
+    private void replaceElement(TemplateEndpoint templateEndpoint, OMElement element) {
+        Iterator attributesItr = element.getAllAttributes();
+        while (attributesItr.hasNext()) {
+            OMAttribute attribute = (OMAttribute) attributesItr.next();
+
+            String replace = replace(attribute.getAttributeValue(), templateEndpoint);
+
+            if (replace != null) {
+                attribute.setAttributeValue(replace);
+            }
+        }
+
+        if (element.getText() != null && !"".equals(element.getText())) {
+            String replace = replace(element.getText(), templateEndpoint);
+
+            if (replace != null) {
+                element.setText(replace);
+            }
+        }
+
+        Iterator elemItr = element.getChildElements();
+        while (elemItr.hasNext()) {
+            OMElement childElement = (OMElement) elemItr.next();
+
+            replaceElement(templateEndpoint, childElement);
+        }
+    }
+
+    private String replace(String value, TemplateEndpoint templateEndpoint) {
+        if (value.startsWith("$")) {
+            String param = value.substring(1);
+
+            if (templateEndpoint.getParameters().containsKey(param)) {
+                return templateEndpoint.getParameterValue(param);
+            }
+        }
+
+        return null;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public String getFileName() {
+        return fileName;
+    }
+
+    public void setFileName(String fileName) {
+        this.fileName = fileName;
+    }
+}

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java?rev=1081684&r1=1081683&r2=1081684&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java Tue Mar 15 08:48:48 2011
@@ -23,6 +23,8 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.synapse.MessageContext;
 import org.apache.synapse.SynapseConstants;
+import org.apache.synapse.config.Entry;
+import org.apache.synapse.config.SynapseConfiguration;
 import org.apache.synapse.core.SynapseEnvironment;
 
 import java.util.HashMap;
@@ -41,6 +43,8 @@ public class TemplateEndpoint extends Ab
 
     @Override
     public void send(MessageContext synCtx) {
+        reLoadAndInitEndpoint(synCtx.getEnvironment());
+
         if (realEndpoint != null) {
             realEndpoint.send(synCtx);
         } else {
@@ -90,14 +94,45 @@ public class TemplateEndpoint extends Ab
                     " cannot be found for the endpoint " + getName());
         }
 
-        realEndpoint = endpointTemplate.create(this,
-                synapseEnvironment.getSynapseConfiguration().getProperties());
+        reLoadAndInitEndpoint(synapseEnvironment);
+    }
 
-        realEndpoint.init(synapseEnvironment);
+    /**
+     * Reload as needed , either from registry , local entries or predefined endpoints
+     * @param se synapse environment
+     */
+    private synchronized void reLoadAndInitEndpoint(SynapseEnvironment se) {
+        SynapseConfiguration synCfg = se.getSynapseConfiguration();
+
+        boolean reLoad = (realEndpoint == null);
+        if (!reLoad) {
+            Entry entry = synCfg.getEntryDefinition(template);
+            if (entry != null && entry.isDynamic()) {
+                if (!entry.isCached() || entry.isExpired()) {
+                    reLoad = true;
+                }
+            } else {
+                // If the endpoint is static we should reload it from the Synapse config
+                reLoad = true;
+            }
+        }
 
-        if (realEndpoint == null) {
-            handleException("Couldn't retrieve the endpoint " + getName() +
-                    " from the template: " + endpointTemplate.getName());
+        if (reLoad) {
+            if (log.isDebugEnabled()) {
+                log.debug("Loading template endpoint with key : " + template);
+            }
+
+            Template eprTemplate = synCfg.getEndpointTemplates().get(template);
+
+            if (eprTemplate != null) {
+                realEndpoint = eprTemplate.create(this, synCfg.getProperties());
+            } else {
+                log.warn("Couldn't retrieve the endpoint template with the key:" + template);
+            }
+
+            if (realEndpoint != null && !realEndpoint.isInitialized()) {
+                realEndpoint.init(se);
+            }
         }
     }
 }