diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-10-09 15:58:55 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-10-10 15:25:04 +0200 |
commit | 8cee55e18d3d0bf8959423f2ab7a9ffcc2fcd45b (patch) | |
tree | ad464a27b2e6dc725f917f70c51a5db6762e1849 /server/sonar-search | |
parent | 5851a9a6c9d34d3239de2e8457e00889ae6d9317 (diff) | |
download | sonarqube-8cee55e18d3d0bf8959423f2ab7a9ffcc2fcd45b.tar.gz sonarqube-8cee55e18d3d0bf8959423f2ab7a9ffcc2fcd45b.zip |
Remove dangerous coupling of sonar-server on sonar-search
Diffstat (limited to 'server/sonar-search')
3 files changed, 39 insertions, 44 deletions
diff --git a/server/sonar-search/src/main/java/org/sonar/search/SearchSettings.java b/server/sonar-search/src/main/java/org/sonar/search/SearchSettings.java index b33c8b50f5b..c1c7d564002 100644 --- a/server/sonar-search/src/main/java/org/sonar/search/SearchSettings.java +++ b/server/sonar-search/src/main/java/org/sonar/search/SearchSettings.java @@ -117,7 +117,8 @@ class SearchSettings { private void configurePlugins(ImmutableSettings.Builder builder) { builder .put("script.default_lang", "native") - .put("script.native." + ListUpdate.NAME + ".type", ListUpdate.UpdateListScriptFactory.class.getName()); + .put(String.format("script.native.%s.type", ProcessConstants.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME), + ListUpdate.UpdateListScriptFactory.class.getName()); } private void configureNetwork(ImmutableSettings.Builder builder) { diff --git a/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java b/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java index 46c4ceed487..721ba6b2328 100644 --- a/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java +++ b/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.script.AbstractExecutableScript; import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.NativeScriptFactory; +import org.sonar.process.ProcessConstants; import java.util.ArrayList; import java.util.Collection; @@ -32,35 +33,28 @@ import java.util.Map; public class ListUpdate extends AbstractExecutableScript { - public static final String NAME = "listUpdate"; - - public static final String ID_FIELD = "idField"; - public static final String ID_VALUE = "idValue"; - public static final String FIELD = "field"; - public static final String VALUE = "value"; - public static class UpdateListScriptFactory implements NativeScriptFactory { @Override public ExecutableScript newScript(@Nullable Map<String, Object> params) { - String idField = XContentMapValues.nodeStringValue(params.get(ID_FIELD), null); - String idValue = XContentMapValues.nodeStringValue(params.get(ID_VALUE), null); - String field = XContentMapValues.nodeStringValue(params.get(FIELD), null); + String idField = XContentMapValues.nodeStringValue(params.get(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD), null); + String idValue = XContentMapValues.nodeStringValue(params.get(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE), null); + String field = XContentMapValues.nodeStringValue(params.get(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD), null); Map value = null; if (idField == null) { - throw new IllegalStateException("Missing '" + ID_FIELD + "' parameter"); + throw new IllegalStateException(String.format("Missing '%s' parameter", ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD)); } if (idValue == null) { - throw new IllegalStateException("Missing '" + ID_VALUE + "' parameter"); + throw new IllegalStateException(String.format("Missing '%s' parameter", ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE)); } if (field == null) { - throw new IllegalStateException("Missing '" + FIELD + "' parameter"); + throw new IllegalStateException(String.format("Missing '%s' parameter", ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD)); } //NULL case is deletion of nested item - if (params.containsKey(VALUE)) { - Object obj = params.get(VALUE); + if (params.containsKey(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE)) { + Object obj = params.get(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE); if (obj != null) { - value = XContentMapValues.nodeMapValue(params.get(VALUE), "Update item"); + value = XContentMapValues.nodeMapValue(params.get(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE), "Update item"); } } @@ -73,7 +67,6 @@ public class ListUpdate extends AbstractExecutableScript { private final String idValue; private final String field; private final Map<String, Object> value; - private Map<String, Object> ctx; public ListUpdate(String idField, String idValue, String field, @Nullable Map<String, Object> value) { diff --git a/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java b/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java index f32f8f2e057..a645cb99647 100644 --- a/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java +++ b/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java @@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableMap; import org.elasticsearch.script.ExecutableScript; import org.junit.Before; import org.junit.Test; +import org.sonar.process.ProcessConstants; import java.util.Collection; import java.util.HashMap; @@ -53,7 +54,7 @@ public class UpdateListScriptTest { } // Missing ID_VALUE - params.put(ListUpdate.ID_FIELD, "test"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, "test"); try { factory.newScript(params); fail(); @@ -62,7 +63,7 @@ public class UpdateListScriptTest { } // Missing FIELD - params.put(ListUpdate.ID_VALUE, "test"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, "test"); try { factory.newScript(params); fail(); @@ -71,12 +72,12 @@ public class UpdateListScriptTest { } // Has all required attributes and Null Value - params.put(ListUpdate.FIELD, "test"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, "test"); ExecutableScript script = factory.newScript(params); assertThat(script).isNotNull(); // Has all required attributes and VALUE of wrong type - params.put(ListUpdate.VALUE, new Integer(52)); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, new Integer(52)); try { factory.newScript(params); fail(); @@ -85,12 +86,12 @@ public class UpdateListScriptTest { } // Has all required attributes and Proper VALUE - params.put(ListUpdate.VALUE, ImmutableMap.of("key", "value")); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, ImmutableMap.of("key", "value")); script = factory.newScript(params); assertThat(script).isNotNull(); // Missing ID_FIELD - params.remove(ListUpdate.ID_FIELD); + params.remove(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD); try { factory.newScript(params); fail(); @@ -109,10 +110,10 @@ public class UpdateListScriptTest { // 0 Create list when field does not exists Map<String, Object> params = new HashMap<String, Object>(); - params.put(ListUpdate.FIELD, listField); - params.put(ListUpdate.ID_FIELD, "key"); - params.put(ListUpdate.ID_VALUE, "1"); - params.put(ListUpdate.VALUE, mapOf("key", "1", "value", "A")); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, listField); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, "1"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "1", "value", "A")); ExecutableScript script = factory.newScript(params); script.setNextVar("ctx", ImmutableMap.of("_source", source)); @@ -124,10 +125,10 @@ public class UpdateListScriptTest { // Add item to existing list params = new HashMap<String, Object>(); - params.put(ListUpdate.FIELD, listField); - params.put(ListUpdate.ID_FIELD, "key"); - params.put(ListUpdate.ID_VALUE, "2"); - params.put(ListUpdate.VALUE, mapOf("key", "2", "value", "B")); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, listField); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, "2"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "2", "value", "B")); script = factory.newScript(params); script.setNextVar("ctx", ImmutableMap.of("_source", source)); script.run(); @@ -136,10 +137,10 @@ public class UpdateListScriptTest { // updated first item in list params = new HashMap<String, Object>(); - params.put(ListUpdate.FIELD, listField); - params.put(ListUpdate.ID_FIELD, "key"); - params.put(ListUpdate.ID_VALUE, "1"); - params.put(ListUpdate.VALUE, mapOf("key", "1", "value", "a")); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, listField); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, "1"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "1", "value", "a")); script = factory.newScript(params); script.setNextVar("ctx", ImmutableMap.of("_source", source)); script.run(); @@ -148,10 +149,10 @@ public class UpdateListScriptTest { // updated second item in list params = new HashMap<String, Object>(); - params.put(ListUpdate.FIELD, listField); - params.put(ListUpdate.ID_FIELD, "key"); - params.put(ListUpdate.ID_VALUE, "2"); - params.put(ListUpdate.VALUE, mapOf("key", "2", "value", "b")); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, listField); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, "2"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "2", "value", "b")); script = factory.newScript(params); script.setNextVar("ctx", ImmutableMap.of("_source", source)); script.run(); @@ -160,10 +161,10 @@ public class UpdateListScriptTest { // delete first item params = new HashMap<String, Object>(); - params.put(ListUpdate.FIELD, listField); - params.put(ListUpdate.ID_FIELD, "key"); - params.put(ListUpdate.ID_VALUE, "1"); - params.put(ListUpdate.VALUE, null); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_FIELD, listField); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_ID_VALUE, "1"); + params.put(ProcessConstants.ES_PLUGIN_LISTUPDATE_VALUE, null); script = factory.newScript(params); script.setNextVar("ctx", ImmutableMap.of("_source", source)); script.run(); |