Search in sources :

Example 6 with ConfigData

use of org.apache.kafka.common.config.ConfigData in project kafka by apache.

the class DirectoryConfigProviderTest method testGetSetOfKeysAtPath.

@Test
public void testGetSetOfKeysAtPath() {
    Set<String> keys = toSet(asList(foo.getName(), "baz"));
    ConfigData configData = provider.get(dir.getAbsolutePath(), keys);
    assertEquals(Collections.singleton(foo.getName()), configData.data().keySet());
    assertEquals("FOO", configData.data().get(foo.getName()));
    assertNull(configData.ttl());
}
Also used : ConfigData(org.apache.kafka.common.config.ConfigData) Test(org.junit.jupiter.api.Test)

Example 7 with ConfigData

use of org.apache.kafka.common.config.ConfigData in project kafka by apache.

the class FileConfigProvider method get.

/**
 * 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();
        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) {
        log.error("Could not read properties from file {}", path, e);
        throw new ConfigException("Could not read properties from file " + path);
    }
}
Also used : HashMap(java.util.HashMap) ConfigData(org.apache.kafka.common.config.ConfigData) Reader(java.io.Reader) ConfigException(org.apache.kafka.common.config.ConfigException) IOException(java.io.IOException) Properties(java.util.Properties)

Example 8 with ConfigData

use of org.apache.kafka.common.config.ConfigData in project kafka by apache.

the class FileConfigProvider method get.

/**
 * Retrieves the data at the given Properties file.
 *
 * @param path the file where the data resides
 * @return the configuration data
 */
public ConfigData get(String path) {
    Map<String, String> data = new HashMap<>();
    if (path == null || path.isEmpty()) {
        return new ConfigData(data);
    }
    try (Reader reader = reader(path)) {
        Properties properties = new Properties();
        properties.load(reader);
        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) {
        log.error("Could not read properties from file {}", path, e);
        throw new ConfigException("Could not read properties from file " + path);
    }
}
Also used : HashMap(java.util.HashMap) ConfigData(org.apache.kafka.common.config.ConfigData) Reader(java.io.Reader) ConfigException(org.apache.kafka.common.config.ConfigException) IOException(java.io.IOException) Properties(java.util.Properties)

Example 9 with ConfigData

use of org.apache.kafka.common.config.ConfigData in project kafka by apache.

the class DirectoryConfigProviderTest method testEmptyPathWithKey.

@Test
public void testEmptyPathWithKey() {
    ConfigData configData = provider.get("", Collections.singleton("foo"));
    assertTrue(configData.data().isEmpty());
    assertNull(configData.ttl());
}
Also used : ConfigData(org.apache.kafka.common.config.ConfigData) Test(org.junit.jupiter.api.Test)

Example 10 with ConfigData

use of org.apache.kafka.common.config.ConfigData in project kafka by apache.

the class DirectoryConfigProviderTest method testNullPath.

@Test
public void testNullPath() {
    ConfigData configData = provider.get(null);
    assertTrue(configData.data().isEmpty());
    assertNull(configData.ttl());
}
Also used : ConfigData(org.apache.kafka.common.config.ConfigData) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigData (org.apache.kafka.common.config.ConfigData)18 Test (org.junit.jupiter.api.Test)14 HashMap (java.util.HashMap)4 IOException (java.io.IOException)2 Reader (java.io.Reader)2 HashSet (java.util.HashSet)2 Properties (java.util.Properties)2 ConfigException (org.apache.kafka.common.config.ConfigException)2 Test (org.junit.Test)2