Search in sources :

Example 6 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.

the class ExtensionTestCase method extensionTest4.

@Test
public void extensionTest4() throws InterruptedException, ClassNotFoundException {
    log.info("extension test4");
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);
    siddhiManager.setExtension("email:getAll", StringConcatAggregatorString.class);
    String cseEventStream = "define stream cseEventStream (price long, volume long);";
    String query = ("@info(name = 'query1') from cseEventStream select  custom:plus(*) as totalCount " + "insert into mailOutput;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            for (Event inEvent : inEvents) {
                count++;
                if (count == 1) {
                    AssertJUnit.assertEquals(800L, inEvent.getData(0));
                } else if (count == 2) {
                    AssertJUnit.assertEquals(805L, inEvent.getData(0));
                } else if (count == 3) {
                    AssertJUnit.assertEquals(260L, inEvent.getData(0));
                }
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 700L, 100L });
    inputHandler.send(new Object[] { 605L, 200L });
    inputHandler.send(new Object[] { 60L, 200L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) StringConcatAggregatorString(org.wso2.siddhi.core.query.extension.util.StringConcatAggregatorString) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 7 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.

the class ExtensionTestCase method extensionTest3.

@Test
public void extensionTest3() throws InterruptedException {
    log.info("extension test3");
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);
    siddhiManager.setExtension("email:getAllNew", StringConcatAggregatorString.class);
    String cseEventStream = "" + "" + "define stream cseEventStream (symbol string, price float, volume long);";
    String query = ("" + "@info(name = 'query1') " + "from cseEventStream " + "select price , email:getAllNew(symbol,'') as toConcat " + "group by volume " + "insert into mailOutput;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            count = count + inEvents.length;
            if (count == 3) {
                AssertJUnit.assertEquals("WSO2ABC", inEvents[inEvents.length - 1].getData(1));
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 100L });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 60.5f, 200L });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "ABC", 60.5f, 200L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) StringConcatAggregatorString(org.wso2.siddhi.core.query.extension.util.StringConcatAggregatorString) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 8 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.

the class SingleClientDistributedSink method initTransport.

@Override
public void initTransport(OptionHolder sinkOptionHolder, List<OptionHolder> destinationOptionHolders, Annotation sinkAnnotation, ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
    final String transportType = sinkOptionHolder.validateAndGetStaticValue(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
    Extension sinkExtension = DefinitionParserHelper.constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, transportType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK);
    Set<String> allDynamicOptionKeys = findAllDynamicOptions(destinationOptionHolders);
    destinationOptionHolders.forEach(optionHolder -> {
        optionHolder.merge(sinkOptionHolder);
        allDynamicOptionKeys.forEach(optionKey -> {
            String optionValue = optionHolder.getOrCreateOption(optionKey, null).getValue();
            if (optionValue == null || optionValue.isEmpty()) {
                throw new SiddhiAppValidationException("Destination properties can only contain " + "non-empty static values.");
            }
            Option sinkOption = sinkOptionHolder.getOrAddStaticOption(optionKey, optionValue);
            sinkOption.addVariableValue(optionValue);
            destinationCount++;
        });
    });
    this.sink = (Sink) SiddhiClassLoader.loadExtensionImplementation(sinkExtension, SinkExecutorExtensionHolder.getInstance(siddhiAppContext));
    this.sink.initOnlyTransport(streamDefinition, sinkOptionHolder, sinkConfigReader, siddhiAppContext);
}
Also used : Extension(org.wso2.siddhi.query.api.extension.Extension) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)

Example 9 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.

the class DocumentationUtils method addExtensionMetaDataIntoNamespaceList.

/**
 * Generate extension meta data from the annotated data in the class
 *
 * @param namespaceList  The list of namespaces to which the new extension will be added
 * @param extensionClass Class from which meta data should be extracted from
 * @param logger         The maven plugin logger
 */
