org.apache.kafka.common.config.AbstractConfig Java Examples

The following examples show how to use org.apache.kafka.common.config.AbstractConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigDocumentationGenerator.java    From kafka-monitor with Apache License 2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
  if (argv.length < 2) {
    printHelp();
    System.exit(1);
  }

  File outputDir = new File(argv[0]);
  if (!outputDir.exists()) {
    outputDir.mkdirs();
  }

  for (int i = 1; i < argv.length; i++) {
    Class<? extends AbstractConfig> configClass = (Class<? extends AbstractConfig>) Class.forName(argv[i]);
    Field configDefField = configClass.getDeclaredField("CONFIG");
    configDefField.setAccessible(true);
    ConfigDef configDef = (ConfigDef) configDefField.get(null);
    String docClass = configClass.getSimpleName();
    File outputFile = new File(outputDir, docClass + ".html");
    try (FileWriter fout = new FileWriter(outputFile)) {
      printHtmlHeader(fout, docClass);
      fout.write(configDef.toHtmlTable());
      printHtmlFooter(fout);
    }
  }
}
 
Example #2
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Method is used to return a charset(s) for a list key.
 *
 * @param config Config to read from.
 * @param key    Key to read from
 * @return
 */
public static List<Charset> charsets(AbstractConfig config, String key) {
  final List<String> charsetNames = config.getList(key);
  final List<Charset> result = new ArrayList<>(charsetNames.size());

  for (String charsetName : charsetNames) {
    try {
      Charset charset = Charset.forName(charsetName);
      result.add(charset);
    } catch (final UnsupportedCharsetException ex) {
      ConfigException exception = new ConfigException(key, charsetName, "Invalid charset.");
      exception.initCause(ex);
      throw exception;
    }

  }
  return result;
}
 
Example #3
Source File: ConverterMessageBuilder.java    From kafka-connect-mq-sink with Apache License 2.0 6 votes vote down vote up
/**
 * Configure this class.
 * 
 * @param props initial configuration
 *
 * @throws ConnectException   Operation failed and connector should stop.
 */
public void configure(Map<String, String> props) {
    log.trace("[{}] Entry {}.configure, props={}", Thread.currentThread().getId(), this.getClass().getName(), props);

    super.configure(props);

    String converterClass = props.get(MQSinkConnector.CONFIG_NAME_MQ_MESSAGE_BUILDER_VALUE_CONVERTER);

    try {
        Class<? extends Converter> c = Class.forName(converterClass).asSubclass(Converter.class);
        converter = c.newInstance();

        // Make a copy of the configuration to filter out only those that begin "mq.message.builder.value.converter."
        // since those are used to configure the converter itself
        AbstractConfig ac = new AbstractConfig(new ConfigDef(), props, false);

        // Configure the Converter to convert the value, not the key (isKey == false)
        converter.configure(ac.originalsWithPrefix(MQSinkConnector.CONFIG_NAME_MQ_MESSAGE_BUILDER_VALUE_CONVERTER + "."), false);
    }
    catch (ClassNotFoundException | IllegalAccessException | InstantiationException | NullPointerException exc) {
        log.error("Could not instantiate converter for message builder {}", converterClass);
        throw new ConnectException("Could not instantiate converter for message builder", exc);
    }

    log.trace("[{}]  Exit {}.configure", Thread.currentThread().getId(), this.getClass().getName());
}
 
Example #4
Source File: KafkaNodeClient.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public KafkaNodeClient(int id, String host, int port) {
	node = new Node(id, host, port);
	
	//
	LogContext logContext = new LogContext("ctx");

	ConfigDef defConf = new ConfigDef();
	defConf.define(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, ConfigDef.Type.STRING,
			CommonClientConfigs.DEFAULT_SECURITY_PROTOCOL, ConfigDef.Importance.MEDIUM,
			CommonClientConfigs.SECURITY_PROTOCOL_DOC);

	defConf.define(SaslConfigs.SASL_MECHANISM, ConfigDef.Type.STRING, SaslConfigs.DEFAULT_SASL_MECHANISM,
			ConfigDef.Importance.MEDIUM, SaslConfigs.SASL_MECHANISM_DOC);

	metrics = new Metrics(Time.SYSTEM);

	AbstractConfig config = new AbstractConfig(defConf, new Properties());
	channelBuilder = ClientUtils.createChannelBuilder(config);
	selector = new Selector(1000L, metrics, Time.SYSTEM, "cc", channelBuilder, logContext);
	client = new NetworkClient(selector, new Metadata(0, Long.MAX_VALUE, false),
			CLIENT_ID, 10, 1000L, 1000L, 1, 1024, 1000, Time.SYSTEM, true, new ApiVersions(),
			null, logContext);
}
 
