|
| 1 | +package io.avaje.config; |
| 2 | + |
| 3 | +import io.avaje.config.properties.PropertiesLoader; |
| 4 | + |
| 5 | +import java.util.Properties; |
| 6 | +import java.util.function.Consumer; |
| 7 | + |
| 8 | +/** |
| 9 | + * Provides application Configuration based on loading properties and yaml files |
| 10 | + * as well as plugins that supply properties (like dynamic configuration loaded from a db). |
| 11 | + * <p> |
| 12 | + * The application can register onChange listeners to handle changes to configuration |
| 13 | + * properties at runtime. Plugins or code can dynamically load and change properties and |
| 14 | + * this can fire any registered callback handlers. |
| 15 | + * </p> |
| 16 | + */ |
| 17 | +public class Config { |
| 18 | + |
| 19 | + private static ConfigurationData data = init(); |
| 20 | + |
| 21 | + private static ConfigurationData init() { |
| 22 | + Properties properties = PropertiesLoader.load(); |
| 23 | + return new ConfigurationData(properties); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Return a required configuration value as String. |
| 28 | + * <p> |
| 29 | + * IllegalStateException is thrown if the value is not defined in configuration. |
| 30 | + * </p> |
| 31 | + * |
| 32 | + * @param key The configuration key |
| 33 | + * @return The configured value |
| 34 | + */ |
| 35 | + public static String get(String key) { |
| 36 | + return data.get(key); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Return a configuration value as String given a default value. |
| 41 | + * |
| 42 | + * @param key The configuration key |
| 43 | + * @param defaultValue The default value used |
| 44 | + * @return The configured or default value |
| 45 | + */ |
| 46 | + public static String get(String key, String defaultValue) { |
| 47 | + return data.get(key, defaultValue); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Return a required boolean configuration value. |
| 52 | + * <p> |
| 53 | + * IllegalStateException is thrown if the value is not defined in configuration. |
| 54 | + * </p> |
| 55 | + * |
| 56 | + * @param key The configuration key |
| 57 | + * @return The configured value |
| 58 | + */ |
| 59 | + public static boolean getBool(String key) { |
| 60 | + return data.getBool(key); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Return a configuration value as boolean given a default value. |
| 65 | + * |
| 66 | + * @param key The configuration key |
| 67 | + * @param defaultValue The default value used |
| 68 | + * @return The configured or default value |
| 69 | + */ |
| 70 | + public static boolean getBool(String key, boolean defaultValue) { |
| 71 | + return data.getBool(key, defaultValue); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Return a required int configuration value. |
| 76 | + * <p> |
| 77 | + * IllegalStateException is thrown if the value is not defined in configuration. |
| 78 | + * </p> |
| 79 | + * |
| 80 | + * @param key The configuration key |
| 81 | + * @return The configured value |
| 82 | + */ |
| 83 | + public static int getInt(String key) { |
| 84 | + return data.getInt(key); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Return a configuration value as int given a default value. |
| 89 | + * |
| 90 | + * @param key The configuration key |
| 91 | + * @param defaultValue The default value used |
| 92 | + * @return The configured or default value |
| 93 | + */ |
| 94 | + public static int getInt(String key, int defaultValue) { |
| 95 | + return data.getInt(key, defaultValue); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Return a required long configuration value. |
| 100 | + * <p> |
| 101 | + * IllegalStateException is thrown if the value is not defined in configuration. |
| 102 | + * </p> |
| 103 | + * |
| 104 | + * @param key The configuration key |
| 105 | + * @return The configured value |
| 106 | + */ |
| 107 | + public static long getLong(String key) { |
| 108 | + return data.getLong(key); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Return a configuration value as long given a default value. |
| 113 | + * |
| 114 | + * @param key The configuration key |
| 115 | + * @param defaultValue The default value used |
| 116 | + * @return The configured or default value |
| 117 | + */ |
| 118 | + public static long getLong(String key, long defaultValue) { |
| 119 | + return data.getLong(key, defaultValue); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Set a configuration value. |
| 124 | + * <p> |
| 125 | + * This will fire an configuration callback listeners that are registered |
| 126 | + * for this key. |
| 127 | + * </p> |
| 128 | + */ |
| 129 | + public static void setProperty(String key, String value) { |
| 130 | + data.setProperty(key, value); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Register a callback for a change to the given configuration key. |
| 135 | + * |
| 136 | + * @param key The configuration key we want to detect changes to |
| 137 | + * @param callback The callback handling to fire when the configuration changes. |
| 138 | + */ |
| 139 | + public static void onChange(String key, Consumer<String> callback) { |
| 140 | + data.onChange(key, callback); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Register a callback for a change to the given configuration key as an Int value. |
| 145 | + * |
| 146 | + * @param key The configuration key we want to detect changes to |
| 147 | + * @param callback The callback handling to fire when the configuration changes. |
| 148 | + */ |
| 149 | + public static void onChangeInt(String key, Consumer<Integer> callback) { |
| 150 | + data.onChangeInt(key, callback); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Register a callback for a change to the given configuration key as an Long value. |
| 155 | + * |
| 156 | + * @param key The configuration key we want to detect changes to |
| 157 | + * @param callback The callback handling to fire when the configuration changes. |
| 158 | + */ |
| 159 | + public static void onChangeLong(String key, Consumer<Long> callback) { |
| 160 | + data.onChangeLong(key, callback); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Register a callback for a change to the given configuration key as an Boolean value. |
| 165 | + * |
| 166 | + * @param key The configuration key we want to detect changes to |
| 167 | + * @param callback The callback handling to fire when the configuration changes. |
| 168 | + */ |
| 169 | + public static void onChangeBool(String key, Consumer<Boolean> callback) { |
| 170 | + data.onChangeBool(key, callback); |
| 171 | + } |
| 172 | +} |
0 commit comments