Search in sources :

Example 1 with PluginSourceNotMatchException

use of org.embulk.plugin.PluginSourceNotMatchException in project embulk by embulk.

the class MavenPluginSource method newPlugin.

@Override
public <T> T newPlugin(Class<T> pluginInterface, PluginType pluginType) throws PluginSourceNotMatchException {
    final String category;
    if (InputPlugin.class.isAssignableFrom(pluginInterface)) {
        category = "input";
    } else if (OutputPlugin.class.isAssignableFrom(pluginInterface)) {
        category = "output";
    } else if (ParserPlugin.class.isAssignableFrom(pluginInterface)) {
        category = "parser";
    } else if (FormatterPlugin.class.isAssignableFrom(pluginInterface)) {
        category = "formatter";
    } else if (DecoderPlugin.class.isAssignableFrom(pluginInterface)) {
        category = "decoder";
    } else if (EncoderPlugin.class.isAssignableFrom(pluginInterface)) {
        category = "encoder";
    } else if (FilterPlugin.class.isAssignableFrom(pluginInterface)) {
        category = "filter";
    } else if (GuessPlugin.class.isAssignableFrom(pluginInterface)) {
        category = "guess";
    } else if (ExecutorPlugin.class.isAssignableFrom(pluginInterface)) {
        category = "executor";
    } else {
        // unsupported plugin category
        throw new PluginSourceNotMatchException("Plugin interface " + pluginInterface + " is not supported.");
    }
    if (pluginType.getSourceType() != PluginSource.Type.MAVEN) {
        throw new PluginSourceNotMatchException();
    }
    final MavenPluginType mavenPluginType = (MavenPluginType) pluginType;
    final PluginClassLoaderFactory pluginClassLoaderFactory = this.injector.getInstance(PluginClassLoaderFactory.class);
    final MavenArtifactFinder mavenArtifactFinder;
    try {
        mavenArtifactFinder = MavenArtifactFinder.create(getLocalMavenRepository());
    } catch (MavenRepositoryNotFoundException ex) {
        throw new PluginSourceNotMatchException(ex);
    }
    final Path jarPath;
    try {
        jarPath = mavenArtifactFinder.findMavenArtifactJar(mavenPluginType.getGroup(), "embulk-" + category + "-" + mavenPluginType.getName(), mavenPluginType.getClassifier(), mavenPluginType.getVersion());
    } catch (MavenArtifactNotFoundException ex) {
        throw new PluginSourceNotMatchException(ex);
    }
    final Class<?> pluginMainClass;
    try (JarPluginLoader loader = JarPluginLoader.load(jarPath, pluginClassLoaderFactory)) {
        pluginMainClass = loader.getPluginMainClass();
    } catch (InvalidJarPluginException ex) {
        throw new PluginSourceNotMatchException(ex);
    }
    final Object pluginMainObject;
    try {
        // FileInputPlugin and FileOutputPlugin are wrapped with FileInputRunner and FileOutputRunner here.
        if (FileInputPlugin.class.isAssignableFrom(pluginMainClass)) {
            final FileInputPlugin fileInputPluginMainObject;
            try {
                fileInputPluginMainObject = (FileInputPlugin) this.injector.getInstance(pluginMainClass);
            } catch (ClassCastException ex) {
                throw new PluginSourceNotMatchException("[FATAL/INTERNAL] Plugin class \"" + pluginMainClass.getName() + "\" is not file-input.", ex);
            }
            pluginMainObject = new FileInputRunner(fileInputPluginMainObject);
        } else if (FileOutputPlugin.class.isAssignableFrom(pluginMainClass)) {
            final FileOutputPlugin fileOutputPluginMainObject;
            try {
                fileOutputPluginMainObject = (FileOutputPlugin) this.injector.getInstance(pluginMainClass);
            } catch (ClassCastException ex) {
                throw new PluginSourceNotMatchException("[FATAL/INTERNAL] Plugin class \"" + pluginMainClass.getName() + "\" is not file-output.", ex);
            }
            pluginMainObject = new FileOutputRunner(fileOutputPluginMainObject);
        } else {
            if (!pluginInterface.isAssignableFrom(pluginMainClass)) {
                throw new PluginSourceNotMatchException("Plugin class \"" + pluginMainClass.getName() + "\" is not a valid " + category + " plugin.");
            }
            pluginMainObject = this.injector.getInstance(pluginMainClass);
        }
    } catch (ExceptionInInitializerError ex) {
        throw new PluginSourceNotMatchException("Plugin class \"" + pluginMainClass.getName() + "\" is not instantiatable due to exception in initialization.", ex);
    } catch (SecurityException ex) {
        throw new PluginSourceNotMatchException("Plugin class \"" + pluginMainClass.getName() + "\" is not instantiatable due to security manager.", ex);
    }
    try {
        return pluginInterface.cast(pluginMainObject);
    } catch (ClassCastException ex) {
        throw new PluginSourceNotMatchException("[FATAL/INTERNAL] Plugin class \"" + pluginMainClass.getName() + "\" is not " + category + " actually.", ex);
    }
}
Also used : Path(java.nio.file.Path) PluginSourceNotMatchException(org.embulk.plugin.PluginSourceNotMatchException) FileInputRunner(org.embulk.spi.FileInputRunner) JarPluginLoader(org.embulk.plugin.jar.JarPluginLoader) OutputPlugin(org.embulk.spi.OutputPlugin) FileOutputPlugin(org.embulk.spi.FileOutputPlugin) FileOutputPlugin(org.embulk.spi.FileOutputPlugin) FileOutputRunner(org.embulk.spi.FileOutputRunner) MavenPluginType(org.embulk.plugin.MavenPluginType) InvalidJarPluginException(org.embulk.plugin.jar.InvalidJarPluginException) FileInputPlugin(org.embulk.spi.FileInputPlugin) GuessPlugin(org.embulk.spi.GuessPlugin) PluginClassLoaderFactory(org.embulk.plugin.PluginClassLoaderFactory) EncoderPlugin(org.embulk.spi.EncoderPlugin) FormatterPlugin(org.embulk.spi.FormatterPlugin)

