001package io.avaje.config; 002 003import java.util.Optional; 004import java.util.Properties; 005import java.util.function.Consumer; 006 007/** 008 * Provides application Configuration based on loading properties and yaml files 009 * as well as plugins that supply properties (like dynamic configuration loaded from a db). 010 * <p> 011 * The application can register onChange listeners to handle changes to configuration 012 * properties at runtime. Plugins or code can dynamically load and change properties and 013 * this can fire any registered callback handlers. 014 * </p> 015 */ 016public class Config { 017 018 private static Configuration data = CoreConfiguration.load(); 019 020 /** 021 * Hide constructor. 022 */ 023 private Config() { 024 } 025 026 /** 027 * Return the loaded properties as standard Properties map. 028 */ 029 public static Properties asProperties() { 030 return data.asProperties(); 031 } 032 033 /** 034 * Return the underlying configuration. 035 */ 036 public static Configuration asConfiguration() { 037 return data; 038 } 039 040 /** 041 * Put all loaded properties into System properties. 042 */ 043 public static void loadIntoSystemProperties() { 044 data.loadIntoSystemProperties(); 045 } 046 047 /** 048 * Return a required configuration value as String. 049 * <p> 050 * IllegalStateException is thrown if the value is not defined in configuration. 051 * </p> 052 * 053 * @param key The configuration key 054 * @return The configured value 055 */ 056 public static String get(String key) { 057 return data.get(key); 058 } 059 060 /** 061 * Return a configuration string value with a given default. 062 * 063 * @param key The configuration key 064 * @param defaultValue The default value used 065 * @return The configured or default value 066 */ 067 public static String get(String key, String defaultValue) { 068 return data.get(key, defaultValue); 069 } 070 071 /** 072 * Return a configuration value that might not exist. 073 * 074 * @param key The configuration key 075 * @return The configured value wrapped as optional 076 */ 077 public static Optional<String> getOptional(String key) { 078 return data.getOptional(key); 079 } 080 081 /** 082 * Return a required boolean configuration value. 083 * <p> 084 * IllegalStateException is thrown if the value is not defined in configuration. 085 * </p> 086 * 087 * @param key The configuration key 088 * @return The configured value 089 */ 090 public static boolean getBool(String key) { 091 return data.getBool(key); 092 } 093 094 /** 095 * Return a configuration value as boolean given a default value. 096 * 097 * @param key The configuration key 098 * @param defaultValue The default value used 099 * @return The configured or default value 100 */ 101 public static boolean getBool(String key, boolean defaultValue) { 102 return data.getBool(key, defaultValue); 103 } 104 105 /** 106 * Return a required int configuration value. 107 * <p> 108 * IllegalStateException is thrown if the value is not defined in configuration. 109 * </p> 110 * 111 * @param key The configuration key 112 * @return The configured value 113 */ 114 public static int getInt(String key) { 115 return data.getInt(key); 116 } 117 118 /** 119 * Return a configuration value as int given a default value. 120 * 121 * @param key The configuration key 122 * @param defaultValue The default value used 123 * @return The configured or default value 124 */ 125 public static int getInt(String key, int defaultValue) { 126 return data.getInt(key, defaultValue); 127 } 128 129 /** 130 * Return a required long configuration value. 131 * <p> 132 * IllegalStateException is thrown if the value is not defined in configuration. 133 * </p> 134 * 135 * @param key The configuration key 136 * @return The configured value 137 */ 138 public static long getLong(String key) { 139 return data.getLong(key); 140 } 141 142 /** 143 * Return a configuration value as long given a default value. 144 * 145 * @param key The configuration key 146 * @param defaultValue The default value used 147 * @return The configured or default value 148 */ 149 public static long getLong(String key, long defaultValue) { 150 return data.getLong(key, defaultValue); 151 } 152 153 /** 154 * Set a configuration value. 155 * <p> 156 * This will fire an configuration callback listeners that are registered 157 * for this key. 158 * </p> 159 */ 160 public static void setProperty(String key, String value) { 161 data.setProperty(key, value); 162 } 163 164 /** 165 * Register a callback for a change to the given configuration key. 166 * 167 * @param key The configuration key we want to detect changes to 168 * @param callback The callback handling to fire when the configuration changes. 169 */ 170 public static void onChange(String key, Consumer<String> callback) { 171 data.onChange(key, callback); 172 } 173 174 /** 175 * Register a callback for a change to the given configuration key as an Int value. 176 * 177 * @param key The configuration key we want to detect changes to 178 * @param callback The callback handling to fire when the configuration changes. 179 */ 180 public static void onChangeInt(String key, Consumer<Integer> callback) { 181 data.onChangeInt(key, callback); 182 } 183 184 /** 185 * Register a callback for a change to the given configuration key as an Long value. 186 * 187 * @param key The configuration key we want to detect changes to 188 * @param callback The callback handling to fire when the configuration changes. 189 */ 190 public static void onChangeLong(String key, Consumer<Long> callback) { 191 data.onChangeLong(key, callback); 192 } 193 194 /** 195 * Register a callback for a change to the given configuration key as an Boolean value. 196 * 197 * @param key The configuration key we want to detect changes to 198 * @param callback The callback handling to fire when the configuration changes. 199 */ 200 public static void onChangeBool(String key, Consumer<Boolean> callback) { 201 data.onChangeBool(key, callback); 202 } 203}