private static void addExtensionMetaDataIntoNamespaceList(List<NamespaceMetaData> namespaceList, Class<?> extensionClass, Log logger) {
    Extension extensionAnnotation = extensionClass.getAnnotation(Extension.class);
    if (extensionAnnotation != null) {
        // Discarding extension classes without annotation
        ExtensionMetaData extensionMetaData = new ExtensionMetaData();
        // Finding extension type
        String extensionType = null;
        for (Map.Entry<ExtensionType, Class<?>> entry : ExtensionType.getSuperClassMap().entrySet()) {
            Class<?> superClass = entry.getValue();
            if (superClass.isAssignableFrom(extensionClass) && superClass != extensionClass) {
                extensionType = entry.getKey().getValue();
                break;
            }
        }
        // Discarding the extension if it belongs to an unknown type
        if (extensionType == null) {
            logger.warn("Discarding extension (belonging to an unknown extension type): " + extensionClass.getCanonicalName());
            return;
        }
        extensionMetaData.setName(extensionAnnotation.name());
        extensionMetaData.setDescription(extensionAnnotation.description());
        // Adding query parameters
        ParameterMetaData[] parameters = new ParameterMetaData[extensionAnnotation.parameters().length];
        for (int i = 0; i < extensionAnnotation.parameters().length; i++) {
            Parameter parameterAnnotation = extensionAnnotation.parameters()[i];
            ParameterMetaData parameter = new ParameterMetaData();
            parameter.setName(parameterAnnotation.name());
            parameter.setType(Arrays.asList(parameterAnnotation.type()));
            parameter.setDescription(parameterAnnotation.description());
            parameter.setOptional(parameterAnnotation.optional());
            parameter.setDynamic(parameterAnnotation.dynamic());
            parameter.setDefaultValue(parameterAnnotation.defaultValue());
            parameters[i] = parameter;
        }
        extensionMetaData.setParameters(Arrays.asList(parameters));
        // Adding system parameters
        SystemParameterMetaData[] systemParameters = new SystemParameterMetaData[extensionAnnotation.systemParameter().length];
        for (int i = 0; i < extensionAnnotation.systemParameter().length; i++) {
            SystemParameter systemParameterAnnotation = extensionAnnotation.systemParameter()[i];
            SystemParameterMetaData systemParameter = new SystemParameterMetaData();
            systemParameter.setName(systemParameterAnnotation.name());
            systemParameter.setDescription(systemParameterAnnotation.description());
            systemParameter.setDefaultValue(systemParameterAnnotation.defaultValue());
            systemParameter.setPossibleParameters(Arrays.asList(systemParameterAnnotation.possibleParameters()));
            systemParameters[i] = systemParameter;
        }
        extensionMetaData.setSystemParameters(Arrays.asList(systemParameters));
        // Adding return attributes
        ReturnAttributeMetaData[] returnAttributes = new ReturnAttributeMetaData[extensionAnnotation.returnAttributes().length];
        for (int i = 0; i < extensionAnnotation.returnAttributes().length; i++) {
            ReturnAttribute parameterAnnotation = extensionAnnotation.returnAttributes()[i];
            ReturnAttributeMetaData returnAttribute = new ReturnAttributeMetaData();
            returnAttribute.setName(parameterAnnotation.name());
            returnAttribute.setType(Arrays.asList(parameterAnnotation.type()));
            returnAttribute.setDescription(parameterAnnotation.description());
            returnAttributes[i] = returnAttribute;
        }
        extensionMetaData.setReturnAttributes(Arrays.asList(returnAttributes));
        // Adding examples
        ExampleMetaData[] examples = new ExampleMetaData[extensionAnnotation.examples().length];
        for (int i = 0; i < extensionAnnotation.examples().length; i++) {
            Example exampleAnnotation = extensionAnnotation.examples()[i];
            ExampleMetaData exampleMetaData = new ExampleMetaData();
            exampleMetaData.setSyntax(exampleAnnotation.syntax());
            exampleMetaData.setDescription(exampleAnnotation.description());
            examples[i] = exampleMetaData;
        }
        extensionMetaData.setExamples(Arrays.asList(examples));
        // Finding the namespace
        String namespaceName = extensionAnnotation.namespace();
        if (Objects.equals(namespaceName, "")) {
            namespaceName = Constants.CORE_NAMESPACE;
        }
        // Finding the relevant namespace in the namespace list
        NamespaceMetaData namespace = null;
        for (NamespaceMetaData existingNamespace : namespaceList) {
            if (Objects.equals(existingNamespace.getName(), namespaceName)) {
                namespace = existingNamespace;
                break;
            }
        }
        // Creating namespace if it doesn't exist
        if (namespace == null) {
            namespace = new NamespaceMetaData();
            namespace.setName(namespaceName);
            namespace.setExtensionMap(new TreeMap<>());
            namespaceList.add(namespace);
        }
        // Adding to the relevant extension metadata list in the namespace
        List<ExtensionMetaData> extensionMetaDataList = namespace.getExtensionMap().computeIfAbsent(extensionType, k -> new ArrayList<>());
        extensionMetaDataList.add(extensionMetaData);
    }
}
Also used : ReturnAttribute(org.wso2.siddhi.annotation.ReturnAttribute) Example(org.wso2.siddhi.annotation.Example) SystemParameter(org.wso2.siddhi.annotation.SystemParameter) ExampleMetaData(org.wso2.siddhi.doc.gen.commons.metadata.ExampleMetaData) NamespaceMetaData(org.wso2.siddhi.doc.gen.commons.metadata.NamespaceMetaData) Extension(org.wso2.siddhi.annotation.Extension) ReturnAttributeMetaData(org.wso2.siddhi.doc.gen.commons.metadata.ReturnAttributeMetaData) ExtensionType(org.wso2.siddhi.doc.gen.commons.metadata.ExtensionType) ExtensionMetaData(org.wso2.siddhi.doc.gen.commons.metadata.ExtensionMetaData) SystemParameterMetaData(org.wso2.siddhi.doc.gen.commons.metadata.SystemParameterMetaData) SystemParameter(org.wso2.siddhi.annotation.SystemParameter) Parameter(org.wso2.siddhi.annotation.Parameter) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) ParameterMetaData(org.wso2.siddhi.doc.gen.commons.metadata.ParameterMetaData) SystemParameterMetaData(org.wso2.siddhi.doc.gen.commons.metadata.SystemParameterMetaData)