Example #5
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Method is used to return enums from a list.
 *
 * @param enumClass
 * @param config
 * @param key
 * @param <T>
 * @return
 * @see com.github.jcustenborder.kafka.connect.utils.config.validators.Validators#validEnum(Class, Enum[])
 */
public static <T extends Enum<T>> List<T> getEnums(Class<T> enumClass, AbstractConfig config, String key) {
  Preconditions.checkNotNull(enumClass, "enumClass cannot be null");
  Preconditions.checkState(enumClass.isEnum(), "enumClass must be an enum.");
  Preconditions.checkState(
      ConfigDef.Type.LIST == config.typeOf(key),
      "'%s' must be a list",
      key
  );
  List<T> result = new ArrayList<>();
  List<String> values = config.getList(key);
  for (String value : values) {
    result.add(Enum.valueOf(enumClass, value));
  }
  return result;
}
 
Example #6
Source File: MongoWrapper.java    From MongoDb-Sink-Connector with Apache License 2.0 6 votes vote down vote up
private MongoClient createClient(AbstractConfig config, MongoClientOptions options) {
    String host = config.getString(MONGO_HOST);
    int port = config.getInt(MONGO_PORT);

    try {
        MongoClientOptions actualOptions;
        if (options != null) {
            actualOptions = options;
        } else {
            actualOptions = new MongoClientOptions.Builder().build();
        }
        ServerAddress server = new ServerAddress(host, port);
        if (credentials != null) {
            return new MongoClient(server, credentials, actualOptions);
        } else {
            return new MongoClient(server, actualOptions);
        }
    } catch (MongoException ex) {
        log.error("Failed to create MongoDB client to {}:{} with credentials {}", host, port,
                credentials, ex);
        throw new ConnectException("MongoDb client cannot be created.", ex);
    }
}
 
Example #7
Source File: MongoWrapperTest.java    From MongoDb-Sink-Connector with Apache License 2.0 5 votes vote down vote up
@Test
public void checkConnectionWithCredentials() throws Exception {
    Map<String, Object> configMap = new HashMap<>();
    configMap.put(MONGO_USERNAME, "myuser");
    configMap.put(MONGO_PASSWORD, "mypassword");
    configMap.put(MONGO_PORT, MONGO_PORT_DEFAULT + 1000);
    configMap.put(MONGO_HOST, "localhost");
    configMap.put(MONGO_DATABASE, "mydb");
    configMap.put(COLLECTION_FORMAT, "{$topic}");
    configMap.put(TOPICS_CONFIG, "a");

    Field credentialsField = MongoWrapper.class.getDeclaredField("credentials");
    credentialsField.setAccessible(true);
    MongoClientOptions timeout = MongoClientOptions.builder()
            .connectTimeout(1)
            .socketTimeout(1)
            .serverSelectionTimeout(1)
            .build();
    MongoWrapper wrapper = new MongoWrapper(new AbstractConfig(CONFIG_DEF, configMap), timeout);

    assertThat(credentialsField.get(wrapper), is(not(nullValue())));
    assertFalse(wrapper.checkConnection());

    thrown.expect(MongoException.class);
    try {
        wrapper.store("mytopic", new Document());
    } finally {
        wrapper.close();
    }
}
 
Example #8
Source File: TaskHelper.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
public static <CFG extends AbstractConfig> void logRecordContent(Logger logger, ConnectRecord<?> record, CFG config) {
    if (logger != null && record != null && config != null) {
        // do not log record's content by default, as it may contain sensitive information
        LoggingLevel level = LoggingLevel.OFF;
        try {
            final String key = (record instanceof SourceRecord)
                ? CamelSourceConnectorConfig.CAMEL_SOURCE_CONTENT_LOG_LEVEL_CONF
                : CamelSinkConnectorConfig.CAMEL_SINK_CONTENT_LOG_LEVEL_CONF;
            level = LoggingLevel.valueOf(config.getString(key).toUpperCase());
        } catch (Exception e) {
            logger.warn("Invalid value for contentLogLevel property");
        }
        switch (level) {
            case TRACE:
                logger.trace(record.toString());
                break;
            case DEBUG:
                logger.debug(record.toString());
                break;
            case INFO:
                logger.info(record.toString());
                break;
            case WARN:
                logger.warn(record.toString());
                break;
            case ERROR:
                logger.error(record.toString());
                break;
            default:
                break;
        }
    }
}
 
