Java Code Examples for org.apache.kafka.common.config.AbstractConfig#getString()

The following examples show how to use org.apache.kafka.common.config.AbstractConfig#getString() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 3 votes vote down vote up
/**
 * Method is used to return an enum value from a given string.
 *
 * @param enumClass Class for the resulting enum value
 * @param config    config to read the value from
 * @param key       key for the value
 * @param <T>       Enum class to return type for.
 * @return enum value for the given key.
 * @see com.github.jcustenborder.kafka.connect.utils.config.validators.Validators#validEnum(Class, Enum[])
 */
public static <T extends Enum<T>> T getEnum(Class<T> enumClass, AbstractConfig config, String key) {
  Preconditions.checkNotNull(enumClass, "enumClass cannot be null");
  Preconditions.checkState(enumClass.isEnum(), "enumClass must be an enum.");
  String textValue = config.getString(key);
  return Enum.valueOf(enumClass, textValue);
}
 
Example 12
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 2 votes vote down vote up
/**
 * Method is used to return an InetSocketAddress from a hostname:port string.
 *
 * @param config config to read the value from
 * @param key    key for the value
 * @return InetSocketAddress for the supplied string.
 */
public static InetSocketAddress inetSocketAddress(AbstractConfig config, String key) {
  Preconditions.checkNotNull(config, "config cannot be null");
  String value = config.getString(key);
  return parseInetSocketAddress(value);
}
 
Example 13
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 2 votes vote down vote up
/**
 * Method is used to parse a string ConfigDef item to a 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 HostAndPort based on the ConfigItem.
 */
public static HostAndPort hostAndPort(AbstractConfig config, String key, Integer defaultPort) {
  final String input = config.getString(key);
  return hostAndPort(input, defaultPort);
}
 
Example 14
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 2 votes vote down vote up
/**
 * Method is used to retrieve a URL from a configuration key.
 *
 * @param config Config to read
 * @param key    Key to read
 * @return URL for the value.
 */
public static URL url(AbstractConfig config, String key) {
  final String value = config.getString(key);
  return url(key, value);
}
 
Example 15
Source File: ConfigUtils.java    From connect-utils with Apache License 2.0 2 votes vote down vote up
/**
 * Method is used to retrieve a URI from a configuration key.
 *
 * @param config Config to read
 * @param key    Key to read
 * @return URI for the value.
 */
public static URI uri(AbstractConfig config, String key) {
  final String value = config.getString(key);
  return uri(key, value);
}