/** * 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) { return getConfiguredInstances(getList(key), t, configOverrides); }
@Test public void testEmptyList() { AbstractConfig conf; ConfigDef configDef = new ConfigDef().define("a", Type.LIST, "", new ConfigDef.NonNullValidator(), Importance.HIGH, "doc"); conf = new AbstractConfig(configDef, Collections.emptyMap()); assertEquals(Collections.emptyList(), conf.getList("a")); conf = new AbstractConfig(configDef, Collections.singletonMap("a", "")); assertEquals(Collections.emptyList(), conf.getList("a")); conf = new AbstractConfig(configDef, Collections.singletonMap("a", "b,c,d")); assertEquals(Arrays.asList("b", "c", "d"), conf.getList("a")); }
/** * Method is used to retrieve a list and convert it to an immutable set. * * @param config Config to read * @param key Key to read * @return ImmutableSet with the contents of the config key. * @see com.google.common.collect.ImmutableSet */ public static Set<String> getSet(AbstractConfig config, String key) { List<String> value = config.getList(key); return ImmutableSet.copyOf(value); }
/** * 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); }
/** * 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); }
/** * 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); }
/** * 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); }
@Override public void start(Map<String, String> props) { AbstractConfig parsedConfig = new AbstractConfig(CONFIG_DEF, props); filename = parsedConfig.getString(FILE_CONFIG); List<String> topics = parsedConfig.getList(TOPIC_CONFIG); if (topics.size() != 1) { throw new ConfigException("'topic' in FileStreamSourceConnector configuration requires definition of a single topic"); } topic = topics.get(0); batchSize = parsedConfig.getInt(TASK_BATCH_SIZE_CONFIG); }
public <T> List<T> getConfiguredInstances(String key, Class<T> t) { List<String> klasses = getList(key); List<T> objects = new ArrayList<T>(); for (String klass : klasses) { Class<?> c; try { c = Class.forName(klass); } catch (ClassNotFoundException e) { throw new ConfigException(key, klass, "Class " + klass + " could not be found."); } 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(this.originals); objects.add(t.cast(o)); } return objects; }