Java Code Examples for io.siddhi.core.util.transport.OptionHolder#validateAndGetStaticValue()

The following examples show how to use io.siddhi.core.util.transport.OptionHolder#validateAndGetStaticValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonSinkMapper.java    From siddhi-map-json with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize the mapper and the mapping configurations.
 *
 * @param streamDefinition          The stream definition
 * @param optionHolder              Option holder containing static and dynamic options
 * @param payloadTemplateBuilderMap Unmapped list of payloads for reference
 */
@Override
public void init(StreamDefinition streamDefinition, OptionHolder optionHolder,
                 Map<String, TemplateBuilder> payloadTemplateBuilderMap, ConfigReader mapperConfigReader,
                 SiddhiAppContext siddhiAppContext) {

    this.attributeNameArray = streamDefinition.getAttributeNameArray();
    this.enclosingElement = optionHolder.validateAndGetStaticValue(ENCLOSING_ELEMENT_IDENTIFIER, null);
    this.isJsonValidationEnabled = Boolean.parseBoolean(optionHolder
            .validateAndGetStaticValue(JSON_VALIDATION_IDENTIFIER, "false"));

    //if @payload() is added there must be at least 1 element in it, otherwise a SiddhiParserException raised
    if (payloadTemplateBuilderMap != null && payloadTemplateBuilderMap.size() != 1) {
        throw new SiddhiAppCreationException("Json sink-mapper does not support multiple @payload mappings, " +
                "error at the mapper of '" + streamDefinition.getId() + "'");
    }
    if (payloadTemplateBuilderMap != null &&
            payloadTemplateBuilderMap.get(payloadTemplateBuilderMap.keySet().iterator().next()).isObjectMessage()) {
        throw new SiddhiAppCreationException("Json sink-mapper does not support object @payload mappings, " +
                "error at the mapper of '" + streamDefinition.getId() + "'");
    }
}
 
Example 2
Source File: PartitionedDistributionStrategy.java    From siddhi with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize the Distribution strategy with the information it will require to make decisions.
 *
 * @param streamDefinition         The stream attached to the sink this DistributionStrategy is used in
 * @param transportOptionHolder    Sink options of the sink which uses this DistributionStrategy
 * @param destinationOptionHolders The list of options under @destination of the relevant sink.
 * @param configReader             This hold the {@link PartitionedDistributionStrategy} configuration reader.
 */
@Override
public void init(StreamDefinition streamDefinition, OptionHolder transportOptionHolder,
                 OptionHolder distributionOptionHolder, List<OptionHolder> destinationOptionHolders,
                 ConfigReader configReader) {
    totalDestinationCount = destinationOptionHolders.size();
    String partitionKey = distributionOptionHolder.validateAndGetStaticValue(SiddhiConstants
            .PARTITION_KEY_FIELD_KEY);

    if (partitionKey == null || partitionKey.isEmpty()) {
        throw new SiddhiAppValidationException("PartitionKey is required for partitioned distribution " +
                "strategy.");
    }
    try {
        int partitionKeyFieldPosition = streamDefinition.getAttributePosition(partitionKey);
        partitionOption = new Option(partitionKeyFieldPosition);
    } catch (AttributeNotExistException e) {
        throw new SiddhiAppValidationException("Could not find partition key attribute", e);
    }

}
 
Example 3
Source File: JsonSourceMapper.java    From siddhi-map-json with Apache License 2.0 5 votes vote down vote up
@Override
public void init(StreamDefinition streamDefinition, OptionHolder optionHolder,
                 List<AttributeMapping> attributeMappingList, ConfigReader configReader,
                 SiddhiAppContext siddhiAppContext) {

    this.streamDefinition = streamDefinition;
    this.streamAttributes = this.streamDefinition.getAttributeList();
    this.streamAttributesSize = this.streamDefinition.getAttributeList().size();
    this.failOnMissingAttribute = Boolean.parseBoolean(optionHolder.
            validateAndGetStaticValue(FAIL_ON_MISSING_ATTRIBUTE_IDENTIFIER, "true"));
    this.factory = new JsonFactory();
    if (attributeMappingList != null && attributeMappingList.size() > 0) {
        this.mappingPositions = new MappingPositionData[attributeMappingList.size()];
        isCustomMappingEnabled = true;
        enclosingElement = optionHolder.validateAndGetStaticValue(ENCLOSING_ELEMENT_IDENTIFIER,
                DEFAULT_ENCLOSING_ELEMENT);
        for (int i = 0; i < attributeMappingList.size(); i++) {
            AttributeMapping attributeMapping = attributeMappingList.get(i);
            String attributeName = attributeMapping.getName();
            int position = this.streamDefinition.getAttributePosition(attributeName);
            this.mappingPositions[i] = new MappingPositionData(position, attributeMapping.getMapping());
        }
    } else {
        this.mappingPositions = new MappingPositionData[streamAttributesSize];
        for (int i = 0; i < streamAttributesSize; i++) {
            this.mappingPositions[i] = new MappingPositionData(i, DEFAULT_JSON_MAPPING_PREFIX + this
                    .streamDefinition.getAttributeList().get(i).getName());
        }
    }
}
 
