@Override public void ensureValid(String name, Object value) { if (value == null) { // Pass in the string null to avoid the spotbugs warning throw new ConfigException(name, "null", "entry must be non null"); } }
value = parseType(key.name, props.get(key.name), key.type); } catch (ConfigException e) { config.addErrorMessage(e.getMessage()); key.validator.ensureValid(key.name, value); } catch (ConfigException e) { config.addErrorMessage(e.getMessage());
static URI uri(String key, String value) { try { return new URI(value); } catch (URISyntaxException e) { ConfigException configException = new ConfigException( key, value, "Could not parse to URI." ); configException.initCause(e); throw configException; } }
static void validate(String config, String value) { try { Charset.forName(value); } catch (Exception ex) { ConfigException configException = new ConfigException( config, value, "Charset is invalid." ); configException.initCause(ex); throw configException; } }
public void ensureValid(String name, Object o) { if (o == null) throw new ConfigException(name, null, "Value must be non-null"); Number n = (Number) o; if (min != null && n.doubleValue() < min.doubleValue()) throw new ConfigException(name, o, "Value must be at least " + min); if (max != null && n.doubleValue() > max.doubleValue()) throw new ConfigException(name, o, "Value must be no more than " + max); }
static URL url(String key, String value) { try { return new URL(value); } catch (MalformedURLException e) { ConfigException configException = new ConfigException( key, value, "Could not parse to URL." ); configException.initCause(e); throw configException; } }
private void validate(String name, Map<String, Object> parsed, Map<String, ConfigValue> configs) { if (!configKeys.containsKey(name)) { return; } ConfigKey key = configKeys.get(name); ConfigValue value = configs.get(name); if (key.recommender != null) { try { List<Object> recommendedValues = key.recommender.validValues(name, parsed); List<Object> originalRecommendedValues = value.recommendedValues(); if (!originalRecommendedValues.isEmpty()) { Set<Object> originalRecommendedValueSet = new HashSet<>(originalRecommendedValues); recommendedValues.removeIf(o -> !originalRecommendedValueSet.contains(o)); } value.recommendedValues(recommendedValues); value.visible(key.recommender.visible(name, parsed)); } catch (ConfigException e) { value.addErrorMessage(e.getMessage()); } } configs.put(name, value); for (String dependent: key.dependents) { validate(dependent, parsed, configs); } }
@Override public void ensureValid(String name, Object o) { String s = (String) o; if (s != null && s.isEmpty()) { throw new ConfigException(name, o, "String must be non-empty"); } }
static void validate(String config, String value) { try { new URL(value); } catch (MalformedURLException e) { ConfigException configException = new ConfigException( config, value, "Could not parse to URL." ); configException.initCause(e); throw configException; } }
@Test public void testConstructorWithNotStringKey() { Properties props = new Properties(); props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999"); props.put(1, "not string key"); try (KafkaProducer<?, ?> ff = new KafkaProducer<>(props, new StringSerializer(), new StringSerializer())) { fail("Constructor should throw exception"); } catch (ConfigException e) { assertTrue("Unexpected exception message: " + e.getMessage(), e.getMessage().contains("not string key")); } }
public ConfigDef define(ConfigKey key) { if (configKeys.containsKey(key.name)) { throw new ConfigException("Configuration " + key.name + " is defined twice."); } if (key.group != null && !groups.contains(key.group)) { groups.add(key.group); } configKeys.put(key.name, key); return this; }
value.addErrorMessage(e.getMessage());
private static int parseAcks(String acksString) { try { return acksString.trim().equalsIgnoreCase("all") ? -1 : Integer.parseInt(acksString.trim()); } catch (NumberFormatException e) { throw new ConfigException("Invalid configuration value for 'acks': " + acksString); } }
protected Object get(String key) { if (!values.containsKey(key)) throw new ConfigException(String.format("Unknown configuration '%s'", key)); used.add(key); return values.get(key); }
private static Map<String, Object> propsToMap(Properties properties) { Map<String, Object> map = new HashMap<>(properties.size()); for (Map.Entry<Object, Object> entry : properties.entrySet()) { if (entry.getKey() instanceof String) { String k = (String) entry.getKey(); map.put(k, properties.get(k)); } else { throw new ConfigException(entry.getKey().toString(), entry.getValue(), "Key must be a string."); } } return map; }
@Override public void ensureValid(String name, Object o) { String s = (String) o; if (!validStrings.contains(s)) { throw new ConfigException(name, o, "String must be one of: " + Utils.join(validStrings, ", ")); } }
public void ensureValid(String name, Object o) { if (o == null) throw new ConfigException(name, o, "Value must be non-null"); Number n = (Number) o; if (min != null && n.doubleValue() < min.doubleValue()) throw new ConfigException(name, o, "Value must be at least " + min); if (max != null && n.doubleValue() > max.doubleValue()) throw new ConfigException(name, o, "Value must be no more than " + max); }
private String[] parseIntoParts(String name, String value, Predicate<Integer> lengthPredicate, Pattern delimiterPattern) { String[] nodes = delimiterPattern.split(value); if (lengthPredicate.test(nodes.length) || Arrays.stream(nodes).anyMatch(Strings::isNullOrEmpty)) { throw new ConfigException("Invalid format: " + name); } return nodes; }
/** * Get a configuration and throw exception if the configuration was not provided. * @param configs the config map. * @param configName the config to get. * @return the configuration string. */ public static String getRequiredConfig(Map<String, ?> configs, String configName) { String value = (String) configs.get(configName); if (value == null || value.isEmpty()) { throw new ConfigException(String.format("Configuration %s must be provided.", configName)); } return value; }
@Override public void ensureValid(String name, Object value) { String s = (String) value; if (s == null) { // This can happen during creation of the config object due to no default value being defined for the // name configuration - a missing name parameter is caught when checking for mandatory parameters, // thus we can ok a null value here return; } else if (s.isEmpty()) { throw new ConfigException(name, value, "String may not be empty"); } // Check name string for illegal characters ArrayList<Integer> foundIllegalCharacters = new ArrayList<>(); for (int i = 0; i < s.length(); i++) { if (Character.isISOControl(s.codePointAt(i))) { foundIllegalCharacters.add(s.codePointAt(i)); } } if (!foundIllegalCharacters.isEmpty()) { throw new ConfigException(name, value, "String may not contain control sequences but had the following ASCII chars: " + Utils.join(foundIllegalCharacters, ", ")); } }