Java Code Examples for org.apache.kafka.common.utils.Utils#newInstance()

The following examples show how to use org.apache.kafka.common.utils.Utils#newInstance() . 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: KarelDbEngine.java    From kareldb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T getConfiguredInstance(String className, Map<String, ?> configs) {
    try {
        Class<T> cls = (Class<T>) Class.forName(className);
        if (cls == null) {
            return null;
        }
        Object o = Utils.newInstance(cls);
        if (o instanceof Configurable) {
            ((Configurable) o).configure(configs);
        }
        return cls.cast(o);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: LiKafkaProducerConfig.java    From li-apache-kafka-clients with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public <T> T getConfiguredInstance(String key, Class<T> t, Producer<byte[], byte[]> producer) {
  Class<?> c = getClass(key);
  if (c == null) {
    return null;
  }
  Object o = Utils.newInstance(c);

  if (!t.isInstance(o)) {
    throw new KafkaException(c.getName() + " is not an instance of " + t.getName());
  }

  if (o instanceof Configurable) {
    ((Configurable) o).configure(configsWithCurrentProducer(producer));
  }

  return t.cast(o);
}
 
Example 3
Source File: SerdeCryptoBase.java    From kafka-end-2-end-encryption with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T> T newInstance(Map<String, ?> map, String key, Class<T> klass) throws KafkaException {
    Object val = map.get(key);
    if (val == null) {
        throw new KafkaException("No value for '" + key + "' found");
    } else if (val instanceof String) {
        try {
            return (T) Utils.newInstance(Class.forName((String) val));
        } catch (Exception e) {
            throw new KafkaException(e);
        }
    } else if (val instanceof Class) {
        return (T) Utils.newInstance((Class<T>) val);
    } else {
        throw new KafkaException("Unexpected type '" + val.getClass() + "' for '" + key + "'");
    }
}
 
Example 4
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
private Serde<?> getKeySerde(String keySerdeString) {
	Serde<?> keySerde;
	try {
		if (StringUtils.hasText(keySerdeString)) {
			keySerde = Utils.newInstance(keySerdeString, Serde.class);
		}
		else {
			keySerde = getFallbackSerde("default.key.serde");
		}
		keySerde.configure(this.streamConfigGlobalProperties, true);

	}
	catch (ClassNotFoundException ex) {
		throw new IllegalStateException("Serde class not found: ", ex);
	}
	return keySerde;
}
 
Example 5
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
private Serde<?> getKeySerde(String keySerdeString, ResolvableType resolvableType) {
	Serde<?> keySerde = null;
	try {
		if (StringUtils.hasText(keySerdeString)) {
			keySerde = Utils.newInstance(keySerdeString, Serde.class);
		}
		else {
			if (resolvableType != null &&
					(isResolvalbeKafkaStreamsType(resolvableType) || isResolvableKStreamArrayType(resolvableType))) {
				ResolvableType generic = resolvableType.isArray() ? resolvableType.getComponentType().getGeneric(0) : resolvableType.getGeneric(0);
				Serde<?> fallbackSerde = getFallbackSerde("default.key.serde");
				keySerde = getSerde(generic, fallbackSerde);
			}
			if (keySerde == null) {
				keySerde = Serdes.ByteArray();
			}
		}
		keySerde.configure(this.streamConfigGlobalProperties, true);
	}
	catch (ClassNotFoundException ex) {
		throw new IllegalStateException("Serde class not found: ", ex);
	}
	return keySerde;
}
 
Example 6
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Serde<?> getValueSerde(String valueSerdeString, ResolvableType resolvableType)
		throws ClassNotFoundException {
	Serde<?> valueSerde = null;
	if (StringUtils.hasText(valueSerdeString)) {
		valueSerde = Utils.newInstance(valueSerdeString, Serde.class);
	}
	else {

		if (resolvableType != null && ((isResolvalbeKafkaStreamsType(resolvableType)) ||
				(isResolvableKStreamArrayType(resolvableType)))) {
			Serde<?> fallbackSerde = getFallbackSerde("default.value.serde");
			ResolvableType generic = resolvableType.isArray() ? resolvableType.getComponentType().getGeneric(1) : resolvableType.getGeneric(1);
			valueSerde = getSerde(generic, fallbackSerde);
		}
		if (valueSerde == null) {

			valueSerde = Serdes.ByteArray();
		}
	}
	valueSerde.configure(streamConfigGlobalProperties, false);
	return valueSerde;
}
 
Example 7
Source File: ClientUtils.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
public static <T> T getConfiguredInstance(Class<T> cls, Map<String, ?> configs) {
    if (cls == null) {
        return null;
    }
    Object o = Utils.newInstance(cls);
    if (o instanceof Configurable) {
        ((Configurable) o).configure(configs);
    }
    return cls.cast(o);
}
 
Example 8
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
private Serde<?> getValueSerde(String valueSerdeString)
		throws ClassNotFoundException {
	Serde<?> valueSerde;
	if (StringUtils.hasText(valueSerdeString)) {
		valueSerde = Utils.newInstance(valueSerdeString, Serde.class);
	}
	else {
		valueSerde = getFallbackSerde("default.value.serde");
	}
	valueSerde.configure(this.streamConfigGlobalProperties, false);
	return valueSerde;
}
 
Example 9
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
private Serde<?> getFallbackSerde(String s) throws ClassNotFoundException {
	return this.binderConfigurationProperties.getConfiguration()
			.containsKey(s)
			? Utils.newInstance(this.binderConfigurationProperties
					.getConfiguration().get(s),
			Serde.class)
			: Serdes.ByteArray();
}
 
Example 10
Source File: CouchbaseSourceTask.java    From kafka-connect-couchbase with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Map<String, String> properties) {
  this.connectorName = properties.get("name");

  CouchbaseSourceTaskConfig config;
  try {
    config = ConfigHelper.parse(CouchbaseSourceTaskConfig.class, properties);
    if (isNullOrEmpty(connectorName)) {
      throw new ConfigException("Connector must have a non-blank 'name' config property.");
    }
  } catch (ConfigException e) {
    throw new ConnectException("Couldn't start CouchbaseSourceTask due to configuration error", e);
  }

  LogRedaction.setRedactionLevel(config.logRedaction());
  RedactionLevel.set(toDcp(config.logRedaction()));

  filter = Utils.newInstance(config.eventFilter());
  sourceHandler = Utils.newInstance(config.sourceHandler());

  topic = config.topic();
  bucket = config.bucket();
  connectorNameInOffsets = config.connectorNameInOffsets();
  batchSizeMax = config.batchSizeMax();

  Short[] partitions = toBoxedShortArray(config.partitions());
  Map<Short, SeqnoAndVbucketUuid> partitionToSavedSeqno = readSourceOffsets(partitions);

  running = true;
  queue = new LinkedBlockingQueue<>();
  errorQueue = new LinkedBlockingQueue<>(1);
  couchbaseReader = new CouchbaseReader(config, connectorName, queue, errorQueue, partitions, partitionToSavedSeqno);
  couchbaseReader.start();
}