Example 4
Source File: TestTrpInMemorySource.java    From siddhi-map-json with Apache License 2.0 5 votes vote down vote up
@Override
public StateFactory<State> init(SourceEventListener sourceEventListener, OptionHolder optionHolder,
                                String[] requestedTransportPropertyNames, ConfigReader configReader,
                                SiddhiAppContext siddhiAppContext) {
    super.init(sourceEventListener, optionHolder, requestedTransportPropertyNames, configReader, siddhiAppContext);
    symbol = optionHolder.validateAndGetStaticValue("prop1");
    price = optionHolder.validateAndGetStaticValue("prop2");
    fail = optionHolder.validateAndGetStaticValue("fail", "false");
    this.sourceEventListener = sourceEventListener;
    String topic = optionHolder.validateAndGetStaticValue(TOPIC_KEY, "input inMemory source");
    this.subscriber = new InMemoryBroker.Subscriber() {
        @Override
        public void onMessage(Object event) {
            if (fail.equals("true")) {
                sourceEventListener.onEvent(event, new String[]{symbol});
            } else {
                if (requestedTransportPropertyNames.length == 1 &&
                        requestedTransportPropertyNames[0].equals("symbol")) {
                    sourceEventListener.onEvent(event, new String[]{symbol, price});
                }
            }
        }

        @Override
        public String getTopic() {
            return topic;
        }
    };
    return null;
}
 
Example 5
Source File: KafkaMultiDCSink.java    From siddhi-io-kafka with Apache License 2.0 5 votes vote down vote up
@Override
protected StateFactory<KafkaSinkState> init(StreamDefinition outputStreamDefinition, OptionHolder optionHolder,
                                            ConfigReader sinkConfigReader,
                                            SiddhiAppContext siddhiAppContext) {
    StateFactory<KafkaSinkState> stateStateFactory = super.init(outputStreamDefinition, optionHolder,
            sinkConfigReader, siddhiAppContext);
    topic = optionHolder.validateAndGetStaticValue(KAFKA_PUBLISH_TOPIC);
    partitionNo = Integer.parseInt(optionHolder.validateAndGetStaticValue(KAFKA_PARTITION_NO, "0"));
    if (bootstrapServers.split(",").length != 2) {
        throw new SiddhiAppValidationException("There should be two servers listed in 'bootstrap.servers' " +
                "configuration");
    }

    return stateStateFactory;
}
 
