Search in sources :

Example 16 with TemplateMediator

use of org.apache.synapse.mediators.template.TemplateMediator in project wso2-synapse by wso2.

the class TemplateMediatorSerializer method serializeSpecificMediator.

@Override
protected OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof TemplateMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }
    TemplateMediator mediator = (TemplateMediator) m;
    OMElement templateElem = fac.createOMElement("template", synNS);
    if (mediator.getName() != null) {
        templateElem.addAttribute(fac.createOMAttribute("name", nullNS, mediator.getName()));
        serializeParams(templateElem, mediator);
        serializeBody(templateElem, mediator.getList());
        saveTracingState(templateElem, mediator);
    }
    return templateElem;
}
Also used : TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) OMElement(org.apache.axiom.om.OMElement)

Example 17 with TemplateMediator

use of org.apache.synapse.mediators.template.TemplateMediator in project wso2-synapse by wso2.

the class MultiXMLConfigurationSerializer method serializeLocalEntry.

public OMElement serializeLocalEntry(Object o, SynapseConfiguration synapseConfig, OMElement parent) throws Exception {
    if (o instanceof TemplateMediator) {
        return serializeTemplate((TemplateMediator) o, synapseConfig, parent);
    } else if (o instanceof SequenceMediator) {
        return serializeSequence((SequenceMediator) o, synapseConfig, parent);
    } else if (o instanceof Template) {
        return serializeTemplate((Template) o, synapseConfig, parent);
    } else if (o instanceof Endpoint) {
        return serializeEndpoint((Endpoint) o, synapseConfig, parent);
    } else if (o instanceof Entry) {
        Entry entry = (Entry) o;
        if ((SynapseConstants.SERVER_HOST.equals(entry.getKey()) || SynapseConstants.SERVER_IP.equals(entry.getKey())) || entry.getType() == Entry.REMOTE_ENTRY) {
            return null;
        }
        File entriesDir = createDirectory(currentDirectory, MultiXMLConfigurationBuilder.LOCAL_ENTRY_DIR);
        OMElement entryElem = EntrySerializer.serializeEntry(entry, null);
        String fileName = entry.getFileName();
        if (fileName != null) {
            if (currentDirectory == rootDirectory) {
                handleDeployment(entriesDir, fileName, entry.getKey(), synapseConfig.getArtifactDeploymentStore());
            }
            File entryFile = new File(entriesDir, fileName);
            writeToFile(entryElem, entryFile);
        } else if (parent != null) {
            parent.addChild(entryElem);
        }
        return entryElem;
    }
    return null;
}
Also used : Entry(org.apache.synapse.config.Entry) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) Endpoint(org.apache.synapse.endpoints.Endpoint) AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) OMElement(org.apache.axiom.om.OMElement) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) Template(org.apache.synapse.endpoints.Template)

Example 18 with TemplateMediator

use of org.apache.synapse.mediators.template.TemplateMediator in project wso2-synapse by wso2.

the class AbstractRegistry method getResource.

/**
 * Get the resource for the given key from this registry
 * @param entry The Enrty instance that contains meta-data
 * @param properties bag of properties with additional information
 * @return the matching resultant object
 */
public Object getResource(Entry entry, Properties properties) {
    OMNode omNode = null;
    RegistryEntry re = null;
    // if we have an unexpired cached copy, return the cached object
    if (entry.isCached() && !entry.isExpired()) {
        return entry.getValue();
    // if we have not cached the referenced object, fetch it and its RegistryEntry
    } else if (!entry.isCached()) {
        try {
            omNode = lookup(entry.getKey());
            entry.setEntryProperties(getResourceProperties(entry.getKey()));
        } catch (OMException e) {
            log.error("Error reading registry resource file : " + entry.getKey(), e);
        }
        if (omNode == null && entry.getEntryProperties() != null && !entry.getEntryProperties().isEmpty()) {
            // Collection
            re = getRegistryEntry(entry.getKey());
            if (re != null) {
                setExpiryTime(entry, re);
                entry.setVersion(re.getVersion());
            }
        }
        if (omNode == null) {
            return null;
        } else {
            re = getRegistryEntry(entry.getKey());
        }
    // if we have cached it before, and now the cache has expired
    // get its *new* registry entry and compare versions and pick new cache duration
    } else if (entry.isExpired()) {
        if (log.isDebugEnabled()) {
            log.debug("Cached object has expired for key : " + entry.getKey());
        }
        re = getRegistryEntry(entry.getKey());
        if (re.getVersion() != Long.MIN_VALUE && re.getVersion() == entry.getVersion()) {
            if (log.isDebugEnabled()) {
                log.debug("Expired version number is same as current version in registry");
            }
            // renew cache lease for another cachable duration (as returned by the
            // new getRegistryEntry() call
            setExpiryTime(entry, re);
            if (log.isDebugEnabled()) {
                log.debug("Renew cache lease for another " + re.getCachableDuration() / 1000 + "s");
            }
            // return cached object
            return entry.getValue();
        } else {
            omNode = lookup(entry.getKey());
            entry.setEntryProperties(getResourceProperties(entry.getKey()));
            if (omNode == null && entry.getEntryProperties() != null && !entry.getEntryProperties().isEmpty()) {
                // Collection
                re = getRegistryEntry(entry.getKey());
                if (re != null) {
                    setExpiryTime(entry, re);
                    entry.setVersion(re.getVersion());
                }
            }
            if (omNode == null) {
                return null;
            }
        }
    }
    // if we get here, we have received the raw omNode from the
    // registry and our previous copy (if we had one) has expired or is not valid
    Object expiredValue = entry.getValue();
    // resource into the appropriate object - e.g. sequence or endpoint
    if (entry.getMapper() != null) {
        entry.setValue(entry.getMapper().getObjectFromOMNode(omNode, properties));
        if (entry.getValue() instanceof SequenceMediator) {
            SequenceMediator seq = (SequenceMediator) entry.getValue();
            seq.setDynamic(true);
            seq.setRegistryKey(entry.getKey());
        } else if (entry.getValue() instanceof Endpoint) {
            Endpoint ep = (Endpoint) entry.getValue();
        } else if (entry.getValue() instanceof TemplateMediator) {
            ((TemplateMediator) entry.getValue()).setDynamic(true);
        }
    } else {
        // else cache the raw OMNode
        if (re != null && re.getType() != null) {
            XMLToObjectMapper mapper = getMapper(re.getType());
            if (mapper != null) {
                entry.setMapper(mapper);
                entry.setValue(mapper.getObjectFromOMNode(omNode, properties));
            } else {
                entry.setValue(omNode);
            }
        }
    }
    if (expiredValue != null) {
        // Destroy the old resource so that everything is properly cleaned up
        if (expiredValue instanceof SequenceMediator) {
            ((SequenceMediator) expiredValue).destroy();
        } else if (expiredValue instanceof Endpoint) {
            ((Endpoint) expiredValue).destroy();
        }
    }
    // increment cache expiry time as specified by the last getRegistryEntry() call
    if (re != null) {
        setExpiryTime(entry, re);
        entry.setVersion(re.getVersion());
    }
    return entry.getValue();
}
Also used : OMNode(org.apache.axiom.om.OMNode) Endpoint(org.apache.synapse.endpoints.Endpoint) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) OMException(org.apache.axiom.om.OMException) XMLToObjectMapper(org.apache.synapse.config.XMLToObjectMapper)

