diff --git a/logstash-core/lib/logstash/environment.rb b/logstash-core/lib/logstash/environment.rb index d49d3d9b6fa..d1c2966f624 100644 --- a/logstash-core/lib/logstash/environment.rb +++ b/logstash-core/lib/logstash/environment.rb @@ -58,8 +58,8 @@ def self.as_java_range(r) Setting::BooleanSetting.new("pipeline.reloadable", true), Setting::BooleanSetting.new("pipeline.plugin_classloaders", false), Setting::BooleanSetting.new("pipeline.separate_logs", false), - Setting::CoercibleString.new("pipeline.ordered", "auto", true, ["auto", "true", "false"]), - Setting::CoercibleString.new("pipeline.ecs_compatibility", "v8", true, %w(disabled v1 v8)), + Setting::CoercibleStringSetting.new("pipeline.ordered", "auto", true, ["auto", "true", "false"]), + Setting::CoercibleStringSetting.new("pipeline.ecs_compatibility", "v8", true, %w(disabled v1 v8)), Setting.new("path.plugins", Array, []), Setting::NullableStringSetting.new("interactive", nil, false), Setting::BooleanSetting.new("config.debug", false), diff --git a/logstash-core/lib/logstash/settings.rb b/logstash-core/lib/logstash/settings.rb index c23455df930..b0020b89b46 100644 --- a/logstash-core/lib/logstash/settings.rb +++ b/logstash-core/lib/logstash/settings.rb @@ -499,28 +499,8 @@ def build_password_policies policies end end - - # The CoercibleString allows user to enter any value which coerces to a String. - # For example for true/false booleans; if the possible_strings are ["foo", "true", "false"] - # then these options in the config file or command line will be all valid: "foo", true, false, "true", "false" - # - class CoercibleString < Coercible - def initialize(name, default = nil, strict = true, possible_strings = [], &validator_proc) - @possible_strings = possible_strings - super(name, Object, default, strict, &validator_proc) - end - - def coerce(value) - value.to_s - end - - def validate(value) - super(value) - unless @possible_strings.empty? || @possible_strings.include?(value) - raise ArgumentError.new("Invalid value \"#{value}\". Options are: #{@possible_strings.inspect}") - end - end - end + + java_import org.logstash.settings.CoercibleStringSetting class ExistingFilePath < Setting def initialize(name, default = nil, strict = true) diff --git a/logstash-core/src/main/java/org/logstash/settings/CoercibleStringSetting.java b/logstash-core/src/main/java/org/logstash/settings/CoercibleStringSetting.java new file mode 100644 index 00000000000..25b3f42e359 --- /dev/null +++ b/logstash-core/src/main/java/org/logstash/settings/CoercibleStringSetting.java @@ -0,0 +1,66 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.logstash.settings; + +import java.util.List; + +public class CoercibleStringSetting extends Coercible { + + private final List possibleStrings; + + @SuppressWarnings("this-escape") + public CoercibleStringSetting(String name, Object defaultValue, boolean strict, List possibleStrings) { + // this super doesn't call validate, with strict false it permits to set + // possibleStrings field used in validate later. + super(name, defaultValue, false, noValidator()); + this.possibleStrings = possibleStrings; + + if (strict) { + String coercedDefault = coerce(defaultValue); + validate(coercedDefault); + this.defaultValue = coercedDefault; + } else { + this.defaultValue = defaultValue; + } + } + + @Override + public String coerce(Object value) { + if (value == null) { + return ""; + } + return value.toString(); + } + + @Override + public void validate(Object input) throws IllegalArgumentException { + super.validate(input); + + staticValidate(input.toString(), possibleStrings, this.getName()); + } + + private static void staticValidate(String input, List possibleStrings, String name) { + if (!possibleStrings.isEmpty() && !possibleStrings.contains(input)) { + throw new IllegalArgumentException(String.format("Invalid value \"%s\". Options are: %s", input, possibleStrings)); + } + } + + +}