Search in sources :

Example 1 with BinderType

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

the class BinderConfigurationParsingTests method testParseTwoBindersWithMultipleClasses.

@Test
@SuppressWarnings("unchecked")
public void testParseTwoBindersWithMultipleClasses() 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," + "org.springframework.cloud.stream.binder.stub2.StubBinder2ConfigurationB";
    Resource binderConfigurationResource = new InputStreamResource(new ByteArrayInputStream(binderConfiguration.getBytes()));
    Collection<BinderType> binderConfigurations = BinderFactoryConfiguration.parseBinderConfigurations(classLoader, binderConfigurationResource);
    Assert.assertThat(binderConfigurations.size(), equalTo(2));
    Assert.assertThat(binderConfigurations, containsInAnyOrder(both(hasProperty("defaultName", equalTo("binder1"))).and(hasProperty("configurationClasses", hasItemInArray(StubBinder1Configuration.class))), both(hasProperty("defaultName", equalTo("binder2"))).and(hasProperty("configurationClasses", arrayContainingInAnyOrder(StubBinder2ConfigurationA.class, StubBinder2ConfigurationB.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 2 with BinderType

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

the class BinderConfigurationParsingTests method testParseOneBinderConfiguration.

@Test
public void testParseOneBinderConfiguration() throws Exception {
    // this is just checking that resources are passed and classes are loaded properly
    // class values used here are not binder configurations
    String oneBinderConfiguration = "binder1=org.springframework.cloud.stream.binder.stub1.StubBinder1Configuration";
    Resource resource = new InputStreamResource(new ByteArrayInputStream(oneBinderConfiguration.getBytes()));
    Collection<BinderType> binderConfigurations = BinderFactoryConfiguration.parseBinderConfigurations(classLoader, resource);
    Assert.assertNotNull(binderConfigurations);
    Assert.assertThat(binderConfigurations.size(), equalTo(1));
    Assert.assertThat(binderConfigurations, contains(both(hasProperty("defaultName", equalTo("binder1"))).and(hasProperty("configurationClasses", hasItemInArray(StubBinder1Configuration.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 3 with BinderType

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

the class BinderFactoryConfiguration method getBinderConfigurations.

private 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, true));
            }
        }
    }
    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)

Example 4 with BinderType

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

the class BinderFactoryAutoConfiguration 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) StreamFunctionProperties(org.springframework.cloud.stream.function.StreamFunctionProperties) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) Properties(java.util.Properties) BinderType(org.springframework.cloud.stream.binder.BinderType) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with BinderType

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

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