Search in sources :

Example 1 with Extension

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

the class DefinitionParserHelper method addEventSource.

public static void addEventSource(StreamDefinition streamDefinition, ConcurrentMap<String, List<Source>> eventSourceMap, SiddhiAppContext siddhiAppContext) {
    for (Annotation sourceAnnotation : streamDefinition.getAnnotations()) {
        if (SiddhiConstants.ANNOTATION_SOURCE.equalsIgnoreCase(sourceAnnotation.getName())) {
            try {
                sourceAnnotation = updateAnnotationRef(sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE, siddhiAppContext);
                Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sourceAnnotation.getAnnotations());
                if (mapAnnotation == null) {
                    mapAnnotation = Annotation.annotation(SiddhiConstants.ANNOTATION_MAP).element(SiddhiConstants.ANNOTATION_ELEMENT_TYPE, "passThrough");
                }
                final String sourceType = sourceAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                if (sourceType == null) {
                    throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + sourceAnnotation + "'", sourceAnnotation, siddhiAppContext);
                }
                final String mapType = mapAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                if (mapType == null) {
                    throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + mapAnnotation + "'", mapAnnotation, siddhiAppContext);
                }
                SourceHandlerManager sourceHandlerManager = siddhiAppContext.getSiddhiContext().getSourceHandlerManager();
                SourceHandler sourceHandler = null;
                if (sourceHandlerManager != null) {
                    sourceHandler = sourceHandlerManager.generateSourceHandler(sourceType);
                }
                // load input transport extension
                Extension sourceExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SOURCE, sourceType, sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE);
                Source source = (Source) SiddhiClassLoader.loadExtensionImplementation(sourceExtension, SourceExecutorExtensionHolder.getInstance(siddhiAppContext));
                ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sourceExtension.getNamespace(), sourceExtension.getName());
                // load input mapper extension
                Extension mapperExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_MAP, mapType, sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE_MAPPER);
                SourceMapper sourceMapper = (SourceMapper) SiddhiClassLoader.loadExtensionImplementation(mapperExtension, SourceMapperExecutorExtensionHolder.getInstance(siddhiAppContext));
                ConfigReader mapperConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(mapperExtension.getNamespace(), mapperExtension.getName());
                validateSourceMapperCompatibility(streamDefinition, sourceType, mapType, source, sourceMapper, sourceAnnotation);
                io.siddhi.annotation.Extension sourceExt = source.getClass().getAnnotation(io.siddhi.annotation.Extension.class);
                OptionHolder sourceOptionHolder = constructOptionHolder(streamDefinition, sourceAnnotation, sourceExt, null, true);
                Map<String, String> deploymentProperties = createDeploymentProperties(sourceAnnotation, sourceExt);
                OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sourceMapper.getClass().getAnnotation(io.siddhi.annotation.Extension.class), null, false);
                AttributesHolder attributesHolder = getAttributeMappings(mapAnnotation, mapType, streamDefinition);
                String[] transportPropertyNames = getTransportPropertyNames(attributesHolder);
                source.init(sourceType, sourceOptionHolder, sourceMapper, transportPropertyNames, configReader, mapType, mapOptionHolder, attributesHolder.payloadMappings, attributesHolder.transportMappings, mapperConfigReader, sourceHandler, streamDefinition, deploymentProperties, siddhiAppContext);
                if (sourceHandlerManager != null) {
                    sourceHandlerManager.registerSourceHandler(sourceHandler.getId(), sourceHandler);
                }
                List<Source> eventSources = eventSourceMap.get(streamDefinition.getId());
                if (eventSources == null) {
                    eventSources = new ArrayList<>();
                    eventSources.add(source);
                    eventSourceMap.put(streamDefinition.getId(), eventSources);
                } else {
                    eventSources.add(source);
                }
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, sourceAnnotation, siddhiAppContext);
                throw t;
            }
        }
    }
}
Also used : SourceHandler(io.siddhi.core.stream.input.source.SourceHandler) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SourceHandlerManager(io.siddhi.core.stream.input.source.SourceHandlerManager) ConfigReader(io.siddhi.core.util.config.ConfigReader) Annotation(io.siddhi.query.api.annotation.Annotation) Source(io.siddhi.core.stream.input.source.Source) Extension(io.siddhi.query.api.extension.Extension) OptionHolder(io.siddhi.core.util.transport.OptionHolder) SourceMapper(io.siddhi.core.stream.input.source.SourceMapper)

Example 2 with Extension

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

the class DefinitionParserHelper method addEventSink.

public static void addEventSink(StreamDefinition streamDefinition, StreamJunction streamJunction, ConcurrentMap<String, List<Sink>> eventSinkMap, SiddhiAppContext siddhiAppContext) {
    for (Annotation sinkAnnotation : streamDefinition.getAnnotations()) {
        if (SiddhiConstants.ANNOTATION_SINK.equalsIgnoreCase(sinkAnnotation.getName())) {
            try {
                sinkAnnotation = updateAnnotationRef(sinkAnnotation, SiddhiConstants.NAMESPACE_SINK, siddhiAppContext);
                Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sinkAnnotation.getAnnotations());
                String sinkType = sinkAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                if (sinkType == null) {
                    throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + sinkAnnotation + "'", sinkAnnotation, siddhiAppContext);
                }
                if (mapAnnotation == null) {
                    mapAnnotation = Annotation.annotation(SiddhiConstants.ANNOTATION_MAP).element(SiddhiConstants.ANNOTATION_ELEMENT_TYPE, "passThrough");
                }
                Annotation distributionAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_DISTRIBUTION, sinkAnnotation.getAnnotations());
                if (mapAnnotation != null) {
                    String[] supportedDynamicOptions = null;
                    List<OptionHolder> destinationOptHolders = new ArrayList<>();
                    List<Map<String, String>> destinationDeploymentProperties = new ArrayList<>();
                    Extension sinkExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, sinkType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK);
                    ConfigReader sinkConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sinkExtension.getNamespace(), sinkExtension.getName());
                    final boolean isDistributedTransport = (distributionAnnotation != null);
                    boolean isMultiClient = false;
                    if (isDistributedTransport) {
                        Sink sink = createSink(sinkExtension, siddhiAppContext);
                        isMultiClient = isMultiClientDistributedTransport(sink, streamDefinition, distributionAnnotation, siddhiAppContext);
                        supportedDynamicOptions = sink.getSupportedDynamicOptions();
                        destinationOptHolders = createDestinationOptionHolders(distributionAnnotation, streamDefinition, sink, siddhiAppContext);
                        destinationDeploymentProperties = createDestinationDeploymentProperties(distributionAnnotation, sink);
                    }
                    final String mapType = mapAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                    if (mapType != null) {
                        Sink sink;
                        if (isDistributedTransport) {
                            sink = (isMultiClient) ? new MultiClientDistributedSink() : new SingleClientDistributedSink();
                        } else {
                            sink = createSink(sinkExtension, siddhiAppContext);
                        }
                        if (supportedDynamicOptions == null) {
                            supportedDynamicOptions = sink.getSupportedDynamicOptions();
                        }
                        // load output mapper extension
                        Extension mapperExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_MAP, mapType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK_MAPPER);
                        ConfigReader mapperConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sinkExtension.getNamespace(), sinkExtension.getName());
                        SinkMapper sinkMapper = (SinkMapper) SiddhiClassLoader.loadExtensionImplementation(mapperExtension, SinkMapperExecutorExtensionHolder.getInstance(siddhiAppContext));
                        io.siddhi.annotation.Extension sinkExt = sink.getClass().getAnnotation(io.siddhi.annotation.Extension.class);
                        OptionHolder transportOptionHolder = constructOptionHolder(streamDefinition, sinkAnnotation, sinkExt, supportedDynamicOptions, true);
                        Map<String, String> deploymentProperties = createDeploymentProperties(sinkAnnotation, sinkExt);
                        OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sinkMapper.getClass().getAnnotation(io.siddhi.annotation.Extension.class), sinkMapper.getSupportedDynamicOptions(), false);
                        List<Element> payloadElementList = getPayload(mapAnnotation);
                        OptionHolder distributionOptHolder = null;
                        SinkHandlerManager sinkHandlerManager = siddhiAppContext.getSiddhiContext().getSinkHandlerManager();
                        SinkHandler sinkHandler = null;
                        if (sinkHandlerManager != null) {
                            sinkHandler = sinkHandlerManager.generateSinkHandler();
                        }
                        if (isDistributedTransport) {
                            distributionOptHolder = constructOptionHolder(streamDefinition, distributionAnnotation, sinkExt, supportedDynamicOptions, true);
                            String strategyType = distributionOptHolder.validateAndGetStaticValue(SiddhiConstants.DISTRIBUTION_STRATEGY_KEY);
                            Extension strategyExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, strategyType, sinkAnnotation, SiddhiConstants.NAMESPACE_DISTRIBUTION_STRATEGY);
                            ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(strategyExtension.getNamespace(), strategyExtension.getName());
                            DistributionStrategy distributionStrategy = (DistributionStrategy) SiddhiClassLoader.loadExtensionImplementation(strategyExtension, DistributionStrategyExtensionHolder.getInstance(siddhiAppContext));
                            distributionStrategy.init(streamDefinition, transportOptionHolder, distributionOptHolder, destinationOptHolders, configReader);
                            ((DistributedTransport) sink).init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, streamJunction, siddhiAppContext, destinationOptHolders, sinkAnnotation, distributionStrategy, supportedDynamicOptions, deploymentProperties, destinationDeploymentProperties);
                        } else {
                            sink.init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, deploymentProperties, streamJunction, siddhiAppContext);
                        }
                        if (sinkHandlerManager != null) {
                            sinkHandlerManager.registerSinkHandler(sinkHandler.getId(), sinkHandler);
                        }
                        validateSinkMapperCompatibility(streamDefinition, sinkType, mapType, sink, sinkMapper, sinkAnnotation);
                        // Setting the output group determiner
                        OutputGroupDeterminer groupDeterminer = constructOutputGroupDeterminer(transportOptionHolder, distributionOptHolder, streamDefinition, destinationOptHolders.size());
                        if (groupDeterminer != null) {
                            sink.getMapper().setGroupDeterminer(groupDeterminer);
                        }
                        List<Sink> eventSinks = eventSinkMap.get(streamDefinition.getId());
                        if (eventSinks == null) {
                            eventSinks = new ArrayList<>();
                            eventSinks.add(sink);
                            eventSinkMap.put(streamDefinition.getId(), eventSinks);
                        } else {
                            eventSinks.add(sink);
                        }
                    } else {
                        throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + mapAnnotation + "'", mapAnnotation, siddhiAppContext);
                    }
                } else {
                    throw new SiddhiAppCreationException("Both @sink(type=) and @map(type=) are required.", sinkAnnotation.getQueryContextStartIndex(), sinkAnnotation.getQueryContextEndIndex());
                }
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, sinkAnnotation, siddhiAppContext);
                throw t;
            }
        }
    }
}
Also used : Element(io.siddhi.query.api.annotation.Element) ArrayList(java.util.ArrayList) OutputGroupDeterminer(io.siddhi.core.stream.output.sink.OutputGroupDeterminer) SingleClientDistributedSink(io.siddhi.core.util.transport.SingleClientDistributedSink) SingleClientDistributedSink(io.siddhi.core.util.transport.SingleClientDistributedSink) MultiClientDistributedSink(io.siddhi.core.util.transport.MultiClientDistributedSink) Sink(io.siddhi.core.stream.output.sink.Sink) OptionHolder(io.siddhi.core.util.transport.OptionHolder) SinkMapper(io.siddhi.core.stream.output.sink.SinkMapper) SinkHandlerManager(io.siddhi.core.stream.output.sink.SinkHandlerManager) SinkHandler(io.siddhi.core.stream.output.sink.SinkHandler) DistributedTransport(io.siddhi.core.stream.output.sink.distributed.DistributedTransport) DistributionStrategy(io.siddhi.core.stream.output.sink.distributed.DistributionStrategy) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(io.siddhi.core.util.config.ConfigReader) Annotation(io.siddhi.query.api.annotation.Annotation) Extension(io.siddhi.query.api.extension.Extension) MultiClientDistributedSink(io.siddhi.core.util.transport.MultiClientDistributedSink) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 3 with Extension

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

