Simplify ScriptModule and script registration elastic/elasticsearchelastic/elasticsearch#18903 (elastic/elasticsearch#2535)

follow up PR for elastic/elasticsearchelastic/elasticsearch#18903

Original commit: elastic/x-pack-elasticsearch@d6ab3ab141
This commit is contained in:
Simon Willnauer 2016-06-16 09:35:16 +02:00 committed by GitHub
parent f92314ba00
commit 36ad326483
8 changed files with 44 additions and 38 deletions

View File

@ -14,7 +14,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
import org.elasticsearch.script.ScriptContextRegistry; import org.elasticsearch.script.ScriptContextRegistry;
import org.elasticsearch.script.ScriptEngineRegistry; import org.elasticsearch.script.ScriptEngineRegistry;
import org.elasticsearch.script.ScriptEngineService;
import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptSettings; import org.elasticsearch.script.ScriptSettings;
import org.elasticsearch.script.groovy.GroovyScriptEngineService; import org.elasticsearch.script.groovy.GroovyScriptEngineService;
@ -25,8 +24,7 @@ import org.junit.Ignore;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.Collections;
import java.util.Set;
@Ignore // not a test. @Ignore // not a test.
@SuppressForbidden(reason = "gradle is broken and tries to run me as a test") @SuppressForbidden(reason = "gradle is broken and tries to run me as a test")
@ -38,19 +36,13 @@ public final class MessyTestUtils {
.put("path.home", LuceneTestCase.createTempDir()) .put("path.home", LuceneTestCase.createTempDir())
.build(); .build();
GroovyScriptEngineService groovyScriptEngineService = new GroovyScriptEngineService(settings); GroovyScriptEngineService groovyScriptEngineService = new GroovyScriptEngineService(settings);
Set<ScriptEngineService> engineServiceSet = new HashSet<>(); ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singleton(groovyScriptEngineService));
engineServiceSet.add(groovyScriptEngineService);
ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(
Arrays.asList(
new ScriptEngineRegistry.ScriptEngineRegistration(GroovyScriptEngineService.class, GroovyScriptEngineService.NAME)
)
);
ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Arrays.asList(ScriptServiceProxy.INSTANCE)); ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Arrays.asList(ScriptServiceProxy.INSTANCE));
ClusterService clusterService = Mockito.mock(ClusterService.class); ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build()); Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build());
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings), engineServiceSet, return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings),
new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings), new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings),
clusterService); clusterService);
} }

View File

@ -49,18 +49,14 @@ public class WatcherTemplateTests extends ESTestCase {
public void init() throws Exception { public void init() throws Exception {
Settings setting = Settings.builder().put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING, true).build(); Settings setting = Settings.builder().put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING, true).build();
Environment environment = Mockito.mock(Environment.class); Environment environment = Mockito.mock(Environment.class);
Set<ScriptEngineService> engines = Collections.singleton(new MustacheScriptEngineService(setting));
ResourceWatcherService resourceWatcherService = Mockito.mock(ResourceWatcherService.class); ResourceWatcherService resourceWatcherService = Mockito.mock(ResourceWatcherService.class);
ScriptContextRegistry registry = new ScriptContextRegistry(Collections.singletonList(ScriptServiceProxy.INSTANCE)); ScriptContextRegistry registry = new ScriptContextRegistry(Collections.singletonList(ScriptServiceProxy.INSTANCE));
ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry( ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(
Arrays.asList( Collections.singleton(new MustacheScriptEngineService(setting))
new ScriptEngineRegistry.ScriptEngineRegistration(MustacheScriptEngineService.class,
MustacheScriptEngineService.NAME)
)
); );
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, registry); ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, registry);
ScriptService scriptService = new ScriptService(setting, environment, engines, resourceWatcherService, scriptEngineRegistry, ScriptService scriptService = new ScriptService(setting, environment, resourceWatcherService, scriptEngineRegistry,
registry, scriptSettings); registry, scriptSettings);
ClusterService clusterService = Mockito.mock(ClusterService.class); ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build()); Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build());

View File

@ -15,6 +15,7 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.ScriptQueryBuilder; import org.elasticsearch.index.query.ScriptQueryBuilder;
import org.elasticsearch.marvel.Monitoring; import org.elasticsearch.marvel.Monitoring;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.script.AbstractSearchScript; import org.elasticsearch.script.AbstractSearchScript;
import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.NativeScriptFactory; import org.elasticsearch.script.NativeScriptFactory;
@ -34,6 +35,8 @@ import org.elasticsearch.xpack.graph.action.Vertex;
import org.elasticsearch.xpack.graph.action.VertexRequest; import org.elasticsearch.xpack.graph.action.VertexRequest;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
@ -346,7 +349,7 @@ public class GraphTests extends ESSingleNodeTestCase {
assertThat(why, strongVertex.getWeight(), greaterThan(weakVertex.getWeight())); assertThat(why, strongVertex.getWeight(), greaterThan(weakVertex.getWeight()));
} }
public static class ScriptedTimeoutPlugin extends Plugin { public static class ScriptedTimeoutPlugin extends Plugin implements ScriptPlugin {
@Override @Override
public String name() { public String name() {
return "test-scripted-graph-timeout"; return "test-scripted-graph-timeout";
@ -357,8 +360,9 @@ public class GraphTests extends ESSingleNodeTestCase {
return "Test for scripted timeouts on graph searches"; return "Test for scripted timeouts on graph searches";
} }
public void onModule(ScriptModule module) { @Override
module.registerScript(NativeTestScriptedTimeout.TEST_NATIVE_SCRIPT_TIMEOUT, NativeTestScriptedTimeout.Factory.class); public List<NativeScriptFactory> getNativeScripts() {
return Collections.singletonList(new NativeTestScriptedTimeout.Factory());
} }
} }
@ -377,6 +381,11 @@ public class GraphTests extends ESSingleNodeTestCase {
public boolean needsScores() { public boolean needsScores() {
return false; return false;
} }
@Override
public String getName() {
return TEST_NATIVE_SCRIPT_TIMEOUT;
}
} }
@Override @Override

