All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.embulk.plugin.BuiltinPluginSource Maven / Gradle / Ivy

package org.embulk.plugin;

import com.google.inject.Injector;  // Only for instantiating a plugin.
import java.util.LinkedHashMap;
import java.util.Map;
import org.embulk.spi.DecoderPlugin;
import org.embulk.spi.EncoderPlugin;
import org.embulk.spi.ExecutorPlugin;
import org.embulk.spi.FileInputPlugin;
import org.embulk.spi.FileInputRunner;
import org.embulk.spi.FileOutputPlugin;
import org.embulk.spi.FileOutputRunner;
import org.embulk.spi.FilterPlugin;
import org.embulk.spi.FormatterPlugin;
import org.embulk.spi.GuessPlugin;
import org.embulk.spi.InputPlugin;
import org.embulk.spi.OutputPlugin;
import org.embulk.spi.ParserPlugin;

public class BuiltinPluginSource implements PluginSource {
    private BuiltinPluginSource(
            final Injector injector,
            final Map> decoderPlugins,
            final Map> encoderPlugins,
            final Map> executorPlugins,
            final Map> fileInputPlugins,
            final Map> fileOutputPlugins,
            final Map> filterPlugins,
            final Map> formatterPlugins,
            final Map> guessPlugins,
            final Map> inputPlugins,
            final Map> outputPlugins,
            final Map> parserPlugins) {
        this.injector = injector;
        this.decoderPlugins = decoderPlugins;
        this.encoderPlugins = encoderPlugins;
        this.executorPlugins = executorPlugins;
        this.fileInputPlugins = fileInputPlugins;
        this.fileOutputPlugins = fileOutputPlugins;
        this.filterPlugins = filterPlugins;
        this.formatterPlugins = formatterPlugins;
        this.guessPlugins = guessPlugins;
        this.inputPlugins = inputPlugins;
        this.outputPlugins = outputPlugins;
        this.parserPlugins = parserPlugins;
    }

    public static class Builder {
        private Builder(final Injector injector) {
            this.injector = injector;
            this.decoderPlugins = new LinkedHashMap<>();
            this.encoderPlugins = new LinkedHashMap<>();
            this.executorPlugins = new LinkedHashMap<>();
            this.fileInputPlugins = new LinkedHashMap<>();
            this.fileOutputPlugins = new LinkedHashMap<>();
            this.filterPlugins = new LinkedHashMap<>();
            this.formatterPlugins = new LinkedHashMap<>();
            this.guessPlugins = new LinkedHashMap<>();
            this.inputPlugins = new LinkedHashMap<>();
            this.outputPlugins = new LinkedHashMap<>();
            this.parserPlugins = new LinkedHashMap<>();
        }

