You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zr...@apache.org on 2018/07/25 19:10:26 UTC

[camel] branch master updated: CAMEL-12682: add support for auto discovery of ...

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


The following commit(s) were added to refs/heads/master by this push:
     new b62825f  CAMEL-12682: add support for auto discovery of ...
b62825f is described below

commit b62825f404520ff006e058141419bdb541e2e794
Author: Zoran Regvart <zr...@apache.org>
AuthorDate: Wed Jul 25 21:09:42 2018 +0200

    CAMEL-12682: add support for auto discovery of ...
    
    ...Registry beans
    
    This adds support for Spring beans of type `Registry` to be present in
    the `ApplicationContext` and replace the default
    `ApplicationContextRegistry`.
    
    Multiple `Registry`-ies can be present in the `ApplicationContext` and
    order preference can be expressed with standard Spring `Ordered`
    contract.
    
    This allows having a custom `Registry` and `ApplicationContextRegistry`
    in the same `ApplicationContext` if needed.
---
 .../camel/spring/boot/CamelAutoConfiguration.java  | 17 +++++++++++++
 .../spring/boot/CamelAutoConfigurationTest.java    | 29 ++++++++++++++++++++++
 2 files changed, 46 insertions(+)

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 2388cb5..811f3a4 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
@@ -19,6 +19,7 @@ package org.apache.camel.spring.boot;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.function.Consumer;
@@ -37,6 +38,8 @@ import org.apache.camel.component.properties.PropertiesParser;
 import org.apache.camel.health.HealthCheckRegistry;
 import org.apache.camel.health.HealthCheckRepository;
 import org.apache.camel.health.HealthCheckService;
+import org.apache.camel.impl.CompositeRegistry;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.FileWatcherReloadStrategy;
 import org.apache.camel.processor.interceptor.BacklogTracer;
 import org.apache.camel.processor.interceptor.DefaultTraceFormatter;
@@ -54,6 +57,7 @@ import org.apache.camel.spi.LifecycleStrategy;
 import org.apache.camel.spi.LogListener;
 import org.apache.camel.spi.ManagementNamingStrategy;
 import org.apache.camel.spi.ManagementStrategy;
+import org.apache.camel.spi.Registry;
 import org.apache.camel.spi.ReloadStrategy;
 import org.apache.camel.spi.RouteController;
 import org.apache.camel.spi.RoutePolicyFactory;
@@ -77,6 +81,7 @@ import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Import;
 import org.springframework.context.annotation.Lazy;
+import org.springframework.core.OrderComparator;
 import org.springframework.core.env.ConfigurableEnvironment;
 import org.springframework.core.env.Environment;
 import org.springframework.core.env.MutablePropertySources;
@@ -118,6 +123,18 @@ public class CamelAutoConfiguration {
                                          CamelContext camelContext,
                                          CamelConfigurationProperties config) throws Exception {
 
+        if (camelContext instanceof DefaultCamelContext) {
+            final DefaultCamelContext defaultContext = (DefaultCamelContext) camelContext;
+            final Map<String, Registry> registryBeans = applicationContext.getBeansOfType(Registry.class);
+
+            if (!registryBeans.isEmpty()) {
+                final List<Registry> registries = new ArrayList<>(registryBeans.values());
+                OrderComparator.sort(registries);
+                final Registry registry = new CompositeRegistry(registries);
+                defaultContext.setRegistry(registry);
+            }
+        }
+
         if (ObjectHelper.isNotEmpty(config.getFileConfigurations())) {
             Environment env = applicationContext.getEnvironment();
             if (env instanceof ConfigurableEnvironment) {
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java
index 624dfba..6dd5295 100644
--- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java
@@ -24,6 +24,8 @@ import org.apache.camel.Route;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.Registry;
+import org.assertj.core.api.Assertions;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -32,11 +34,14 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.Ordered;
 import org.springframework.test.annotation.DirtiesContext;
 import org.springframework.test.context.junit4.SpringRunner;
 
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
 
 @DirtiesContext
 @RunWith(SpringRunner.class)
@@ -177,6 +182,12 @@ public class CamelAutoConfigurationTest extends Assert {
         assertEquals(camelContext.getExecutorServiceManager().getThreadNamePattern(), "customThreadName #counter#");
     }
 
+    @Test
+    public void shouldComposeRegistries() {
+        final Registry registry = camelContext.getRegistry();
+        Assertions.assertThat(registry.lookupByName("bean")).isEqualTo(Ordered.HIGHEST_PRECEDENCE);
+    }
+
     @Configuration
     public static class TestConfig {
         // Constants
@@ -197,5 +208,23 @@ public class CamelAutoConfigurationTest extends Assert {
         CamelContextConfiguration camelContextConfiguration() {
             return mock(CamelContextConfiguration.class);
         }
+
+        @Bean
+        Registry customRegistry1() {
+            return mockRegistryWithBeanValueAndOrder(Ordered.LOWEST_PRECEDENCE);
+        }
+
+        @Bean
+        Registry customRegistry2() {
+            return mockRegistryWithBeanValueAndOrder(Ordered.HIGHEST_PRECEDENCE);
+        }
+
+        private Registry mockRegistryWithBeanValueAndOrder(int value) {
+            final Registry registry = mock(Registry.class, withSettings().extraInterfaces(Ordered.class));
+            when(registry.lookupByName("bean")).thenReturn(value);
+            when(((Ordered) registry).getOrder()).thenReturn(value);
+            return registry;
+        }
+
     }
 }
\ No newline at end of file