Search in sources :

Example 11 with SynapseImport

use of org.apache.synapse.libraries.imports.SynapseImport in project wso2-synapse by wso2.

the class MediatorFactoryFinder method getMediator.

/**
 * This method returns a Processor given an OMElement. This will be used
 * recursively by the elements which contain processor elements themselves
 * (e.g. rules)
 *
 * @param element XML representation of a mediator
 * @param properties bag of properties to pass in any information to the factory
 * @return Processor
 */
public Mediator getMediator(OMElement element, Properties properties) {
    String localName = element.getLocalName();
    QName qName;
    if (element.getNamespace() != null) {
        qName = new QName(element.getNamespace().getNamespaceURI(), localName);
    } else {
        qName = new QName(localName);
    }
    if (log.isDebugEnabled()) {
        log.debug("getMediator(" + qName + ")");
    }
    Class cls = factoryMap.get(qName);
    if (cls == null && localName.indexOf('.') > -1) {
        String newLocalName = localName.substring(0, localName.indexOf('.'));
        qName = new QName(element.getNamespace().getNamespaceURI(), newLocalName);
        if (log.isDebugEnabled()) {
            log.debug("getMediator.2(" + qName + ")");
        }
        cls = factoryMap.get(qName);
    }
    if (cls == null) {
        if (synapseLibraryMap != null && !synapseLibraryMap.isEmpty()) {
            for (Map.Entry<String, Library> entry : synapseLibraryMap.entrySet()) {
                if (entry.getValue().getLibArtifactDetails().containsKey(localName)) {
                    return getDynamicInvokeMediator(element, entry.getValue().getPackage());
                }
            }
        }
        if (!synapseImportMap.isEmpty()) {
            for (Map.Entry<String, SynapseImport> entry : synapseImportMap.entrySet()) {
                if (localName.startsWith(entry.getValue().getLibName())) {
                    return getDynamicInvokeMediator(element, entry.getValue().getLibPackage());
                }
            }
        }
        String msg = "Unknown mediator referenced by configuration element : " + qName;
        log.error(msg);
        throw new SynapseException(msg);
    }
    try {
        MediatorFactory mf = (MediatorFactory) cls.newInstance();
        return mf.createMediator(element, properties);
    } catch (InstantiationException e) {
        String msg = "Error initializing mediator factory : " + cls;
        log.error(msg);
        throw new SynapseException(msg, e);
    } catch (IllegalAccessException e) {
        String msg = "Error initializing mediator factory : " + cls;
        log.error(msg);
        throw new SynapseException(msg, e);
    }
}
Also used : SynapseImport(org.apache.synapse.libraries.imports.SynapseImport) SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) EventPublisherMediatorFactory(org.apache.synapse.config.xml.eventing.EventPublisherMediatorFactory) Library(org.apache.synapse.libraries.model.Library) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with SynapseImport

use of org.apache.synapse.libraries.imports.SynapseImport in project wso2-synapse by wso2.

the class SynapseImportSerializer method serializeImport.

/**
 * This method will implements the serialization of SynapseImport object to its configuration
 *
 * @param s the type SynapseImport which is subjected to the serialization
 * @return OMElement serialized in to xml from the given parameters
 */
public static OMElement serializeImport(SynapseImport s) {
    if (!(s instanceof SynapseImport)) {
        handleException("Unsupported Synapse Import passed in for serialization");
    }
    SynapseImport synapseImport = (SynapseImport) s;
    OMElement importElem = fac.createOMElement("import", synNS);
    if (synapseImport.getLibName() != null) {
        importElem.addAttribute(fac.createOMAttribute("name", nullNS, s.getLibName()));
    } else {
        handleException("Invalid Synapse Import. Target Library name is required");
    }
    if (synapseImport.getLibPackage() != null) {
        importElem.addAttribute(fac.createOMAttribute("package", nullNS, s.getLibPackage()));
    } else {
        handleException("Invalid Synapse Import. Target Library package is required");
    }
    if (synapseImport.isStatus()) {
        importElem.addAttribute(fac.createOMAttribute("status", nullNS, "enabled"));
    } else {
        importElem.addAttribute(fac.createOMAttribute("status", nullNS, "disabled"));
    }
    return importElem;
}
Also used : SynapseImport(org.apache.synapse.libraries.imports.SynapseImport) OMElement(org.apache.axiom.om.OMElement)

