...
Code Block | ||
---|---|---|
| ||
public interface ConfigProvider extends Configurable, Closeable { // Configure this class with the initialization parameters void configure(Map<String, ?> configs); // LookupLook up the data at the given path. Map<String, String> get(String path); // LookupLook up the data with the given keys at the given path. Map<String, String> get(String path, Set<String> keys); // The ConfigProvider is responsible for making this callback whenever the key changes. // Some ConfigProviders may want to have a background thread with a configurable update interval. void subscribe(String path, Set<String> keys, ConfigChangeCallback callback); // Inverse of subscribe void unsubscribe(String path, Set<String> key); // Close all subscriptions and clean up all resources void close(); } public interface ConfigChangeCallback { // A delayMs of 0 indicates an immediate change; a positive delayMs indicates // that a future change is anticipated (such as a lease duration) void onChange(String path, Map<String, String> values, int delayMs); } |
...
Code Block |
---|
//** * An implementation of {@link ConfigProvider} that simply uses a Properties file. */ public class FileConfigProvider implements ConfigProvider { public final static String FILE_NAME = "filename"; private Properties properties; /** * Configure this class with the initialization parameters */ public void configure(Map<String, ?> configs) { String fileName = (String) configs.get(FILE_NAME); try (FileReader fileReader = new FileReader(fileName)) { properties = new Properties(); properties.load(fileReader); } catch (IOException e) { throw new ConfigException("File name " + fileName + " not found for FileConfigProvider"); } } /** * LookupLook up all the data at the given path(the path is ignored in this example). */ public Map<String, String> get(String path) { Map<String, String> data = new HashMap<>(); Enumeration<Object> keys = properties.keys(); while (keys.hasMoreElements()) { String key = keys.nextElement().toString(); String value = properties.getProperty(key); if (value != null) { data.put(key, value); } } return new data; } /** * LookupLook up the data with the given keys at the given path(the path is ignored in this example). */ public Map<String, String> get(String path, Set<String> keys) { Map<String, String> data = new HashMap<>(); for (String key : keys) { String value = properties.getProperty(key); if (value != null) { data.put(key, value); } } return data; } public void subscribe(String path, Set<String> keys, ConfigChangeCallback callback) { throw new UnsupportedOperationException(); } public void unsubscribe(String path, Set<String> key) { throw new UnsupportedOperationException(); } public void close() { } } |
...