public ConfigDef.Type typeOf(String key) { ConfigDef.ConfigKey configKey = definition.configKeys().get(key); if (configKey == null) return null; return configKey.type; }
public void embed(final String keyPrefix, final String groupPrefix, final int startingOrd, final ConfigDef child) { int orderInGroup = startingOrd; for (ConfigKey key : child.sortedConfigs()) { define(new ConfigKey( keyPrefix + key.name, key.type, key.defaultValue, embeddedValidator(keyPrefix, key.validator), key.importance, key.documentation, groupPrefix + (key.group == null ? "" : ": " + key.group), orderInGroup++, key.width, key.displayName, embeddedDependents(keyPrefix, key.dependents), embeddedRecommender(keyPrefix, key.recommender), key.internalConfig)); } }
if (entry.getKey().startsWith(prefix) && entry.getKey().length() > prefix.length()) { String keyWithNoPrefix = entry.getKey().substring(prefix.length()); ConfigDef.ConfigKey configKey = definition.configKeys().get(keyWithNoPrefix); if (configKey != null) result.put(keyWithNoPrefix, definition.parseValue(configKey, entry.getValue(), true)); else { String keyWithNoSecondaryPrefix = keyWithNoPrefix.substring(keyWithNoPrefix.indexOf('.') + 1); configKey = definition.configKeys().get(keyWithNoSecondaryPrefix); if (configKey != null) result.put(keyWithNoPrefix, definition.parseValue(configKey, entry.getValue(), true));
private static ExpiringCredentialRefreshConfig refreshConfigThatPerformsReloginEveryGivenPercentageOfLifetime( double refreshWindowFactor, short minPeriodSeconds, short bufferSeconds, boolean clientReloginAllowedBeforeLogout) { Map<Object, Object> configs = new HashMap<>(); configs.put(SaslConfigs.SASL_LOGIN_REFRESH_WINDOW_FACTOR, refreshWindowFactor); configs.put(SaslConfigs.SASL_LOGIN_REFRESH_WINDOW_JITTER, 0); configs.put(SaslConfigs.SASL_LOGIN_REFRESH_MIN_PERIOD_SECONDS, minPeriodSeconds); configs.put(SaslConfigs.SASL_LOGIN_REFRESH_BUFFER_SECONDS, bufferSeconds); return new ExpiringCredentialRefreshConfig(new ConfigDef().withClientSaslSupport().parse(configs), clientReloginAllowedBeforeLogout); } }
/** * Define a new configuration with no special validation logic * @param name The name of the config parameter * @param type The type of the config * @param defaultValue The default value to use if this config isn't present * @param importance The importance of this config: is this something you will likely need to change. * @param documentation The documentation string for the config * @return This ConfigDef so you can chain calls */ public ConfigDef define(String name, Type type, Object defaultValue, Importance importance, String documentation) { return define(name, type, defaultValue, null, importance, documentation); }
@Test public void testValidateCannotParse() { Map<String, ConfigValue> expected = new HashMap<>(); String errorMessageB = "Invalid value non_integer for configuration a: Not a number of type INT"; ConfigValue configA = new ConfigValue("a", null, Collections.emptyList(), Arrays.asList(errorMessageB)); expected.put("a", configA); ConfigDef def = new ConfigDef().define("a", Type.INT, Importance.HIGH, "docs"); Map<String, String> props = new HashMap<>(); props.put("a", "non_integer"); List<ConfigValue> configs = def.validate(props); for (ConfigValue config: configs) { String name = config.name(); ConfigValue expectedConfig = expected.get(name); assertEquals(expectedConfig, config); } }
@Test public void testDynamicUpdateModeInDocs() throws Exception { final ConfigDef configDef = new ConfigDef() .define("my.broker.config", Type.LONG, Importance.HIGH, "docs") .define("my.cluster.config", Type.LONG, Importance.HIGH, "docs") .define("my.readonly.config", Type.LONG, Importance.HIGH, "docs"); final Map<String, String> updateModes = new HashMap<>(); updateModes.put("my.broker.config", "per-broker"); updateModes.put("my.cluster.config", "cluster-wide"); final String html = configDef.toHtmlTable(updateModes); Set<String> configsInHtml = new HashSet<>(); for (String line : html.split("\n")) { if (line.contains("my.broker.config")) { assertTrue(line.contains("per-broker")); configsInHtml.add("my.broker.config"); } else if (line.contains("my.cluster.config")) { assertTrue(line.contains("cluster-wide")); configsInHtml.add("my.cluster.config"); } else if (line.contains("my.readonly.config")) { assertTrue(line.contains("read-only")); configsInHtml.add("my.readonly.config"); } } assertEquals(configDef.names(), configsInHtml); }
expected.put("d", configD); ConfigDef def = new ConfigDef() .define("a", Type.INT, Importance.HIGH, "docs", "group", 1, Width.SHORT, "a", Arrays.asList("b", "c"), new IntegerRecommender(false)) .define("b", Type.INT, Importance.HIGH, "docs", "group", 2, Width.SHORT, "b", new IntegerRecommender(true)) .define("c", Type.INT, Importance.HIGH, "docs", "group", 3, Width.SHORT, "c", new IntegerRecommender(true)) .define("d", Type.INT, Importance.HIGH, "docs", "group", 4, Width.SHORT, "d", Arrays.asList("b"), new IntegerRecommender(false)); for (String name : def.configKeys().keySet()) { configValues.put(name, new ConfigValue(name)); Map<String, Object> parsed = def.parseForValidate(props, configValues);
@Test public void testNames() { final ConfigDef configDef = new ConfigDef() .define("a", Type.STRING, Importance.LOW, "docs") .define("b", Type.STRING, Importance.LOW, "docs"); Set<String> names = configDef.names(); assertEquals(new HashSet<>(Arrays.asList("a", "b")), names); // should be unmodifiable try { names.add("new"); fail(); } catch (UnsupportedOperationException e) { // expected } }
@Test public void testBaseConfigDefDependents() { // Creating a ConfigDef based on another should compute the correct number of configs with no parent, even // if the base ConfigDef has already computed its parentless configs final ConfigDef baseConfigDef = new ConfigDef().define("a", Type.STRING, Importance.LOW, "docs"); assertEquals(new HashSet<>(Arrays.asList("a")), baseConfigDef.getConfigsWithNoParent()); final ConfigDef configDef = new ConfigDef(baseConfigDef) .define("parent", Type.STRING, Importance.HIGH, "parent docs", "group", 1, Width.LONG, "Parent", Collections.singletonList("child")) .define("child", Type.STRING, Importance.HIGH, "docs"); assertEquals(new HashSet<>(Arrays.asList("a", "parent")), configDef.getConfigsWithNoParent()); }
@Test public void testGroupInference() { List<String> expected1 = Arrays.asList("group1", "group2"); ConfigDef def1 = new ConfigDef() .define("a", Type.INT, Importance.HIGH, "docs", "group1", 1, Width.SHORT, "a") .define("b", Type.INT, Importance.HIGH, "docs", "group2", 1, Width.SHORT, "b") .define("c", Type.INT, Importance.HIGH, "docs", "group1", 2, Width.SHORT, "c"); assertEquals(expected1, def1.groups()); List<String> expected2 = Arrays.asList("group2", "group1"); ConfigDef def2 = new ConfigDef() .define("a", Type.INT, Importance.HIGH, "docs", "group2", 1, Width.SHORT, "a") .define("b", Type.INT, Importance.HIGH, "docs", "group2", 2, Width.SHORT, "b") .define("c", Type.INT, Importance.HIGH, "docs", "group1", 2, Width.SHORT, "c"); assertEquals(expected2, def2.groups()); }
private static ConfigDef getConfig() { Map<String, ConfigDef.ConfigKey> everything = new HashMap<>(conf().configKeys()); ConfigDef visible = new ConfigDef(); for (ConfigDef.ConfigKey key : everything.values()) { visible.define(key); } return visible; }
@Test public void testInternalConfigDoesntShowUpInDocs() throws Exception { final String name = "my.config"; final ConfigDef configDef = new ConfigDef().defineInternal(name, Type.STRING, "", Importance.LOW); assertFalse(configDef.toHtmlTable().contains("my.config")); assertFalse(configDef.toEnrichedRst().contains("my.config")); assertFalse(configDef.toRst().contains("my.config")); }
private void validateFieldDef(Field expected) { ConfigDef configDef = connector.config(); assertThat(configDef.names()).contains(expected.name()); ConfigDef.ConfigKey key = configDef.configKeys().get(expected.name()); assertThat(key).isNotNull(); assertThat(key.name).isEqualTo(expected.name()); assertThat(key.displayName).isEqualTo(expected.displayName()); assertThat(key.importance).isEqualTo(expected.importance()); assertThat(key.documentation).isEqualTo(expected.description()); assertThat(key.type).isEqualTo(expected.type()); assertThat(key.defaultValue).isEqualTo(expected.defaultValue()); assertThat(key.dependents).isEqualTo(expected.dependents()); assertThat(key.width).isNotNull(); assertThat(key.group).isNotNull(); assertThat(key.orderInGroup).isGreaterThan(0); assertThat(key.validator).isNull(); assertThat(key.recommender).isNull(); } }
@Test public void testCanAddInternalConfig() throws Exception { final String configName = "internal.config"; final ConfigDef configDef = new ConfigDef().defineInternal(configName, Type.STRING, "", Importance.LOW); final HashMap<String, String> properties = new HashMap<>(); properties.put(configName, "value"); final List<ConfigValue> results = configDef.validate(properties); final ConfigValue configValue = results.get(0); assertEquals("value", configValue.value()); assertEquals(configName, configValue.name()); }
@Test public void testSslPasswords() { ConfigDef def = new ConfigDef(); SslConfigs.addClientSslSupport(def); Properties props = new Properties(); props.put(SslConfigs.SSL_KEY_PASSWORD_CONFIG, "key_password"); props.put(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, "keystore_password"); props.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, "truststore_password"); Map<String, Object> vals = def.parse(props); assertEquals(new Password("key_password"), vals.get(SslConfigs.SSL_KEY_PASSWORD_CONFIG)); assertEquals(Password.HIDDEN, vals.get(SslConfigs.SSL_KEY_PASSWORD_CONFIG).toString()); assertEquals(new Password("keystore_password"), vals.get(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG)); assertEquals(Password.HIDDEN, vals.get(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG).toString()); assertEquals(new Password("truststore_password"), vals.get(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG)); assertEquals(Password.HIDDEN, vals.get(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG).toString()); }
protected static ConfigDef configDef() { ConfigDef config = new ConfigDef(); Field.group(config, "MongoDB", HOSTS, USER, PASSWORD, LOGICAL_NAME, CONNECT_BACKOFF_INITIAL_DELAY_MS, CONNECT_BACKOFF_MAX_DELAY_MS, MAX_FAILED_CONNECTIONS, AUTO_DISCOVER_MEMBERS, SSL_ENABLED, SSL_ALLOW_INVALID_HOSTNAMES); Field.group(config, "Events", DATABASE_WHITELIST, DATABASE_BLACKLIST, COLLECTION_WHITELIST, COLLECTION_BLACKLIST, FIELD_BLACKLIST, FIELD_RENAMES, CommonConnectorConfig.TOMBSTONES_ON_DELETE); Field.group(config, "Connector", MAX_COPY_THREADS, CommonConnectorConfig.MAX_QUEUE_SIZE, CommonConnectorConfig.MAX_BATCH_SIZE, CommonConnectorConfig.POLL_INTERVAL_MS, CommonConnectorConfig.SNAPSHOT_DELAY_MS); return config; }