Search in sources :

Example 1 with DefaultBinderTypeRegistry

use of org.springframework.cloud.stream.binder.DefaultBinderTypeRegistry in project spring-cloud-stream by spring-cloud.

the class BinderFactoryAutoConfiguration method binderTypeRegistry.

@Bean
public BinderTypeRegistry binderTypeRegistry(ConfigurableApplicationContext configurableApplicationContext) {
    Map<String, BinderType> binderTypes = new HashMap<>();
    ClassLoader classLoader = configurableApplicationContext.getClassLoader();
    try {
        Enumeration<URL> resources = classLoader.getResources("META-INF/spring.binders");
        // see if test binder is available on the classpath and if so add it to the binderTypes
        try {
            BinderType bt = new BinderType("integration", new Class[] { classLoader.loadClass("org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration") });
            binderTypes.put("integration", bt);
        } catch (Exception e) {
        // ignore. means test binder is not available
        }
        if (binderTypes.isEmpty() && !Boolean.valueOf(this.selfContained) && (resources == null || !resources.hasMoreElements())) {
            this.logger.debug("Failed to locate 'META-INF/spring.binders' resources on the classpath." + " Assuming standard boot 'META-INF/spring.factories' configuration is used");
        } else {
            while (resources.hasMoreElements()) {
                URL url = resources.nextElement();
                UrlResource resource = new UrlResource(url);
                for (BinderType binderType : parseBinderConfigurations(classLoader, resource)) {
                    binderTypes.put(binderType.getDefaultName(), binderType);
                }
            }
        }
    } catch (IOException | ClassNotFoundException e) {
        throw new BeanCreationException("Cannot create binder factory:", e);
    }
    return new DefaultBinderTypeRegistry(binderTypes);
}
Also used : DefaultBinderTypeRegistry(org.springframework.cloud.stream.binder.DefaultBinderTypeRegistry) BeanCreationException(org.springframework.beans.factory.BeanCreationException) HashMap(java.util.HashMap) IOException(java.io.IOException) URL(java.net.URL) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) UrlResource(org.springframework.core.io.UrlResource) BinderType(org.springframework.cloud.stream.binder.BinderType) Bean(org.springframework.context.annotation.Bean)

Example 2 with DefaultBinderTypeRegistry

use of org.springframework.cloud.stream.binder.DefaultBinderTypeRegistry in project spring-cloud-stream by spring-cloud.

the class BinderFactoryConfiguration method binderTypeRegistry.

@Bean
@ConditionalOnMissingBean(BinderTypeRegistry.class)
public BinderTypeRegistry binderTypeRegistry(ConfigurableApplicationContext configurableApplicationContext) {
    Map<String, BinderType> binderTypes = new HashMap<>();
    ClassLoader classLoader = configurableApplicationContext.getClassLoader();
    // the above can never be null since it will default to ClassUtils.getDefaultClassLoader(..)
    try {
        Enumeration<URL> resources = classLoader.getResources("META-INF/spring.binders");
        if (!Boolean.valueOf(this.selfContained) && (resources == null || !resources.hasMoreElements())) {
            throw new BeanCreationException("Cannot create binder factory, no `META-INF/spring.binders` " + "resources found on the classpath");
        }
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            UrlResource resource = new UrlResource(url);
            for (BinderType binderType : parseBinderConfigurations(classLoader, resource)) {
                binderTypes.put(binderType.getDefaultName(), binderType);
            }
        }
    } catch (IOException | ClassNotFoundException e) {
        throw new BeanCreationException("Cannot create binder factory:", e);
    }
    return new DefaultBinderTypeRegistry(binderTypes);
}
Also used : DefaultBinderTypeRegistry(org.springframework.cloud.stream.binder.DefaultBinderTypeRegistry) BeanCreationException(org.springframework.beans.factory.BeanCreationException) HashMap(java.util.HashMap) IOException(java.io.IOException) URL(java.net.URL) UrlResource(org.springframework.core.io.UrlResource) BinderType(org.springframework.cloud.stream.binder.BinderType) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with DefaultBinderTypeRegistry

use of org.springframework.cloud.stream.binder.DefaultBinderTypeRegistry in project spring-cloud-stream by spring-cloud.

the class TestChannelBinderConfiguration method binderTypeRegistry.

@Bean
public BinderTypeRegistry binderTypeRegistry() {
    BinderType binderType = new BinderType(NAME, new Class[] { TestChannelBinderConfiguration.class });
    BinderTypeRegistry btr = new DefaultBinderTypeRegistry(Collections.singletonMap(NAME, binderType));
    return btr;
}
Also used : DefaultBinderTypeRegistry(org.springframework.cloud.stream.binder.DefaultBinderTypeRegistry) BinderType(org.springframework.cloud.stream.binder.BinderType) DefaultBinderTypeRegistry(org.springframework.cloud.stream.binder.DefaultBinderTypeRegistry) BinderTypeRegistry(org.springframework.cloud.stream.binder.BinderTypeRegistry) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

BinderType (org.springframework.cloud.stream.binder.BinderType)3 DefaultBinderTypeRegistry (org.springframework.cloud.stream.binder.DefaultBinderTypeRegistry)3 Bean (org.springframework.context.annotation.Bean)3 IOException (java.io.IOException)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 UrlResource (org.springframework.core.io.UrlResource)2 BinderTypeRegistry (org.springframework.cloud.stream.binder.BinderTypeRegistry)1