Example 10 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.

the class DocumentationUtils method getExtensionMetaData.

/**
 * Returns the extension extension meta data
 * Gets the meta data from the siddhi manager
 *
 * @param targetDirectoryPath The path of the target directory of the maven module containing extensions
 * @param logger              The maven plugin logger
 * @return NamespaceMetaData namespace meta data list
 * @throws MojoFailureException   If this fails to access project dependencies
 * @throws MojoExecutionException If the classes directory from which classes are loaded is invalid
 */
public static List<NamespaceMetaData> getExtensionMetaData(String targetDirectoryPath, List<String> runtimeClasspathElements, Log logger) throws MojoFailureException, MojoExecutionException {
    List<NamespaceMetaData> namespaceMetaDataList = new ArrayList<NamespaceMetaData>();
    // +1 to include the module's target/classes folder
    int urlCount = runtimeClasspathElements.size() + 1;
    // Creating a list of URLs with all project dependencies
    URL[] urls = new URL[urlCount];
    for (int i = 0; i < runtimeClasspathElements.size(); i++) {
        try {
            urls[i] = new File(runtimeClasspathElements.get(i)).toURI().toURL();
        } catch (MalformedURLException e) {
            throw new MojoFailureException("Unable to access project dependency: " + runtimeClasspathElements.get(i), e);
        }
    }
    File classesDirectory = new File(targetDirectoryPath + File.separator + Constants.CLASSES_DIRECTORY);
    try {
        // Adding the generated classes to the class loader
        urls[urlCount - 1] = classesDirectory.toURI().toURL();
        ClassLoader urlClassLoader = AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> new URLClassLoader(urls, Thread.currentThread().getContextClassLoader()));
        // Getting extensions from all the class files in the classes directory
        addExtensionInDirectory(classesDirectory, classesDirectory.getAbsolutePath(), urlClassLoader, namespaceMetaDataList, logger);
    } catch (MalformedURLException e) {
        throw new MojoExecutionException("Invalid classes directory: " + classesDirectory.getAbsolutePath(), e);
    }
    for (NamespaceMetaData aNamespaceMetaData : namespaceMetaDataList) {
        for (List<ExtensionMetaData> extensionMetaData : aNamespaceMetaData.getExtensionMap().values()) {
            Collections.sort(extensionMetaData);
        }
    }
    Collections.sort(namespaceMetaDataList);
    return namespaceMetaDataList;
}
Also used : MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) NamespaceMetaData(org.wso2.siddhi.doc.gen.commons.metadata.NamespaceMetaData) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) ExtensionMetaData(org.wso2.siddhi.doc.gen.commons.metadata.ExtensionMetaData) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Aggregations

ArrayList (java.util.ArrayList)12 Attribute (org.wso2.charon3.core.attributes.Attribute)10 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)10 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)10 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)10 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)7 Extension (org.wso2.siddhi.query.api.extension.Extension)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 List (java.util.List)6 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)6 ConfigReader (org.wso2.siddhi.core.util.config.ConfigReader)6 Test (org.testng.annotations.Test)5 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)5 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)5 Event (org.wso2.siddhi.core.event.Event)5 StringConcatAggregatorString (org.wso2.siddhi.core.query.extension.util.StringConcatAggregatorString)5 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)5 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)5 Map (java.util.Map)4