From dd89a7b06145d8ecc17b9ccdc8c053c526e2f985 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 11 Jul 2016 18:05:33 -0700 Subject: [PATCH] Remove guice from watcher http client and related classes. This is the first deguicing for xplugins to use the new createComponents(). The removal was very straightforward. One thing to note is HttpAuthFactory only has one implementation (basic auth), but I kept the registry and such for now. Also, HttpRequest.Parser is only used in 2 tests, not at all in main code, it should probably be removed. Original commit: elastic/x-pack-elasticsearch@839e7e49005966befcd704acaf6a0df4f9bf12d3 --- .../license/plugin/Licensing.java | 9 +++-- .../license/plugin/LicensingModule.java | 24 ------------- .../xpack/security/Security.java | 4 +++ .../org/elasticsearch/xpack/XPackPlugin.java | 29 ++++++++++++++-- .../xpack/common/http/HttpClient.java | 1 - .../xpack/common/http/HttpClientModule.java | 34 ------------------- .../xpack/common/http/HttpRequest.java | 1 - .../common/http/HttpRequestTemplate.java | 1 - .../common/http/auth/HttpAuthRegistry.java | 1 - .../http/auth/basic/BasicAuthFactory.java | 1 - .../xpack/notification/Notification.java | 1 - 11 files changed, 37 insertions(+), 69 deletions(-) delete mode 100644 elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/LicensingModule.java delete mode 100644 elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpClientModule.java diff --git a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/Licensing.java b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/Licensing.java index b92bd35752d..037c7b3b206 100644 --- a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/Licensing.java +++ b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/Licensing.java @@ -19,6 +19,8 @@ import org.elasticsearch.license.plugin.action.get.GetLicenseAction; import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction; import org.elasticsearch.license.plugin.action.put.PutLicenseAction; import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction; +import org.elasticsearch.license.plugin.core.LicenseeRegistry; +import org.elasticsearch.license.plugin.core.LicensesManagerService; import org.elasticsearch.license.plugin.core.LicensesMetaData; import org.elasticsearch.license.plugin.core.LicensesService; import org.elasticsearch.license.plugin.rest.RestDeleteLicenseAction; @@ -47,7 +49,6 @@ public class Licensing implements ActionPlugin { MetaData.registerPrototype(LicensesMetaData.TYPE, LicensesMetaData.PROTO); } - @Inject public Licensing(Settings settings) { isTransportClient = transportClientMode(settings); isTribeNode = isTribeNode(settings); @@ -82,7 +83,11 @@ public class Licensing implements ActionPlugin { public Collection nodeModules() { if (isTransportClient == false && isTribeNode == false) { - return Collections.singletonList(new LicensingModule()); + return Collections.singletonList(b -> { + b.bind(LicensesService.class).asEagerSingleton(); + b.bind(LicenseeRegistry.class).to(LicensesService.class); + b.bind(LicensesManagerService.class).to(LicensesService.class); + }); } return Collections.emptyList(); } diff --git a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/LicensingModule.java b/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/LicensingModule.java deleted file mode 100644 index ab37ca62c3e..00000000000 --- a/elasticsearch/x-pack/license-plugin/src/main/java/org/elasticsearch/license/plugin/LicensingModule.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.license.plugin; - -import org.elasticsearch.common.inject.AbstractModule; -import org.elasticsearch.license.core.LicenseVerifier; -import org.elasticsearch.license.plugin.core.LicenseeRegistry; -import org.elasticsearch.license.plugin.core.LicensesManagerService; -import org.elasticsearch.license.plugin.core.LicensesService; - -public class LicensingModule extends AbstractModule { - - @Override - protected void configure() { - bind(LicenseVerifier.class).asEagerSingleton(); - bind(LicensesService.class).asEagerSingleton(); - bind(LicenseeRegistry.class).to(LicensesService.class); - bind(LicensesManagerService.class).to(LicensesService.class); - } - -} diff --git a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/Security.java b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/Security.java index e8b03c7fc73..21d29bb730f 100644 --- a/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/Security.java +++ b/elasticsearch/x-pack/security/src/main/java/org/elasticsearch/xpack/security/Security.java @@ -133,6 +133,10 @@ public class Security implements ActionPlugin { } } + public CryptoService getCryptoService() { + return cryptoService; + } + public Collection nodeModules() { List modules = new ArrayList<>(); diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/XPackPlugin.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/XPackPlugin.java index d3d1aa4e49a..846e5c2a2ab 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/XPackPlugin.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/XPackPlugin.java @@ -12,7 +12,9 @@ import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.elasticsearch.SpecialPermission; import org.elasticsearch.action.ActionRequest; @@ -41,7 +43,12 @@ import org.elasticsearch.xpack.action.TransportXPackUsageAction; import org.elasticsearch.xpack.action.XPackInfoAction; import org.elasticsearch.xpack.action.XPackUsageAction; import org.elasticsearch.xpack.common.ScriptServiceProxy; -import org.elasticsearch.xpack.common.http.HttpClientModule; +import org.elasticsearch.xpack.common.http.HttpClient; +import org.elasticsearch.xpack.common.http.HttpRequestTemplate; +import org.elasticsearch.xpack.common.http.auth.HttpAuthFactory; +import org.elasticsearch.xpack.common.http.auth.HttpAuthRegistry; +import org.elasticsearch.xpack.common.http.auth.basic.BasicAuth; +import org.elasticsearch.xpack.common.http.auth.basic.BasicAuthFactory; import org.elasticsearch.xpack.common.text.TextTemplateModule; import org.elasticsearch.xpack.extensions.XPackExtension; import org.elasticsearch.xpack.extensions.XPackExtensionsService; @@ -100,6 +107,7 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin { } protected final Settings settings; + private final Environment env; protected boolean transportClientMode; protected final XPackExtensionsService extensionsService; @@ -113,7 +121,7 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin { public XPackPlugin(Settings settings) throws IOException { this.settings = settings; this.transportClientMode = transportClientMode(settings); - final Environment env = transportClientMode ? null : new Environment(settings); + this.env = transportClientMode ? null : new Environment(settings); this.licensing = new Licensing(settings); this.security = new Security(settings, env); @@ -151,7 +159,6 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin { modules.addAll(graph.createGuiceModules()); if (transportClientMode == false) { - modules.add(new HttpClientModule()); modules.add(new TextTemplateModule()); } return modules; @@ -169,6 +176,22 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin { return services; } + @Override + public Collection createComponents() { + List components = new ArrayList<>(); + if (transportClientMode == false) { + // watcher http stuff + Map httpAuthFactories = new HashMap<>(); + httpAuthFactories.put(BasicAuth.TYPE, new BasicAuthFactory(security.getCryptoService())); + // TODO: add more auth types, or remove this indirection + HttpAuthRegistry httpAuthRegistry = new HttpAuthRegistry(httpAuthFactories); + components.add(new HttpRequestTemplate.Parser(httpAuthRegistry)); + components.add(new HttpClient(settings, httpAuthRegistry, env)); + } + + return components; + } + @Override public Settings additionalSettings() { Settings.Builder builder = Settings.builder(); diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpClient.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpClient.java index 47147abe980..da5f0aa8a93 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpClient.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpClient.java @@ -79,7 +79,6 @@ public class HttpClient extends AbstractLifecycleComponent { private SSLSocketFactory sslSocketFactory; private HttpProxy proxy = HttpProxy.NO_PROXY; - @Inject public HttpClient(Settings settings, HttpAuthRegistry httpAuthRegistry, Environment env) { super(settings); this.httpAuthRegistry = httpAuthRegistry; diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpClientModule.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpClientModule.java deleted file mode 100644 index 548d487ec4c..00000000000 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpClientModule.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.common.http; - -import org.elasticsearch.common.inject.AbstractModule; -import org.elasticsearch.common.inject.multibindings.MapBinder; -import org.elasticsearch.xpack.common.http.auth.basic.BasicAuth; -import org.elasticsearch.xpack.common.http.auth.basic.BasicAuthFactory; -import org.elasticsearch.xpack.common.http.auth.HttpAuthFactory; -import org.elasticsearch.xpack.common.http.auth.HttpAuthRegistry; - - -/** - */ -public class HttpClientModule extends AbstractModule { - - @Override - protected void configure() { - bind(HttpRequestTemplate.Parser.class).asEagerSingleton(); - bind(HttpRequest.Parser.class).asEagerSingleton(); - bind(HttpClient.class).asEagerSingleton(); - - MapBinder parsersBinder = MapBinder.newMapBinder(binder(), String.class, HttpAuthFactory.class); - - bind(BasicAuthFactory.class).asEagerSingleton(); - parsersBinder.addBinding(BasicAuth.TYPE).to(BasicAuthFactory.class); - - bind(HttpAuthRegistry.class).asEagerSingleton(); - } - -} diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpRequest.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpRequest.java index 1f7730dd895..3b6311a62ae 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpRequest.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpRequest.java @@ -250,7 +250,6 @@ public class HttpRequest implements ToXContent { private final HttpAuthRegistry httpAuthRegistry; - @Inject public Parser(HttpAuthRegistry httpAuthRegistry) { this.httpAuthRegistry = httpAuthRegistry; } diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpRequestTemplate.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpRequestTemplate.java index 537cb5b2e1f..936e3faa54e 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpRequestTemplate.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/HttpRequestTemplate.java @@ -255,7 +255,6 @@ public class HttpRequestTemplate implements ToXContent { private final HttpAuthRegistry httpAuthRegistry; - @Inject public Parser(HttpAuthRegistry httpAuthRegistry) { this.httpAuthRegistry = httpAuthRegistry; } diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/auth/HttpAuthRegistry.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/auth/HttpAuthRegistry.java index da461ba9aa6..bf301ae0580 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/auth/HttpAuthRegistry.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/auth/HttpAuthRegistry.java @@ -21,7 +21,6 @@ public class HttpAuthRegistry { private final Map factories; - @Inject public HttpAuthRegistry(Map factories) { this.factories = factories; } diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/auth/basic/BasicAuthFactory.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/auth/basic/BasicAuthFactory.java index 48dfbe42cf8..9ef970f1da7 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/auth/basic/BasicAuthFactory.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/common/http/auth/basic/BasicAuthFactory.java @@ -20,7 +20,6 @@ public class BasicAuthFactory extends HttpAuthFactory>asList( - HttpClient.class, EmailService.class, HipChatService.class, SlackService.class,