Remove LazyInitializable from ScriptServiceProxy

Closes elastic/elasticsearch#2062

Original commit: elastic/x-pack-elasticsearch@4eaf323158
This commit is contained in:
Martijn van Groningen 2016-04-22 14:02:07 +02:00
parent b9515357fa
commit 4650592150
20 changed files with 39 additions and 50 deletions

View File

@ -16,7 +16,7 @@ import org.elasticsearch.watcher.condition.script.ExecutableScriptCondition;
import org.elasticsearch.watcher.condition.script.ScriptCondition;
import org.elasticsearch.watcher.execution.WatchExecutionContext;
import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTestCase;
import org.elasticsearch.watcher.watch.Payload;
import org.junit.AfterClass;

View File

@ -20,7 +20,7 @@ import org.elasticsearch.script.ScriptSettings;
import org.elasticsearch.script.groovy.GroovyScriptEngineService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.ResourceWatcherService;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.junit.Ignore;
import org.mockito.Mockito;

View File

@ -23,7 +23,7 @@ import org.elasticsearch.watcher.condition.script.ExecutableScriptCondition;
import org.elasticsearch.watcher.condition.script.ScriptCondition;
import org.elasticsearch.watcher.execution.WatchExecutionContext;
import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTestCase;
import org.elasticsearch.watcher.watch.Payload;
import org.junit.After;

View File

@ -14,7 +14,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.script.ScriptException;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptService.ScriptType;
import org.elasticsearch.search.internal.InternalSearchResponse;
import org.elasticsearch.test.ESTestCase;
@ -25,7 +24,7 @@ import org.elasticsearch.watcher.condition.script.ScriptCondition;
import org.elasticsearch.watcher.condition.script.ScriptConditionFactory;
import org.elasticsearch.watcher.execution.WatchExecutionContext;
import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.watch.Payload;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

View File

@ -21,7 +21,7 @@ import org.elasticsearch.script.ScriptSettings;
import org.elasticsearch.script.mustache.MustacheScriptEngineService;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.watcher.ResourceWatcherService;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.support.text.DefaultTextTemplateEngine;
import org.elasticsearch.watcher.support.text.TextTemplate;
import org.elasticsearch.watcher.support.text.TextTemplateEngine;

View File