        public Builder registerDecoderPlugin(final String name, final Class decoderImpl) {
            if (null != this.decoderPlugins.putIfAbsent(name, decoderImpl)) {
                throw new IllegalStateException("A decoder plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public Builder registerEncoderPlugin(final String name, final Class encoderImpl) {
            if (null != this.encoderPlugins.put(name, encoderImpl)) {
                throw new IllegalStateException("An encoder plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public Builder registerExecutorPlugin(final String name, final Class executorImpl) {
            if (null != this.executorPlugins.put(name, executorImpl)) {
                throw new IllegalStateException("An executor plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public Builder registerFileInputPlugin(final String name, final Class fileInputImpl) {
            if (this.inputPlugins.containsKey(name)) {
                throw new IllegalStateException("An input plugin with a name \"" + name + "\" is already registered.");
            } else if (null != this.fileInputPlugins.put(name, fileInputImpl)) {
                throw new IllegalStateException("A file input plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public Builder registerFileOutputPlugin(final String name, final Class fileOutputImpl) {
            if (this.outputPlugins.containsKey(name)) {
                throw new IllegalStateException("An output plugin with a name \"" + name + "\" is already registered.");
            } else if (null != this.fileOutputPlugins.put(name, fileOutputImpl)) {
                throw new IllegalStateException("A file output plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public Builder registerFilterPlugin(final String name, final Class filterImpl) {
            if (null != this.filterPlugins.put(name, filterImpl)) {
                throw new IllegalStateException("A filter plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public Builder registerFormatterPlugin(final String name, final Class formatterImpl) {
            if (null != this.formatterPlugins.put(name, formatterImpl)) {
                throw new IllegalStateException("A formatter plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public Builder registerGuessPlugin(final String name, final Class guessImpl) {
            if (null != this.guessPlugins.put(name, guessImpl)) {
                throw new IllegalStateException("A guess plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public Builder registerInputPlugin(final String name, final Class inputImpl) {
            if (this.fileInputPlugins.containsKey(name)) {
                throw new IllegalStateException("A file input plugin with a name \"" + name + "\" is already registered.");
            } else if (this.fileInputPlugins.containsKey(name) || null != this.inputPlugins.put(name, inputImpl)) {
                throw new IllegalStateException("An input plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public Builder registerOutputPlugin(final String name, final Class outputImpl) {
            if (this.fileOutputPlugins.containsKey(name)) {
                throw new IllegalStateException("A file output plugin with a name \"" + name + "\" is already registered.");
            } else if (null != this.outputPlugins.put(name, outputImpl)) {
                throw new IllegalStateException("An output plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public Builder registerParserPlugin(final String name, final Class parserImpl) {
            if (null != this.parserPlugins.put(name, parserImpl)) {
                throw new IllegalStateException("A parser plugin with a name \"" + name + "\" is already registered.");
            }
            return this;
        }

        public BuiltinPluginSource build() {
            return new BuiltinPluginSource(
                    this.injector,
                    this.decoderPlugins,
                    this.encoderPlugins,
                    this.executorPlugins,
                    this.fileInputPlugins,
                    this.fileOutputPlugins,
                    this.filterPlugins,
                    this.formatterPlugins,
                    this.guessPlugins,
                    this.inputPlugins,
                    this.outputPlugins,
                    this.parserPlugins);
        }

        private final Injector injector;
        private final LinkedHashMap> decoderPlugins;
        private final LinkedHashMap> encoderPlugins;
        private final LinkedHashMap> executorPlugins;
        private final LinkedHashMap> fileInputPlugins;
        private final LinkedHashMap> fileOutputPlugins;
        private final LinkedHashMap> filterPlugins;
        private final LinkedHashMap> formatterPlugins;
        private final LinkedHashMap> guessPlugins;
        private final LinkedHashMap> inputPlugins;
        private final LinkedHashMap> outputPlugins;
        private final LinkedHashMap> parserPlugins;
    }

    public static Builder builder(final Injector injector) {
        return new Builder(injector);
    }

    @Override
    public  T newPlugin(final Class pluginInterface, final PluginType type) throws PluginSourceNotMatchException {
        if (type.getSourceType() != PluginSource.Type.DEFAULT) {
            throw new PluginSourceNotMatchException();
        }
        final String name = type.getName();

        if (InputPlugin.class.isAssignableFrom(pluginInterface)) {
            // Duplications between Input Plugins and File Input Plugins are rejected when registered above.
            final Class inputPluginImpl = this.inputPlugins.get(name);
            if (inputPluginImpl != null) {
                return pluginInterface.cast((InputPlugin) this.injector.getInstance(inputPluginImpl));
            }
            final Class fileInputPluginImpl = this.fileInputPlugins.get(name);
            if (fileInputPluginImpl != null) {
                return pluginInterface.cast(new FileInputRunner((FileInputPlugin) this.injector.getInstance(fileInputPluginImpl)));
            }
        } else if (OutputPlugin.class.isAssignableFrom(pluginInterface)) {
            // Duplications between Output Plugins and File Output Plugins are rejected when registered above.
            final Class outputPluginImpl = this.outputPlugins.get(name);
            if (outputPluginImpl != null) {
                return pluginInterface.cast((OutputPlugin) this.injector.getInstance(outputPluginImpl));
            }
            final Class fileOutputPluginImpl = this.fileOutputPlugins.get(name);
            if (fileOutputPluginImpl != null) {
                return pluginInterface.cast(new FileOutputRunner((FileOutputPlugin) this.injector.getInstance(fileOutputPluginImpl)));
            }
        } else {
            final Class impl;
            if (DecoderPlugin.class.isAssignableFrom(pluginInterface)) {
                impl = this.decoderPlugins.get(name);
            } else if (EncoderPlugin.class.isAssignableFrom(pluginInterface)) {
                impl = this.encoderPlugins.get(name);
            } else if (ExecutorPlugin.class.isAssignableFrom(pluginInterface)) {
                impl = this.executorPlugins.get(name);
            } else if (FilterPlugin.class.isAssignableFrom(pluginInterface)) {
                impl = this.filterPlugins.get(name);
            } else if (FormatterPlugin.class.isAssignableFrom(pluginInterface)) {
                impl = this.formatterPlugins.get(name);
            } else if (GuessPlugin.class.isAssignableFrom(pluginInterface)) {
                impl = this.guessPlugins.get(name);
            } else if (ParserPlugin.class.isAssignableFrom(pluginInterface)) {
                impl = this.parserPlugins.get(name);
            } else {
                throw new PluginSourceNotMatchException("Plugin interface " + pluginInterface + " is not supported.");
            }

            if (impl != null) {
                return pluginInterface.cast(this.injector.getInstance(impl));
            }
        }
        throw new PluginSourceNotMatchException();
    }

    private final Injector injector;
    private final Map> decoderPlugins;
    private final Map> encoderPlugins;
    private final Map> executorPlugins;
    private final Map> fileInputPlugins;
    private final Map> fileOutputPlugins;
    private final Map> filterPlugins;
    private final Map> formatterPlugins;
    private final Map> guessPlugins;
    private final Map> inputPlugins;
    private final Map> outputPlugins;
    private final Map> parserPlugins;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy