|
| 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 | + |
| 20 | +package org.logstash.settings; |
| 21 | + |
| 22 | +import org.jruby.RubyInteger; |
| 23 | +import org.jruby.RubyRange; |
| 24 | +import org.logstash.RubyUtil; |
| 25 | + |
| 26 | +// Ideally would be a Coercible<Range<Integer>>, but given the fact that |
| 27 | +// values can be effectively coerced into the constructor, it needs instances |
| 28 | +// of Objects to represent Integer, String, Long to be later coerced into Range<Integer>. |
| 29 | +@SuppressWarnings({"rawtypes", "unchecked"}) |
| 30 | +public class PortRangeSetting extends Coercible<Object> { |
| 31 | + |
| 32 | + private static final Range<Integer> VALID_PORT_RANGE = new Range<>(1, 65535); |
| 33 | + public static final String PORT_SEPARATOR = "-"; |
| 34 | + |
| 35 | + public PortRangeSetting(String name, Object defaultValue) { |
| 36 | + super(name, defaultValue, true, PortRangeSetting::isValid); |
| 37 | + } |
| 38 | + |
| 39 | + public static boolean isValid(Object range) { |
| 40 | + if (!(range instanceof Range)) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + return VALID_PORT_RANGE.contains((Range<Integer>) range); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Range<Integer> coerce(Object obj) { |
| 49 | + if (obj instanceof Range) { |
| 50 | + return (Range) obj; |
| 51 | + } |
| 52 | + |
| 53 | + if (obj instanceof Integer) { |
| 54 | + Integer val = (Integer) obj; |
| 55 | + return new Range<>(val, val); |
| 56 | + } |
| 57 | + |
| 58 | + if (obj instanceof Long) { |
| 59 | + Long val = (Long) obj; |
| 60 | + return new Range<>(val.intValue(), val.intValue()); |
| 61 | + } |
| 62 | + |
| 63 | + if (obj instanceof String) { |
| 64 | + String val = ((String) obj).trim(); |
| 65 | + String[] parts = val.split(PORT_SEPARATOR); |
| 66 | + String firstStr = parts[0]; |
| 67 | + String lastStr; |
| 68 | + if (parts.length == 1) { |
| 69 | + lastStr = firstStr; |
| 70 | + } else { |
| 71 | + lastStr = parts[1]; |
| 72 | + } |
| 73 | + try { |
| 74 | + int first = Integer.parseInt(firstStr); |
| 75 | + int last = Integer.parseInt(lastStr); |
| 76 | + return new Range<>(first, last); |
| 77 | + } catch(NumberFormatException e) { |
| 78 | + throw new IllegalArgumentException("Could not coerce [" + obj + "](type: " + obj.getClass() + ") into a port range"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (obj instanceof RubyRange) { |
| 83 | + RubyRange rubyRange = (RubyRange) obj; |
| 84 | + RubyInteger begin = rubyRange.begin(RubyUtil.RUBY.getCurrentContext()).convertToInteger(); |
| 85 | + RubyInteger end = rubyRange.end(RubyUtil.RUBY.getCurrentContext()).convertToInteger(); |
| 86 | + return new Range<>(begin.getIntValue(), end.getIntValue()); |
| 87 | + } |
| 88 | + throw new IllegalArgumentException("Could not coerce [" + obj + "](type: " + obj.getClass() + ") into a port range"); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void validate(Object value) throws IllegalArgumentException { |
| 93 | + if (!isValid(value)) { |
| 94 | + final String msg = String.format("Invalid value \"%s: %s\", valid options are within the range of %d-%d", |
| 95 | + getName(), value, VALID_PORT_RANGE.getFirst(), VALID_PORT_RANGE.getLast()); |
| 96 | + |
| 97 | + throw new IllegalArgumentException(msg); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments