Search in sources :

Example 1 with PluginDefinition

use of org.apache.inlong.manager.workflow.plugin.PluginDefinition in project incubator-inlong by apache.

the class PluginService method pluginReload.

/**
 * Reload the plugin from the plugin path
 */
public void pluginReload() {
    Path path = Paths.get(pluginLoc).toAbsolutePath();
    log.info("search for plugin in {}", path);
    if (!path.toFile().exists()) {
        log.warn("plugin directory not found");
        return;
    }
    PluginClassLoader pluginLoader = PluginClassLoader.getFromPluginUrl(path.toString(), Thread.currentThread().getContextClassLoader());
    Map<String, PluginDefinition> pluginDefinitions = pluginLoader.getPluginDefinitions();
    if (MapUtils.isEmpty(pluginDefinitions)) {
        log.warn("pluginDefinition not found in {}", pluginLoc);
        return;
    }
    List<Plugin> plugins = new ArrayList<>();
    for (PluginDefinition pluginDefinition : pluginDefinitions.values()) {
        String pluginClassName = pluginDefinition.getPluginClass();
        try {
            Class<?> pluginClass = pluginLoader.loadClass(pluginClassName);
            Object plugin = pluginClass.getDeclaredConstructor().newInstance();
            plugins.add((Plugin) plugin);
        } catch (Throwable e) {
            throw new RuntimeException(e.getMessage());
        }
    }
    this.plugins.addAll(plugins);
    for (PluginBinder pluginBinder : pluginBinders) {
        for (Plugin plugin : plugins) {
            log.info(String.format("PluginBinder:%s load Plugin:%s", pluginBinder.getClass().getSimpleName(), plugin.getClass().getSimpleName()));
            pluginBinder.acceptPlugin(plugin);
        }
    }
}
Also used : Path(java.nio.file.Path) PluginBinder(org.apache.inlong.manager.workflow.plugin.PluginBinder) ArrayList(java.util.ArrayList) PluginDefinition(org.apache.inlong.manager.workflow.plugin.PluginDefinition) Plugin(org.apache.inlong.manager.workflow.plugin.Plugin)

Example 2 with PluginDefinition

use of org.apache.inlong.manager.workflow.plugin.PluginDefinition in project incubator-inlong by apache.

the class PluginClassLoader method loadPluginDefinition.

/**
 * load pluginDefinition in **.jar/META-INF/plugin.yaml
 */
private void loadPluginDefinition() throws IOException {
    List<PluginDefinition> definitions = new ArrayList();
    for (File jarFile : pluginDirectory.listFiles()) {
        if (!jarFile.getName().endsWith(".jar")) {
            log.warn("{}' is not plugin jar , please check", jarFile);
            continue;
        }
        JarFile pluginJar = new JarFile(jarFile);
        String pluginDef = readPluginDef(pluginJar);
        pluginDef = pluginDef.replaceAll("[\\x00]+", "");
        PluginDefinition definition = yamlMapper.readValue(pluginDef, PluginDefinition.class);
        addURL(new URL("file://" + jarFile.getAbsolutePath()));
        checkPluginValid(jarFile, definition);
        definitions.add(definition);
    }
    pluginDefinitionMap = definitions.stream().collect(Collectors.toMap(definition -> definition.getName(), definition -> definition));
}
Also used : PluginDefinition(org.apache.inlong.manager.workflow.plugin.PluginDefinition) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL)

Example 3 with PluginDefinition

use of org.apache.inlong.manager.workflow.plugin.PluginDefinition in project incubator-inlong by apache.

the class PluginClassLoaderTest method testLoadPlugin.

@Test
public void testLoadPlugin() {
    String path = this.getClass().getClassLoader().getResource("").getPath();
    PluginClassLoader pluginClassLoader = PluginClassLoader.getFromPluginUrl(path + "plugins", Thread.currentThread().getContextClassLoader());
    Map<String, PluginDefinition> pluginDefinitionMap = pluginClassLoader.getPluginDefinitions();
    Assert.assertEquals(1, pluginDefinitionMap.size());
    PluginDefinition pluginDefinition = Lists.newArrayList(pluginDefinitionMap.values()).get(0);
    Assert.assertNotNull(pluginDefinition);
    String pluginClass = pluginDefinition.getPluginClass();
    Assert.assertTrue(StringUtils.isNotEmpty(pluginClass));
    try {
        Class cls = pluginClassLoader.loadClass(pluginClass);
        Plugin plugin = (Plugin) cls.getDeclaredConstructor().newInstance();
        Assert.assertTrue(plugin instanceof ProcessPlugin);
    } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
        Assert.assertTrue(e instanceof ClassNotFoundException);
        Assert.fail();
    }
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) PluginDefinition(org.apache.inlong.manager.workflow.plugin.PluginDefinition) ProcessPlugin(org.apache.inlong.manager.workflow.plugin.ProcessPlugin) Plugin(org.apache.inlong.manager.workflow.plugin.Plugin) ProcessPlugin(org.apache.inlong.manager.workflow.plugin.ProcessPlugin) Test(org.junit.Test)

Aggregations

PluginDefinition (org.apache.inlong.manager.workflow.plugin.PluginDefinition)3 ArrayList (java.util.ArrayList)2 Plugin (org.apache.inlong.manager.workflow.plugin.Plugin)2 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 JarFile (java.util.jar.JarFile)1 PluginBinder (org.apache.inlong.manager.workflow.plugin.PluginBinder)1 ProcessPlugin (org.apache.inlong.manager.workflow.plugin.ProcessPlugin)1 Test (org.junit.Test)1