the class MultiClientDistributedSink method initTransport.

@Override
public void initTransport(OptionHolder sinkOptionHolder, List<OptionHolder> destinationOptionHolders, Map<String, String> deploymentProperties, List<Map<String, String>> destinationDeploymentProperties, Annotation sinkAnnotation, ConfigReader sinkConfigReader, DistributionStrategy strategy, String type, SiddhiAppContext siddhiAppContext) {
    String transportType = sinkOptionHolder.validateAndGetStaticValue(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
    Extension sinkExtension = DefinitionParserHelper.constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, transportType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK);
    for (int i = 0; i < destinationOptionHolders.size(); i++) {
        OptionHolder destinationOption = destinationOptionHolders.get(i);
        Sink sink = (Sink) SiddhiClassLoader.loadExtensionImplementation(sinkExtension, SinkExecutorExtensionHolder.getInstance(siddhiAppContext));
        destinationOption.merge(sinkOptionHolder);
        sink.initOnlyTransport(streamDefinition, destinationOption, sinkConfigReader, type, new MultiClientConnectionCallback(transports.size(), strategy), destinationDeploymentProperties.get(i), siddhiAppContext);
        if (!sink.getServiceDeploymentInfoList().isEmpty()) {
            ((ServiceDeploymentInfo) sink.getServiceDeploymentInfoList().get(0)).addDeploymentProperties(deploymentProperties);
        }
        transports.add(sink);
    }
}
Also used : Extension(io.siddhi.query.api.extension.Extension) Sink(io.siddhi.core.stream.output.sink.Sink) ServiceDeploymentInfo(io.siddhi.core.stream.ServiceDeploymentInfo)

