From 86d0d67036f29f4c7f2f37773e2cdaeb37b9ebf3 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 11 Jul 2016 23:16:23 -0700 Subject: [PATCH] Plugins: Add some basic services to createComponents This adds the first few basic services needed for any plugin to create its own components that interact with the rest of the system. --- .../java/org/elasticsearch/node/Node.java | 4 +- .../org/elasticsearch/plugins/Plugin.java | 20 ++++++--- .../elasticsearch/plugins/PluginsService.java | 45 +++++++++---------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/node/Node.java b/core/src/main/java/org/elasticsearch/node/Node.java index 454a49291d8..a528de6d082 100644 --- a/core/src/main/java/org/elasticsearch/node/Node.java +++ b/core/src/main/java/org/elasticsearch/node/Node.java @@ -303,7 +303,9 @@ public class Node implements Closeable { resourcesToClose.add(bigArrays); modules.add(settingsModule); client = new NodeClient(settings, threadPool); - Collection pluginComponents = pluginsService.createComponenents(); + Collection pluginComponents = pluginsService.filterPlugins(Plugin.class).stream() + .flatMap(p -> p.createComponents(client, clusterService, threadPool).stream()) + .collect(Collectors.toList()); modules.add(b -> { b.bind(PluginsService.class).toInstance(pluginsService); b.bind(Client.class).toInstance(client); diff --git a/core/src/main/java/org/elasticsearch/plugins/Plugin.java b/core/src/main/java/org/elasticsearch/plugins/Plugin.java index 3c9cf675a63..57ccd876eef 100644 --- a/core/src/main/java/org/elasticsearch/plugins/Plugin.java +++ b/core/src/main/java/org/elasticsearch/plugins/Plugin.java @@ -19,8 +19,13 @@ package org.elasticsearch.plugins; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + import org.elasticsearch.action.ActionModule; -import org.elasticsearch.common.component.AbstractComponent; +import org.elasticsearch.client.Client; +import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.component.LifecycleComponent; import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.settings.Setting; @@ -30,10 +35,7 @@ import org.elasticsearch.index.IndexModule; import org.elasticsearch.indices.analysis.AnalysisModule; import org.elasticsearch.script.ScriptModule; import org.elasticsearch.threadpool.ExecutorBuilder; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import org.elasticsearch.threadpool.ThreadPool; /** * An extension point allowing to plug in custom functionality. @@ -65,13 +67,17 @@ public abstract class Plugin { } /** - * Returns components maintained by this plugin. + * Returns components added by this plugin. * * Any components returned that implement {@link LifecycleComponent} will have their lifecycle managed. * Note: To aid in the migration away from guice, all objects returned as components will be bound in guice * to themselves. + * + * @param client A client to make requests to the system + * @param clusterService A service to allow watching and updating cluster state + * @param threadPool A service to allow retrieving an executor to run an async action */ - public Collection createComponents() { + public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool) { return Collections.emptyList(); } diff --git a/core/src/main/java/org/elasticsearch/plugins/PluginsService.java b/core/src/main/java/org/elasticsearch/plugins/PluginsService.java index a03b7c30b7f..ecb51da1aae 100644 --- a/core/src/main/java/org/elasticsearch/plugins/PluginsService.java +++ b/core/src/main/java/org/elasticsearch/plugins/PluginsService.java @@ -19,6 +19,26 @@ package org.elasticsearch.plugins; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; + import org.apache.lucene.analysis.util.CharFilterFactory; import org.apache.lucene.analysis.util.TokenFilterFactory; import org.apache.lucene.analysis.util.TokenizerFactory; @@ -42,26 +62,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.IndexModule; import org.elasticsearch.threadpool.ExecutorBuilder; -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.URL; -import java.net.URLClassLoader; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.function.Function; -import java.util.stream.Collectors; - import static org.elasticsearch.common.io.FileSystemUtils.isAccessibleDirectory; /** @@ -293,11 +293,6 @@ public class PluginsService extends AbstractComponent { return services; } - /** Gets components from each plugin. This method should be called exactly once. */ - public Collection createComponenents() { - return plugins.stream().flatMap(p -> p.v2().createComponents().stream()).collect(Collectors.toList()); - } - public void onIndexModule(IndexModule indexModule) { for (Tuple plugin : plugins) { plugin.v2().onIndexModule(indexModule);