Search in sources :

Example 11 with TemplateMediator

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

the class SynapseConfiguration method getSequenceTemplates.

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

Example 12 with TemplateMediator

use of org.apache.synapse.mediators.template.TemplateMediator 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)

Example 13 with TemplateMediator

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

the class MultiXMLConfigurationSerializer method serializeSynapseXML.

/**
 * Serialize only the elements defined in the top level synapse.xml file back to the
 * synapse.xml file. This method ignores the elements defined in files other than the
 * synapse.xml. Can be used in situations where only the synapse.xml file should be
 * updated at runtime.
 *
 * @param synapseConfig Current Synapse configuration
 * @throws Exception on file I/O error
 */
public void serializeSynapseXML(SynapseConfiguration synapseConfig) throws Exception {
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMElement definitions = fac.createOMElement("definitions", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
    if (synapseConfig.getRegistry() != null && !Boolean.valueOf(synapseConfig.getProperty(MultiXMLConfigurationBuilder.SEPARATE_REGISTRY_DEFINITION))) {
        RegistrySerializer.serializeRegistry(definitions, synapseConfig.getRegistry());
    }
    if (synapseConfig.getTaskManager() != null && !Boolean.valueOf(synapseConfig.getProperty(MultiXMLConfigurationBuilder.SEPARATE_TASK_MANAGER_DEFINITION))) {
        TaskManagerSerializer.serializetaskManager(definitions, synapseConfig.getTaskManager());
    }
    Collection<ProxyService> proxyServices = synapseConfig.getProxyServices();
    Collection<SynapseEventSource> eventSources = synapseConfig.getEventSources();
    Collection<Startup> tasks = synapseConfig.getStartups();
    Collection localEntries = synapseConfig.getLocalRegistry().values();
    Collection<PriorityExecutor> executors = synapseConfig.getPriorityExecutors().values();
    Collection<MessageStore> messageStores = synapseConfig.getMessageStores().values();
    Collection<MessageProcessor> messageProcessors = synapseConfig.getMessageProcessors().values();
    Collection<API> apiCollection = synapseConfig.getAPIs();
    Collection<SynapseImport> synapseImportsCollection = synapseConfig.getSynapseImports().values();
    Collection<InboundEndpoint> inboundEndpoints = synapseConfig.getInboundEndpoints();
    Collection<String> comments = synapseConfig.getCommentedTextList();
    for (ProxyService service : proxyServices) {
        if (service.getFileName() == null) {
            ProxyServiceSerializer.serializeProxy(definitions, service);
        }
    }
    for (SynapseEventSource source : eventSources) {
        if (source.getFileName() == null) {
            EventSourceSerializer.serializeEventSource(definitions, source);
        }
    }
    for (Startup task : tasks) {
        if (task instanceof AbstractStartup && task.getFileName() == null) {
            StartupFinder.getInstance().serializeStartup(definitions, task);
        }
    }
    for (Object o : localEntries) {
        if (o instanceof TemplateMediator) {
            TemplateMediator template = (TemplateMediator) o;
            if (template.getFileName() == null) {
                MediatorSerializerFinder.getInstance().getSerializer(template).serializeMediator(definitions, template);
            }
        } else if (o instanceof SequenceMediator) {
            SequenceMediator seq = (SequenceMediator) o;
            if (seq.getFileName() == null) {
                MediatorSerializerFinder.getInstance().getSerializer(seq).serializeMediator(definitions, seq);
            }
        } else if (o instanceof Template) {
            Template templEndpoint = (Template) o;
            if (templEndpoint.getFileName() == null) {
                new TemplateSerializer().serializeEndpointTemplate(templEndpoint, definitions);
            }
        } else if (o instanceof AbstractEndpoint) {
            AbstractEndpoint endpoint = (AbstractEndpoint) o;
            if (endpoint.getFileName() == null) {
                OMElement endpointElem = EndpointSerializer.getElementFromEndpoint(endpoint);
                definitions.addChild(endpointElem);
            }
        } else if (o instanceof Entry) {
            Entry entry = (Entry) o;
            if (entry.getFileName() == null) {
                if ((SynapseConstants.SERVER_HOST.equals(entry.getKey()) || SynapseConstants.SERVER_IP.equals(entry.getKey())) || entry.getType() == Entry.REMOTE_ENTRY) {
                    continue;
                }
                EntrySerializer.serializeEntry(entry, definitions);
            }
        }
    }
    for (PriorityExecutor executor : executors) {
        PriorityExecutorSerializer.serialize(definitions, executor, SynapseConstants.SYNAPSE_NAMESPACE);
    }
    for (MessageStore messageStore : messageStores) {
        if (messageStore.getFileName() == null) {
            MessageStoreSerializer.serializeMessageStore(definitions, messageStore);
        }
    }
    for (MessageProcessor messageProcessor : messageProcessors) {
        if (messageProcessor.getFileName() == null) {
            MessageProcessorSerializer.serializeMessageProcessor(definitions, messageProcessor);
        }
    }
    for (API api : apiCollection) {
        if (api.getFileName() == null) {
            APISerializer.serializeAPI(definitions, api);
        }
    }
    for (SynapseImport synapseImport : synapseImportsCollection) {
        if (synapseImport.getFileName() == null) {
            SynapseImportSerializer.serializeImport(definitions, synapseImport);
        }
    }
    for (InboundEndpoint inboundEndpoint : inboundEndpoints) {
        if (inboundEndpoint.getFileName() == null) {
            InboundEndpointSerializer.serializeInboundEndpoint(definitions, inboundEndpoint);
        }
    }
    serializeComments(comments, definitions);
    serializeSynapseXML(definitions);
}
Also used : MessageStore(org.apache.synapse.message.store.MessageStore) SynapseEventSource(org.apache.synapse.eventing.SynapseEventSource) MessageProcessor(org.apache.synapse.message.processor.MessageProcessor) OMElement(org.apache.axiom.om.OMElement) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) Template(org.apache.synapse.endpoints.Template) SynapseImport(org.apache.synapse.libraries.imports.SynapseImport) Entry(org.apache.synapse.config.Entry) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) TemplateSerializer(org.apache.synapse.config.xml.endpoints.TemplateSerializer) AbstractStartup(org.apache.synapse.startup.AbstractStartup) AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) OMFactory(org.apache.axiom.om.OMFactory) InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) ProxyService(org.apache.synapse.core.axis2.ProxyService) Collection(java.util.Collection) API(org.apache.synapse.rest.API) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) Startup(org.apache.synapse.Startup) AbstractStartup(org.apache.synapse.startup.AbstractStartup)

