...
Code Block |
---|
//**
* An implementation of {@link ConfigProvider} that simply uses a Properties file.
*/
public class FileConfigProvider implements ConfigProvider {
private static final Logger log = LoggerFactory.getLogger(FileConfigProvider.class);
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");
}
}
/**
* Lookup up the data at the given path.
*/
public ConfigData get(ConfigContext ctx, 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 ConfigData(Collections.<String, String>emptyMap(), data);
}
/**
* Lookup up the data with the given keys at the given path.
*/
public ConfigData get(ConfigContext ctx, 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 new ConfigData(Collections.<String, String>emptyMap(), data);
}
// 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.
public void subscribe(String key, ConfigurationChangeCallback callback) {
throw new UnsupportedOperationException();
}
// Inverse of subscribe
public void unsubscribe(String key) {
throw new UnsupportedOperationException();
}
public void close() {
}
}
|
...