View File

@ -22,6 +22,8 @@ import org.elasticsearch.index.IndexModule;
import org.elasticsearch.license.plugin.Licensing; import org.elasticsearch.license.plugin.Licensing;
import org.elasticsearch.marvel.Monitoring; import org.elasticsearch.marvel.Monitoring;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptModule; import org.elasticsearch.script.ScriptModule;
import org.elasticsearch.shield.Security; import org.elasticsearch.shield.Security;
import org.elasticsearch.shield.authc.AuthenticationModule; import org.elasticsearch.shield.authc.AuthenticationModule;
@ -31,6 +33,7 @@ import org.elasticsearch.xpack.action.TransportXPackInfoAction;
import org.elasticsearch.xpack.action.TransportXPackUsageAction; 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.http.HttpClientModule; import org.elasticsearch.xpack.common.http.HttpClientModule;
import org.elasticsearch.xpack.common.init.LazyInitializationModule; import org.elasticsearch.xpack.common.init.LazyInitializationModule;
import org.elasticsearch.xpack.common.init.LazyInitializationService; import org.elasticsearch.xpack.common.init.LazyInitializationService;
@ -55,7 +58,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class XPackPlugin extends Plugin { public class XPackPlugin extends Plugin implements ScriptPlugin {
public static final String NAME = "x-pack"; public static final String NAME = "x-pack";
@ -182,8 +185,9 @@ public class XPackPlugin extends Plugin {
return builder.build(); return builder.build();
} }
public void onModule(ScriptModule module) { @Override
watcher.onModule(module); public ScriptContext.Plugin getCustomScriptContexts() {
return ScriptServiceProxy.INSTANCE;
} }
public void onModule(SettingsModule module) { public void onModule(SettingsModule module) {

View File

@ -140,9 +140,6 @@ public class Watcher {
return Settings.EMPTY; return Settings.EMPTY;
} }
public void onModule(ScriptModule module) {
module.registerScriptContext(ScriptServiceProxy.INSTANCE);
}
public void onModule(SettingsModule module) { public void onModule(SettingsModule module) {
for (TemplateConfig templateConfig : WatcherIndexTemplateRegistry.TEMPLATE_CONFIGS) { for (TemplateConfig templateConfig : WatcherIndexTemplateRegistry.TEMPLATE_CONFIGS) {

View File

@ -5,10 +5,9 @@
*/ */
package org.elasticsearch.script; package org.elasticsearch.script;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.common.text.DefaultTextTemplateEngine; import org.elasticsearch.xpack.common.text.DefaultTextTemplateEngine;
import java.util.Collections;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -27,10 +26,10 @@ public class MockMustacheScriptEngine extends MockScriptEngine {
return NAME; return NAME;
} }
public void onModule(ScriptModule module) { @Override
module.addScriptEngine(new ScriptEngineRegistry.ScriptEngineRegistration(MockMustacheScriptEngine.class, NAME, true)); public ScriptEngineService getScriptEngineService(Settings settings) {
return new MockMustacheScriptEngine();
} }
} }
@Override @Override
@ -51,4 +50,9 @@ public class MockMustacheScriptEngine extends MockScriptEngine {
return super.compile(name, script, params); return super.compile(name, script, params);
} }
@Override
public boolean isInlineScriptEnabled() {
return true;
}
} }

View File

@ -6,12 +6,13 @@
package org.elasticsearch.script; package org.elasticsearch.script;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.search.lookup.SearchLookup; import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -21,7 +22,7 @@ public class SleepScriptEngine implements ScriptEngineService {
public static final String NAME = "sleep"; public static final String NAME = "sleep";
public static class TestPlugin extends Plugin { public static class TestPlugin extends Plugin implements ScriptPlugin {
public TestPlugin() { public TestPlugin() {
} }
@ -36,11 +37,10 @@ public class SleepScriptEngine implements ScriptEngineService {
return "Mock script engine for integration tests"; return "Mock script engine for integration tests";
} }
public void onModule(ScriptModule module) { @Override
module.addScriptEngine(new ScriptEngineRegistry.ScriptEngineRegistration(SleepScriptEngine.class, public ScriptEngineService getScriptEngineService(Settings settings) {
SleepScriptEngine.NAME, true)); return new SleepScriptEngine();
} }
} }
@Override @Override
@ -92,4 +92,8 @@ public class SleepScriptEngine implements ScriptEngineService {
.params(Collections.singletonMap("millis", millis)).build(); .params(Collections.singletonMap("millis", millis)).build();
} }
@Override
public boolean isInlineScriptEnabled() {
return true;
}
} }

View File

@ -254,7 +254,7 @@ public final class WatcherTestUtils {
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
ClusterService clusterService = Mockito.mock(ClusterService.class); ClusterService clusterService = Mockito.mock(ClusterService.class);
Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build()); Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build());
return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings), Collections.emptySet(), return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings),
new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings), new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings),
clusterService); clusterService);
} }