001/*
002 * Copyright (C) 2014 Jörg Prante
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.xbib.elasticsearch.plugin.jdbc.util;
017
018import org.xbib.elasticsearch.river.jdbc.RiverFlow;
019import org.xbib.elasticsearch.river.jdbc.RiverMouth;
020import org.xbib.elasticsearch.river.jdbc.RiverSource;
021import org.xbib.elasticsearch.river.jdbc.strategy.simple.SimpleRiverFlow;
022import org.xbib.elasticsearch.river.jdbc.strategy.simple.SimpleRiverMouth;
023import org.xbib.elasticsearch.river.jdbc.strategy.simple.SimpleRiverSource;
024
025import java.util.ServiceLoader;
026
027/**
028 * The river service loader
029 */
030public class RiverServiceLoader {
031
032    /**
033     * A river flow encapsulates the thread that moves the data from source to mouth
034     *
035     * @param strategy the strategy
036     * @return a river flow, or the SimpleRiverFlow
037     */
038    public static RiverFlow newRiverFlow(String strategy) {
039        ServiceLoader<RiverFlow> riverFlowServiceLoader = ServiceLoader.load(RiverFlow.class);
040        for (RiverFlow riverFlow : riverFlowServiceLoader) {
041            if (strategy.equals(riverFlow.strategy())) {
042                return riverFlow.newInstance();
043            }
044        }
045        return new SimpleRiverFlow();
046    }
047
048    /**
049     * A river source is the origin, the data producing side
050     *
051     * @param strategy the strategy
052     * @return a river source, or the SimpleRiverSource
053     */
054    public static RiverSource newRiverSource(String strategy) {
055        ServiceLoader<RiverSource> sourceLoader = ServiceLoader.load(RiverSource.class);
056        for (RiverSource riverSource : sourceLoader) {
057            if (strategy.equals(riverSource.strategy())) {
058                return riverSource.newInstance();
059            }
060        }
061        return new SimpleRiverSource();
062    }
063
064    /**
065     * A river mouth is the Elasticsearch side of the river, where the bulk processor lives
066     *
067     * @param strategy the strategy
068     * @return a new instance of a river mouth, or an instance of the SimpleRiverMouth if strategy does not exist
069     */
070    public static RiverMouth newRiverMouth(String strategy) {
071        ServiceLoader<RiverMouth> riverMouthLoader = ServiceLoader.load(RiverMouth.class);
072        for (RiverMouth riverMouth : riverMouthLoader) {
073            if (strategy.equals(riverMouth.strategy())) {
074                return riverMouth.newInstance();
075            }
076        }
077        return new SimpleRiverMouth();
078    }
079
080}