Repository: camel Updated Branches: refs/heads/master ea3a02e97 -> 365ac51a0
CAMEL-11084: camel-dns : make it easy to configure components and service-discovery in spring-boot Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/46dc46b0 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/46dc46b0 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/46dc46b0 Branch: refs/heads/master Commit: 46dc46b06ce537e9d59f3a6bbdfa387e6cc809e2 Parents: d122dad Author: lburgazzoli <[email protected]> Authored: Tue Mar 28 16:33:44 2017 +0200 Committer: lburgazzoli <[email protected]> Committed: Tue Mar 28 17:06:47 2017 +0200 ---------------------------------------------------------------------- ...CallServiceDiscoveryConfigurationCommon.java | 66 ++++++++++++++++++++ ...ServiceDiscoveryConfigurationProperties.java | 48 ++++++++++++++ .../cloud/DnsCloudAutoConfiguration.java | 15 +++-- .../springboot/cloud/DnsCloudConfiguration.java | 53 ---------------- .../cloud/DnsServiceDiscoveryDisabledTest.java | 64 +++++++++++++++++++ .../cloud/DnsServiceDiscoveryEnabledTest.java | 65 +++++++++++++++++++ .../src/test/resources/logback.xml | 39 ++++++++++++ .../SpringBootAutoConfigurationMojo.java | 20 ++++-- 8 files changed, 305 insertions(+), 65 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/46dc46b0/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/cloud/springboot/DnsServiceCallServiceDiscoveryConfigurationCommon.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/cloud/springboot/DnsServiceCallServiceDiscoveryConfigurationCommon.java b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/cloud/springboot/DnsServiceCallServiceDiscoveryConfigurationCommon.java new file mode 100644 index 0000000..8515557 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/cloud/springboot/DnsServiceCallServiceDiscoveryConfigurationCommon.java @@ -0,0 +1,66 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.model.cloud.springboot; + +import java.util.List; +import org.apache.camel.model.PropertyDefinition; + +/** + * Generated by camel-package-maven-plugin - do not edit this file! + */ +public class DnsServiceCallServiceDiscoveryConfigurationCommon { + + /** + * The transport protocol of the desired service. + */ + private String proto = "_tcp"; + /** + * The domain name; + */ + private String domain; + /** + * Set client properties to use. These properties are specific to what + * service call implementation are in use. For example if using ribbon then + * the client properties are define in + * com.netflix.client.config.CommonClientConfigKey. + */ + private List<PropertyDefinition> properties; + + public String getProto() { + return proto; + } + + public void setProto(String proto) { + this.proto = proto; + } + + public String getDomain() { + return domain; + } + + public void setDomain(String domain) { + this.domain = domain; + } + + public List<PropertyDefinition> getProperties() { + return properties; + } + + public void setProperties(List<PropertyDefinition> properties) { + this.properties = properties; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/46dc46b0/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/cloud/springboot/DnsServiceCallServiceDiscoveryConfigurationProperties.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/cloud/springboot/DnsServiceCallServiceDiscoveryConfigurationProperties.java b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/cloud/springboot/DnsServiceCallServiceDiscoveryConfigurationProperties.java new file mode 100644 index 0000000..76f9c7a --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/model/cloud/springboot/DnsServiceCallServiceDiscoveryConfigurationProperties.java @@ -0,0 +1,48 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.model.cloud.springboot; + +import java.util.HashMap; +import java.util.Map; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "camel.cloud.dns.service-discovery") +public class DnsServiceCallServiceDiscoveryConfigurationProperties + extends + DnsServiceCallServiceDiscoveryConfigurationCommon { + + /** + * Enable the component + */ + private boolean enabled = true; + /** + * Define additional configuration definitions + */ + private Map<String, DnsServiceCallServiceDiscoveryConfigurationCommon> configurations = new HashMap<>(); + + public Map<String, DnsServiceCallServiceDiscoveryConfigurationCommon> getConfigurations() { + return configurations; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/46dc46b0/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java index 57fbed3..b0ee4c0 100644 --- a/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java @@ -18,13 +18,13 @@ package org.apache.camel.component.dns.springboot.cloud; import java.util.HashMap; import java.util.Map; - import javax.annotation.PostConstruct; import org.apache.camel.CamelContext; import org.apache.camel.cloud.ServiceDiscovery; -import org.apache.camel.component.dns.DnsConfiguration; import org.apache.camel.component.dns.cloud.DnsServiceDiscoveryFactory; +import org.apache.camel.model.cloud.springboot.DnsServiceCallServiceDiscoveryConfigurationCommon; +import org.apache.camel.model.cloud.springboot.DnsServiceCallServiceDiscoveryConfigurationProperties; import org.apache.camel.spring.boot.CamelAutoConfiguration; import org.apache.camel.spring.boot.util.GroupCondition; import org.apache.camel.util.IntrospectionSupport; @@ -44,12 +44,12 @@ import org.springframework.context.annotation.Lazy; @ConditionalOnBean(CamelAutoConfiguration.class) @Conditional(DnsCloudAutoConfiguration.Condition.class) @AutoConfigureAfter(CamelAutoConfiguration.class) -@EnableConfigurationProperties(DnsCloudConfiguration.class) +@EnableConfigurationProperties(DnsServiceCallServiceDiscoveryConfigurationProperties.class) public class DnsCloudAutoConfiguration { @Autowired private CamelContext camelContext; @Autowired - private DnsCloudConfiguration configuration; + private DnsServiceCallServiceDiscoveryConfigurationProperties configuration; @Autowired private ConfigurableBeanFactory beanFactory; @@ -71,10 +71,9 @@ public class DnsCloudAutoConfiguration { @PostConstruct public void postConstruct() { if (beanFactory != null) { - DnsCloudConfiguration.ServiceDiscoveryConfiguration discovery = configuration.getServiceDiscovery(); Map<String, Object> parameters = new HashMap<>(); - for (Map.Entry<String, DnsConfiguration> entry : discovery.getConfigurations().entrySet()) { + for (Map.Entry<String, DnsServiceCallServiceDiscoveryConfigurationCommon> entry : configuration.getConfigurations().entrySet()) { // clean up params parameters.clear(); @@ -100,8 +99,8 @@ public class DnsCloudAutoConfiguration { public static class Condition extends GroupCondition { public Condition() { super( - "camel.cloud", - "camel.cloud.dns" + "camel.cloud.dns", + "camel.cloud.dns.service-discovery" ); } } http://git-wip-us.apache.org/repos/asf/camel/blob/46dc46b0/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudConfiguration.java b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudConfiguration.java deleted file mode 100644 index 789315e..0000000 --- a/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudConfiguration.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.dns.springboot.cloud; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.camel.component.dns.DnsConfiguration; -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties(prefix = "camel.cloud.dns") -public class DnsCloudConfiguration { - private boolean enabled = true; - private ServiceDiscoveryConfiguration serviceDiscovery = new ServiceDiscoveryConfiguration(); - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public ServiceDiscoveryConfiguration getServiceDiscovery() { - return serviceDiscovery; - } - - // ************************************************************************* - // - // ************************************************************************* - - public class ServiceDiscoveryConfiguration extends DnsConfiguration { - private final Map<String, DnsConfiguration> configurations = new HashMap<>(); - - public Map<String, DnsConfiguration> getConfigurations() { - return configurations; - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/46dc46b0/platforms/spring-boot/components-starter/camel-dns-starter/src/test/java/org/apache/camel/component/dns/springboot/cloud/DnsServiceDiscoveryDisabledTest.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-dns-starter/src/test/java/org/apache/camel/component/dns/springboot/cloud/DnsServiceDiscoveryDisabledTest.java b/platforms/spring-boot/components-starter/camel-dns-starter/src/test/java/org/apache/camel/component/dns/springboot/cloud/DnsServiceDiscoveryDisabledTest.java new file mode 100644 index 0000000..cc967c0 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-dns-starter/src/test/java/org/apache/camel/component/dns/springboot/cloud/DnsServiceDiscoveryDisabledTest.java @@ -0,0 +1,64 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.dns.springboot.cloud; + +import java.util.Map; + +import org.apache.camel.cloud.ServiceDiscovery; +import org.apache.camel.model.cloud.springboot.DnsServiceCallServiceDiscoveryConfigurationProperties; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@DirtiesContext +@SpringBootApplication +@SpringBootTest( + classes = { + DnsServiceDiscoveryEnabledTest.TestConfiguration.class + }, + properties = { + "debug=false", + "camel.cloud.dns.service-discovery.enabled=false" +}) +public class DnsServiceDiscoveryDisabledTest { + @Autowired + ApplicationContext context; + + @Test + public void testConfiguration() throws Exception { + Map<String, ?> beans; + + beans = context.getBeansOfType(DnsServiceCallServiceDiscoveryConfigurationProperties.class); + Assert.assertTrue(beans.isEmpty()); + + beans = context.getBeansOfType(ServiceDiscovery.class); + Assert.assertFalse(beans.isEmpty()); + Assert.assertFalse(beans.containsKey("dns-service-discovery")); + } + + @Configuration + public static class TestConfiguration { + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/46dc46b0/platforms/spring-boot/components-starter/camel-dns-starter/src/test/java/org/apache/camel/component/dns/springboot/cloud/DnsServiceDiscoveryEnabledTest.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-dns-starter/src/test/java/org/apache/camel/component/dns/springboot/cloud/DnsServiceDiscoveryEnabledTest.java b/platforms/spring-boot/components-starter/camel-dns-starter/src/test/java/org/apache/camel/component/dns/springboot/cloud/DnsServiceDiscoveryEnabledTest.java new file mode 100644 index 0000000..bc1bcc7 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-dns-starter/src/test/java/org/apache/camel/component/dns/springboot/cloud/DnsServiceDiscoveryEnabledTest.java @@ -0,0 +1,65 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.dns.springboot.cloud; + +import java.util.Map; + +import org.apache.camel.cloud.ServiceDiscovery; +import org.apache.camel.model.cloud.springboot.DnsServiceCallServiceDiscoveryConfigurationProperties; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@DirtiesContext +@SpringBootApplication +@SpringBootTest( + classes = { + DnsServiceDiscoveryEnabledTest.TestConfiguration.class + }, + properties = { + "debug=false", + "camel.cloud.dns.service-discovery.enabled=true" +}) +public class DnsServiceDiscoveryEnabledTest { + @Autowired + ApplicationContext context; + + @Test + public void testConfiguration() throws Exception { + Map<String, ?> beans; + + beans = context.getBeansOfType(DnsServiceCallServiceDiscoveryConfigurationProperties.class); + Assert.assertFalse(beans.isEmpty()); + Assert.assertEquals(1, beans.size()); + + beans = context.getBeansOfType(ServiceDiscovery.class); + Assert.assertFalse(beans.isEmpty()); + Assert.assertTrue(beans.containsKey("dns-service-discovery")); + } + + @Configuration + public static class TestConfiguration { + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/46dc46b0/platforms/spring-boot/components-starter/camel-dns-starter/src/test/resources/logback.xml ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-dns-starter/src/test/resources/logback.xml b/platforms/spring-boot/components-starter/camel-dns-starter/src/test/resources/logback.xml new file mode 100644 index 0000000..55e0b6b --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-dns-starter/src/test/resources/logback.xml @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with + ~ this work for additional information regarding copyright ownership. + ~ The ASF licenses this file to You under the Apache License, Version 2.0 + ~ (the "License"); you may not use this file except in compliance with + ~ the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<configuration> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <!-- encoders are assigned the type + ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%-15.15thread] %-5level %-30.30logger - %msg%n</pattern> + </encoder> + </appender> + + <appender name="FILE" class="ch.qos.logback.core.FileAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%-15.15thread] %-5level %-30.30logger - %msg%n</pattern> + </encoder> + <file>target/camel-dns-starter-test.log</file> + </appender> + + <root level="INFO"> + <appender-ref ref="FILE"/> + </root> + +</configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/46dc46b0/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootAutoConfigurationMojo.java ---------------------------------------------------------------------- diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootAutoConfigurationMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootAutoConfigurationMojo.java index 033db0e..e31d1ba 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootAutoConfigurationMojo.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootAutoConfigurationMojo.java @@ -207,7 +207,7 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { String pkg = model.getJavaType().substring(0, pos) + ".springboot"; // Generate properties, auto-configuration happens in camel-hystrix-starter - createHystrixConfigurationSource(pkg, model, "camel.hystrix"); + createOtherModelConfigurationSource(pkg, model, "camel.hystrix"); } // Consul @@ -219,7 +219,19 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { String pkg = model.getJavaType().substring(0, pos) + ".springboot"; // Generate properties, auto-configuration happens in camel-consul-starter - createHystrixConfigurationSource(pkg, model, "camel.cloud.consul.service-discovery"); + createOtherModelConfigurationSource(pkg, model, "camel.cloud.consul.service-discovery"); + } + + // DNS + json = loadModelJson(files, "dnsServiceDiscovery"); + if (json != null) { + OtherModel model = generateOtherModel(json); + + int pos = model.getJavaType().lastIndexOf("."); + String pkg = model.getJavaType().substring(0, pos) + ".springboot"; + + // Generate properties, auto-configuration happens in camel-consul-starter + createOtherModelConfigurationSource(pkg, model, "camel.cloud.dns.service-discovery"); } // Etcd @@ -231,11 +243,11 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { String pkg = model.getJavaType().substring(0, pos) + ".springboot"; // Generate properties, auto-configuration happens in camel-etcd-starter - createHystrixConfigurationSource(pkg, model, "camel.cloud.etcd.service-discovery"); + createOtherModelConfigurationSource(pkg, model, "camel.cloud.etcd.service-discovery"); } } - private void createHystrixConfigurationSource(String packageName, OtherModel model, String propertiesPrefix) throws MojoFailureException { + private void createOtherModelConfigurationSource(String packageName, OtherModel model, String propertiesPrefix) throws MojoFailureException { final int pos = model.getJavaType().lastIndexOf("."); final String commonName = model.getJavaType().substring(pos + 1) + "Common"; final String configName = model.getJavaType().substring(pos + 1) + "Properties";
