Search in sources :

Example 6 with BinderType

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

the class BinderFactoryConfiguration method parseBinderConfigurations.

static Collection<BinderType> parseBinderConfigurations(ClassLoader classLoader, Resource resource) throws IOException, ClassNotFoundException {
    Properties properties = PropertiesLoaderUtils.loadProperties(resource);
    Collection<BinderType> parsedBinderConfigurations = new ArrayList<>();
    for (Map.Entry<?, ?> entry : properties.entrySet()) {
        String binderType = (String) entry.getKey();
        String[] binderConfigurationClassNames = StringUtils.commaDelimitedListToStringArray((String) entry.getValue());
        Class<?>[] binderConfigurationClasses = new Class[binderConfigurationClassNames.length];
        int i = 0;
        for (String binderConfigurationClassName : binderConfigurationClassNames) {
            binderConfigurationClasses[i++] = ClassUtils.forName(binderConfigurationClassName, classLoader);
        }
        parsedBinderConfigurations.add(new BinderType(binderType, binderConfigurationClasses));
    }
    return parsedBinderConfigurations;
}
Also used : ArrayList(java.util.ArrayList) Properties(java.util.Properties) BinderType(org.springframework.cloud.stream.binder.BinderType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with BinderType

use of org.springframework.cloud.stream.binder.BinderType 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 8 with BinderType

use of org.springframework.cloud.stream.binder.BinderType 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)

Example 9 with BinderType

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

the class BinderConfigurationParsingTests method testParseTwoBindersConfigurations.

@SuppressWarnings("unchecked")
@Test
public void testParseTwoBindersConfigurations() throws Exception {
    // this is just checking that resources are passed and classes are loaded properly
    // class values used here are not binder configurations
    String binderConfiguration = "binder1=org.springframework.cloud.stream.binder.stub1.StubBinder1Configuration\n" + "binder2=org.springframework.cloud.stream.binder.stub2.StubBinder2ConfigurationA";
    Resource twoBinderConfigurationResource = new InputStreamResource(new ByteArrayInputStream(binderConfiguration.getBytes()));
    Collection<BinderType> twoBinderConfigurations = BinderFactoryConfiguration.parseBinderConfigurations(classLoader, twoBinderConfigurationResource);
    Assert.assertThat(twoBinderConfigurations.size(), equalTo(2));
    Assert.assertThat(twoBinderConfigurations, containsInAnyOrder(both(hasProperty("defaultName", equalTo("binder1"))).and(hasProperty("configurationClasses", hasItemInArray(StubBinder1Configuration.class))), both(hasProperty("defaultName", equalTo("binder2"))).and(hasProperty("configurationClasses", hasItemInArray(StubBinder2ConfigurationA.class)))));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) BinderType(org.springframework.cloud.stream.binder.BinderType) InputStreamResource(org.springframework.core.io.InputStreamResource) Test(org.junit.Test)

Example 10 with BinderType

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

the class BindingServiceConfiguration method getBinderConfigurations.

private static Map<String, BinderConfiguration> getBinderConfigurations(BinderTypeRegistry binderTypeRegistry, BindingServiceProperties bindingServiceProperties) {
    Map<String, BinderConfiguration> binderConfigurations = new HashMap<>();
    Map<String, BinderProperties> declaredBinders = bindingServiceProperties.getBinders();
    boolean defaultCandidatesExist = false;
    Iterator<Map.Entry<String, BinderProperties>> binderPropertiesIterator = declaredBinders.entrySet().iterator();
    while (!defaultCandidatesExist && binderPropertiesIterator.hasNext()) {
        defaultCandidatesExist = binderPropertiesIterator.next().getValue().isDefaultCandidate();
    }
    List<String> existingBinderConfigurations = new ArrayList<>();
    for (Map.Entry<String, BinderProperties> binderEntry : declaredBinders.entrySet()) {
        BinderProperties binderProperties = binderEntry.getValue();
        if (binderTypeRegistry.get(binderEntry.getKey()) != null) {
            binderConfigurations.put(binderEntry.getKey(), new BinderConfiguration(binderEntry.getKey(), binderProperties.getEnvironment(), binderProperties.isInheritEnvironment(), binderProperties.isDefaultCandidate()));
            existingBinderConfigurations.add(binderEntry.getKey());
        } else {
            Assert.hasText(binderProperties.getType(), "No 'type' property present for custom binder " + binderEntry.getKey());
            binderConfigurations.put(binderEntry.getKey(), new BinderConfiguration(binderProperties.getType(), binderProperties.getEnvironment(), binderProperties.isInheritEnvironment(), binderProperties.isDefaultCandidate()));
            existingBinderConfigurations.add(binderEntry.getKey());
        }
    }
    for (Map.Entry<String, BinderConfiguration> configurationEntry : binderConfigurations.entrySet()) {
        if (configurationEntry.getValue().isDefaultCandidate()) {
            defaultCandidatesExist = true;
        }
    }
    if (!defaultCandidatesExist) {
        for (Map.Entry<String, BinderType> binderEntry : binderTypeRegistry.getAll().entrySet()) {
            if (!existingBinderConfigurations.contains(binderEntry.getKey())) {
                binderConfigurations.put(binderEntry.getKey(), new BinderConfiguration(binderEntry.getKey(), new HashMap<>(), true, !"integration".equals(binderEntry.getKey())));
            }
        }
    }
    return binderConfigurations;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BinderConfiguration(org.springframework.cloud.stream.binder.BinderConfiguration) HashMap(java.util.HashMap) Map(java.util.Map) BinderType(org.springframework.cloud.stream.binder.BinderType)

Aggregations

BinderType (org.springframework.cloud.stream.binder.BinderType)10 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Test (org.junit.Test)3 DefaultBinderTypeRegistry (org.springframework.cloud.stream.binder.DefaultBinderTypeRegistry)3 Bean (org.springframework.context.annotation.Bean)3 InputStreamResource (org.springframework.core.io.InputStreamResource)3 Resource (org.springframework.core.io.Resource)3 IOException (java.io.IOException)2 URL (java.net.URL)2 Properties (java.util.Properties)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 BinderConfiguration (org.springframework.cloud.stream.binder.BinderConfiguration)2 UrlResource (org.springframework.core.io.UrlResource)2 EnableConfigurationProperties (org.springframework.boot.context.properties.EnableConfigurationProperties)1 BinderTypeRegistry (org.springframework.cloud.stream.binder.BinderTypeRegistry)1 StreamFunctionProperties (org.springframework.cloud.stream.function.StreamFunctionProperties)1