Search in sources :

Example 1 with Template

use of org.apache.synapse.endpoints.Template in project wso2-synapse by wso2.

the class TemplateDeployer method restoreSynapseArtifact.

@Override
public void restoreSynapseArtifact(String artifactName) {
    if (log.isDebugEnabled()) {
        log.debug("Restoring the Template with name : " + artifactName + " : Started");
    }
    try {
        Template st = getSynapseConfiguration().getEndpointTemplate(artifactName);
        if (st != null) {
            TemplateSerializer ts = new TemplateSerializer();
            OMElement stElem = ts.serializeEndpointTemplate(st, null);
            if (st.getFileName() != null) {
                String fileName = getServerConfigurationInformation().getSynapseXMLLocation() + File.separator + MultiXMLConfigurationBuilder.TEMPLATES_DIR + File.separator + st.getFileName();
                writeToFile(stElem, fileName);
                if (log.isDebugEnabled()) {
                    log.debug("Restoring the Endpoint Template with name : " + artifactName + " : Completed");
                }
                log.info("Template named '" + artifactName + "' has been restored");
            }
        } else {
            TemplateMediator mt = getSynapseConfiguration().getSequenceTemplate(artifactName);
            if (mt != null) {
                TemplateMediatorSerializer ts = new TemplateMediatorSerializer();
                OMElement stElem = ts.serializeMediator(null, mt);
                if (mt.getFileName() != null) {
                    String fileName = getServerConfigurationInformation().getSynapseXMLLocation() + File.separator + MultiXMLConfigurationBuilder.TEMPLATES_DIR + File.separator + st.getFileName();
                    writeToFile(stElem, fileName);
                    if (log.isDebugEnabled()) {
                        log.debug("Restoring the Sequence Template with name : " + artifactName + " : Completed");
                    }
                    log.info("Template named '" + artifactName + "' has been restored");
                }
            } else {
                handleSynapseArtifactDeploymentError("Couldn't restore the Template named '" + artifactName + "', filename cannot be found");
            }
        }
    } catch (Exception e) {
        handleSynapseArtifactDeploymentError("Restoring of the Template named '" + artifactName + "' has failed", e);
    }
}
Also used : TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) TemplateMediatorSerializer(org.apache.synapse.config.xml.TemplateMediatorSerializer) TemplateSerializer(org.apache.synapse.config.xml.endpoints.TemplateSerializer) OMElement(org.apache.axiom.om.OMElement) SynapseException(org.apache.synapse.SynapseException) DeploymentException(org.apache.axis2.deployment.DeploymentException) Template(org.apache.synapse.endpoints.Template)

Example 2 with Template

use of org.apache.synapse.endpoints.Template in project wso2-synapse by wso2.

the class TemplateDeployer method updateSynapseArtifact.

@Override
public String updateSynapseArtifact(OMElement artifactConfig, String fileName, String existingArtifactName, Properties properties) {
    if (log.isDebugEnabled()) {
        log.debug("Template update from file : " + fileName + " has started");
    }
    try {
        OMElement element = artifactConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "endpoint"));
        if (element != null) {
            org.apache.synapse.config.xml.endpoints.TemplateFactory templateFactory = new org.apache.synapse.config.xml.endpoints.TemplateFactory();
            Template tm = templateFactory.createEndpointTemplate(artifactConfig, properties);
            tm.setFileName(new File(fileName).getName());
            if (log.isDebugEnabled()) {
                log.debug("Endpoint Template named '" + tm.getName() + "' has been built from the file " + fileName);
            }
            Template existingSt = getSynapseConfiguration().getEndpointTemplate(existingArtifactName);
            if (existingArtifactName.equals(tm.getName())) {
                getSynapseConfiguration().updateEndpointTemplate(tm.getName(), tm);
            } else {
                getSynapseConfiguration().addEndpointTemplate(tm.getName(), tm);
                getSynapseConfiguration().removeEndpointTemplate(existingSt.getName());
                log.info("Template: " + existingArtifactName + " has been undeployed");
            }
            log.info("Template: " + tm.getName() + " has been updated from the file: " + fileName);
            return tm.getName();
        } else {
            element = artifactConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "sequence"));
            if (element != null) {
                Mediator mediator = MediatorFactoryFinder.getInstance().getMediator(artifactConfig, properties);
                if (mediator instanceof TemplateMediator) {
                    TemplateMediator tm = (TemplateMediator) mediator;
                    tm.setFileName((new File(fileName)).getName());
                    if (log.isDebugEnabled()) {
                        log.debug("Sequence Template named '" + tm.getName() + "' has been built from the file " + fileName);
                    }
                    tm.init(getSynapseEnvironment());
                    if (log.isDebugEnabled()) {
                        log.debug("Initialized the Template : " + tm.getName());
                    }
                    TemplateMediator existingSt = getSynapseConfiguration().getSequenceTemplate(existingArtifactName);
                    if (existingArtifactName.equals(tm.getName())) {
                        getSynapseConfiguration().updateSequenceTemplate(tm.getName(), tm);
                    } else {
                        getSynapseConfiguration().addSequenceTemplate(tm.getName(), tm);
                        getSynapseConfiguration().removeSequenceTemplate(existingSt.getName());
                        log.info("Template: " + existingArtifactName + " has been undeployed");
                    }
                    existingSt.destroy();
                    log.info("Template: " + tm.getName() + " has been updated from the file: " + fileName);
                    return tm.getName();
                }
            }
        }
    } catch (DeploymentException e) {
        handleSynapseArtifactDeploymentError("Error while updating the Template from the " + "file: " + fileName);
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) Template(org.apache.synapse.endpoints.Template) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) Mediator(org.apache.synapse.Mediator) DeploymentException(org.apache.axis2.deployment.DeploymentException) File(java.io.File)