Example 2 with PluginSourceNotMatchException

use of org.embulk.plugin.PluginSourceNotMatchException in project embulk by embulk.

the class JRubyPluginSource method newPlugin.

public <T> T newPlugin(Class<T> iface, PluginType type) throws PluginSourceNotMatchException {
    if (type.getSourceType() != PluginSource.Type.DEFAULT) {
        throw new PluginSourceNotMatchException();
    }
    final String name = type.getName();
    final String category;
    if (InputPlugin.class.isAssignableFrom(iface)) {
        category = "input";
    } else if (OutputPlugin.class.isAssignableFrom(iface)) {
        category = "output";
    } else if (ParserPlugin.class.isAssignableFrom(iface)) {
        category = "parser";
    } else if (FormatterPlugin.class.isAssignableFrom(iface)) {
        category = "formatter";
    } else if (DecoderPlugin.class.isAssignableFrom(iface)) {
        category = "decoder";
    } else if (EncoderPlugin.class.isAssignableFrom(iface)) {
        category = "encoder";
    } else if (FilterPlugin.class.isAssignableFrom(iface)) {
        category = "filter";
    } else if (GuessPlugin.class.isAssignableFrom(iface)) {
        category = "guess";
    } else if (ExecutorPlugin.class.isAssignableFrom(iface)) {
        category = "executor";
    } else {
        // unsupported plugin category
        throw new PluginSourceNotMatchException("Plugin interface " + iface + " is not supported in JRuby");
    }
    String methodName = "new_java_" + category;
    try {
        // get Embulk::Plugin
        // this.rubyPluginManager = ((RubyModule) jruby.get("Embulk")).const_get(
        // RubySymbol.newSymbol(
        // jruby.getProvider().getRuntime(), "Plugin"));
        final Object rubyPluginManager = jruby.runScriptlet("Embulk::Plugin");
        return jruby.callMethod(rubyPluginManager, methodName, name, iface);
    } catch (Throwable ex) {
        throw new PluginSourceNotMatchException(ex);
    }
}
Also used : GuessPlugin(org.embulk.spi.GuessPlugin) PluginSourceNotMatchException(org.embulk.plugin.PluginSourceNotMatchException) OutputPlugin(org.embulk.spi.OutputPlugin) EncoderPlugin(org.embulk.spi.EncoderPlugin) FormatterPlugin(org.embulk.spi.FormatterPlugin)

Aggregations

PluginSourceNotMatchException (org.embulk.plugin.PluginSourceNotMatchException)2 EncoderPlugin (org.embulk.spi.EncoderPlugin)2 FormatterPlugin (org.embulk.spi.FormatterPlugin)2 GuessPlugin (org.embulk.spi.GuessPlugin)2 OutputPlugin (org.embulk.spi.OutputPlugin)2 Path (java.nio.file.Path)1 MavenPluginType (org.embulk.plugin.MavenPluginType)1 PluginClassLoaderFactory (org.embulk.plugin.PluginClassLoaderFactory)1 InvalidJarPluginException (org.embulk.plugin.jar.InvalidJarPluginException)1 JarPluginLoader (org.embulk.plugin.jar.JarPluginLoader)1 FileInputPlugin (org.embulk.spi.FileInputPlugin)1 FileInputRunner (org.embulk.spi.FileInputRunner)1 FileOutputPlugin (org.embulk.spi.FileOutputPlugin)1 FileOutputRunner (org.embulk.spi.FileOutputRunner)1