Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Here is an example of a FileConfigProvider:

Code Block
//**
 * An implementation of {@link ConfigProvider} that simplyrepresents uses a Properties file.
 * All property keys and values are stored as cleartext.
 */
public class FileConfigProvider implements ConfigProvider {

    public final static String FILE_NAME = "filename";
void configure(Map<String, ?> configs) {
    private Properties properties;}

    /**
     * ConfigureRetrieves thisthe classdata withat the initialization parametersgiven Properties file.
     */
    public * void configure(Map<String, ?> configs) {
   @param path the file where the data resides
     String* fileName@return =the (String) configs.get(FILE_NAME);configuration data
     */
   try (FileReaderpublic fileReaderConfigData = new FileReader(fileName))get(String path) {
        Map<String, String> data  properties = new PropertiesHashMap<>();
        if (path == null || propertiespath.loadisEmpty(fileReader);
        } catch (IOException e) {
            throwreturn new ConfigException("File name " + fileName + " not found for FileConfigProvider");ConfigData(data);
        }
        }
try (Reader reader  }

    /**= reader(path)) {
     * Look up all the data (the pathProperties isproperties ignored= in this example).new Properties();
     */
    public Map<String, String> get(String path) {properties.load(reader);
        Map<String, String> data = new HashMap<>();
        Enumeration<Object> 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(data);
        }

 catch (IOException e) /**{
     * Look up the data with the given keys (the path is ignored in this example).
     */
    public Map<String, String> get(String path, Set<String> keys) {      throw new ConfigException("Could not read properties from file " + path);
        }
    }

    /**
     * Retrieves the data with the given keys at the given Properties file.
     *
     * @param path the file where the data resides
     * @param keys the keys whose values will be retrieved
     * @return the configuration data
     */
    public ConfigData get(String path, Set<String> keys) {
        Map<String, String> data = new HashMap<>();
        if (path == null || path.isEmpty()) {
            return new ConfigData(data);
        }
        try (Reader reader = reader(path)) {
            Properties properties = new Properties();
        Map<String, String> data = new HashMap<>();
 properties.load(reader);
            for (String key : keys) {
                String value = properties.getProperty(key);
                if (value != null) {
                    data.put(key, value);
                }
            }
            return new ConfigData(data);
        } catch (IOException e) {
        
    publicthrow voidnew subscribe(String path, Set<String> keys, ConfigChangeCallback callback) {
ConfigException("Could not read properties from file " + path);
        }
  throw new UnsupportedOperationException();
 }

     }
 // visible for testing
    publicprotected voidReader unsubscribereader(String path,) Set<String>throws key)IOException {
        return new throwBufferedReader(new InputStreamReader(new UnsupportedOperationExceptionFileInputStream(path), StandardCharsets.UTF_8));
    }
 
    public void close() {
    }
}

...