Example 19 with TemplateMediator

use of org.apache.synapse.mediators.template.TemplateMediator in project wso2-synapse by wso2.

the class SynapseXMLConfigurationFactory method defineMediatorTemplate.

public static Mediator defineMediatorTemplate(SynapseConfiguration config, OMElement ele, Properties properties) {
    Mediator mediator = null;
    String name = ele.getAttributeValue(new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
    if (name != null) {
        try {
            mediator = MediatorFactoryFinder.getInstance().getMediator(ele, properties);
            if (mediator != null) {
                config.addSequenceTemplate(name, (TemplateMediator) mediator);
            }
        } catch (Exception e) {
            String msg = "Template configuration: " + name + " cannot be built";
            handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_TEMPLATES, msg, e);
        }
        return mediator;
    } else {
        String msg = "Invalid mediation template definition without a name";
        handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_SEQUENCES, msg);
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) Mediator(org.apache.synapse.Mediator) SynapseException(org.apache.synapse.SynapseException)

Example 20 with TemplateMediator

use of org.apache.synapse.mediators.template.TemplateMediator in project wso2-synapse by wso2.

the class MultiXMLConfigurationSerializerTest method testSerializeSynapseXML5.

/**
 * Test serializeSynapseXML method with TemplateMediator added for SynapseConfiguration and
 * assert synapse.xml is created.
 */
@Test
public void testSerializeSynapseXML5() throws Exception {
    MultiXMLConfigurationSerializer serializer = new MultiXMLConfigurationSerializer(TEST_DIRECTORY_NAME);
    SynapseConfiguration configuration = new SynapseConfiguration();
    org.apache.synapse.mediators.TestMediator t1 = new org.apache.synapse.mediators.TestMediator();
    org.apache.synapse.mediators.TestMediator t2 = new org.apache.synapse.mediators.TestMediator();
    org.apache.synapse.mediators.TestMediator t3 = new org.apache.synapse.mediators.TestMediator();
    TemplateMediator templateMediator = new TemplateMediator();
    templateMediator.addChild(t1);
    templateMediator.addChild(t2);
    templateMediator.addChild(t3);
    configuration.addSequence("testSequence", templateMediator);
    serializer.serializeSynapseXML(configuration);
    Assert.assertTrue("Error in serializing Synapse configuration.", new File(TEST_DIRECTORY_NAME + File.separator + SYNAPSE_XML).exists());
    removeTestFolder(TEST_DIRECTORY_NAME);
}
Also used : TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) File(java.io.File) Test(org.junit.Test)

Aggregations

TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)29 OMElement (org.apache.axiom.om.OMElement)10 Template (org.apache.synapse.endpoints.Template)9 SynapseException (org.apache.synapse.SynapseException)7 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)7 Endpoint (org.apache.synapse.endpoints.Endpoint)6 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)6 TestMediator (org.apache.synapse.mediators.TestMediator)6 ProxyService (org.apache.synapse.core.axis2.ProxyService)5 QName (javax.xml.namespace.QName)4 DeploymentException (org.apache.axis2.deployment.DeploymentException)4 Mediator (org.apache.synapse.Mediator)4 PriorityExecutor (org.apache.synapse.commons.executors.PriorityExecutor)4 File (java.io.File)3 Entry (org.apache.synapse.config.Entry)3 SynapseEventSource (org.apache.synapse.eventing.SynapseEventSource)3 MessageProcessor (org.apache.synapse.message.processor.MessageProcessor)3 MessageStore (org.apache.synapse.message.store.MessageStore)3 API (org.apache.synapse.rest.API)3 Test (org.junit.Test)3