Search in sources :

Example 1 with SubscriptionManager

use of org.wso2.eventing.SubscriptionManager in project wso2-synapse by wso2.

the class EventSourceFactory method createEventSource.

@SuppressWarnings({ "UnusedDeclaration" })
public static SynapseEventSource createEventSource(OMElement elem, Properties properties) {
    SynapseEventSource eventSource = null;
    OMAttribute name = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
    if (name == null) {
        handleException("The 'name' attribute is required for a event source de");
    } else {
        eventSource = new SynapseEventSource(name.getAttributeValue());
    }
    OMElement subscriptionManagerElem = elem.getFirstChildWithName(SUBSCRIPTION_MANAGER_QNAME);
    if (eventSource != null && subscriptionManagerElem != null) {
        OMAttribute clazz = subscriptionManagerElem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "class"));
        if (clazz != null) {
            String className = clazz.getAttributeValue();
            try {
                Class subscriptionManagerClass = Class.forName(className);
                SubscriptionManager manager = (SubscriptionManager) subscriptionManagerClass.newInstance();
                Iterator itr = subscriptionManagerElem.getChildrenWithName(PROPERTIES_QNAME);
                while (itr.hasNext()) {
                    OMElement propElem = (OMElement) itr.next();
                    String propName = propElem.getAttribute(new QName("name")).getAttributeValue();
                    String propValue = propElem.getAttribute(new QName("value")).getAttributeValue();
                    if (propName != null && !"".equals(propName.trim()) && propValue != null && !"".equals(propValue.trim())) {
                        propName = propName.trim();
                        propValue = propValue.trim();
                        PasswordManager passwordManager = PasswordManager.getInstance();
                        String key = eventSource.getName() + "." + propName;
                        if (passwordManager.isInitialized() && passwordManager.isTokenProtected(key)) {
                            eventSource.putConfigurationProperty(propName, propValue);
                            propValue = passwordManager.resolve(propValue);
                        }
                        manager.addProperty(propName, propValue);
                    }
                }
                eventSource.setSubscriptionManager(manager);
                eventSource.getSubscriptionManager().init();
            } catch (ClassNotFoundException e) {
                handleException("SubscriptionManager class not found", e);
            } catch (IllegalAccessException e) {
                handleException("Unable to access the SubscriptionManager object", e);
            } catch (InstantiationException e) {
                handleException("Unable to instantiate the SubscriptionManager object", e);
            }
        } else {
            handleException("SynapseSubscription manager class is a required attribute");
        }
    } else {
        handleException("SynapseSubscription Manager has not been specified for the event source");
    }
    try {
        createStaticSubscriptions(elem, eventSource);
    } catch (EventException e) {
        handleException("Static subscription creation failure", e);
    }
    return eventSource;
}
Also used : SynapseEventSource(org.apache.synapse.eventing.SynapseEventSource) EventException(org.wso2.eventing.exceptions.EventException) QName(javax.xml.namespace.QName) PasswordManager(org.wso2.securevault.PasswordManager) OMElement(org.apache.axiom.om.OMElement) SubscriptionManager(org.wso2.eventing.SubscriptionManager) Iterator(java.util.Iterator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 2 with SubscriptionManager

use of org.wso2.eventing.SubscriptionManager in project wso2-synapse by wso2.

the class EventSourceSerializer method serializeEventSource.

public static OMElement serializeEventSource(OMElement elem, SynapseEventSource eventSource) {
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace nullNS = fac.createOMNamespace(XMLConfigConstants.NULL_NAMESPACE, "");
    OMElement evenSourceElem = fac.createOMElement("eventSource", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
    if (eventSource.getName() != null) {
        evenSourceElem.addAttribute(fac.createOMAttribute("name", nullNS, eventSource.getName()));
    }
    if (eventSource.getSubscriptionManager() != null) {
        OMElement subManagerElem = fac.createOMElement("subscriptionManager", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
        subManagerElem.addAttribute(fac.createOMAttribute("class", nullNS, eventSource.getSubscriptionManager().getClass().getName()));
        Collection<String> names = (Collection<String>) eventSource.getSubscriptionManager().getPropertyNames();
        for (String name : names) {
            String value;
            if (eventSource.isContainsConfigurationProperty(name)) {
                value = eventSource.getConfigurationProperty(name);
            } else {
                value = eventSource.getSubscriptionManager().getPropertyValue(name);
            }
            OMElement propElem = fac.createOMElement("property", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
            propElem.addAttribute(fac.createOMAttribute("name", nullNS, name));
            propElem.addAttribute(fac.createOMAttribute("value", nullNS, value));
            subManagerElem.addChild(propElem);
        }
        evenSourceElem.addChild(subManagerElem);
        // Adding static subscriptions
        List<Subscription> staticSubscriptionList = eventSource.getSubscriptionManager().getStaticSubscriptions();
        for (Iterator<Subscription> iterator = staticSubscriptionList.iterator(); iterator.hasNext(); ) {
            Subscription staticSubscription = iterator.next();
            OMElement staticSubElem = fac.createOMElement("subscription", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
            staticSubElem.addAttribute(fac.createOMAttribute("id", nullNS, staticSubscription.getId()));
            OMElement filterElem = fac.createOMElement("filter", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
            filterElem.addAttribute(fac.createOMAttribute("source", nullNS, (String) staticSubscription.getFilterValue()));
            filterElem.addAttribute(fac.createOMAttribute("dialect", nullNS, (String) staticSubscription.getFilterDialect()));
            staticSubElem.addChild(filterElem);
            OMElement endpointElem = fac.createOMElement("endpoint", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
            OMElement addressElem = fac.createOMElement("address", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
            addressElem.addAttribute(fac.createOMAttribute("uri", nullNS, staticSubscription.getEndpointUrl()));
            endpointElem.addChild(addressElem);
            staticSubElem.addChild(endpointElem);
            if (staticSubscription.getExpires() != null) {
                OMElement expiresElem = fac.createOMElement("expires", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                fac.createOMText(expiresElem, ConverterUtil.convertToString(staticSubscription.getExpires()));
                staticSubElem.addChild(expiresElem);
            }
            evenSourceElem.addChild(staticSubElem);
        }
    }
    if (elem != null) {
        elem.addChild(evenSourceElem);
    }
    return evenSourceElem;
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) Collection(java.util.Collection) OMElement(org.apache.axiom.om.OMElement) Subscription(org.wso2.eventing.Subscription)

Example 3 with SubscriptionManager

use of org.wso2.eventing.SubscriptionManager in project wso2-synapse by wso2.

the class EventSourceSerializerTest method testSerializeEvent3.

/**
 * Test serializeEventSource with subscriptionManager and assert OMElement returned is not null.
 *
 * @throws XMLStreamException - XMLStreamException
 */
@Test
public void testSerializeEvent3() throws XMLStreamException {
    SynapseEventSource synapseEventSource = new SynapseEventSource("Test");
    SubscriptionManager subscriptionManager = new DefaultInMemorySubscriptionManager();
    synapseEventSource.setSubscriptionManager(subscriptionManager);
    OMElement omElement = EventSourceSerializer.serializeEventSource(null, synapseEventSource);
    Assert.assertNotNull("OMElement cannot be null.", omElement);
}
Also used : SynapseEventSource(org.apache.synapse.eventing.SynapseEventSource) OMElement(org.apache.axiom.om.OMElement) DefaultInMemorySubscriptionManager(org.apache.synapse.eventing.managers.DefaultInMemorySubscriptionManager) SubscriptionManager(org.wso2.eventing.SubscriptionManager) DefaultInMemorySubscriptionManager(org.apache.synapse.eventing.managers.DefaultInMemorySubscriptionManager) Test(org.junit.Test)

Example 4 with SubscriptionManager

use of org.wso2.eventing.SubscriptionManager in project wso2-synapse by wso2.

the class EventSourceSerializerTest method testSerializeEvent4.

/**
 * Test SerialEvent and assert OMElement returned is not null.
 *
 * @throws XMLStreamException - XMLStreamException
 * @throws EventException     - EventException
 */
@Test
public void testSerializeEvent4() throws XMLStreamException, EventException {
    String inputXML = "      <eventSource name=\"SampleEventSource\" xmlns=\"http://ws.apache.org/ns/synapse\">\n" + "            <subscriptionManager class=\"org.apache.synapse.eventing.managers." + "DefaultInMemorySubscriptionManager\">\n" + "                <property name=\"topicHeaderName\" value=\"Topic\"/>\n" + "                <property name=\"topicHeaderNS\" value=\"http://apache.org/aip\"/>\n" + "            </subscriptionManager>\n" + "            <subscription id=\"mySubscription\">\n" + "                 <filter source =\"synapse/event/test\" dialect=\"http://synapse.apache.org/" + "eventing/dialect/topicFilter\"/>\n" + "                 <endpoint><address uri=\"http://localhost:9000/services/" + "SimpleStockQuoteService\"/></endpoint>\n" + "            </subscription>\n" + "            <subscription id=\"mySubscription2\">\n" + "                 <filter source =\"synapse/event/test\" dialect=\"http://synapse.apache.org/" + "eventing/dialect/topicFilter\"/>\n" + "                 <endpoint><address uri=\"http://localhost:9000/services/" + "SimpleStockQuoteService\"/></endpoint>\n" + "                 <expires>2020-06-27T21:07:00.000-08:00</expires>\n" + "            </subscription>\n" + "      </eventSource>\n";
    OMElement element = AXIOMUtil.stringToOM(inputXML);
    SynapseEventSource synapseEventSource = new SynapseEventSource("Test");
    SubscriptionManager subscriptionManager = new DefaultInMemorySubscriptionManager();
    subscriptionManager.addProperty("Name", "Test");
    SynapseSubscription synapseSubscription = new SynapseSubscription();
    synapseSubscription.setStaticEntry(true);
    Date date = new Date(System.currentTimeMillis() + 3600000);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    synapseSubscription.setExpires(cal);
    subscriptionManager.subscribe(synapseSubscription);
    synapseEventSource.setSubscriptionManager(subscriptionManager);
    OMElement omElement = EventSourceSerializer.serializeEventSource(element, synapseEventSource);
    Assert.assertNotNull("OMElement cannot be null.", omElement);
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) SynapseEventSource(org.apache.synapse.eventing.SynapseEventSource) Calendar(java.util.Calendar) OMElement(org.apache.axiom.om.OMElement) DefaultInMemorySubscriptionManager(org.apache.synapse.eventing.managers.DefaultInMemorySubscriptionManager) SubscriptionManager(org.wso2.eventing.SubscriptionManager) DefaultInMemorySubscriptionManager(org.apache.synapse.eventing.managers.DefaultInMemorySubscriptionManager) Date(java.util.Date) Test(org.junit.Test)

Aggregations

OMElement (org.apache.axiom.om.OMElement)4 SynapseEventSource (org.apache.synapse.eventing.SynapseEventSource)3 SubscriptionManager (org.wso2.eventing.SubscriptionManager)3 DefaultInMemorySubscriptionManager (org.apache.synapse.eventing.managers.DefaultInMemorySubscriptionManager)2 Test (org.junit.Test)2 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 QName (javax.xml.namespace.QName)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMFactory (org.apache.axiom.om.OMFactory)1 OMNamespace (org.apache.axiom.om.OMNamespace)1 SynapseSubscription (org.apache.synapse.eventing.SynapseSubscription)1 Subscription (org.wso2.eventing.Subscription)1 EventException (org.wso2.eventing.exceptions.EventException)1 PasswordManager (org.wso2.securevault.PasswordManager)1