Example #9
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Method is used to retrieve a list of URI(s) from a configuration key.
 *
 * @param config Config to read
 * @param key    Key to read
 * @return URI for the value.
 */
public static List<URI> uris(AbstractConfig config, String key) {
  List<URI> result = new ArrayList<>();
  List<String> input = config.getList(key);
  for (String s : input) {
    result.add(uri(key, s));
  }
  return ImmutableList.copyOf(result);
}
 
Example #10
Source File: MongoWrapperTest.java    From MongoDb-Sink-Connector with Apache License 2.0 5 votes vote down vote up
@Test
public void checkConnectionWithEmptyCredentials() throws Exception {
    Map<String, Object> configMap = new HashMap<>();
    configMap.put(MONGO_USERNAME, "");
    configMap.put(MONGO_PASSWORD, "");
    configMap.put(MONGO_PORT, MONGO_PORT_DEFAULT + 1000);
    configMap.put(MONGO_HOST, "localhost");
    configMap.put(MONGO_DATABASE, "mydb");
    configMap.put(COLLECTION_FORMAT, "{$topic}");
    configMap.put(TOPICS_CONFIG, "a");

    Field credentialsField = MongoWrapper.class.getDeclaredField("credentials");
    credentialsField.setAccessible(true);
    MongoClientOptions timeout = MongoClientOptions.builder()
            .connectTimeout(1)
            .socketTimeout(1)
            .serverSelectionTimeout(1)
            .build();
    MongoWrapper wrapper = new MongoWrapper(new AbstractConfig(CONFIG_DEF, configMap), timeout);

    assertThat(credentialsField.get(wrapper), is(nullValue()));
    assertFalse(wrapper.checkConnection());

    thrown.expect(MongoException.class);
    try {
        wrapper.store("mytopic", new Document());
    } finally {
        wrapper.close();
    }
}
 
Example #11
Source File: MongoWrapperTest.java    From MongoDb-Sink-Connector with Apache License 2.0 5 votes vote down vote up
@Test
public void checkConnectionWithoutCredentials() throws Exception {
    Map<String, Object> configMap = new HashMap<>();
    configMap.put(MONGO_USERNAME, null);
    configMap.put(MONGO_PASSWORD, null);
    configMap.put(MONGO_PORT, MONGO_PORT_DEFAULT + 1000);
    configMap.put(MONGO_HOST, "localhost");
    configMap.put(MONGO_DATABASE, "mydb");
    configMap.put(COLLECTION_FORMAT, "{$topic}");
    configMap.put(TOPICS_CONFIG, "a");

    Field credentialsField = MongoWrapper.class.getDeclaredField("credentials");
    credentialsField.setAccessible(true);
    MongoClientOptions timeout = MongoClientOptions.builder()
            .connectTimeout(1)
            .socketTimeout(1)
            .serverSelectionTimeout(1)
            .build();
    MongoWrapper wrapper = new MongoWrapper(new AbstractConfig(CONFIG_DEF, configMap), timeout);

    assertThat(credentialsField.get(wrapper), is(nullValue()));
    assertFalse(wrapper.checkConnection());

    thrown.expect(MongoException.class);
    try {
        wrapper.store("mytopic", new Document());
    } finally {
        wrapper.close();
    }
}
 
Example #12
Source File: MongoWrapper.java    From MongoDb-Sink-Connector with Apache License 2.0 5 votes vote down vote up
private MongoCredential createCredentials(AbstractConfig config, String dbName) {
    String userName = config.getString(MONGO_USERNAME);
    String password = config.getString(MONGO_PASSWORD);
    if (isValid(userName) && isValid(password)) {
        return MongoCredential.createCredential(userName, dbName, password.toCharArray());
    } else {
        return null;
    }
}
 
Example #13
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Method will create a KeyStore based on the KeyStore type specified in the config.
 *
 * @param config Config to read from.
 * @param key    Key to read from
 * @return KeyStore based on the type specified in the config.
 */
public static KeyStore keyStore(AbstractConfig config, String key) {
  final String keyStoreType = config.getString(key);
  try {
    return KeyStore.getInstance(keyStoreType);
  } catch (KeyStoreException e) {
    ConfigException exception = new ConfigException(
        key,
        keyStoreType,
        "Invalid KeyStore type."
    );
    exception.initCause(e);
    throw exception;
  }
}
 
Example #14
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Method will create a KeyManagerFactory based on the Algorithm type specified in the config.
 *
 * @param config Config to read from.
 * @param key    Key to read from
 * @return KeyManagerFactory based on the type specified in the config.
 */
