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