Example 6
Source File: KafkaMultiDCSource.java    From siddhi-io-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public StateFactory<KafkaMultiDCSourceState> init(SourceEventListener sourceEventListener,
                                                  OptionHolder optionHolder, String[] transportPropertyNames,
                                                  ConfigReader configReader, SiddhiAppContext siddhiAppContext) {
    this.eventListener = sourceEventListener;
    String serverList = optionHolder.validateAndGetStaticValue(KafkaSource
            .ADAPTOR_SUBSCRIBER_ZOOKEEPER_CONNECT_SERVERS);
    boolean isBinaryMessage = Boolean.parseBoolean(
            optionHolder.validateAndGetStaticValue(KafkaSource.IS_BINARY_MESSAGE, "false"));
    bootstrapServers = serverList.split(",");
    if (bootstrapServers.length != 2) {
        throw new SiddhiAppValidationException("There should be two servers listed in " +
                "'bootstrap.servers' configuration to ensure fault tolerant kafka messaging.");
    }
    synchronizer = new SourceSynchronizer(sourceEventListener, bootstrapServers, 1000,
            10);
    LOG.info("Initializing kafka source for bootstrap server :" + bootstrapServers[0]);
    Interceptor interceptor = new Interceptor(bootstrapServers[0], synchronizer, isBinaryMessage);
    OptionHolder options = createOptionHolders(bootstrapServers[0], optionHolder);
    KafkaSource source = new KafkaSource();
    stateFactories.put(bootstrapServers[0], source.init(interceptor, options, transportPropertyNames,
            configReader, siddhiAppContext));
    sources.put(bootstrapServers[0], source);

    LOG.info("Initializing kafka source for bootstrap server :" + bootstrapServers[1]);
    interceptor = new Interceptor(bootstrapServers[1], synchronizer, isBinaryMessage);
    options = createOptionHolders(bootstrapServers[1], optionHolder);
    source = new KafkaSource();
    stateFactories.put(bootstrapServers[1], source.init(interceptor, options, transportPropertyNames,
            configReader, siddhiAppContext));
    sources.put(bootstrapServers[1], source);
    return new KafkaMultiDCSourceStateFactory(stateFactories);
}
 
Example 7
Source File: KafkaMultiDCSource.java    From siddhi-io-kafka with Apache License 2.0 5 votes vote down vote up
private OptionHolder createOptionHolders(String server, OptionHolder originalOptionHolder) {
    Map<String, String> options = new HashMap<>();

    options.put(KafkaSource.ADAPTOR_SUBSCRIBER_ZOOKEEPER_CONNECT_SERVERS, server);
    options.put(KafkaSource.ADAPTOR_SUBSCRIBER_GROUP_ID, UUID.randomUUID().toString());
    options.put(KafkaSource.THREADING_OPTION, KafkaSource.SINGLE_THREADED);
    options.put(KafkaSource.SEQ_ENABLED, "false");

    String partition = originalOptionHolder.validateAndGetStaticValue(KAFKA_PARTITION_NO, "0");
    options.put(KafkaSource.ADAPTOR_SUBSCRIBER_PARTITION_NO_LIST, partition);

    String topic = originalOptionHolder.validateAndGetStaticValue(KAFKA_TOPIC);
    options.put(KafkaSource.ADAPTOR_SUBSCRIBER_TOPIC, topic);

    String optionalConfigs = originalOptionHolder.validateAndGetStaticValue(
            KafkaSource.ADAPTOR_OPTIONAL_CONFIGURATION_PROPERTIES, null);
    options.put(KafkaSource.ADAPTOR_OPTIONAL_CONFIGURATION_PROPERTIES, optionalConfigs);

    String isBinaryMessage = originalOptionHolder.validateAndGetStaticValue(KafkaSource.IS_BINARY_MESSAGE,
            "false");
    options.put(KafkaSource.IS_BINARY_MESSAGE, isBinaryMessage);

    Extension extension = KafkaSource.class.getAnnotation(io.siddhi.annotation.Extension.class);

    OptionHolder holder = new OptionHolder(eventListener.getStreamDefinition(), options, new HashMap<>(),
            extension);

    return holder;
}
 
Example 8
Source File: KafkaSink.java    From siddhi-io-kafka with Apache License 2.0 5 votes vote down vote up
@Override
protected StateFactory<KafkaSinkState> init(StreamDefinition outputStreamDefinition, OptionHolder optionHolder,
                                            ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
    bootstrapServers = optionHolder.validateAndGetStaticValue(KAFKA_BROKER_LIST);
    optionalConfigs = optionHolder.validateAndGetStaticValue(KAFKA_OPTIONAL_CONFIGURATION_PROPERTIES, null);
    topicOption = optionHolder.validateAndGetOption(KAFKA_PUBLISH_TOPIC);
    partitionOption = optionHolder.getOrCreateOption(KAFKA_PARTITION_NO, null);
    sequenceId = optionHolder.validateAndGetStaticValue(SEQ_ID, null);
    isBinaryMessage = Boolean.parseBoolean(optionHolder.validateAndGetStaticValue(IS_BINARY_MESSAGE,
            "false"));
    isSequenced = sequenceId != null;
    keyOption = optionHolder.getOrCreateOption(KAFKA_MESSAGE_KEY, null);

    return () -> new KafkaSinkState(isSequenced);
}
 
