This is an automated email from the ASF dual-hosted git repository. zregvart pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit abc340506ea584b153ab2b9ac21531d01593500a Author: Zoran Regvart <zregv...@apache.org> AuthorDate: Thu Jun 7 14:30:34 2018 +0200 Refactor spring-boot CamelAutoConfiguration This refactors spring-boot CamelAutoConfiguration to use helper methods in order to reduce the amount of duplicated code. --- .../camel/spring/boot/CamelAutoConfiguration.java | 250 +++++++-------------- 1 file changed, 85 insertions(+), 165 deletions(-) diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java index 95cb392..2388cb5 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java @@ -21,6 +21,8 @@ import java.util.Collection; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.function.Consumer; +import java.util.function.Predicate; import org.apache.camel.CamelContext; import org.apache.camel.ConsumerTemplate; @@ -361,120 +363,36 @@ public class CamelAutoConfiguration { LOG.info("Using custom Tracer: {}", tracer); camelContext.addInterceptStrategy(tracer); } - BacklogTracer backlogTracer = getSingleBeanOfType(applicationContext, BacklogTracer.class); - if (backlogTracer != null) { - LOG.info("Using custom BacklogTracer: {}", backlogTracer); - camelContext.addInterceptStrategy(backlogTracer); - } - HandleFault handleFault = getSingleBeanOfType(applicationContext, HandleFault.class); - if (handleFault != null) { - LOG.info("Using custom HandleFault: {}", handleFault); - camelContext.addInterceptStrategy(handleFault); - } - InflightRepository inflightRepository = getSingleBeanOfType(applicationContext, InflightRepository.class); - if (inflightRepository != null) { - LOG.info("Using custom InflightRepository: {}", inflightRepository); - camelContext.setInflightRepository(inflightRepository); - } - AsyncProcessorAwaitManager asyncProcessorAwaitManager = getSingleBeanOfType(applicationContext, AsyncProcessorAwaitManager.class); - if (asyncProcessorAwaitManager != null) { - LOG.info("Using custom AsyncProcessorAwaitManager: {}", asyncProcessorAwaitManager); - camelContext.setAsyncProcessorAwaitManager(asyncProcessorAwaitManager); - } - ManagementStrategy managementStrategy = getSingleBeanOfType(applicationContext, ManagementStrategy.class); - if (managementStrategy != null) { - LOG.info("Using custom ManagementStrategy: {}", managementStrategy); - camelContext.setManagementStrategy(managementStrategy); - } - ManagementNamingStrategy managementNamingStrategy = getSingleBeanOfType(applicationContext, ManagementNamingStrategy.class); - if (managementNamingStrategy != null) { - LOG.info("Using custom ManagementNamingStrategy: {}", managementNamingStrategy); - camelContext.getManagementStrategy().setManagementNamingStrategy(managementNamingStrategy); - } - EventFactory eventFactory = getSingleBeanOfType(applicationContext, EventFactory.class); - if (eventFactory != null) { - LOG.info("Using custom EventFactory: {}", eventFactory); - camelContext.getManagementStrategy().setEventFactory(eventFactory); - } - UnitOfWorkFactory unitOfWorkFactory = getSingleBeanOfType(applicationContext, UnitOfWorkFactory.class); - if (unitOfWorkFactory != null) { - LOG.info("Using custom UnitOfWorkFactory: {}", unitOfWorkFactory); - camelContext.setUnitOfWorkFactory(unitOfWorkFactory); - } - RuntimeEndpointRegistry runtimeEndpointRegistry = getSingleBeanOfType(applicationContext, RuntimeEndpointRegistry.class); - if (runtimeEndpointRegistry != null) { - LOG.info("Using custom RuntimeEndpointRegistry: {}", runtimeEndpointRegistry); - camelContext.setRuntimeEndpointRegistry(runtimeEndpointRegistry); - } - // custom type converters defined as <bean>s - Map<String, TypeConverters> typeConverters = applicationContext.getBeansOfType(TypeConverters.class); - if (typeConverters != null && !typeConverters.isEmpty()) { - for (Map.Entry<String, TypeConverters> entry : typeConverters.entrySet()) { - TypeConverters converter = entry.getValue(); - LOG.info("Adding custom TypeConverters with id: {} and implementation: {}", entry.getKey(), converter); - camelContext.getTypeConverterRegistry().addTypeConverters(converter); - } - } - // set the event notifier strategies if defined - Map<String, EventNotifier> eventNotifiers = applicationContext.getBeansOfType(EventNotifier.class); - if (eventNotifiers != null && !eventNotifiers.isEmpty()) { - for (Map.Entry<String, EventNotifier> entry : eventNotifiers.entrySet()) { - EventNotifier notifier = entry.getValue(); - // do not add if already added, for instance a tracer that is also an InterceptStrategy class - if (!camelContext.getManagementStrategy().getEventNotifiers().contains(notifier)) { - LOG.info("Using custom EventNotifier with id: {} and implementation: {}", entry.getKey(), notifier); - camelContext.getManagementStrategy().addEventNotifier(notifier); - } - } - } - // set endpoint strategies if defined - Map<String, EndpointStrategy> endpointStrategies = applicationContext.getBeansOfType(EndpointStrategy.class); - if (endpointStrategies != null && !endpointStrategies.isEmpty()) { - for (Map.Entry<String, EndpointStrategy> entry : endpointStrategies.entrySet()) { - EndpointStrategy strategy = entry.getValue(); - LOG.info("Using custom EndpointStrategy with id: {} and implementation: {}", entry.getKey(), strategy); - camelContext.addRegisterEndpointCallback(strategy); - } - } - // shutdown - ShutdownStrategy shutdownStrategy = getSingleBeanOfType(applicationContext, ShutdownStrategy.class); - if (shutdownStrategy != null) { - LOG.info("Using custom ShutdownStrategy: " + shutdownStrategy); - camelContext.setShutdownStrategy(shutdownStrategy); - } - // add global interceptors - Map<String, InterceptStrategy> interceptStrategies = applicationContext.getBeansOfType(InterceptStrategy.class); - if (interceptStrategies != null && !interceptStrategies.isEmpty()) { - for (Map.Entry<String, InterceptStrategy> entry : interceptStrategies.entrySet()) { - InterceptStrategy strategy = entry.getValue(); - // do not add if already added, for instance a tracer that is also an InterceptStrategy class - if (!camelContext.getInterceptStrategies().contains(strategy)) { - LOG.info("Using custom InterceptStrategy with id: {} and implementation: {}", entry.getKey(), strategy); - camelContext.addInterceptStrategy(strategy); - } - } - } - // set the lifecycle strategy if defined - Map<String, LifecycleStrategy> lifecycleStrategies = applicationContext.getBeansOfType(LifecycleStrategy.class); - if (lifecycleStrategies != null && !lifecycleStrategies.isEmpty()) { - for (Map.Entry<String, LifecycleStrategy> entry : lifecycleStrategies.entrySet()) { - LifecycleStrategy strategy = entry.getValue(); - // do not add if already added, for instance a tracer that is also an InterceptStrategy class - if (!camelContext.getLifecycleStrategies().contains(strategy)) { - LOG.info("Using custom LifecycleStrategy with id: {} and implementation: {}", entry.getKey(), strategy); - camelContext.addLifecycleStrategy(strategy); - } - } - } - // cluster service - Map<String, CamelClusterService> clusterServices = applicationContext.getBeansOfType(CamelClusterService.class); - if (clusterServices != null && !clusterServices.isEmpty()) { - for (Map.Entry<String, CamelClusterService> entry : clusterServices.entrySet()) { - CamelClusterService service = entry.getValue(); - LOG.info("Using CamelClusterService with id: {} and implementation: {}", service.getId(), service); - camelContext.addService(service); - } - } + + final ManagementStrategy managementStrategy = camelContext.getManagementStrategy(); + + registerPropertyForBeanType(applicationContext, BacklogTracer.class, camelContext::addInterceptStrategy); + registerPropertyForBeanType(applicationContext, HandleFault.class, camelContext::addInterceptStrategy); + registerPropertyForBeanType(applicationContext, InflightRepository.class, camelContext::setInflightRepository); + registerPropertyForBeanType(applicationContext, AsyncProcessorAwaitManager.class, camelContext::setAsyncProcessorAwaitManager); + registerPropertyForBeanType(applicationContext, ManagementStrategy.class, camelContext::setManagementStrategy); + registerPropertyForBeanType(applicationContext, ManagementNamingStrategy.class, managementStrategy::setManagementNamingStrategy); + registerPropertyForBeanType(applicationContext, EventFactory.class, managementStrategy::setEventFactory); + registerPropertyForBeanType(applicationContext, UnitOfWorkFactory.class, camelContext::setUnitOfWorkFactory); + registerPropertyForBeanType(applicationContext, RuntimeEndpointRegistry.class, camelContext::setRuntimeEndpointRegistry); + + registerPropertiesForBeanTypes(applicationContext, TypeConverters.class, camelContext.getTypeConverterRegistry()::addTypeConverters); + + final Predicate<EventNotifier> containsEventNotifier = managementStrategy.getEventNotifiers()::contains; + registerPropertiesForBeanTypesWithCondition(applicationContext, EventNotifier.class, containsEventNotifier.negate(), managementStrategy::addEventNotifier); + + registerPropertiesForBeanTypes(applicationContext, EndpointStrategy.class, camelContext::addRegisterEndpointCallback); + + registerPropertyForBeanType(applicationContext, ShutdownStrategy.class, camelContext::setShutdownStrategy); + + final Predicate<InterceptStrategy> containsInterceptStrategy = camelContext.getInterceptStrategies()::contains; + registerPropertiesForBeanTypesWithCondition(applicationContext, InterceptStrategy.class, containsInterceptStrategy.negate(), camelContext::addInterceptStrategy); + + final Predicate<LifecycleStrategy> containsLifecycleStrategy = camelContext.getLifecycleStrategies()::contains; + registerPropertiesForBeanTypesWithCondition(applicationContext, LifecycleStrategy.class, containsLifecycleStrategy.negate(), camelContext::addLifecycleStrategy); + + registerPropertiesForBeanTypes(applicationContext, CamelClusterService.class, addServiceToContext(camelContext)); + // service registry Map<String, ServiceRegistry> serviceRegistries = applicationContext.getBeansOfType(ServiceRegistry.class); if (serviceRegistries != null && !serviceRegistries.isEmpty()) { @@ -489,15 +407,9 @@ public class CamelAutoConfiguration { camelContext.addService(service); } } - // add route policy factories - Map<String, RoutePolicyFactory> routePolicyFactories = applicationContext.getBeansOfType(RoutePolicyFactory.class); - if (routePolicyFactories != null && !routePolicyFactories.isEmpty()) { - for (Map.Entry<String, RoutePolicyFactory> entry : routePolicyFactories.entrySet()) { - RoutePolicyFactory factory = entry.getValue(); - LOG.info("Using custom RoutePolicyFactory with id: {} and implementation: {}", entry.getKey(), factory); - camelContext.addRoutePolicyFactory(factory); - } - } + + registerPropertiesForBeanTypes(applicationContext, RoutePolicyFactory.class, camelContext::addRoutePolicyFactory); + // add SSL context parameters GlobalSSLContextParametersSupplier sslContextParametersSupplier = getSingleBeanOfType(applicationContext, GlobalSSLContextParametersSupplier.class); if (sslContextParametersSupplier != null) { @@ -513,54 +425,62 @@ public class CamelAutoConfiguration { healthCheckRegistry = camelContext.getHealthCheckRegistry(); healthCheckRegistry.setCamelContext(camelContext); } - // Health check repository - Map<String, HealthCheckRepository> repositories = applicationContext.getBeansOfType(HealthCheckRepository.class); - if (ObjectHelper.isNotEmpty(repositories)) { - for (HealthCheckRepository repository: repositories.values()) { - healthCheckRegistry.addRepository(repository); - } - } - // Health check service - HealthCheckService healthCheckService = getSingleBeanOfType(applicationContext, HealthCheckService.class); - if (healthCheckService != null) { - LOG.info("Using HealthCheckService: {}", healthCheckService); - camelContext.addService(healthCheckService); - } - // Route controller - RouteController routeController = getSingleBeanOfType(applicationContext, RouteController.class); - if (routeController != null) { - LOG.info("Using RouteController: " + routeController); - camelContext.setRouteController(routeController); - } - // UuidGenerator - UuidGenerator uuidGenerator = getSingleBeanOfType(applicationContext, UuidGenerator.class); - if (uuidGenerator != null) { - LOG.info("Using custom UuidGenerator: {}", uuidGenerator); - camelContext.setUuidGenerator(uuidGenerator); - } - // LogListener - Map<String, LogListener> logListeners = applicationContext.getBeansOfType(LogListener.class); - if (logListeners != null && !logListeners.isEmpty()) { - for (Map.Entry<String, LogListener> entry : logListeners.entrySet()) { - LogListener logListener = entry.getValue(); - if (!camelContext.getLogListeners().contains(logListener)) { - LOG.info("Using custom LogListener with id: {} and implementation: {}", entry.getKey(), logListener); - camelContext.addLogListener(logListener); - } - } - } - // ExecutorServiceManager - final ExecutorServiceManager executorServiceManager = getSingleBeanOfType(applicationContext, ExecutorServiceManager.class); - if (executorServiceManager != null) { - LOG.info("Using custom ExecutorServiceManager: {}", executorServiceManager); - camelContext.setExecutorServiceManager(executorServiceManager); - } + registerPropertiesForBeanTypes(applicationContext, HealthCheckRepository.class, healthCheckRegistry::addRepository); + + registerPropertyForBeanType(applicationContext, HealthCheckService.class, addServiceToContext(camelContext)); + registerPropertyForBeanType(applicationContext, RouteController.class, camelContext::setRouteController); + registerPropertyForBeanType(applicationContext, UuidGenerator.class, camelContext::setUuidGenerator); + + final Predicate<LogListener> containsLogListener = camelContext.getLogListeners()::contains; + registerPropertiesForBeanTypesWithCondition(applicationContext, LogListener.class, containsLogListener.negate(), camelContext::addLogListener); + + registerPropertyForBeanType(applicationContext, ExecutorServiceManager.class, camelContext::setExecutorServiceManager); // set the default thread pool profile if defined initThreadPoolProfiles(applicationContext, camelContext); } + private static <T> Consumer<T> addServiceToContext(final CamelContext camelContext) { + return service -> { + try { + camelContext.addService(service); + } catch (Exception e) { + throw new RuntimeException("Unable to add service to Camel context", e); + } + }; + } + + private static <T> void registerPropertyForBeanType(final ApplicationContext applicationContext, final Class<T> beanType, final Consumer<T> propertySetter) { + T propertyBean = getSingleBeanOfType(applicationContext, beanType); + if (propertyBean == null) { + return; + } + + LOG.info("Using custom {}: {}", beanType.getSimpleName(), propertyBean); + propertySetter.accept(propertyBean); + } + + private static <T> void registerPropertiesForBeanTypes(final ApplicationContext applicationContext, final Class<T> beanType, final Consumer<T> propertySetter) { + registerPropertiesForBeanTypesWithCondition(applicationContext, beanType, b -> true, propertySetter); + } + + private static <T> void registerPropertiesForBeanTypesWithCondition(final ApplicationContext applicationContext, final Class<T> beanType, final Predicate<T> condition, + final Consumer<T> propertySetter) { + final Map<String, T> beans = applicationContext.getBeansOfType(beanType); + if (!ObjectHelper.isNotEmpty(beans)) { + return; + } + + final String simpleName = beanType.getSimpleName(); + beans.forEach((name, bean) -> { + if (condition.test(bean)) { + LOG.info("Adding custom {} with id: {} and implementation: {}", simpleName, name, bean); + propertySetter.accept(bean); + } + }); + } + private static void initThreadPoolProfiles(ApplicationContext applicationContext, CamelContext camelContext) { Set<String> defaultIds = new HashSet<>(); -- To stop receiving notification emails like this one, please contact zregv...@apache.org.