use of org.apache.kafka.common.Configurable in project kafka by apache.
the class AbstractConfig method getConfiguredInstance.
/**
* Get a configured instance of the give class specified by the given configuration key. If the object implements
* Configurable configure it using the configuration.
*
* @param key The configuration key for the class
* @param t The interface the class should implement
* @return A configured instance of the class
*/
public <T> T getConfiguredInstance(String key, Class<T> t) {
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(originals());
return t.cast(o);
}
use of org.apache.kafka.common.Configurable in project kafka by apache.
the class AbstractConfig method getConfiguredInstances.
/**
* Get a list of configured instances of the given class specified by the given configuration key. The configuration
* may specify either null or an empty string to indicate no configured instances. In both cases, this method
* returns an empty list to indicate no configured instances.
* @param key The configuration key for the class
* @param t The interface the class should implement
* @param configOverrides Configuration overrides to use.
* @return The list of configured instances
*/
public <T> List<T> getConfiguredInstances(String key, Class<T> t, Map<String, Object> configOverrides) {
List<String> klasses = getList(key);
List<T> objects = new ArrayList<T>();
if (klasses == null)
return objects;
Map<String, Object> configPairs = originals();
configPairs.putAll(configOverrides);
for (Object klass : klasses) {
Object o;
if (klass instanceof String) {
try {
o = Utils.newInstance((String) klass, t);
} catch (ClassNotFoundException e) {
throw new KafkaException(klass + " ClassNotFoundException exception occurred", e);
}
} else if (klass instanceof Class<?>) {
o = Utils.newInstance((Class<?>) klass);
} else
throw new KafkaException("List contains element of type " + klass.getClass().getName() + ", expected String or Class");
if (!t.isInstance(o))
throw new KafkaException(klass + " is not an instance of " + t.getName());
if (o instanceof Configurable)
((Configurable) o).configure(configPairs);
objects.add(t.cast(o));
}
return objects;
}
Aggregations