|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.logstash.settings; |
| 20 | + |
| 21 | +import co.elastic.logstash.api.DeprecationLogger; |
| 22 | +import org.apache.logging.log4j.LogManager; |
| 23 | +import org.apache.logging.log4j.Logger; |
| 24 | +import org.logstash.RubyUtil; |
| 25 | +import org.logstash.log.DefaultDeprecationLogger; |
| 26 | +import org.logstash.util.TimeValue; |
| 27 | + |
| 28 | +import java.math.BigInteger; |
| 29 | + |
| 30 | +/** |
| 31 | + * A setting that represents a time value with units (e.g., "18m", "5s", "100ms"). |
| 32 | + * Accepts strings with time units or integer values (interpreted as nanoseconds with deprecation warning). |
| 33 | + */ |
| 34 | +public class TimeValueSetting extends Coercible<TimeValue> { |
| 35 | + |
| 36 | + private static final Logger LOGGER = LogManager.getLogger(); |
| 37 | + private static final DeprecationLogger DEPRECATION_LOGGER = new DefaultDeprecationLogger(LOGGER); |
| 38 | + |
| 39 | + public TimeValueSetting(String name, String defaultValue) { |
| 40 | + super(name, coerceStatic(name, defaultValue), true, noValidator()); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public TimeValue coerce(Object value) { |
| 45 | + return coerceStatic(getName(), value); |
| 46 | + } |
| 47 | + |
| 48 | + private static TimeValue coerceStatic(String name, Object value) { |
| 49 | + if (value instanceof Integer || value instanceof Long || value instanceof BigInteger) { |
| 50 | + DEPRECATION_LOGGER.deprecated( |
| 51 | + "Integer value for `" + name + "` does not have a time unit and will be interpreted in nanoseconds. " + |
| 52 | + "Time units will be required in a future release of Logstash. " + |
| 53 | + "Acceptable unit suffixes are: `d`, `h`, `m`, `s`, `ms`, `micros`, and `nanos`."); |
| 54 | + if (((Number) value).longValue() > Integer.MAX_VALUE) { |
| 55 | + throw RubyUtil.RUBY.newArgumentError( |
| 56 | + "Numeric value for `" + name + "` exceeds the maximum int (" + Integer.MAX_VALUE + |
| 57 | + ") supported value for nanoseconds."); |
| 58 | + } |
| 59 | + return new TimeValue(((Number) value).intValue(), "nanosecond"); |
| 60 | + } else if (value instanceof Number) { |
| 61 | + throw RubyUtil.RUBY.newArgumentError( |
| 62 | + "Non-integer numeric value for `" + name + "` is not supported without a time unit. " + |
| 63 | + "Please specify a time unit suffix such as `d`, `h`, `m`, `s`, `ms`, `micros`, or `nanos`."); |
| 64 | + } |
| 65 | + return TimeValue.fromValue(value); |
| 66 | + } |
| 67 | +} |
0 commit comments