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@839e7e4900
This commit is contained in:
Ryan Ernst 2016-07-11 18:05:33 -07:00
parent e92860f552
commit dd89a7b061
11 changed files with 37 additions and 69 deletions

View File

@ -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.get.TransportGetLicenseAction;
import org.elasticsearch.license.plugin.action.put.PutLicenseAction; import org.elasticsearch.license.plugin.action.put.PutLicenseAction;
import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction; 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.LicensesMetaData;
import org.elasticsearch.license.plugin.core.LicensesService; import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.license.plugin.rest.RestDeleteLicenseAction; import org.elasticsearch.license.plugin.rest.RestDeleteLicenseAction;
@ -47,7 +49,6 @@ public class Licensing implements ActionPlugin {
MetaData.registerPrototype(LicensesMetaData.TYPE, LicensesMetaData.PROTO); MetaData.registerPrototype(LicensesMetaData.TYPE, LicensesMetaData.PROTO);
} }
@Inject
public Licensing(Settings settings) { public Licensing(Settings settings) {
isTransportClient = transportClientMode(settings); isTransportClient = transportClientMode(settings);
isTribeNode = isTribeNode(settings); isTribeNode = isTribeNode(settings);
@ -82,7 +83,11 @@ public class Licensing implements ActionPlugin {
public Collection<Module> nodeModules() { public Collection<Module> nodeModules() {
if (isTransportClient == false && isTribeNode == false) { if (isTransportClient == false && isTribeNode == false) {
return Collections.<Module>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(); return Collections.emptyList();
} }

View File

@ -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);
}
}

View File

@ -133,6 +133,10 @@ public class Security implements ActionPlugin {
} }
} }
public CryptoService getCryptoService() {
return cryptoService;
}
public Collection<Module> nodeModules() { public Collection<Module> nodeModules() {
List<Module> modules = new ArrayList<>(); List<Module> modules = new ArrayList<>();

View File

@ -12,7 +12,9 @@ import java.security.PrivilegedAction;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.elasticsearch.SpecialPermission; import org.elasticsearch.SpecialPermission;
import org.elasticsearch.action.ActionRequest; 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.XPackInfoAction;
import org.elasticsearch.xpack.action.XPackUsageAction; import org.elasticsearch.xpack.action.XPackUsageAction;
import org.elasticsearch.xpack.common.ScriptServiceProxy; 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.common.text.TextTemplateModule;
import org.elasticsearch.xpack.extensions.XPackExtension; import org.elasticsearch.xpack.extensions.XPackExtension;
import org.elasticsearch.xpack.extensions.XPackExtensionsService; import org.elasticsearch.xpack.extensions.XPackExtensionsService;
@ -100,6 +107,7 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
} }
protected final Settings settings; protected final Settings settings;
private final Environment env;
protected boolean transportClientMode; protected boolean transportClientMode;
protected final XPackExtensionsService extensionsService; protected final XPackExtensionsService extensionsService;
@ -113,7 +121,7 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
public XPackPlugin(Settings settings) throws IOException { public XPackPlugin(Settings settings) throws IOException {
this.settings = settings; this.settings = settings;
this.transportClientMode = transportClientMode(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.licensing = new Licensing(settings);
this.security = new Security(settings, env); this.security = new Security(settings, env);
@ -151,7 +159,6 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
modules.addAll(graph.createGuiceModules()); modules.addAll(graph.createGuiceModules());
if (transportClientMode == false) { if (transportClientMode == false) {
modules.add(new HttpClientModule());
modules.add(new TextTemplateModule()); modules.add(new TextTemplateModule());
} }
return modules; return modules;
@ -169,6 +176,22 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
return services; return services;
} }
@Override
public Collection<Object> createComponents() {
List<Object> components = new ArrayList<>();
if (transportClientMode == false) {
// watcher http stuff
Map<String, HttpAuthFactory> 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 @Override
public Settings additionalSettings() { public Settings additionalSettings() {
Settings.Builder builder = Settings.builder(); Settings.Builder builder = Settings.builder();

View File

@ -79,7 +79,6 @@ public class HttpClient extends AbstractLifecycleComponent {
private SSLSocketFactory sslSocketFactory; private SSLSocketFactory sslSocketFactory;
private HttpProxy proxy = HttpProxy.NO_PROXY; private HttpProxy proxy = HttpProxy.NO_PROXY;
@Inject
public HttpClient(Settings settings, HttpAuthRegistry httpAuthRegistry, Environment env) { public HttpClient(Settings settings, HttpAuthRegistry httpAuthRegistry, Environment env) {
super(settings); super(settings);
this.httpAuthRegistry = httpAuthRegistry; this.httpAuthRegistry = httpAuthRegistry;

View File

@ -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<String, HttpAuthFactory> parsersBinder = MapBinder.newMapBinder(binder(), String.class, HttpAuthFactory.class);
bind(BasicAuthFactory.class).asEagerSingleton();
parsersBinder.addBinding(BasicAuth.TYPE).to(BasicAuthFactory.class);
bind(HttpAuthRegistry.class).asEagerSingleton();
}
}

View File

@ -250,7 +250,6 @@ public class HttpRequest implements ToXContent {
private final HttpAuthRegistry httpAuthRegistry; private final HttpAuthRegistry httpAuthRegistry;
@Inject
public Parser(HttpAuthRegistry httpAuthRegistry) { public Parser(HttpAuthRegistry httpAuthRegistry) {
this.httpAuthRegistry = httpAuthRegistry; this.httpAuthRegistry = httpAuthRegistry;
} }

View File

@ -255,7 +255,6 @@ public class HttpRequestTemplate implements ToXContent {
private final HttpAuthRegistry httpAuthRegistry; private final HttpAuthRegistry httpAuthRegistry;
@Inject
public Parser(HttpAuthRegistry httpAuthRegistry) { public Parser(HttpAuthRegistry httpAuthRegistry) {
this.httpAuthRegistry = httpAuthRegistry; this.httpAuthRegistry = httpAuthRegistry;
} }

View File

@ -21,7 +21,6 @@ public class HttpAuthRegistry {
private final Map<String, HttpAuthFactory> factories; private final Map<String, HttpAuthFactory> factories;
@Inject
public HttpAuthRegistry(Map<String, HttpAuthFactory> factories) { public HttpAuthRegistry(Map<String, HttpAuthFactory> factories) {
this.factories = factories; this.factories = factories;
} }

View File

@ -20,7 +20,6 @@ public class BasicAuthFactory extends HttpAuthFactory<BasicAuth, ApplicableBasic
private final CryptoService cryptoService; private final CryptoService cryptoService;
@Inject
public BasicAuthFactory(@Nullable CryptoService cryptoService) { public BasicAuthFactory(@Nullable CryptoService cryptoService) {
this.cryptoService = cryptoService; this.cryptoService = cryptoService;
} }

View File

@ -59,7 +59,6 @@ public class Notification {
return Collections.emptyList(); return Collections.emptyList();
} }
return Arrays.<Class<? extends LifecycleComponent>>asList( return Arrays.<Class<? extends LifecycleComponent>>asList(
HttpClient.class,
EmailService.class, EmailService.class,
HipChatService.class, HipChatService.class,
SlackService.class, SlackService.class,