Example 9
Source File: KafkaSource.java    From siddhi-io-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public StateFactory<KafkaSourceState> init(SourceEventListener sourceEventListener, OptionHolder optionHolder,
                                           String[] strings, ConfigReader configReader,
                                           SiddhiAppContext siddhiAppContext) {
    this.siddhiAppContext = siddhiAppContext;
    this.optionHolder = optionHolder;
    this.sourceEventListener = sourceEventListener;
    bootstrapServers = optionHolder.validateAndGetStaticValue(ADAPTOR_SUBSCRIBER_ZOOKEEPER_CONNECT_SERVERS);
    groupID = optionHolder.validateAndGetStaticValue(ADAPTOR_SUBSCRIBER_GROUP_ID);
    threadingOption = optionHolder.validateAndGetStaticValue(THREADING_OPTION);
    String partitionList = optionHolder.validateAndGetStaticValue(ADAPTOR_SUBSCRIBER_PARTITION_NO_LIST, null);
    partitions = (partitionList != null) ? partitionList.split(KafkaIOUtils.HEADER_SEPARATOR) : null;
    String topicList = optionHolder.validateAndGetStaticValue(ADAPTOR_SUBSCRIBER_TOPIC);
    topics = topicList.split(KafkaIOUtils.HEADER_SEPARATOR);
    seqEnabled = optionHolder.validateAndGetStaticValue(SEQ_ENABLED, "false").equalsIgnoreCase("true");
    optionalConfigs = optionHolder.validateAndGetStaticValue(ADAPTOR_OPTIONAL_CONFIGURATION_PROPERTIES, null);
    isBinaryMessage = Boolean.parseBoolean(optionHolder.validateAndGetStaticValue(IS_BINARY_MESSAGE,
            "false"));
    enableOffsetCommit = Boolean.parseBoolean(optionHolder.validateAndGetStaticValue(ADAPTOR_ENABLE_OFFSET_COMMIT,
            "true"));
    enableAsyncCommit = Boolean.parseBoolean(optionHolder.validateAndGetStaticValue(ADAPTOR_ENABLE_ASYNC_COMMIT,
            "true"));
    topicOffsetMapConfig = optionHolder.validateAndGetStaticValue(TOPIC_OFFSET_MAP, null);
    if (PARTITION_WISE.equals(threadingOption) && null == partitions) {
        throw new SiddhiAppValidationException("Threading option is selected as 'partition.wise' but " +
                "there are no partitions given");
    }

    return () -> new KafkaSourceState(seqEnabled);
}
 
Example 10
Source File: LogSink.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected StateFactory<State> init(StreamDefinition outputStreamDefinition, OptionHolder optionHolder,
                                   ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
    String defaultPrefix = siddhiAppContext.getName() + " : " + outputStreamDefinition.getId();
    logPrefix = optionHolder.validateAndGetStaticValue(PREFIX, defaultPrefix);
    logPriority = LogPriority.valueOf(optionHolder.validateAndGetStaticValue(PRIORITY, "INFO")
            .toUpperCase());
    return null;
}
 
Example 11
Source File: InMemorySource.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public StateFactory<State> init(SourceEventListener sourceEventListener, OptionHolder optionHolder,
                                String[] requestedTransportPropertyNames, ConfigReader configReader,
                                SiddhiAppContext siddhiAppContext) {
    this.sourceEventListener = sourceEventListener;
    String topic = optionHolder.validateAndGetStaticValue(TOPIC_KEY, "input inMemory source");
    this.subscriber = new InMemoryBroker.Subscriber() {
        @Override
        public void onMessage(Object event) {
            if (paused) {
                pauseLock.lock();
                try {
                    while (paused) {
                        unpaused.await();
                    }
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                } finally {
                    pauseLock.unlock();
                }
            }
            sourceEventListener.onEvent(event, null);
        }

        @Override
        public String getTopic() {
            return topic;
        }
    };
    return null;
}
 