Example 3 with Template

use of org.apache.synapse.endpoints.Template in project wso2-synapse by wso2.

the class SynapseConfiguration method getEndpointTemplate.

public Template getEndpointTemplate(String key) {
    Object o = getEntry(key);
    if (o instanceof Template) {
        return (Template) o;
    }
    Entry entry = null;
    if (o == null) {
        entry = new Entry(key);
        entry.setType(Entry.REMOTE_ENTRY);
    } else {
        Object object = localRegistry.get(key);
        if (object instanceof Entry) {
            entry = (Entry) object;
        }
    }
    assertEntryNull(entry, key);
    // noinspection ConstantConditions
    if (entry.getMapper() == null) {
        entry.setMapper(new XMLToTemplateMapper());
    }
    if (entry.getType() == Entry.REMOTE_ENTRY) {
        if (registry != null) {
            o = registry.getResource(entry, getProperties());
            if (o != null && o instanceof Template) {
                localRegistry.put(key, entry);
                return (Template) o;
            } else if (o instanceof OMNode) {
                Template m = new TemplateFactory().createEndpointTemplate((OMElement) o, properties);
                if (m != null) {
                    entry.setValue(m);
                    return m;
                }
            }
        }
    } else {
        Object value = entry.getValue();
        if (value instanceof OMNode) {
            Object object = entry.getMapper().getObjectFromOMNode((OMNode) value, getProperties());
            if (object instanceof Template) {
                entry.setValue(object);
                return (Template) object;
            }
        }
    }
    // load from available libraries
    Template templateFromLib = LibDeployerUtils.getLibArtifact(synapseLibraries, key, Template.class);
    if (templateFromLib != null) {
        return templateFromLib;
    }
    return null;
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMElement(org.apache.axiom.om.OMElement) XMLToTemplateMapper(org.apache.synapse.config.xml.XMLToTemplateMapper) TemplateFactory(org.apache.synapse.config.xml.endpoints.TemplateFactory) Template(org.apache.synapse.endpoints.Template)

Example 4 with Template

use of org.apache.synapse.endpoints.Template in project wso2-synapse by wso2.

the class SynapseConfiguration method getEndpointTemplates.

/**
 * Returns the map of defined synapse endpoint templates in the configuration excluding the
 * fetched sequences from remote registry.
 *
 * @return Map of Templates defined in the local configuration
 */
public Map<String, Template> getEndpointTemplates() {
    Map<String, Template> definedTemplates = new HashMap<String, Template>();
    synchronized (this) {
        for (Object o : localRegistry.values()) {
            if (o instanceof Template) {
                Template template = (Template) o;
                definedTemplates.put(template.getName(), template);
            }
        }
    }
    return definedTemplates;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Template(org.apache.synapse.endpoints.Template)

Example 5 with Template

use of org.apache.synapse.endpoints.Template in project wso2-synapse by wso2.

the class MultiXMLConfigurationBuilder method createTemplates.

private static void createTemplates(SynapseConfiguration synapseConfig, String rootDirPath, Properties properties) {
    File templatesDir = new File(rootDirPath, TEMPLATES_DIR);
    if (templatesDir.exists()) {
        if (log.isDebugEnabled()) {
            log.debug("Loading template from : " + templatesDir.getPath());
        }
        Iterator templates = FileUtils.iterateFiles(templatesDir, extensions, false);
        while (templates.hasNext()) {
            File file = (File) templates.next();
            try {
                OMElement document = getOMElement(file);
                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());
                    }
                } else {
                    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.getName());
                        }
                    }
                }
            } catch (Exception e) {
                String msg = "Template configuration cannot be built from : " + file.getName();
                handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_TEMPLATES, msg, e);
            }
        }
    }
}
Also used : TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) XMLStreamException(javax.xml.stream.XMLStreamException) SynapseException(org.apache.synapse.SynapseException) Template(org.apache.synapse.endpoints.Template)

Aggregations

Template (org.apache.synapse.endpoints.Template)15 OMElement (org.apache.axiom.om.OMElement)9 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)9 QName (javax.xml.namespace.QName)5 SynapseException (org.apache.synapse.SynapseException)5 DeploymentException (org.apache.axis2.deployment.DeploymentException)4 Entry (org.apache.synapse.config.Entry)3 TemplateSerializer (org.apache.synapse.config.xml.endpoints.TemplateSerializer)3 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)3 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)3 File (java.io.File)2 Iterator (java.util.Iterator)2 Mediator (org.apache.synapse.Mediator)2 TemplateFactory (org.apache.synapse.config.xml.endpoints.TemplateFactory)2 ProxyService (org.apache.synapse.core.axis2.ProxyService)2 AbstractEndpoint (org.apache.synapse.endpoints.AbstractEndpoint)2 Endpoint (org.apache.synapse.endpoints.Endpoint)2 SynapseEventSource (org.apache.synapse.eventing.SynapseEventSource)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1