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)))));
}
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)))));
}
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;
}
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;
}
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);
}
Aggregations