Example 12
Source File: DefinitionParserHelper.java    From siddhi with Apache License 2.0 5 votes vote down vote up
private static OutputGroupDeterminer constructOutputGroupDeterminer(OptionHolder transportOptHolder,
                                                                    OptionHolder distributedOptHolder,
                                                                    StreamDefinition streamDef,
                                                                    int destinationCount) {

    OutputGroupDeterminer groupDeterminer = null;
    if (distributedOptHolder != null) {
        String strategy = distributedOptHolder.validateAndGetStaticValue(SiddhiConstants.DISTRIBUTION_STRATEGY_KEY);
        if (strategy.equalsIgnoreCase(SiddhiConstants.DISTRIBUTION_STRATEGY_PARTITIONED)) {
            String partitionKeyField = distributedOptHolder.validateAndGetStaticValue(SiddhiConstants
                    .PARTITION_KEY_FIELD_KEY);
            int partitioningFieldIndex = streamDef.getAttributePosition(partitionKeyField);
            groupDeterminer = new PartitionedGroupDeterminer(partitioningFieldIndex, destinationCount);
        }
    }

    if (groupDeterminer == null) {
        List<Option> dynamicTransportOptions = new ArrayList<Option>(transportOptHolder.getDynamicOptionsKeys()
                .size());
        for (String option : transportOptHolder.getDynamicOptionsKeys()) {
            dynamicTransportOptions.add(transportOptHolder.validateAndGetOption(option));
        }

        if (dynamicTransportOptions.size() > 0) {
            groupDeterminer = new DynamicOptionGroupDeterminer(dynamicTransportOptions);
        }
    }

    return groupDeterminer;
}
 
Example 13
Source File: TestTrpInMemorySource.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public StateFactory<State> init(SourceEventListener sourceEventListener, OptionHolder optionHolder,
                                String[] requestedTransportPropertyNames, ConfigReader configReader,
                                SiddhiAppContext siddhiAppContext) {
    super.init(sourceEventListener, optionHolder, requestedTransportPropertyNames, configReader, siddhiAppContext);
    symbol = optionHolder.validateAndGetStaticValue("prop1");
    price = optionHolder.validateAndGetStaticValue("prop2");
    fail = optionHolder.validateAndGetStaticValue("fail", "false");
    this.sourceEventListener = sourceEventListener;
    String topic = optionHolder.validateAndGetStaticValue(TOPIC_KEY, "input inMemory source");
    this.subscriber = new InMemoryBroker.Subscriber() {
        @Override
        public void onMessage(Object event) {
            if (fail.equals("true")) {
                sourceEventListener.onEvent(event, new String[]{symbol});
            } else {
                if (requestedTransportPropertyNames.length == 2 &&
                        requestedTransportPropertyNames[0].equals("symbol") &&
                        requestedTransportPropertyNames[1].equals("price")) {
                    sourceEventListener.onEvent(event, new String[]{symbol, price});
                }
            }
        }

        @Override
        public String getTopic() {
            return topic;
        }
    };
    return null;
}
 
Example 14
Source File: TestDepInMemorySource.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public StateFactory<State> init(SourceEventListener sourceEventListener, OptionHolder optionHolder,
                                String[] requestedTransportPropertyNames, ConfigReader configReader,
                                SiddhiAppContext siddhiAppContext) {
    super.init(sourceEventListener, optionHolder, requestedTransportPropertyNames, configReader, siddhiAppContext);
    symbol = optionHolder.validateAndGetStaticValue("prop1");
    price = optionHolder.validateAndGetStaticValue("prop2");
    fail = optionHolder.validateAndGetStaticValue("fail", "false");
    this.sourceEventListener = sourceEventListener;
    String topic = optionHolder.validateAndGetStaticValue(TOPIC_KEY, "input inMemory source");
    this.subscriber = new InMemoryBroker.Subscriber() {
        @Override
        public void onMessage(Object event) {
            if (fail.equals("true")) {
                sourceEventListener.onEvent(event, new String[]{symbol});
            } else {
                if (requestedTransportPropertyNames.length == 2 &&
                        requestedTransportPropertyNames[0].equals("symbol") &&
                        requestedTransportPropertyNames[1].equals("price")) {
                    sourceEventListener.onEvent(event, new String[]{symbol, price});
                }
            }
        }

        @Override
        public String getTopic() {
            return topic;
        }
    };
    return null;
}