public static KeyManagerFactory keyManagerFactory(AbstractConfig config, String key) {
  final String keyManagerFactoryType = config.getString(key);
  try {
    return KeyManagerFactory.getInstance(keyManagerFactoryType);
  } catch (NoSuchAlgorithmException e) {
    ConfigException exception = new ConfigException(
        key,
        keyManagerFactoryType,
        "Unknown Algorithm."
    );
    exception.initCause(e);
    throw exception;
  }
}
 
Example #15
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Method will create a TrustManagerFactory based on the Algorithm type specified in the config.
 *
 * @param config Config to read from.
 * @param key    Key to read from
 * @return TrustManagerFactory based on the type specified in the config.
 */
public static TrustManagerFactory trustManagerFactory(AbstractConfig config, String key) {
  final String trustManagerFactoryType = config.getString(key);
  try {
    return TrustManagerFactory.getInstance(trustManagerFactoryType);
  } catch (NoSuchAlgorithmException e) {
    ConfigException exception = new ConfigException(
        key,
        trustManagerFactoryType,
        "Unknown Algorithm."
    );
    exception.initCause(e);
    throw exception;
  }
}
 
Example #16
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Method will create a SSLContext based on the Algorithm type specified in the config.
 *
 * @param config Config to read from.
 * @param key    Key to read from
 * @return SSLContext based on the type specified in the config.
 */
public static SSLContext sslContext(AbstractConfig config, String key) {
  final String trustManagerFactoryType = config.getString(key);
  try {
    return SSLContext.getInstance(trustManagerFactoryType);
  } catch (NoSuchAlgorithmException e) {
    ConfigException exception = new ConfigException(
        key,
        trustManagerFactoryType,
        "Unknown Algorithm."
    );
    exception.initCause(e);
    throw exception;
  }
}
 
Example #17
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Method is used to return a charset for a string key.
 *
 * @param config Config to read from.
 * @param key    Key to read from
 * @return
 */
public static Charset charset(AbstractConfig config, String key) {
  final String charsetName = config.getString(key);
  try {
    return Charset.forName(charsetName);
  } catch (final UnsupportedCharsetException ex) {
    ConfigException exception = new ConfigException(key, charsetName, "Invalid charset.");
    exception.initCause(ex);
    throw exception;
  }
}
 
Example #18
Source File: MongoWrapper.java    From MongoDb-Sink-Connector with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link MongoClient}.
 *
 * @param config Configuration of the client, according to {@link MongoDbSinkConnector}.
 * @throws ConnectException a MongoClient could not be created.
 */
public MongoWrapper(AbstractConfig config, MongoClientOptions options) {
    collectionFormat = config.getString(COLLECTION_FORMAT);
    String dbName = config.getString(MONGO_DATABASE);
    collectionCache = new HashMap<>();
    credentials = createCredentials(config, dbName);
    mongoClient = createClient(config, options);
    database = mongoClient.getDatabase(dbName);
}
 
Example #19
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Method is used to retrieve a list of URL(s) from a configuration key.
 *
 * @param config Config to read
 * @param key    Key to read
 * @return URL for the value.
 */
public static List<URL> urls(AbstractConfig config, String key) {
  List<URL> result = new ArrayList<>();
  List<String> input = config.getList(key);
  for (String s : input) {
    result.add(url(key, s));
  }
  return ImmutableList.copyOf(result);
}
 
Example #20
Source File: TaskConfigBuilder.java    From kafka-connect-jenkins with Apache License 2.0 5 votes vote down vote up
/**
 * @param maxTasks            The maximum simultaneous tasks configured to be run by connector.
 * @param taskConfigKey       The key to be used while adding the task config.
 * @param connectorCfg        The reference to the custom config class you wrote extending from {@link AbstractConfig}. Null if not applicable.
 * @param taskConfigExtractor The class used to extract the task configuration. This is used as the value for key <i>taskConfigKey</i>.
 */
public TaskConfigBuilder(int maxTasks, String taskConfigKey, AbstractConfig connectorCfg, TaskConfigExtractor taskConfigExtractor) {
    this.maxTasks = maxTasks;
    this.connectorCfg = connectorCfg;
    this.taskCfgExtractor = taskConfigExtractor;
    this.taskCfgKey = taskConfigKey;
}
 
Example #21
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Method is used to parse a list ConfigDef item to a list of HostAndPort
 *
 * @param config      Config to read from
 * @param key         ConfigItem to get the host string from.
 * @param defaultPort The default port to use if a port was not specified. Can be null.
 * @return
 */
