Search in sources :

Example 76 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class SiddhiCompiler method parseStreamDefinition.

public static StreamDefinition parseStreamDefinition(String source) {
    ANTLRInputStream input = new ANTLRInputStream(source);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.definition_stream_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (StreamDefinition) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) SiddhiQLBaseVisitorImpl(org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 77 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project wso2-synapse by wso2.

the class EventSourceFactory method createStaticSubscriptions.

/**
 * Generate the static subscriptions
 *
 * @param elem containing the static subscription configurations
 * @param synapseEventSource event source to which the static subscriptions belong to
 * @throws EventException in-case of a failure in creating static subscriptions
 */
private static void createStaticSubscriptions(OMElement elem, SynapseEventSource synapseEventSource) throws EventException {
    for (Iterator iterator = elem.getChildrenWithName(SUBSCRIPTION_QNAME); iterator.hasNext(); ) {
        SynapseSubscription synapseSubscription = new SynapseSubscription();
        OMElement elmSubscription = (OMElement) iterator.next();
        synapseSubscription.setId(elmSubscription.getAttribute(ID_QNAME).getAttributeValue());
        // process the filter
        OMElement elmFilter = elmSubscription.getFirstChildWithName(FILTER_QNAME);
        OMAttribute dialectAttr = elmFilter.getAttribute(FILTER_DIALECT_QNAME);
        if (dialectAttr != null && dialectAttr.getAttributeValue() != null) {
            OMAttribute sourceAttr = elmFilter.getAttribute(FILTER_SOURCE_QNAME);
            if (sourceAttr != null) {
                synapseSubscription.setFilterDialect(dialectAttr.getAttributeValue());
                synapseSubscription.setFilterValue(sourceAttr.getAttributeValue());
            } else {
                handleException("Error in creating static subscription. Filter source not defined");
            }
        } else {
            handleException("Error in creating static subscription. Filter dialect not defined");
        }
        OMElement elmEndpoint = elmSubscription.getFirstChildWithName(ENDPOINT_QNAME);
        if (elmEndpoint != null) {
            OMElement elmAddress = elmEndpoint.getFirstChildWithName(ADDRESS_QNAME);
            if (elmAddress != null) {
                OMAttribute uriAttr = elmAddress.getAttribute(EP_URI_QNAME);
                if (uriAttr != null) {
                    synapseSubscription.setEndpointUrl(uriAttr.getAttributeValue());
                    synapseSubscription.setAddressUrl(uriAttr.getAttributeValue());
                } else {
                    handleException("Error in creating static subscription. URI not defined");
                }
            } else {
                handleException("Error in creating static subscription. Address not defined");
            }
        } else {
            handleException("Error in creating static subscription. Endpoint not defined");
        }
        OMElement elmExpires = elmSubscription.getFirstChildWithName(EXPIRES_QNAME);
        if (elmExpires != null) {
            try {
                if (elmExpires.getText().startsWith("P")) {
                    synapseSubscription.setExpires(ConverterUtil.convertToDuration(elmExpires.getText()).getAsCalendar());
                } else {
                    synapseSubscription.setExpires(ConverterUtil.convertToDateTime(elmExpires.getText()));
                }
            } catch (Exception e) {
                handleException("Error in creating static subscription. invalid date format", e);
            }
        } else {
            synapseSubscription.setExpires(null);
        }
        synapseSubscription.setStaticEntry(true);
        synapseEventSource.getSubscriptionManager().subscribe(synapseSubscription);
    }
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute) SynapseException(org.apache.synapse.SynapseException) EventException(org.wso2.eventing.exceptions.EventException)

Example 78 with Source

use of org.wso2.siddhi.core.stream.input.source.Source 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 79 with Source

use of org.wso2.siddhi.core.stream.input.source.Source 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)

Example 80 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project wso2-dss-connectors by wso2-attic.

the class MongoDBDataSource method init.

@Override
public void init(Map<String, String> params) throws DataServiceFault {
    String serversParam = params.get(MongoDBDSConstants.SERVERS);
    if (DBUtils.isEmptyString(serversParam)) {
        throw new DataServiceFault("The data source param '" + MongoDBDSConstants.SERVERS + "' is required");
    }
    String[] servers = serversParam.split(",");
    String database = params.get(MongoDBDSConstants.DATABASE);
    if (DBUtils.isEmptyString(database)) {
        throw new DataServiceFault("The data source param '" + MongoDBDSConstants.DATABASE + "' is required");
    }
    try {
        this.mongo = new Mongo(this.createServerAddresses(servers), this.extractMongoOptions(params));
        String writeConcern = params.get(MongoDBDSConstants.WRITE_CONCERN);
        if (!DBUtils.isEmptyString(writeConcern)) {
            this.getMongo().setWriteConcern(WriteConcern.valueOf(writeConcern));
        }
        String readPref = params.get(MongoDBDSConstants.READ_PREFERENCE);
        if (!DBUtils.isEmptyString(readPref)) {
            this.getMongo().setReadPreference(ReadPreference.valueOf(readPref));
        }
        this.jongo = new Jongo(this.getMongo().getDB(database));
    } catch (Exception e) {
        throw new DataServiceFault(e);
    }
}
Also used : Mongo(com.mongodb.Mongo) Jongo(org.jongo.Jongo) DataServiceFault(org.wso2.carbon.dataservices.core.DataServiceFault)

Aggregations

Test (org.testng.annotations.Test)22 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 ArrayList (java.util.ArrayList)11 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)11 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)11 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)11 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)8 ParseTree (org.antlr.v4.runtime.tree.ParseTree)8 OMElement (org.apache.axiom.om.OMElement)8 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)8 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)8 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)8 Event (org.wso2.siddhi.core.event.Event)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Compiler (org.wso2.ballerinalang.compiler.Compiler)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 SiddhiQLBaseVisitorImpl (org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)6