@ -52,7 +52,7 @@ import org.elasticsearch.watcher.support.WatcherIndexTemplateRegistry.TemplateCo
import org.elasticsearch.watcher.support.clock.ClockModule;
import org.elasticsearch.watcher.support.http.HttpClient;
import org.elasticsearch.watcher.support.http.HttpClientModule;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.support.init.proxy.WatcherClientProxy;
import org.elasticsearch.watcher.support.secret.SecretModule;
import org.elasticsearch.watcher.support.secret.SecretService;
@ -240,7 +240,6 @@ public class Watcher {
public void onModule(LazyInitializationModule module) {
if (enabled) {
module.registerLazyInitializable(WatcherClientProxy.class);
module.registerLazyInitializable(ScriptServiceProxy.class);
module.registerLazyInitializable(ChainTransformFactory.class);
module.registerLazyInitializable(ChainInputFactory.class);
}

View File

@ -11,7 +11,7 @@ import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.watcher.condition.ExecutableCondition;
import org.elasticsearch.watcher.execution.WatchExecutionContext;
import org.elasticsearch.watcher.support.Variables;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import java.util.Map;

View File

@ -10,7 +10,7 @@ import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.watcher.condition.ConditionFactory;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import java.io.IOException;

View File

@ -3,49 +3,35 @@
* 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.watcher.support.init.proxy;
package org.elasticsearch.watcher.support;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.shield.SecurityContext;
import org.elasticsearch.shield.user.XPackUser;
import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.xpack.common.init.LazyInitializable;
import java.util.Map;
import static java.util.Collections.emptyMap;
/**
*A lazily initialized proxy to the elasticsearch {@link ScriptService}. Inject this proxy whenever the script
* service needs to be injected to avoid circular dependencies issues.
* Wraps {@link ScriptService} but ensure that all scripts are run or compiled as {@link XPackUser}.
*/
public class ScriptServiceProxy implements LazyInitializable {
public class ScriptServiceProxy {
private ScriptService service;
private SecurityContext securityContext;
private ClusterService clusterService;
private final ScriptService service;
private final SecurityContext securityContext;
private final ClusterService clusterService;
/**
* Creates a proxy to the given script service (can be used for testing)
*/
public static ScriptServiceProxy of(ScriptService service, ClusterService clusterService) {
ScriptServiceProxy proxy = new ScriptServiceProxy();
proxy.service = service;
proxy.securityContext = SecurityContext.Insecure.INSTANCE;
proxy.clusterService = clusterService;
return proxy;
}
@Override
public void init(Injector injector) {
this.service = injector.getInstance(ScriptService.class);
this.securityContext = injector.getInstance(SecurityContext.class);
this.clusterService = injector.getInstance(ClusterService.class);
@Inject
public ScriptServiceProxy(ScriptService service, SecurityContext securityContext, ClusterService clusterService) {
this.service = service;
this.securityContext = securityContext;
this.clusterService = clusterService;
}
public CompiledScript compile(Script script) {
@ -73,4 +59,11 @@ public class ScriptServiceProxy implements LazyInitializable {
return INSTANCE.getKey();
}
}
/**
* Factory helper method for testing.
*/
public static ScriptServiceProxy of(ScriptService service, ClusterService clusterService) {
return new ScriptServiceProxy(service, SecurityContext.Insecure.INSTANCE, clusterService);
}
}

View File

@ -13,7 +13,7 @@ import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.Template;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import java.util.Collections;
import java.util.HashMap;

View File

@ -10,7 +10,7 @@ import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.watcher.execution.WatchExecutionContext;
import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.transform.ExecutableTransform;
import org.elasticsearch.watcher.watch.Payload;

View File

@ -9,7 +9,7 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.transform.TransformFactory;
import java.io.IOException;

View File

@ -7,6 +7,7 @@ package org.elasticsearch.watcher.watch;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.watcher.WatcherService;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
/**
*
@ -19,5 +20,6 @@ public class WatchModule extends AbstractModule {
bind(WatchLockService.class).asEagerSingleton();
bind(WatcherService.class).asEagerSingleton();
bind(WatchStore.class).asEagerSingleton();
bind(ScriptServiceProxy.class).asEagerSingleton();
}
}

View File

@ -20,7 +20,7 @@ import org.elasticsearch.search.internal.InternalSearchResponse;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.execution.WatchExecutionContext;
import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTestCase;
import org.elasticsearch.watcher.test.WatcherTestUtils;
import org.elasticsearch.watcher.watch.Payload;

View File

@ -15,7 +15,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.script.ScriptException;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptService.ScriptType;
import org.elasticsearch.search.internal.InternalSearchResponse;
import org.elasticsearch.test.ESTestCase;
@ -23,7 +22,7 @@ import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.condition.Condition;
import org.elasticsearch.watcher.execution.WatchExecutionContext;
import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.watch.Payload;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

View File

@ -17,10 +17,7 @@ import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptService.ScriptType;
import org.elasticsearch.script.Template;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.text.DefaultTextTemplateEngine;
import org.elasticsearch.watcher.support.text.TextTemplate;
import org.elasticsearch.watcher.support.text.TextTemplateEngine;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.junit.Before;
import java.util.Collections;

View File

@ -54,7 +54,7 @@ import org.elasticsearch.watcher.WatcherLicensee;
import org.elasticsearch.watcher.support.WatcherIndexTemplateRegistry;
import org.elasticsearch.watcher.support.clock.ClockMock;
import org.elasticsearch.watcher.support.http.HttpClient;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.support.xcontent.XContentSource;
import org.elasticsearch.watcher.trigger.ScheduleTriggerEngineMock;
import org.elasticsearch.watcher.trigger.TriggerService;

View File

@ -54,7 +54,7 @@ import org.elasticsearch.watcher.support.http.HttpClient;
import org.elasticsearch.watcher.support.http.HttpMethod;
import org.elasticsearch.watcher.support.http.HttpRequestTemplate;
import org.elasticsearch.watcher.support.init.proxy.WatcherClientProxy;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.support.secret.Secret;
import org.elasticsearch.watcher.support.text.TextTemplate;
import org.elasticsearch.watcher.support.text.TextTemplateEngine;

View File

@ -19,7 +19,7 @@ import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.execution.WatchExecutionContext;
import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.watcher.support.Variables;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.transform.Transform;
import org.elasticsearch.watcher.watch.Payload;
import org.junit.After;

View File

@ -85,7 +85,7 @@ import org.elasticsearch.watcher.support.http.HttpMethod;
import org.elasticsearch.watcher.support.http.HttpRequestTemplate;
import org.elasticsearch.watcher.support.http.auth.HttpAuthRegistry;
import org.elasticsearch.watcher.support.http.auth.basic.BasicAuthFactory;
import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy;
import org.elasticsearch.watcher.support.ScriptServiceProxy;
import org.elasticsearch.watcher.support.init.proxy.WatcherClientProxy;
import org.elasticsearch.watcher.support.secret.SecretService;
import org.elasticsearch.watcher.support.text.TextTemplate;