From c01d36e9ee8f0216c62ec4765ed840faad23ab11 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 22 Jan 2016 21:51:37 -0500 Subject: [PATCH] Script default lang setting This commit converts the script default language setting "script.default_lang" to the new settings infrastructure. --- .../elasticsearch/script/ScriptModule.java | 1 + .../elasticsearch/script/ScriptService.java | 4 +-- .../elasticsearch/script/ScriptSettings.java | 14 ++++++++ .../script/ScriptSettingsTests.java | 32 +++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/script/ScriptModule.java b/core/src/main/java/org/elasticsearch/script/ScriptModule.java index f4700440cd9..1ccc7eb956d 100644 --- a/core/src/main/java/org/elasticsearch/script/ScriptModule.java +++ b/core/src/main/java/org/elasticsearch/script/ScriptModule.java @@ -93,6 +93,7 @@ public class ScriptModule extends AbstractModule { scriptSettings.getScriptTypeSettings().forEach(settingsModule::registerSetting); scriptSettings.getScriptContextSettings().forEach(settingsModule::registerSetting); scriptSettings.getScriptLanguageSettings().forEach(settingsModule::registerSetting); + settingsModule.registerSetting(scriptSettings.getDefaultScriptLanguageSetting()); bind(ScriptContextRegistry.class).toInstance(scriptContextRegistry); bind(ScriptEngineRegistry.class).toInstance(scriptEngineRegistry); diff --git a/core/src/main/java/org/elasticsearch/script/ScriptService.java b/core/src/main/java/org/elasticsearch/script/ScriptService.java index 3e40c0b1d8c..df5b6ad31dd 100644 --- a/core/src/main/java/org/elasticsearch/script/ScriptService.java +++ b/core/src/main/java/org/elasticsearch/script/ScriptService.java @@ -85,11 +85,9 @@ public class ScriptService extends AbstractComponent implements Closeable { static final String DISABLE_DYNAMIC_SCRIPTING_SETTING = "script.disable_dynamic"; - public static final String DEFAULT_SCRIPTING_LANGUAGE_SETTING = "script.default_lang"; public static final Setting SCRIPT_CACHE_SIZE_SETTING = Setting.intSetting("script.cache.max_size", 100, 0, false, Setting.Scope.CLUSTER); public static final Setting SCRIPT_CACHE_EXPIRE_SETTING = Setting.positiveTimeSetting("script.cache.expire", TimeValue.timeValueMillis(0), false, Setting.Scope.CLUSTER); public static final String SCRIPT_INDEX = ".scripts"; - public static final String DEFAULT_LANG = "groovy"; public static final Setting SCRIPT_AUTO_RELOAD_ENABLED_SETTING = Setting.boolSetting("script.auto_reload_enabled", true, false, Setting.Scope.CLUSTER); private final String defaultLang; @@ -154,7 +152,7 @@ public class ScriptService extends AbstractComponent implements Closeable { this.scriptContextRegistry = scriptContextRegistry; int cacheMaxSize = SCRIPT_CACHE_SIZE_SETTING.get(settings); - this.defaultLang = settings.get(DEFAULT_SCRIPTING_LANGUAGE_SETTING, DEFAULT_LANG); + this.defaultLang = scriptSettings.getDefaultScriptLanguageSetting().get(settings); CacheBuilder cacheBuilder = CacheBuilder.builder(); if (cacheMaxSize >= 0) { diff --git a/core/src/main/java/org/elasticsearch/script/ScriptSettings.java b/core/src/main/java/org/elasticsearch/script/ScriptSettings.java index 99418f082fb..b593a327a43 100644 --- a/core/src/main/java/org/elasticsearch/script/ScriptSettings.java +++ b/core/src/main/java/org/elasticsearch/script/ScriptSettings.java @@ -33,6 +33,8 @@ import java.util.stream.Collectors; public class ScriptSettings { + public final static String DEFAULT_LANG = "groovy"; + private final static Map> SCRIPT_TYPE_SETTING_MAP; static { @@ -50,6 +52,7 @@ public class ScriptSettings { private final Map> scriptContextSettingMap; private final List> scriptLanguageSettings; + private final Setting defaultScriptLanguageSetting; public ScriptSettings(ScriptEngineRegistry scriptEngineRegistry, ScriptContextRegistry scriptContextRegistry) { Map> scriptContextSettingMap = contextSettings(scriptContextRegistry); @@ -57,6 +60,13 @@ public class ScriptSettings { List> scriptLanguageSettings = languageSettings(SCRIPT_TYPE_SETTING_MAP, scriptContextSettingMap, scriptEngineRegistry, scriptContextRegistry); this.scriptLanguageSettings = Collections.unmodifiableList(scriptLanguageSettings); + + this.defaultScriptLanguageSetting = new Setting<>("script.default_lang", DEFAULT_LANG, setting -> { + if (!"groovy".equals(setting) && !scriptEngineRegistry.getRegisteredLanguages().containsKey(setting)) { + throw new IllegalArgumentException("unregistered default language [" + setting + "]"); + } + return setting; + }, false, Setting.Scope.CLUSTER); } private static Map> contextSettings(ScriptContextRegistry scriptContextRegistry) { @@ -148,4 +158,8 @@ public class ScriptSettings { public Iterable> getScriptLanguageSettings() { return scriptLanguageSettings; } + + public Setting getDefaultScriptLanguageSetting() { + return defaultScriptLanguageSetting; + } } diff --git a/core/src/test/java/org/elasticsearch/script/ScriptSettingsTests.java b/core/src/test/java/org/elasticsearch/script/ScriptSettingsTests.java index a407aac63c5..450007a3ae2 100644 --- a/core/src/test/java/org/elasticsearch/script/ScriptSettingsTests.java +++ b/core/src/test/java/org/elasticsearch/script/ScriptSettingsTests.java @@ -64,6 +64,38 @@ public class ScriptSettingsTests extends ESTestCase { assertTrue(sawConflictingSettings); } + public void testDefaultLanguageIsGroovy() { + ScriptEngineRegistry scriptEngineRegistry = + new ScriptEngineRegistry(Collections.singletonList(new ScriptEngineRegistry.ScriptEngineRegistration(CustomScriptEngineService.class, CustomScriptEngineService.TYPES))); + ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList()); + ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); + assertThat(scriptSettings.getDefaultScriptLanguageSetting().get(Settings.EMPTY), equalTo("groovy")); + } + + public void testCustomDefaultLanguage() { + ScriptEngineRegistry scriptEngineRegistry = + new ScriptEngineRegistry(Collections.singletonList(new ScriptEngineRegistry.ScriptEngineRegistration(CustomScriptEngineService.class, CustomScriptEngineService.TYPES))); + ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList()); + ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); + String defaultLanguage = randomFrom(CustomScriptEngineService.TYPES); + Settings settings = Settings.builder().put("script.default_lang", defaultLanguage).build(); + assertThat(scriptSettings.getDefaultScriptLanguageSetting().get(settings), equalTo(defaultLanguage)); + } + + public void testInvalidDefaultLanguage() { + ScriptEngineRegistry scriptEngineRegistry = + new ScriptEngineRegistry(Collections.singletonList(new ScriptEngineRegistry.ScriptEngineRegistration(CustomScriptEngineService.class, CustomScriptEngineService.TYPES))); + ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList()); + ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); + Settings settings = Settings.builder().put("script.default_lang", "C++").build(); + try { + scriptSettings.getDefaultScriptLanguageSetting().get(settings); + fail("should have seen unregistered default language"); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), containsString("unregistered default language [C++]")); + } + } + private static class CustomScriptEngineService implements ScriptEngineService { public static final List TYPES = Collections.unmodifiableList(Arrays.asList("test1", "test2", "test3"));