*/
public static final String ENABLE_STOP_COMMAND = "sonar.enableStopCommand";
- // Constants declared by the ES plugin ListUpdate (see sonar-search)
- // that are used by sonar-server
- public static final String ES_PLUGIN_LISTUPDATE_SCRIPT_NAME = "listUpdate";
public static final String ES_PLUGIN_LISTUPDATE_ID_FIELD = "idField";
public static final String ES_PLUGIN_LISTUPDATE_ID_VALUE = "idValue";
public static final String ES_PLUGIN_LISTUPDATE_FIELD = "field";
*/
package org.sonar.search;
+import java.io.File;
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.TreeSet;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.sonar.process.MessageException;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
-import org.sonar.search.script.ListUpdate;
-
-import java.io.File;
-import java.util.Arrays;
-import java.util.LinkedHashSet;
-import java.util.Set;
-import java.util.TreeSet;
class SearchSettings {
ImmutableSettings.Builder builder = ImmutableSettings.settingsBuilder();
configureFileSystem(builder);
configureIndexDefaults(builder);
- configurePlugins(builder);
configureNetwork(builder);
configureCluster(builder);
configureMarvel(builder);
builder.put("path.logs", logDir.getAbsolutePath());
}
- private void configurePlugins(ImmutableSettings.Builder builder) {
- builder
- .put("script.default_lang", "native")
- .put(String.format("script.native.%s.type", ProcessProperties.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME),
- ListUpdate.UpdateListScriptFactory.class.getName());
- }
-
private void configureNetwork(ImmutableSettings.Builder builder) {
// the following properties can't be null as default values are defined by app process
String host = props.nonNullValue(ProcessProperties.SEARCH_HOST);
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.search.script;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import org.elasticsearch.common.Nullable;
-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.ProcessProperties;
-
-public class ListUpdate extends AbstractExecutableScript {
-
- public static class UpdateListScriptFactory implements NativeScriptFactory {
- @Override
- public ExecutableScript newScript(@Nullable Map<String, Object> params) {
- String idField = XContentMapValues.nodeStringValue(params.get(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD), null);
- String idValue = XContentMapValues.nodeStringValue(params.get(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE), null);
- String field = XContentMapValues.nodeStringValue(params.get(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD), null);
- Map value = null;
- if (idField == null) {
- throw new IllegalStateException(String.format("Missing '%s' parameter", ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD));
- }
- if (idValue == null) {
- throw new IllegalStateException(String.format("Missing '%s' parameter", ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE));
- }
- if (field == null) {
- throw new IllegalStateException(String.format("Missing '%s' parameter", ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD));
- }
-
- // NULL case is deletion of nested item
- if (params.containsKey(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE)) {
- Object obj = params.get(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE);
- if (obj != null) {
- value = XContentMapValues.nodeMapValue(params.get(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE), "Update item");
- }
- }
-
- return new ListUpdate(idField, idValue, field, value);
- }
- }
-
- private final String idField;
- 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) {
- this.idField = idField;
- this.idValue = idValue;
- this.field = field;
- this.value = value;
- }
-
- @Override
- public void setNextVar(String name, Object value) {
- if ("ctx".equals(name)) {
- ctx = (Map<String, Object>) value;
- }
- }
-
- @Override
- public Object unwrap(Object value) {
- return value;
- }
-
- @Override
- public Object run() {
- try {
- // Get the Document's source from ctx
- Map<String, Object> source = XContentMapValues.nodeMapValue(ctx.get("_source"), "source from context");
-
- // Get the Object for list update
- Object fieldValue = source.get(field);
-
- if (fieldValue == null && value != null) {
- // 0. The field does not exist (this is a upsert then)
- List values = new ArrayList<>(1);
- values.add(value);
- source.put(field, values);
- } else if (!XContentMapValues.isArray(fieldValue) && value != null) {
- // 1. The field is not yet a list
- Map currentFieldValue = XContentMapValues.nodeMapValue(fieldValue, "current FieldValue");
- if (XContentMapValues.nodeStringValue(currentFieldValue.get(idField), null).equals(idValue)) {
- source.put(field, value);
- } else {
- List values = new ArrayList<>(2);
- values.add(fieldValue);
- values.add(value);
- source.put(field, values);
- }
- } else {
- // 3. field is a list
- Collection items = (Collection) fieldValue;
- Object target = null;
- for (Object item : items) {
- Map<String, Object> fields = (Map<String, Object>) item;
- String itemIdValue = XContentMapValues.nodeStringValue(fields.get(idField), null);
- if (itemIdValue != null && itemIdValue.equals(idValue)) {
- target = item;
- break;
- }
- }
- if (target != null) {
- items.remove(target);
- }
-
- // Supporting the update by NULL = deletion case
- if (value != null) {
- items.add(value);
- }
- source.put(field, items);
- }
- } catch (Exception e) {
- throw new IllegalStateException("failed to execute listUpdate script", e);
- }
- return null;
-
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@ParametersAreNonnullByDefault
-package org.sonar.search.script;
-
-import javax.annotation.ParametersAreNonnullByDefault;
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.search.script;
-
-import com.google.common.collect.ImmutableMap;
-import org.elasticsearch.script.ExecutableScript;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.process.ProcessProperties;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
-
-public class UpdateListScriptTest {
-
- ListUpdate.UpdateListScriptFactory factory;
-
- @Before
- public void setUp() {
- factory = new ListUpdate.UpdateListScriptFactory();
- }
-
- @Test
- public void fail_missing_attributes_field() {
- Map<String, Object> params = new HashMap<>();
-
- // Missing everything
- try {
- factory.newScript(params);
- fail();
- } catch (Exception e) {
- assertThat(e.getMessage()).isEqualTo("Missing 'idField' parameter");
- }
-
- // Missing ID_VALUE
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "test");
- try {
- factory.newScript(params);
- fail();
- } catch (Exception e) {
- assertThat(e.getMessage()).isEqualTo("Missing 'idValue' parameter");
- }
-
- // Missing FIELD
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "test");
- try {
- factory.newScript(params);
- fail();
- } catch (Exception e) {
- assertThat(e.getMessage()).isEqualTo("Missing 'field' parameter");
- }
-
- // Has all required attributes and Null Value
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, "test");
- ExecutableScript script = factory.newScript(params);
- assertThat(script).isNotNull();
-
- // Has all required attributes and VALUE of wrong type
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, new Integer(52));
- try {
- factory.newScript(params);
- fail();
- } catch (Exception e) {
-
- }
-
- // Has all required attributes and Proper VALUE
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, ImmutableMap.of("key", "value"));
- script = factory.newScript(params);
- assertThat(script).isNotNull();
-
- // Missing ID_FIELD
- params.remove(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD);
- try {
- factory.newScript(params);
- fail();
- } catch (Exception e) {
- assertThat(e.getMessage()).isEqualTo("Missing 'idField' parameter");
- }
- }
-
- @Test
- public void update_list() {
-
- String listField = "listField";
- Collection<Map<String, Object>> mapFields;
- Map source = new HashMap<>();
- source.put("field1", "value1");
-
- // 0 Create list when field does not exists
- Map<String, Object> params = new HashMap<>();
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, listField);
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key");
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "1");
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "1", "value", "A"));
-
- ExecutableScript script = factory.newScript(params);
- script.setNextVar("ctx", ImmutableMap.of("_source", source));
- script.run();
-
- mapFields = (Collection) source.get(listField);
- System.out.println("source = " + source);
- assertThat(mapFields).hasSize(1);
-
- // Add item to existing list
- params = new HashMap<>();
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, listField);
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key");
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "2");
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "2", "value", "B"));
- script = factory.newScript(params);
- script.setNextVar("ctx", ImmutableMap.of("_source", source));
- script.run();
- mapFields = (Collection) source.get(listField);
- assertThat(mapFields).hasSize(2);
-
- // updated first item in list
- params = new HashMap<>();
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, listField);
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key");
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "1");
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "1", "value", "a"));
- script = factory.newScript(params);
- script.setNextVar("ctx", ImmutableMap.of("_source", source));
- script.run();
- mapFields = (Collection) source.get(listField);
- assertThat(mapFields).hasSize(2);
-
- // updated second item in list
- params = new HashMap<>();
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, listField);
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key");
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "2");
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "2", "value", "b"));
- script = factory.newScript(params);
- script.setNextVar("ctx", ImmutableMap.of("_source", source));
- script.run();
- mapFields = (Collection) source.get(listField);
- assertThat(mapFields).hasSize(2);
-
- // delete first item
- params = new HashMap<>();
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, listField);
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key");
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "1");
- params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, null);
- script = factory.newScript(params);
- script.setNextVar("ctx", ImmutableMap.of("_source", source));
- script.run();
- mapFields = (Collection) source.get(listField);
- assertThat(mapFields).hasSize(1);
- }
-
- private Map<String, Object> mapOf(String k, String v, String k1, String v1) {
- Map<String, Object> map = new HashMap<>();
- map.put(k, v);
- map.put(k1, v1);
- return map;
- }
-}