Example 13 with SynapseImport

use of org.apache.synapse.libraries.imports.SynapseImport in project wso2-synapse by wso2.

the class SynapseImportFactory method createImport.

@SuppressWarnings({ "UnusedDeclaration" })
public static SynapseImport createImport(OMElement elem, Properties properties) {
    OMAttribute pkgAtt = elem.getAttribute(PACKAGE_Q);
    SynapseImport synapseImport = new SynapseImport();
    OMAttribute nameAtt = elem.getAttribute(NAME_Q);
    if (nameAtt != null) {
        synapseImport.setLibName(nameAtt.getAttributeValue());
    } else {
        handleException("Synapse Import Target Library name is not specified");
    }
    if (pkgAtt != null) {
        synapseImport.setLibPackage(pkgAtt.getAttributeValue());
    } else {
        handleException("Synapse Import Target Library package is not specified");
    }
    OMAttribute status = elem.getAttribute(STATUS_Q);
    if (status != null && status.getAttributeValue().equals("enabled")) {
        synapseImport.setStatus(true);
    } else {
        synapseImport.setStatus(false);
    }
    log.info("Successfully created Synapse Import: " + nameAtt.getAttributeValue());
    return synapseImport;
}
Also used : SynapseImport(org.apache.synapse.libraries.imports.SynapseImport) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 14 with SynapseImport

use of org.apache.synapse.libraries.imports.SynapseImport in project wso2-synapse by wso2.

the class SynapseImportSerializerTest method testSerializeImportDisabled.

/**
 * Test SerializeImport with valid synapseImport, status disabled.
 */
@Test
public void testSerializeImportDisabled() {
    SynapseImport synapseImport = new SynapseImport();
    synapseImport.setLibName("testLibName");
    synapseImport.setLibPackage("testVersion");
    OMElement element = SynapseImportSerializer.serializeImport(synapseImport);
    Assert.assertEquals("asserting OMElement name", "import", element.getLocalName());
    OMAttribute temp = element.getAttribute(new QName("name"));
    Assert.assertEquals("asserting name attribute", "testLibName", temp.getAttributeValue());
    temp = element.getAttribute(new QName("package"));
    Assert.assertEquals("asserting package attribute", "testVersion", temp.getAttributeValue());
    temp = element.getAttribute(new QName("status"));
    Assert.assertEquals("asserting status attribute", "disabled", temp.getAttributeValue());
}
Also used : SynapseImport(org.apache.synapse.libraries.imports.SynapseImport) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute) Test(org.junit.Test)

Example 15 with SynapseImport

use of org.apache.synapse.libraries.imports.SynapseImport in project wso2-synapse by wso2.

the class SynapseImportSerializerTest method testSerializeImportNoLibPackage.

/**
 * Test serializeImport with incomplete synapseImport.
 */
@Test
public void testSerializeImportNoLibPackage() {
    SynapseImport synapseImport = new SynapseImport();
    synapseImport.setLibName("testLibName");
    thrown.expect(SynapseException.class);
    thrown.expectMessage("Invalid Synapse Import. Target Library package is required");
    SynapseImportSerializer.serializeImport(synapseImport);
}
Also used : SynapseImport(org.apache.synapse.libraries.imports.SynapseImport) Test(org.junit.Test)

Aggregations

SynapseImport (org.apache.synapse.libraries.imports.SynapseImport)16 OMElement (org.apache.axiom.om.OMElement)6 Library (org.apache.synapse.libraries.model.Library)5 Test (org.junit.Test)5 IOException (java.io.IOException)3 QName (javax.xml.namespace.QName)3 OMAttribute (org.apache.axiom.om.OMAttribute)3 DeploymentException (org.apache.axis2.deployment.DeploymentException)3 SynapseException (org.apache.synapse.SynapseException)3 File (java.io.File)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Properties (java.util.Properties)1 OMFactory (org.apache.axiom.om.OMFactory)1 Startup (org.apache.synapse.Startup)1 PriorityExecutor (org.apache.synapse.commons.executors.PriorityExecutor)1 Entry (org.apache.synapse.config.Entry)1