Example 4 with Extension

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

the class SingleClientDistributedSink method initTransport.

@Override
public void initTransport(OptionHolder sinkOptionHolder, List<OptionHolder> destinationOptionHolders, Map<String, String> deploymentProperties, List<Map<String, String>> destinationDeploymentProperties, Annotation sinkAnnotation, ConfigReader sinkConfigReader, DistributionStrategy strategy, String type, 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, type, new SingleClientConnectionCallback(destinationCount, strategy), destinationDeploymentProperties.get(0), siddhiAppContext);
    if (!this.sink.getServiceDeploymentInfoList().isEmpty()) {
        ((ServiceDeploymentInfo) this.sink.getServiceDeploymentInfoList().get(0)).addDeploymentProperties(deploymentProperties);
    }
}
Also used : Extension(io.siddhi.query.api.extension.Extension) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) ServiceDeploymentInfo(io.siddhi.core.stream.ServiceDeploymentInfo)

Example 5 with Extension

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

the class DefinitionParserHelper method addFunction.

public static void addFunction(SiddhiAppContext siddhiAppContext, final FunctionDefinition functionDefinition) {
    Extension extension = new Extension() {

        @Override
        public String getNamespace() {
            return "script";
        }

        @Override
        public String getName() {
            return functionDefinition.getLanguage().toLowerCase();
        }
    };
    try {
        Script script = (Script) SiddhiClassLoader.loadExtensionImplementation(extension, ScriptExtensionHolder.getInstance(siddhiAppContext));
        ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(extension.getNamespace(), extension.getName());
        script.setReturnType(functionDefinition.getReturnType());
        script.init(functionDefinition.getId(), functionDefinition.getBody(), configReader);
        siddhiAppContext.getScriptFunctionMap().put(functionDefinition.getId(), script);
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, functionDefinition, siddhiAppContext);
        throw t;
    }
}
Also used : Extension(io.siddhi.query.api.extension.Extension) Script(io.siddhi.core.function.Script) ConfigReader(io.siddhi.core.util.config.ConfigReader)

Aggregations

Extension (io.siddhi.query.api.extension.Extension)7 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)4 ConfigReader (io.siddhi.core.util.config.ConfigReader)4 Annotation (io.siddhi.query.api.annotation.Annotation)3 ServiceDeploymentInfo (io.siddhi.core.stream.ServiceDeploymentInfo)2 Sink (io.siddhi.core.stream.output.sink.Sink)2 OptionHolder (io.siddhi.core.util.transport.OptionHolder)2 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)1 StreamEventCloner (io.siddhi.core.event.stream.StreamEventCloner)1 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)1 Script (io.siddhi.core.function.Script)1 Source (io.siddhi.core.stream.input.source.Source)1 SourceHandler (io.siddhi.core.stream.input.source.SourceHandler)1 SourceHandlerManager (io.siddhi.core.stream.input.source.SourceHandlerManager)1 SourceMapper (io.siddhi.core.stream.input.source.SourceMapper)1 OutputGroupDeterminer (io.siddhi.core.stream.output.sink.OutputGroupDeterminer)1 SinkHandler (io.siddhi.core.stream.output.sink.SinkHandler)1 SinkHandlerManager (io.siddhi.core.stream.output.sink.SinkHandlerManager)1 SinkMapper (io.siddhi.core.stream.output.sink.SinkMapper)1 DistributedTransport (io.siddhi.core.stream.output.sink.distributed.DistributedTransport)1