polishing

Resolves #1477
This commit is contained in:
Oleg Zhurakousky
2018-09-18 18:48:21 +02:00
parent 07b5a691d4
commit c8e9306bc8
5 changed files with 28 additions and 20 deletions

View File

@@ -29,6 +29,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
@@ -44,6 +45,8 @@ import org.springframework.cloud.stream.binder.PollableSource;
import org.springframework.cloud.stream.binder.ProducerProperties;
import org.springframework.cloud.stream.config.BindingServiceProperties;
import org.springframework.cloud.stream.config.MergableProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.integration.support.utils.IntegrationUtils;
import org.springframework.scheduling.TaskScheduler;
@@ -64,7 +67,7 @@ import org.springframework.validation.beanvalidation.CustomValidatorBean;
* @author Janne Valkealahti
* @author Soby Chacko
*/
public class BindingService {
public class BindingService implements ApplicationContextAware {
private final CustomValidatorBean validator;
@@ -80,24 +83,27 @@ public class BindingService {
private final BinderFactory binderFactory;
private final ConfigurableApplicationContext applicationContext;
private ConfigurableApplicationContext applicationContext;
public BindingService(
BindingServiceProperties bindingServiceProperties,
BinderFactory binderFactory) {
this(bindingServiceProperties, binderFactory, null, null);
this(bindingServiceProperties, binderFactory, null);
}
public BindingService(
BindingServiceProperties bindingServiceProperties,
BinderFactory binderFactory, TaskScheduler taskScheduler,
ConfigurableApplicationContext applicationContext) {
BinderFactory binderFactory, TaskScheduler taskScheduler) {
this.bindingServiceProperties = bindingServiceProperties;
this.binderFactory = binderFactory;
this.validator = new CustomValidatorBean();
this.validator.afterPropertiesSet();
this.taskScheduler = taskScheduler;
this.applicationContext = applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = (ConfigurableApplicationContext) applicationContext;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@@ -251,7 +257,7 @@ public class BindingService {
return binding;
}
private void handleExtendedDefaultProperties(ExtendedPropertiesBinder binder, MergableProperties extendedProperties, String filedName) {
private void handleExtendedDefaultProperties(ExtendedPropertiesBinder<?,?,?> binder, MergableProperties extendedProperties, String filedName) {
String defaultsPrefix = binder.getDefaultsPrefix();
Class<?> extendedPropertiesEntryClass = binder.getExtendedPropertiesEntryClass();

View File

@@ -47,7 +47,6 @@ import org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPost
import org.springframework.cloud.stream.function.StreamFunctionProperties;
import org.springframework.cloud.stream.micrometer.DestinationPublishingMetricsAutoConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
@@ -164,10 +163,9 @@ public class BindingServiceConfiguration {
// already exists).
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public BindingService bindingService(BindingServiceProperties bindingServiceProperties,
BinderFactory binderFactory, TaskScheduler taskScheduler,
ConfigurableApplicationContext applicationContext) {
BinderFactory binderFactory, TaskScheduler taskScheduler) {
return new BindingService(bindingServiceProperties, binderFactory, taskScheduler, applicationContext);
return new BindingService(bindingServiceProperties, binderFactory, taskScheduler);
}
@Bean

View File

@@ -35,6 +35,7 @@ import org.springframework.util.ObjectUtils;
*
* @author Oleg Zhurakousky
* @author Soby Chacko
*
* @see BinderProperties
* @see ProducerProperties
* @see ConsumerProperties
@@ -103,19 +104,19 @@ public interface MergableProperties {
}
default boolean isEmptyMapAtDestination(Object v) {
return Map.class.isAssignableFrom(v.getClass()) && CollectionUtils.isEmpty((Map) v);
return Map.class.isAssignableFrom(v.getClass()) && CollectionUtils.isEmpty((Map<?,?>) v);
}
default boolean isMergableByMap(Object v) {
return (Map.class.isAssignableFrom(v.getClass()) && !CollectionUtils.isEmpty((Map) v));
return (Map.class.isAssignableFrom(v.getClass()) && !CollectionUtils.isEmpty((Map<?,?>) v));
}
@SuppressWarnings("unchecked")
default void handleMapMerging(Object value, Object v) {
if (value instanceof Map) {
Map<Object, Object> sourceMap = (Map) value;
Map<Object, Object> sourceMap = (Map<Object, Object>) value;
for (Object key : sourceMap.keySet()) {
Map<Object, Object> targetMap = (Map) v;
Map<Object, Object> targetMap = (Map<Object, Object>) v;
if (!targetMap.containsKey(key)) {
targetMap.put(key, sourceMap.get(key));
}
@@ -124,6 +125,6 @@ public interface MergableProperties {
}
default void copyProperties(Object source, Object target) throws BeansException {
// noop
}
}

View File

@@ -379,7 +379,8 @@ public class BindingServiceTests {
environment.getPropertySources().addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd));
applicationContext.setEnvironment(environment);
BindingService service = new BindingService(serviceProperties, binderFactory, null, applicationContext);
BindingService service = new BindingService(serviceProperties, binderFactory, null);
service.setApplicationContext(applicationContext);
MessageChannel outputChannel = new DirectChannel();
Binder<MessageChannel, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
@@ -413,7 +414,8 @@ public class BindingServiceTests {
environment.getPropertySources().addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd));
applicationContext.setEnvironment(environment);
BindingService service = new BindingService(serviceProperties, binderFactory, null, applicationContext);
BindingService service = new BindingService(serviceProperties, binderFactory, null);
service.setApplicationContext(applicationContext);
MessageChannel inputChannel = new DirectChannel();
Binder<MessageChannel, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
@@ -533,7 +535,7 @@ public class BindingServiceTests {
Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.initialize();
BindingService service = new BindingService(properties, binderFactory, scheduler, null);
BindingService service = new BindingService(properties, binderFactory, scheduler);
MessageChannel inputChannel = new DirectChannel();
final Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
final CountDownLatch fail = new CountDownLatch(2);
@@ -576,7 +578,7 @@ public class BindingServiceTests {
Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.initialize();
BindingService service = new BindingService(properties, binderFactory, scheduler, null);
BindingService service = new BindingService(properties, binderFactory, scheduler);
MessageChannel outputChannel = new DirectChannel();
final Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
final CountDownLatch fail = new CountDownLatch(2);

View File

@@ -31,6 +31,7 @@ import static org.mockito.Mockito.when;
@Configuration
public class MockExtendedBinderConfiguration {
@SuppressWarnings("rawtypes")
@Bean
public Binder<?, ?, ?> extendedPropertiesBinder() {
Binder mock = Mockito.mock(Binder.class, Mockito.withSettings().defaultAnswer(Mockito.RETURNS_MOCKS)