@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"); } }
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); }
@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"); } }
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; }
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, ", ")); } }
@Override public void configure(Map<String, ?> configs) { // clientId must be in configs Object clientIdValue = configs.get(ConsumerConfig.CLIENT_ID_CONFIG); if (clientIdValue == null) throw new ConfigException("Mock consumer interceptor expects configuration " + ProducerConfig.CLIENT_ID_CONFIG); }
@Override public void configure(Map<String, ?> configs) { // ensure this method is called and expected configs are passed in Object o = configs.get(APPEND_STRING_PROP); if (o == null) throw new ConfigException("Mock producer interceptor expects configuration " + APPEND_STRING_PROP); if (o instanceof String) appendStr = (String) o; // clientId also must be in configs Object clientIdValue = configs.get(ProducerConfig.CLIENT_ID_CONFIG); if (clientIdValue == null) throw new ConfigException("Mock producer interceptor expects configuration " + ProducerConfig.CLIENT_ID_CONFIG); }
@SuppressWarnings("unchecked") public AbstractConfig(ConfigDef definition, Map<?, ?> originals, boolean doLog) { /* check that all the keys are really strings */ for (Map.Entry<?, ?> entry : originals.entrySet()) if (!(entry.getKey() instanceof String)) throw new ConfigException(entry.getKey().toString(), entry.getValue(), "Key must be a string."); this.originals = (Map<String, ?>) originals; this.values = definition.parse(this.originals); Map<String, Object> configUpdates = postProcessParsedConfig(Collections.unmodifiableMap(this.values)); for (Map.Entry<String, Object> update : configUpdates.entrySet()) { this.values.put(update.getKey(), update.getValue()); } definition.parse(this.values); this.used = Collections.synchronizedSet(new HashSet<>()); this.definition = definition; if (doLog) logAll(); }
Object parseValue(ConfigKey key, Object value, boolean isSet) { Object parsedValue; if (isSet) { parsedValue = parseType(key.name, value, key.type); // props map doesn't contain setting, the key is required because no default value specified - its an error } else if (NO_DEFAULT_VALUE.equals(key.defaultValue)) { throw new ConfigException("Missing required configuration \"" + key.name + "\" which has no default value."); } else { // otherwise assign setting its default value parsedValue = key.defaultValue; } if (key.validator != null) { key.validator.ensureValid(key.name, parsedValue); } return parsedValue; }
private static int configureInflightRequests(ProducerConfig config, boolean idempotenceEnabled) { if (idempotenceEnabled && 5 < config.getInt(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION)) { throw new ConfigException("Must set " + ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION + " to at most 5" + " to use the idempotent producer."); } return config.getInt(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION); }
/** * Add and validate all the configured extensions. * Token keys, apart from passing regex validation, must not be equal to the reserved key {@link OAuthBearerClientInitialResponse#AUTH_KEY} */ private void handleExtensionsCallback(SaslExtensionsCallback callback) { Map<String, String> extensions = new HashMap<>(); for (Map.Entry<String, String> configEntry : this.moduleOptions.entrySet()) { String key = configEntry.getKey(); if (!key.startsWith(EXTENSION_PREFIX)) continue; extensions.put(key.substring(EXTENSION_PREFIX.length()), configEntry.getValue()); } SaslExtensions saslExtensions = new SaslExtensions(extensions); try { OAuthBearerClientInitialResponse.validateExtensions(saslExtensions); } catch (SaslException e) { throw new ConfigException(e.getMessage()); } callback.extensions(saslExtensions); }
private static short configureAcks(ProducerConfig config, boolean idempotenceEnabled, Logger log) { boolean userConfiguredAcks = false; short acks = (short) parseAcks(config.getString(ProducerConfig.ACKS_CONFIG)); if (config.originals().containsKey(ProducerConfig.ACKS_CONFIG)) { userConfiguredAcks = true; } if (idempotenceEnabled && !userConfiguredAcks) { log.info("Overriding the default {} to all since idempotence is enabled.", ProducerConfig.ACKS_CONFIG); return -1; } if (idempotenceEnabled && acks != -1) { throw new ConfigException("Must set " + ProducerConfig.ACKS_CONFIG + " to all in order to use the idempotent " + "producer. Otherwise we cannot guarantee idempotence."); } return acks; }
private static <T> Class<? extends T> configuredClassOrDefault(Map<String, ?> configs, JaasContext jaasContext, String saslMechanism, String configName, Class<? extends T> defaultClass) { String prefix = jaasContext.type() == JaasContext.Type.SERVER ? ListenerName.saslMechanismPrefix(saslMechanism) : ""; @SuppressWarnings("unchecked") Class<? extends T> clazz = (Class<? extends T>) configs.get(prefix + configName); if (clazz != null && jaasContext.configurationEntries().size() != 1) { String errorMessage = configName + " cannot be specified with multiple login modules in the JAAS context. " + SaslConfigs.SASL_JAAS_CONFIG + " must be configured to override mechanism-specific configs."; throw new ConfigException(errorMessage); } if (clazz == null) clazz = defaultClass; return clazz; }
private static int configureRetries(ProducerConfig config, boolean idempotenceEnabled, Logger log) { boolean userConfiguredRetries = false; if (config.originals().containsKey(ProducerConfig.RETRIES_CONFIG)) { userConfiguredRetries = true; } if (idempotenceEnabled && !userConfiguredRetries) { // We recommend setting infinite retries when the idempotent producer is enabled, so it makes sense to make // this the default. log.info("Overriding the default retries config to the recommended value of {} since the idempotent " + "producer is enabled.", Integer.MAX_VALUE); return Integer.MAX_VALUE; } if (idempotenceEnabled && config.getInt(ProducerConfig.RETRIES_CONFIG) == 0) { throw new ConfigException("Must set " + ProducerConfig.RETRIES_CONFIG + " to non-zero when using the idempotent producer."); } return config.getInt(ProducerConfig.RETRIES_CONFIG); }
@Override public void validateReconfiguration(Map<String, ?> configs) { try { SecurityStore newKeystore = maybeCreateNewKeystore(configs); SecurityStore newTruststore = maybeCreateNewTruststore(configs); if (newKeystore != null || newTruststore != null) { SecurityStore keystore = newKeystore != null ? newKeystore : this.keystore; SecurityStore truststore = newTruststore != null ? newTruststore : this.truststore; createSSLContext(keystore, truststore); } } catch (Exception e) { throw new ConfigException("Validation of dynamic config update failed", e); } }
private static int configureDeliveryTimeout(ProducerConfig config, Logger log) { int deliveryTimeoutMs = config.getInt(ProducerConfig.DELIVERY_TIMEOUT_MS_CONFIG); int lingerMs = config.getInt(ProducerConfig.LINGER_MS_CONFIG); int requestTimeoutMs = config.getInt(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG); if (deliveryTimeoutMs < Integer.MAX_VALUE && deliveryTimeoutMs < lingerMs + requestTimeoutMs) { if (config.originals().containsKey(ProducerConfig.DELIVERY_TIMEOUT_MS_CONFIG)) { // throw an exception if the user explicitly set an inconsistent value throw new ConfigException(ProducerConfig.DELIVERY_TIMEOUT_MS_CONFIG + " should be equal to or larger than " + ProducerConfig.LINGER_MS_CONFIG + " + " + ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG); } else { // override deliveryTimeoutMs default value to lingerMs + requestTimeoutMs for backward compatibility deliveryTimeoutMs = lingerMs + requestTimeoutMs; log.warn("{} should be equal to or larger than {} + {}. Setting it to {}.", ProducerConfig.DELIVERY_TIMEOUT_MS_CONFIG, ProducerConfig.LINGER_MS_CONFIG, ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, deliveryTimeoutMs); } } return deliveryTimeoutMs; }
@Override public void reconfigure(Map<String, ?> configs) throws KafkaException { SecurityStore newKeystore = maybeCreateNewKeystore(configs); SecurityStore newTruststore = maybeCreateNewTruststore(configs); if (newKeystore != null || newTruststore != null) { try { SecurityStore keystore = newKeystore != null ? newKeystore : this.keystore; SecurityStore truststore = newTruststore != null ? newTruststore : this.truststore; this.sslContext = createSSLContext(keystore, truststore); this.keystore = keystore; this.truststore = truststore; } catch (Exception e) { throw new ConfigException("Reconfiguration of SSL keystore/truststore failed", e); } } }