|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.sdk.autoconfigure.spi.internal; |
| 7 | + |
| 8 | +import static io.opentelemetry.api.internal.ConfigUtil.defaultIfNull; |
| 9 | + |
| 10 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 11 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Set; |
| 14 | +import javax.annotation.Nullable; |
| 15 | + |
| 16 | +/** |
| 17 | + * An interface for accessing structured configuration data. |
| 18 | + * |
| 19 | + * <p>An instance of {@link StructuredConfigProperties} is equivalent to a <a |
| 20 | + * href="https://yaml.org/spec/1.2.2/#3211-nodes">YAML mapping node</a>. It has accessors for |
| 21 | + * reading scalar properties, {@link #getStructured(String)} for reading children which are |
| 22 | + * themselves mappings, and {@link #getStructuredList(String)} for reading children which are |
| 23 | + * sequences of mappings. |
| 24 | + */ |
| 25 | +public interface StructuredConfigProperties { |
| 26 | + |
| 27 | + /** |
| 28 | + * Returns a {@link String} configuration property. |
| 29 | + * |
| 30 | + * @return null if the property has not been configured |
| 31 | + * @throws ConfigurationException if the property is not a valid scalar string |
| 32 | + */ |
| 33 | + @Nullable |
| 34 | + String getString(String name); |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns a {@link String} configuration property. |
| 38 | + * |
| 39 | + * @return a {@link String} configuration property or {@code defaultValue} if a property with |
| 40 | + * {@code name} has not been configured |
| 41 | + * @throws ConfigurationException if the property is not a valid scalar string |
| 42 | + */ |
| 43 | + default String getString(String name, String defaultValue) { |
| 44 | + return defaultIfNull(getString(name), defaultValue); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns a {@link Boolean} configuration property. Implementations should use the same rules as |
| 49 | + * {@link Boolean#parseBoolean(String)} for handling the values. |
| 50 | + * |
| 51 | + * @return null if the property has not been configured |
| 52 | + * @throws ConfigurationException if the property is not a valid scalar boolean |
| 53 | + */ |
| 54 | + @Nullable |
| 55 | + Boolean getBoolean(String name); |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns a {@link Boolean} configuration property. |
| 59 | + * |
| 60 | + * @return a {@link Boolean} configuration property or {@code defaultValue} if a property with |
| 61 | + * {@code name} has not been configured |
| 62 | + * @throws ConfigurationException if the property is not a valid scalar boolean |
| 63 | + */ |
| 64 | + default boolean getBoolean(String name, boolean defaultValue) { |
| 65 | + return defaultIfNull(getBoolean(name), defaultValue); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns a {@link Integer} configuration property. |
| 70 | + * |
| 71 | + * <p>If the underlying config property is {@link Long}, it is converted to {@link Integer} with |
| 72 | + * {@link Long#intValue()} which may result in loss of precision. |
| 73 | + * |
| 74 | + * @return null if the property has not been configured |
| 75 | + * @throws ConfigurationException if the property is not a valid scalar integer |
| 76 | + */ |
| 77 | + @Nullable |
| 78 | + Integer getInt(String name); |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns a {@link Integer} configuration property. |
| 82 | + * |
| 83 | + * <p>If the underlying config property is {@link Long}, it is converted to {@link Integer} with |
| 84 | + * {@link Long#intValue()} which may result in loss of precision. |
| 85 | + * |
| 86 | + * @return a {@link Integer} configuration property or {@code defaultValue} if a property with |
| 87 | + * {@code name} has not been configured |
| 88 | + * @throws ConfigurationException if the property is not a valid scalar integer |
| 89 | + */ |
| 90 | + default int getInt(String name, int defaultValue) { |
| 91 | + return defaultIfNull(getInt(name), defaultValue); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns a {@link Long} configuration property. |
| 96 | + * |
| 97 | + * @return null if the property has not been configured |
| 98 | + * @throws ConfigurationException if the property is not a valid scalar long |
| 99 | + */ |
| 100 | + @Nullable |
| 101 | + Long getLong(String name); |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns a {@link Long} configuration property. |
| 105 | + * |
| 106 | + * @return a {@link Long} configuration property or {@code defaultValue} if a property with {@code |
| 107 | + * name} has not been configured |
| 108 | + * @throws ConfigurationException if the property is not a valid scalar long |
| 109 | + */ |
| 110 | + default long getLong(String name, long defaultValue) { |
| 111 | + return defaultIfNull(getLong(name), defaultValue); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns a {@link Double} configuration property. |
| 116 | + * |
| 117 | + * @return null if the property has not been configured |
| 118 | + * @throws ConfigurationException if the property is not a valid scalar double |
| 119 | + */ |
| 120 | + @Nullable |
| 121 | + Double getDouble(String name); |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns a {@link Double} configuration property. |
| 125 | + * |
| 126 | + * @return a {@link Double} configuration property or {@code defaultValue} if a property with |
| 127 | + * {@code name} has not been configured |
| 128 | + * @throws ConfigurationException if the property is not a valid scalar double |
| 129 | + */ |
| 130 | + default double getDouble(String name, double defaultValue) { |
| 131 | + return defaultIfNull(getDouble(name), defaultValue); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns a {@link List} configuration property. Empty values and values which do not map to the |
| 136 | + * {@code scalarType} will be removed. |
| 137 | + * |
| 138 | + * @param name the property name |
| 139 | + * @param scalarType the scalar type, one of {@link String}, {@link Boolean}, {@link Long} or |
| 140 | + * {@link Double} |
| 141 | + * @return a {@link List} configuration property, or null if the property has not been configured |
| 142 | + * @throws ConfigurationException if the property is not a valid sequence of scalars, or if {@code |
| 143 | + * scalarType} is not supported |
| 144 | + */ |
| 145 | + @Nullable |
| 146 | + <T> List<T> getScalarList(String name, Class<T> scalarType); |
| 147 | + |
| 148 | + /** |
| 149 | + * Returns a {@link List} configuration property. Entries which are not strings are converted to |
| 150 | + * their string representation. |
| 151 | + * |
| 152 | + * @see ConfigProperties#getList(String name) |
| 153 | + * @return a {@link List} configuration property or {@code defaultValue} if a property with {@code |
| 154 | + * name} has not been configured |
| 155 | + * @throws ConfigurationException if the property is not a valid sequence of scalars |
| 156 | + */ |
| 157 | + default <T> List<T> getScalarList(String name, Class<T> scalarType, List<T> defaultValue) { |
| 158 | + return defaultIfNull(getScalarList(name, scalarType), defaultValue); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Returns a {@link StructuredConfigProperties} configuration property. |
| 163 | + * |
| 164 | + * @return a map-valued configuration property, or {@code null} if {@code name} has not been |
| 165 | + * configured |
| 166 | + * @throws ConfigurationException if the property is not a mapping |
| 167 | + */ |
| 168 | + @Nullable |
| 169 | + StructuredConfigProperties getStructured(String name); |
| 170 | + |
| 171 | + /** |
| 172 | + * Returns a list of {@link StructuredConfigProperties} configuration property. |
| 173 | + * |
| 174 | + * @return a list of map-valued configuration property, or {@code null} if {@code name} has not |
| 175 | + * been configured |
| 176 | + * @throws ConfigurationException if the property is not a sequence of mappings |
| 177 | + */ |
| 178 | + @Nullable |
| 179 | + List<StructuredConfigProperties> getStructuredList(String name); |
| 180 | + |
| 181 | + /** |
| 182 | + * Returns a set of all configuration property keys. |
| 183 | + * |
| 184 | + * @return the configuration property keys |
| 185 | + */ |
| 186 | + Set<String> getPropertyKeys(); |
| 187 | +} |
0 commit comments