Review the usage of AllNestedConditions for components-starter code auto generation
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/545f3f79 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/545f3f79 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/545f3f79 Branch: refs/heads/master Commit: 545f3f791c03f48906361e00197266f5ade7c768 Parents: c3d7668 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Sat May 20 18:36:32 2017 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Sat May 20 18:37:43 2017 +0200 ---------------------------------------------------------------------- ...OnCamelContextAndAutoConfigurationBeans.java | 36 ++++++ .../SpringBootAutoConfigurationMojo.java | 119 +++++-------------- 2 files changed, 63 insertions(+), 92 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/545f3f79/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/util/ConditionalOnCamelContextAndAutoConfigurationBeans.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/util/ConditionalOnCamelContextAndAutoConfigurationBeans.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/util/ConditionalOnCamelContextAndAutoConfigurationBeans.java new file mode 100644 index 0000000..c7ee1b0 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/util/ConditionalOnCamelContextAndAutoConfigurationBeans.java @@ -0,0 +1,36 @@ +/** + * 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.spring.boot.util; + +import org.apache.camel.CamelContext; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.AllNestedConditions; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; + +public class ConditionalOnCamelContextAndAutoConfigurationBeans extends AllNestedConditions { + public ConditionalOnCamelContextAndAutoConfigurationBeans() { + super(ConfigurationPhase.REGISTER_BEAN); + } + + @ConditionalOnBean(CamelContext.class) + static class OnCamelContext { + } + + @ConditionalOnBean(CamelAutoConfiguration.class) + static class OnCamelAutoConfiguration { + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/545f3f79/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 da7ecb8..50c3b61 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 @@ -69,8 +69,8 @@ import org.jboss.forge.roaster.model.source.PropertySource; import org.jboss.forge.roaster.model.util.Formatter; import org.jboss.forge.roaster.model.util.Strings; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.AllNestedConditions; import org.springframework.boot.autoconfigure.condition.ConditionMessage; import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; @@ -1180,17 +1180,17 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { String name = model.getJavaType().substring(pos + 1); name = name.replace("Component", "ComponentAutoConfiguration"); + String configurationName = name.replace("ComponentAutoConfiguration", "ComponentConfiguration"); + javaClass.setPackage(packageName).setName(name); String doc = "Generated by camel-package-maven-plugin - do not edit this file!"; javaClass.getJavaDoc().setFullText(doc); - javaClass.extendSuperType(AllNestedConditions.class); javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName()); javaClass.addAnnotation(Configuration.class); - javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class"); + javaClass.addAnnotation(Conditional.class).setLiteralValue( + "{ ConditionalOnCamelContextAndAutoConfigurationBeans.class, " + name + ".GroupConditions.class }"); javaClass.addAnnotation(AutoConfigureAfter.class).setLiteralValue("CamelAutoConfiguration.class"); - - String configurationName = name.replace("ComponentAutoConfiguration", "ComponentConfiguration"); javaClass.addAnnotation(EnableConfigurationProperties.class).setLiteralValue( "{ ComponentConfigurationProperties.class, " + configurationName + ".class }" ); @@ -1207,6 +1207,7 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { javaClass.addImport("org.apache.camel.spi.ComponentCustomizer"); javaClass.addImport("org.apache.camel.spring.boot.CamelAutoConfiguration"); javaClass.addImport("org.apache.camel.spring.boot.ComponentConfigurationProperties"); + javaClass.addImport("org.apache.camel.spring.boot.util.ConditionalOnCamelContextAndAutoConfigurationBeans"); javaClass.addImport("org.apache.camel.spring.boot.util.GroupCondition"); javaClass.addImport("org.apache.camel.util.ObjectHelper"); @@ -1242,34 +1243,14 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { .setType(configurationName) .addAnnotation(Autowired.class); - javaClass.addMethod() - .setConstructor(true) - .setPublic() - .setBody("super(ConfigurationPhase.REGISTER_BEAN);"); - - javaClass.addNestedType( - Roaster.create(JavaClassSource.class) - .setName("OnCamelContext") - .setStatic(true) - .addAnnotation(ConditionalOnBean.class) - .setLiteralValue("CamelContext.class") - .getOrigin() - ); - javaClass.addNestedType( - Roaster.create(JavaClassSource.class) - .setName("OnCamelAutoConfiguration") - .setStatic(true) - .addAnnotation(ConditionalOnBean.class) - .setLiteralValue("CamelAutoConfiguration.class") - .getOrigin() - ); javaClass.addNestedType( Roaster.create(JavaClassSource.class) - .setName("Condition") + .setName("GroupConditions") .setStatic(true) + .setPackagePrivate() .extendSuperType(Roaster.create(JavaClassSource.class).setName("GroupCondition")) .addMethod() - .setName("Condition") + .setName("GroupConditions") .setConstructor(true) .setPublic() .setBody("super(\"camel.component\", \"camel.component." + (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US) + "\");") @@ -1292,7 +1273,6 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { method.addAnnotation(Lazy.class); method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases); - method.addAnnotation(ConditionalOnClass.class).setLiteralValue("CamelContext.class"); method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue(model.getShortJavaType() + ".class"); sortImports(javaClass); @@ -1310,17 +1290,16 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { String name = model.getJavaType().substring(pos + 1); name = name.replace("DataFormat", "DataFormatAutoConfiguration"); + String configurationName = name.replace("DataFormatAutoConfiguration", "DataFormatConfiguration"); javaClass.setPackage(packageName).setName(name); String doc = "Generated by camel-package-maven-plugin - do not edit this file!"; javaClass.getJavaDoc().setFullText(doc); - javaClass.extendSuperType(AllNestedConditions.class); javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName()); javaClass.addAnnotation(Configuration.class); - javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class"); + javaClass.addAnnotation(Conditional.class).setLiteralValue( + "{ ConditionalOnCamelContextAndAutoConfigurationBeans.class, " + name + ".GroupConditions.class }"); javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration"); - - String configurationName = name.replace("DataFormatAutoConfiguration", "DataFormatConfiguration"); javaClass.addAnnotation(EnableConfigurationProperties.class).setLiteralValue( "{ DataFormatConfigurationProperties.class, " + configurationName + ".class }" ); @@ -1337,6 +1316,7 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { javaClass.addImport("org.apache.camel.CamelContextAware"); javaClass.addImport("org.apache.camel.spring.boot.CamelAutoConfiguration"); javaClass.addImport("org.apache.camel.spring.boot.DataFormatConfigurationProperties"); + javaClass.addImport("org.apache.camel.spring.boot.util.ConditionalOnCamelContextAndAutoConfigurationBeans"); javaClass.addImport("org.apache.camel.spring.boot.util.GroupCondition"); javaClass.addImport("org.apache.camel.util.ObjectHelper"); javaClass.addImport("org.apache.camel.RuntimeCamelException"); @@ -1377,37 +1357,14 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { .setType(configurationName) .addAnnotation(Autowired.class); - javaClass.addMethod() - .setConstructor(true) - .setPublic() - .setBody("super(ConfigurationPhase.REGISTER_BEAN);"); - - javaClass.addNestedType( - Roaster.create(JavaClassSource.class) - .setName("OnCamelContext") - .setStatic(true) - .addAnnotation(ConditionalOnBean.class) - .setLiteralValue("CamelContext.class") - .getOrigin() - ); javaClass.addNestedType( Roaster.create(JavaClassSource.class) - .setName("OnCamelAutoConfiguration") - .setStatic(true) - .addAnnotation(ConditionalOnBean.class) - .setLiteralValue("CamelAutoConfiguration.class") - .getOrigin() - ); - javaClass.addNestedType( - Roaster.create(JavaClassSource.class) - .setName("Condition") + .setName("GroupConditions") .setStatic(true) + .setPackagePrivate() .extendSuperType(Roaster.create(JavaClassSource.class).setName("GroupCondition")) - .addAnnotation(ConditionalOnBean.class) - .setLiteralValue("CamelAutoConfiguration.class") - .getOrigin() .addMethod() - .setName("Condition") + .setName("GroupConditions") .setConstructor(true) .setPublic() .setBody("super(\"camel.dataformat\", \"camel.dataformat." + (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US) + "\");") @@ -1430,7 +1387,6 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { String[] springBeanAliases = dataFormatAliases.stream().map(alias -> alias + "-dataformat-factory").toArray(size -> new String[size]); method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases); - method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class"); method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class"); sortImports(javaClass); @@ -1448,17 +1404,17 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { String name = model.getJavaType().substring(pos + 1); name = name.replace("Language", "LanguageAutoConfiguration"); + String configurationName = name.replace("LanguageAutoConfiguration", "LanguageConfiguration"); + javaClass.setPackage(packageName).setName(name); String doc = "Generated by camel-package-maven-plugin - do not edit this file!"; javaClass.getJavaDoc().setFullText(doc); - javaClass.extendSuperType(AllNestedConditions.class); javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName()); javaClass.addAnnotation(Configuration.class); - javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class"); + javaClass.addAnnotation(Conditional.class).setLiteralValue( + "{ ConditionalOnCamelContextAndAutoConfigurationBeans.class, " + name + ".GroupConditions.class }"); javaClass.addAnnotation(AutoConfigureAfter.class).setLiteralValue("CamelAutoConfiguration.class"); - - String configurationName = name.replace("LanguageAutoConfiguration", "LanguageConfiguration"); javaClass.addAnnotation(EnableConfigurationProperties.class).setLiteralValue( "{ LanguageConfigurationProperties.class, " + configurationName + ".class }" ); @@ -1469,12 +1425,14 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { javaClass.addImport(model.getJavaType()); javaClass.addImport(List.class); javaClass.addImport(ConditionalOnBean.class); + javaClass.addImport(ConfigurableBeanFactory.class); javaClass.addImport("org.slf4j.Logger"); javaClass.addImport("org.slf4j.LoggerFactory"); javaClass.addImport("org.apache.camel.CamelContext"); javaClass.addImport("org.apache.camel.CamelContextAware"); javaClass.addImport("org.apache.camel.spring.boot.CamelAutoConfiguration"); javaClass.addImport("org.apache.camel.spring.boot.LanguageConfigurationProperties"); + javaClass.addImport("org.apache.camel.spring.boot.util.ConditionalOnCamelContextAndAutoConfigurationBeans"); javaClass.addImport("org.apache.camel.spring.boot.util.GroupCondition"); javaClass.addImport("org.apache.camel.util.ObjectHelper"); javaClass.addImport("org.apache.camel.spi.LanguageCustomizer"); @@ -1511,37 +1469,14 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { .setType(configurationName) .addAnnotation(Autowired.class); - javaClass.addMethod() - .setConstructor(true) - .setPublic() - .setBody("super(ConfigurationPhase.REGISTER_BEAN);"); - javaClass.addNestedType( Roaster.create(JavaClassSource.class) - .setName("OnCamelContext") - .setStatic(true) - .addAnnotation(ConditionalOnBean.class) - .setLiteralValue("CamelContext.class") - .getOrigin() - ); - javaClass.addNestedType( - Roaster.create(JavaClassSource.class) - .setName("OnCamelAutoConfiguration") - .setStatic(true) - .addAnnotation(ConditionalOnBean.class) - .setLiteralValue("CamelAutoConfiguration.class") - .getOrigin() - ); - javaClass.addNestedType( - Roaster.create(JavaClassSource.class) - .setName("Condition") + .setName("GroupConditions") .setStatic(true) + .setPackagePrivate() .extendSuperType(Roaster.create(JavaClassSource.class).setName("GroupCondition")) - .addAnnotation(ConditionalOnBean.class) - .setLiteralValue("CamelAutoConfiguration.class") - .getOrigin() .addMethod() - .setName("Condition") + .setName("GroupConditions") .setConstructor(true) .setPublic() .setBody("super(\"camel.component\", \"camel.component." + (overrideLanguageName != null ? overrideLanguageName : model.getName()).toLowerCase(Locale.US) + "\");") @@ -1563,8 +1498,7 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { String[] springBeanAliases = languageAliases.stream().map(alias -> alias + "-language").toArray(size -> new String[size]); method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases); - method.addAnnotation(Scope.class).setStringValue("prototype"); - method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class"); + method.addAnnotation(Scope.class).setLiteralValue("ConfigurableBeanFactory.SCOPE_PROTOTYPE"); method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class"); sortImports(javaClass); @@ -1641,6 +1575,7 @@ public class SpringBootAutoConfigurationMojo extends AbstractMojo { private static String createDataFormatBody(String shortJavaType) { StringBuilder sb = new StringBuilder(); sb.append("return new DataFormatFactory() {\n"); + sb.append(" @Override\n"); sb.append(" public DataFormat newInstance() {\n"); sb.append(" ").append(shortJavaType).append(" dataformat = new ").append(shortJavaType).append("();").append("\n"); sb.append(" if (CamelContextAware.class.isAssignableFrom(").append(shortJavaType).append(".class)) {\n");