public static List<HostAndPort> hostAndPorts(AbstractConfig config, String key, Integer defaultPort) {
  final List<String> inputs = config.getList(key);
  List<HostAndPort> result = new ArrayList<>();
  for (final String input : inputs) {
    final HostAndPort hostAndPort = hostAndPort(input, defaultPort);
    result.add(hostAndPort);
  }

  return ImmutableList.copyOf(result);
}
 
Example #22
Source File: MongoRyaSinkConnector.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
protected AbstractConfig getConfig() {
    if(config == null) {
        throw new IllegalStateException("The configuration has not been set yet. Invoke start(Map) first.");
    }
    return config;
}
 
Example #23
Source File: AccumuloRyaSinkConnector.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
protected AbstractConfig getConfig() {
    if(config == null) {
        throw new IllegalStateException("The configuration has not been set yet. Invoke start(Map) first.");
    }
    return config;
}
 
Example #24
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Method is used to return a list of InetSocketAddress from a config list of hostname:port strings.
 *
 * @param config config to read the value from
 * @param key    key for the value
 * @return List of InetSocketAddress for the supplied strings.
 */
public static List<InetSocketAddress> inetSocketAddresses(AbstractConfig config, String key) {
  Preconditions.checkNotNull(config, "config cannot be null");
  List<String> value = config.getList(key);
  List<InetSocketAddress> addresses = new ArrayList<>(value.size());
  for (String s : value) {
    addresses.add(parseInetSocketAddress(s));
  }
  return ImmutableList.copyOf(addresses);
}
 
Example #25
Source File: Compatibility.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
static Object createConnectorClientConfigOverridePolicy(Plugins plugins, AbstractConfig config) throws ConnectException {
    if (CLS_CONNECTOR_CLIENT_CONFIG_OVERRIDE_POLICY != null) {
        return plugins.newPlugin(
                config.getString(WorkerConfig.CONNECTOR_CLIENT_POLICY_CLASS_CONFIG),
                config, CLS_CONNECTOR_CLIENT_CONFIG_OVERRIDE_POLICY);
    }
    return null;
}
 
Example #26
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Method is used to return a File checking to ensure that it is an absolute path.
 *
 * @param config config to read the value from
 * @param key    key for the value
 * @return File for the config value.
 */
public static File getAbsoluteFile(AbstractConfig config, String key) {
  Preconditions.checkNotNull(config, "config cannot be null");
  String path = config.getString(key);
  File file = new File(path);
  if (!file.isAbsolute()) {
    throw new ConfigException(
        key,
        path,
        "Must be an absolute path."
    );
  }
  return new File(path);
}
 
Example #27
Source File: KafkaCruiseControlUtils.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Reads the configuration file, parses and validates the configs. Enables the configs to be passed
 * in from environment variables.
 * @param propertiesFile is the file containing the Cruise Control configuration.
 * @return a parsed {@link KafkaCruiseControlConfig}
 * @throws IOException if the configuration file can't be read.
 */
public static KafkaCruiseControlConfig readConfig(String propertiesFile) throws IOException {
  Properties props = new Properties();
  try (InputStream propStream = new FileInputStream(propertiesFile)) {
    props.put(AbstractConfig.CONFIG_PROVIDERS_CONFIG, ENV_CONFIG_PROVIDER_NAME);
    props.put(AbstractConfig.CONFIG_PROVIDERS_CONFIG + ENV_CONFIG_PROVIDER_CLASS_CONFIG, EnvConfigProvider.class.getName());
    props.load(propStream);
  }
  return new KafkaCruiseControlConfig(props);
}
 
Example #28
Source File: Cli.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
private static ConfigDef getConfigDef(Class<? extends AbstractConfig> classs) {
  try {
    java.lang.reflect.Field field = classs.getDeclaredField("CONFIG");
    field.setAccessible(true);
    return (ConfigDef) field.get(null);
  } catch (Exception exception) {
    // uhhh...
    // TODO
    return null;
  }
}
 
Example #29
Source File: ConfigHelper.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
public static String getOverrideOrDefault(
    final AbstractConfig config, final String overrideConfig, final String defaultConfig) {
  String stringConfig = config.getString(overrideConfig);
  if (stringConfig.isEmpty()) {
    stringConfig = config.getString(defaultConfig);
  }
  return stringConfig;
}
 
Example #30
Source File: CustomTransform.java    From kafka-connect-couchbase with Apache License 2.0 4 votes vote down vote up
@Override
public void configure(Map<String, ?> configs) {
  final AbstractConfig config = new AbstractConfig(config(), configs);
  this.textTransformer = Operation.valueOf(config.getString(OP_CONFIG));
}