Search in sources :

Example 6 with HeaderConverter

use of org.apache.kafka.connect.storage.HeaderConverter in project kafka by apache.

the class Plugins method newHeaderConverter.

/**
 * If the given configuration defines a {@link HeaderConverter} using the named configuration property, return a new configured
 * instance.
 *
 * @param config             the configuration containing the {@link Converter}'s configuration; may not be null
 * @param classPropertyName  the name of the property that contains the name of the {@link Converter} class; may not be null
 * @param classLoaderUsage   which classloader should be used
 * @return the instantiated and configured {@link HeaderConverter}; null if the configuration did not define the specified property
 * @throws ConnectException if the {@link HeaderConverter} implementation class could not be found
 */
public HeaderConverter newHeaderConverter(AbstractConfig config, String classPropertyName, ClassLoaderUsage classLoaderUsage) {
    Class<? extends HeaderConverter> klass = null;
    switch(classLoaderUsage) {
        case CURRENT_CLASSLOADER:
            if (!config.originals().containsKey(classPropertyName)) {
                // This connector configuration does not define the header converter via the specified property name
                return null;
            }
            // Attempt to load first with the current classloader, and plugins as a fallback.
            // Note: we can't use config.getConfiguredInstance because we have to remove the property prefixes
            // before calling config(...)
            klass = pluginClassFromConfig(config, classPropertyName, HeaderConverter.class, delegatingLoader.headerConverters());
            break;
        case PLUGINS:
            // Attempt to load with the plugin class loader, which uses the current classloader as a fallback.
            // Note that there will always be at least a default header converter for the worker
            String converterClassOrAlias = config.getClass(classPropertyName).getName();
            try {
                klass = pluginClass(delegatingLoader, converterClassOrAlias, HeaderConverter.class);
            } catch (ClassNotFoundException e) {
                throw new ConnectException("Failed to find any class that implements HeaderConverter and which name matches " + converterClassOrAlias + ", available header converters are: " + pluginNames(delegatingLoader.headerConverters()));
            }
    }
    if (klass == null) {
        throw new ConnectException("Unable to initialize the HeaderConverter specified in '" + classPropertyName + "'");
    }
    String configPrefix = classPropertyName + ".";
    Map<String, Object> converterConfig = config.originalsWithPrefix(configPrefix);
    converterConfig.put(ConverterConfig.TYPE_CONFIG, ConverterType.HEADER.getName());
    log.debug("Configuring the header converter with configuration keys:{}{}", System.lineSeparator(), converterConfig.keySet());
    HeaderConverter plugin;
    ClassLoader savedLoader = compareAndSwapLoaders(klass.getClassLoader());
    try {
        plugin = newPlugin(klass);
        plugin.configure(converterConfig);
    } finally {
        compareAndSwapLoaders(savedLoader);
    }
    return plugin;
}
Also used : HeaderConverter(org.apache.kafka.connect.storage.HeaderConverter) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 7 with HeaderConverter

use of org.apache.kafka.connect.storage.HeaderConverter in project kafka by apache.

the class PluginsTest method shouldInstantiateAndConfigureExplicitlySetHeaderConverterWithCurrentClassLoader.

@Test
public void shouldInstantiateAndConfigureExplicitlySetHeaderConverterWithCurrentClassLoader() {
    assertNotNull(props.get(WorkerConfig.HEADER_CONVERTER_CLASS_CONFIG));
    HeaderConverter headerConverter = plugins.newHeaderConverter(config, WorkerConfig.HEADER_CONVERTER_CLASS_CONFIG, ClassLoaderUsage.CURRENT_CLASSLOADER);
    assertNotNull(headerConverter);
    assertTrue(headerConverter instanceof TestHeaderConverter);
    this.headerConverter = (TestHeaderConverter) headerConverter;
    // Validate extra configs got passed through to overridden converters
    assertConverterType(ConverterType.HEADER, this.headerConverter.configs);
    assertEquals("baz", this.headerConverter.configs.get("extra.config"));
    headerConverter = plugins.newHeaderConverter(config, WorkerConfig.HEADER_CONVERTER_CLASS_CONFIG, ClassLoaderUsage.PLUGINS);
    assertNotNull(headerConverter);
    assertTrue(headerConverter instanceof TestHeaderConverter);
    this.headerConverter = (TestHeaderConverter) headerConverter;
    // Validate extra configs got passed through to overridden converters
    assertConverterType(ConverterType.HEADER, this.headerConverter.configs);
    assertEquals("baz", this.headerConverter.configs.get("extra.config"));
}
Also used : HeaderConverter(org.apache.kafka.connect.storage.HeaderConverter) SimpleHeaderConverter(org.apache.kafka.connect.storage.SimpleHeaderConverter) Test(org.junit.Test)

Aggregations

HeaderConverter (org.apache.kafka.connect.storage.HeaderConverter)7 ConnectException (org.apache.kafka.connect.errors.ConnectException)4 SimpleHeaderConverter (org.apache.kafka.connect.storage.SimpleHeaderConverter)3 Test (org.junit.Test)3 Task (org.apache.kafka.connect.connector.Task)2 SinkTask (org.apache.kafka.connect.sink.SinkTask)2 SourceTask (org.apache.kafka.connect.source.SourceTask)2 Converter (org.apache.kafka.connect.storage.Converter)2 JsonConverter (org.apache.kafka.connect.json.JsonConverter)1 LoggingContext (org.apache.kafka.connect.util.LoggingContext)1