Example 14 with TemplateMediator

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

the class SynapseXMLConfigurationSerializer method serializeConfiguration.

/**
 * Order of entries is irrelevant, however its nice to have some order.
 *
 * @param synCfg configuration to be serialized
 * @return serialized element of the configuration
 */
public OMElement serializeConfiguration(SynapseConfiguration synCfg) {
    OMElement definitions = fac.createOMElement("definitions", synNS);
    // first add the description
    if (synCfg.getDescription() != null) {
        OMElement descElem = fac.createOMElement("description", synNS);
        descElem.setText(synCfg.getDescription());
        definitions.addChild(descElem);
    }
    // then process a remote registry if present
    if (synCfg.getRegistry() != null) {
        RegistrySerializer.serializeRegistry(definitions, synCfg.getRegistry());
    }
    // then process a remote registry if present
    if (synCfg.getTaskManager() != null) {
        TaskManagerSerializer.serializetaskManager(definitions, synCfg.getTaskManager());
    }
    serializeImports(definitions, synCfg.getSynapseImports().values());
    // add proxy services
    Iterator itr = synCfg.getProxyServices().iterator();
    while (itr.hasNext()) {
        ProxyService service = (ProxyService) itr.next();
        ProxyServiceSerializer.serializeProxy(definitions, service);
    }
    // Add Event sources
    for (SynapseEventSource eventSource : synCfg.getEventSources()) {
        EventSourceSerializer.serializeEventSource(definitions, eventSource);
    }
    Map<String, Entry> entries = new HashMap<String, Entry>();
    Map<String, Endpoint> endpoints = new HashMap<String, Endpoint>();
    Map<String, SequenceMediator> sequences = new HashMap<String, SequenceMediator>();
    Map<String, TemplateMediator> templates = new HashMap<String, TemplateMediator>();
    Map<String, Template> endpointTemplates = new HashMap<String, Template>();
    itr = synCfg.getLocalRegistry().keySet().iterator();
    while (itr.hasNext()) {
        Object key = itr.next();
        if (SynapseConstants.SERVER_IP.equals(key) || SynapseConstants.SERVER_HOST.equals(key)) {
            continue;
        }
        Object o = synCfg.getLocalRegistry().get(key);
        if (o instanceof TemplateMediator) {
            templates.put(key.toString(), (TemplateMediator) o);
        } else if (o instanceof SequenceMediator) {
            sequences.put(key.toString(), (SequenceMediator) o);
        } else if (o instanceof Endpoint) {
            endpoints.put(key.toString(), (Endpoint) o);
        } else if (o instanceof Template) {
            endpointTemplates.put(key.toString(), (Template) o);
        } else if (o instanceof Entry) {
            entries.put(key.toString(), (Entry) o);
        } else {
            handleException("Unknown object : " + o.getClass() + " for serialization into Synapse configuration");
        }
    }
    // process entries
    serializeEntries(definitions, entries);
    // process endpoints
    serializeEndpoints(definitions, endpoints);
    // process sequences
    serializeSequences(definitions, sequences);
    // process templates
    serializeMediatorTemplates(definitions, templates);
    // serialize the endpoint templates
    serializeEndpointTemplates(definitions, endpointTemplates);
    // handle startups
    serializeStartups(definitions, synCfg.getStartups());
    // Executors
    serializeExecutors(definitions, synCfg.getPriorityExecutors());
    // Message stores
    serializeMessageStores(definitions, synCfg.getMessageStores());
    // Message Processors
    serializeMessageProcessors(definitions, synCfg.getMessageProcessors());
    serializeAPIs(definitions, synCfg.getAPIs());
    // XML comments
    serializeComments(definitions, synCfg.getCommentedTextList());
    if (synCfg.getInboundEndpoints() != null && synCfg.getInboundEndpoints().size() > 0) {
        Collection<InboundEndpoint> inboundEndpoints = synCfg.getInboundEndpoints();
        for (InboundEndpoint inboundEndpoint : inboundEndpoints) {
            InboundEndpointSerializer.serializeInboundEndpoint(definitions, inboundEndpoint);
        }
    }
    return definitions;
}
Also used : SynapseEventSource(org.apache.synapse.eventing.SynapseEventSource) Template(org.apache.synapse.endpoints.Template) InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) Entry(org.apache.synapse.config.Entry) Endpoint(org.apache.synapse.endpoints.Endpoint) InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) ProxyService(org.apache.synapse.core.axis2.ProxyService) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 15 with TemplateMediator

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

the class TemplateMediatorFactory method createSpecificMediator.

protected Mediator createSpecificMediator(OMElement elem, Properties properties) {
    TemplateMediator templateTemplateMediator = new TemplateMediator();
    OMAttribute nameAttr = elem.getAttribute(ATT_NAME);
    if (nameAttr != null) {
        templateTemplateMediator.setName(nameAttr.getAttributeValue());
        processAuditStatus(templateTemplateMediator, elem);
        initParameters(elem, templateTemplateMediator);
        OMElement templateBodyElem = elem.getFirstChildWithName(TEMPLATE_BODY_Q);
        addChildren(templateBodyElem, templateTemplateMediator, properties);
    } else {
        String msg = "A EIP template should be a named mediator .";
        log.error(msg);
        throw new SynapseException(msg);
    }
    return templateTemplateMediator;
}
Also used : TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) SynapseException(org.apache.synapse.SynapseException) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

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