use of org.apache.camel.tooling.util.srcgen.JavaClass in project camel-spring-boot by apache.
the class SpringBootAutoConfigurationMojo method createRestModuleAutoConfigurationSource.
private void createRestModuleAutoConfigurationSource(String packageName, EipModel model) throws MojoFailureException {
final JavaClass javaClass = new JavaClass(getProjectClassLoader());
final int pos = model.getJavaType().lastIndexOf(".");
final String name = model.getJavaType().substring(pos + 1) + "AutoConfiguration";
final String configType = model.getJavaType().substring(pos + 1) + "Properties";
javaClass.setPackage(packageName);
javaClass.setName(name);
String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
javaClass.getJavaDoc().setFullText(doc);
javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
javaClass.addAnnotation(Configuration.class).setLiteralValue("proxyBeanMethods", "false");
javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
javaClass.addAnnotation(ConditionalOnProperty.class).setStringValue("name", "camel.rest.enabled").setLiteralValue("matchIfMissing", "true");
javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
javaClass.addAnnotation(EnableConfigurationProperties.class).setLiteralValue("value", configType + ".class");
javaClass.addImport("java.util.Map");
javaClass.addImport("java.util.HashMap");
javaClass.addImport("org.apache.camel.util.CollectionHelper");
javaClass.addImport("org.apache.camel.support.IntrospectionSupport");
javaClass.addImport("org.apache.camel.spring.boot.util.CamelPropertiesHelper");
javaClass.addImport("org.apache.camel.CamelContext");
javaClass.addImport("org.apache.camel.spi.RestConfiguration");
javaClass.addField().setName("camelContext").setType(loadClass("org.apache.camel.CamelContext")).setPrivate().addAnnotation(Autowired.class);
javaClass.addField().setName("config").setType(loadClass(packageName + "." + configType)).setPrivate().addAnnotation(Autowired.class);
Method method;
// Configuration
method = javaClass.addMethod();
method.setName("configure" + model.getShortJavaType());
method.setPublic();
method.addThrows(Exception.class);
method.setReturnType(loadClass("org.apache.camel.spi.RestConfiguration"));
method.addAnnotation(Lazy.class);
method.addAnnotation(Bean.class).setLiteralValue("name", "\"rest-configuration\"");
method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
method.addAnnotation(ConditionalOnMissingBean.class);
method.setBody("" + "Map<String, Object> properties = new HashMap<>();\n" + "IntrospectionSupport.getProperties(config, properties, null, false);\n" + "// These options is configured specially further below, so remove them first\n" + "properties.remove(\"enableCors\");\n" + "properties.remove(\"apiProperty\");\n" + "properties.remove(\"componentProperty\");\n" + "properties.remove(\"consumerProperty\");\n" + "properties.remove(\"dataFormatProperty\");\n" + "properties.remove(\"endpointProperty\");\n" + "properties.remove(\"corsHeaders\");\n" + "\n" + "RestConfiguration definition = new RestConfiguration();\n" + "CamelPropertiesHelper.setCamelProperties(camelContext, definition, properties, true);\n" + "\n" + "// Workaround for spring-boot properties name as It would appear\n" + "// as enable-c-o-r-s if left uppercase in Configuration\n" + "definition.setEnableCORS(config.getEnableCors());\n" + "\n" + "if (config.getApiProperty() != null) {\n" + " definition.setApiProperties(new HashMap<>(CollectionHelper.flattenKeysInMap(config.getApiProperty(), \".\")));\n" + "}\n" + "if (config.getComponentProperty() != null) {\n" + " definition.setComponentProperties(new HashMap<>(CollectionHelper.flattenKeysInMap(config.getComponentProperty(), \".\")));\n" + "}\n" + "if (config.getConsumerProperty() != null) {\n" + " definition.setConsumerProperties(new HashMap<>(CollectionHelper.flattenKeysInMap(config.getConsumerProperty(), \".\")));\n" + "}\n" + "if (config.getDataFormatProperty() != null) {\n" + " definition.setDataFormatProperties(new HashMap<>(CollectionHelper.flattenKeysInMap(config.getDataFormatProperty(), \".\")));\n" + "}\n" + "if (config.getEndpointProperty() != null) {\n" + " definition.setEndpointProperties(new HashMap<>(CollectionHelper.flattenKeysInMap(config.getEndpointProperty(), \".\")));\n" + "}\n" + "if (config.getCorsHeaders() != null) {\n" + " Map<String, Object> map = CollectionHelper.flattenKeysInMap(config.getCorsHeaders(), \".\");\n" + " Map<String, String> target = new HashMap<>();\n" + " map.forEach((k, v) -> target.put(k, v.toString()));\n" + " definition.setCorsHeaders(target);\n" + "}\n" + "return definition;");
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
writeSourceIfChanged(javaClass, fileName, true);
writeComponentSpringFactorySource(packageName, name);
}
use of org.apache.camel.tooling.util.srcgen.JavaClass in project camel-spring-boot by apache.
the class SpringBootAutoConfigurationMojo method createEipModelConfigurationSource.
private void createEipModelConfigurationSource(String packageName, EipModel model, String propertiesPrefix, boolean generatedNestedConfig) throws MojoFailureException {
final int pos = model.getJavaType().lastIndexOf(".");
final String commonName = model.getJavaType().substring(pos + 1) + (generatedNestedConfig ? "Common" : "Properties");
final String configName = model.getJavaType().substring(pos + 1) + (generatedNestedConfig ? "Properties" : null);
// Common base class
JavaClass commonClass = new JavaClass(getProjectClassLoader());
commonClass.setPackage(packageName);
commonClass.setName(commonName);
String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
if (!Strings.isNullOrEmpty(model.getDescription())) {
doc = model.getDescription() + "\n\n" + doc;
}
commonClass.getJavaDoc().setFullText(doc);
commonClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
for (EipOptionModel option : model.getOptions()) {
String type = option.getJavaType();
String name = option.getName();
if ("id".equalsIgnoreCase(name) || "parent".equalsIgnoreCase(name) || "camelContext".equalsIgnoreCase(name)) {
// Skip them as they should not be set via spring boot
continue;
}
if ("java.util.List<org.apache.camel.model.PropertyDefinition>".equalsIgnoreCase(type)) {
type = "java.util.Map<java.lang.String, java.lang.String>";
}
// generate inner class for non-primitive options
Property prop = commonClass.addProperty(type, option.getName());
if (!Strings.isNullOrEmpty(option.getDescription())) {
prop.getField().getJavaDoc().setFullText(option.getDescription());
}
if (!isBlank(option.getDefaultValue())) {
if ("java.lang.String".equals(type)) {
prop.getField().setStringInitializer(option.getDefaultValue().toString());
} else if ("long".equals(type) || "java.lang.Long".equals(type)) {
// the value should be a Long number
String value = option.getDefaultValue() + "L";
prop.getField().setLiteralInitializer(value);
} else if ("integer".equals(option.getType()) || "java.lang.Integer".equals(option.getJavaType()) || "boolean".equals(option.getType()) || "java.lang.Boolean".equals(option.getJavaType())) {
prop.getField().setLiteralInitializer(option.getDefaultValue().toString());
} else if (!isBlank(option.getEnums())) {
String enumShortName = type.substring(type.lastIndexOf(".") + 1);
prop.getField().setLiteralInitializer(enumShortName + "." + option.getDefaultValue());
commonClass.addImport(model.getJavaType());
}
}
}
writeSourceIfChanged(commonClass, packageName.replaceAll("\\.", "\\/") + "/" + commonName + ".java", true);
Class commonClazz = generateDummyClass(commonClass.getCanonicalName());
// Config class
if (generatedNestedConfig) {
JavaClass configClass = new JavaClass(getProjectClassLoader());
configClass.setPackage(packageName);
configClass.setName(configName);
configClass.extendSuperType(commonClass);
configClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
configClass.addAnnotation(loadClass("org.springframework.boot.context.properties.ConfigurationProperties")).setStringValue("prefix", propertiesPrefix);
configClass.addImport(Map.class);
configClass.addImport(HashMap.class);
configClass.removeImport(commonClass);
configClass.addField().setName("enabled").setType(boolean.class).setPrivate().setLiteralInitializer("true").getJavaDoc().setFullText("Enable the component");
configClass.addField().setName("configurations").setType(loadType("java.util.Map<java.lang.String, " + packageName + "." + commonName + ">")).setPrivate().setLiteralInitializer("new HashMap<>()").getJavaDoc().setFullText("Define additional configuration definitions");
Method method;
method = configClass.addMethod();
method.setName("getConfigurations");
method.setReturnType(loadType("java.util.Map<java.lang.String, " + packageName + "." + commonName + ">"));
method.setPublic();
method.setBody("return configurations;");
method = configClass.addMethod();
method.setName("isEnabled");
method.setReturnType(boolean.class);
method.setPublic();
method.setBody("return enabled;");
method = configClass.addMethod();
method.setName("setEnabled");
method.addParameter(boolean.class, "enabled");
method.setPublic();
method.setBody("this.enabled = enabled;");
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + configName + ".java";
writeSourceIfChanged(configClass, fileName, true);
}
}
use of org.apache.camel.tooling.util.srcgen.JavaClass in project camel-spring-boot by apache.
the class SpringBootAutoConfigurationMojo method createLanguageAutoConfigurationSource.
private void createLanguageAutoConfigurationSource(String packageName, LanguageModel model, String overrideName, boolean complexOptions) throws MojoFailureException {
final String name = model.getJavaType().substring(model.getJavaType().lastIndexOf(".") + 1).replace("Language", "LanguageAutoConfiguration");
final String configurationName = name.replace("LanguageAutoConfiguration", "LanguageConfiguration");
final String languageName = camelCaseToDash(overrideName != null ? overrideName : model.getName()).toLowerCase(Locale.US);
final Class<?> configClass = generateDummyClass(packageName + "." + configurationName);
final JavaClass javaClass = new JavaClass(getProjectClassLoader());
javaClass.setPackage(packageName);
javaClass.setName(name);
javaClass.getJavaDoc().setFullText("Generated by camel-package-maven-plugin - do not edit this file!");
javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
javaClass.addAnnotation(Configuration.class).setLiteralValue("proxyBeanMethods", "false");
javaClass.addAnnotation(AutoConfigureAfter.class).setLiteralValue("CamelAutoConfiguration.class");
javaClass.addAnnotation(Conditional.class).setLiteralValue("ConditionalOnCamelContextAndAutoConfigurationBeans.class");
javaClass.addAnnotation(EnableConfigurationProperties.class).setLiteralValue("{LanguageConfigurationProperties.class," + configurationName + ".class}");
javaClass.addAnnotation("org.apache.camel.spring.boot.util.ConditionalOnHierarchicalProperties").setStringArrayValue("value", new String[] { "camel.language", "camel.language." + languageName });
javaClass.addImport(ApplicationContext.class);
javaClass.addImport("org.springframework.boot.convert.ApplicationConversionService");
javaClass.addImport("org.apache.camel.CamelContext");
javaClass.addImport("org.apache.camel.spring.boot.CamelAutoConfiguration");
javaClass.addImport("org.apache.camel.spring.boot.LanguageConfigurationProperties");
javaClass.addImport("org.apache.camel.spring.boot.util.CamelPropertiesHelper");
javaClass.addImport("org.apache.camel.spring.boot.util.ConditionalOnCamelContextAndAutoConfigurationBeans");
javaClass.addImport("org.apache.camel.spring.boot.util.HierarchicalPropertiesEvaluator");
javaClass.addImport("org.apache.camel.spi.Language");
javaClass.addImport("org.apache.camel.spi.LanguageCustomizer");
javaClass.addImport(model.getJavaType());
javaClass.addField().setPrivate().setName("applicationContext").setType(ApplicationContext.class).addAnnotation(Autowired.class);
javaClass.addField().setPrivate().setFinal(true).setName("camelContext").setType(loadClass("org.apache.camel.CamelContext"));
javaClass.addField().setPrivate().setName("configuration").setType(configClass).addAnnotation(Autowired.class);
Method ctr = javaClass.addMethod().setConstructor(true).setPublic().setName(name).addParameter("org.apache.camel.CamelContext", "camelContext");
ctr.setBody("this.camelContext = camelContext;\n");
String body = createLanguageBody(model.getShortJavaType(), languageName);
String methodName = "configure" + model.getShortJavaType();
Method method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType("org.apache.camel.spi.LanguageCustomizer");
method.addAnnotation(Lazy.class);
method.addAnnotation(Bean.class);
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
writeSourceIfChanged(javaClass, fileName, false);
}
use of org.apache.camel.tooling.util.srcgen.JavaClass in project camel-spring-boot by apache.
the class SpringBootAutoConfigurationMojo method createDataFormatConfigurationSource.
// CHECKSTYLE:ON
private void createDataFormatConfigurationSource(String packageName, DataFormatModel model, String overrideDataFormatName) throws MojoFailureException {
final JavaClass javaClass = new JavaClass(getProjectClassLoader());
int pos = model.getJavaType().lastIndexOf(".");
String name = model.getJavaType().substring(pos + 1);
name = name.replace("DataFormat", "DataFormatConfiguration");
javaClass.setPackage(packageName).setName(name);
javaClass.extendSuperType("DataFormatConfigurationPropertiesCommon");
javaClass.addImport("org.apache.camel.spring.boot.DataFormatConfigurationPropertiesCommon");
// add bogus field for enabled so spring boot tooling can get the
// javadoc as description in its metadata
Property bogus = javaClass.addProperty("java.lang.Boolean", "enabled");
bogus.getField().getJavaDoc().setText("Whether to enable auto configuration of the " + model.getName() + " data format. This is enabled by default.");
bogus.removeAccessor();
bogus.removeMutator();
String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
if (!Strings.isNullOrEmpty(model.getDescription())) {
doc = model.getDescription() + "\n\n" + doc;
}
javaClass.getJavaDoc().setFullText(doc);
String prefix = "camel.dataformat." + camelCaseToDash(overrideDataFormatName != null ? overrideDataFormatName : model.getName());
// make sure prefix is in lower case
prefix = prefix.toLowerCase(Locale.US);
javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
javaClass.addAnnotation("org.springframework.boot.context.properties.ConfigurationProperties").setStringValue("prefix", prefix);
for (DataFormatOptionModel option : model.getOptions()) {
// skip option with name id in data format as we do not need that
if ("id".equals(option.getName())) {
continue;
}
Object defaultValue = option.getDefaultValue();
String type = option.getJavaType();
type = getSimpleJavaType(type);
// special for bindy
if ("org.apache.camel.model.dataformat.BindyType".equals(option.getJavaType())) {
// force to use a string type
type = "java.lang.String";
defaultValue = null;
} else if (option.getJavaType().contains("org.apache.camel.model.dataformat")) {
// skip options that are from the model as they are not possible to configure anyway
continue;
}
// spring-boot auto configuration does not support complex types
// (unless they are enum, nested)
// and if so then we should use a String type so spring-boot and its
// tooling support that
// as Camel will be able to convert the string value into a lookup
// of the bean in the registry anyway
// and therefore there is no problem, eg
// camel.component.jdbc.data-source = myDataSource
// where the type would have been javax.sql.DataSource
boolean complex = isComplexType(option) && isBlank(option.getEnums());
if (complex) {
// force to use a string type
type = "java.lang.String";
}
Property prop = javaClass.addProperty(type, option.getName());
if (option.isDeprecated()) {
prop.getField().addAnnotation(Deprecated.class);
prop.getAccessor().addAnnotation(Deprecated.class);
prop.getMutator().addAnnotation(Deprecated.class);
// DeprecatedConfigurationProperty must be on getter when
// deprecated
prop.getAccessor().addAnnotation(DeprecatedConfigurationProperty.class);
}
if (!Strings.isNullOrEmpty(option.getDescription())) {
String desc = option.getDescription();
if (complex) {
if (!desc.endsWith(".")) {
desc = desc + ".";
}
desc = desc + " The option is a " + option.getJavaType() + " type.";
}
prop.getField().getJavaDoc().setFullText(desc);
}
if (!isBlank(defaultValue)) {
if ("java.lang.String".equals(option.getJavaType())) {
prop.getField().setStringInitializer(defaultValue.toString());
} else if ("long".equals(option.getJavaType()) || "java.lang.Long".equals(option.getJavaType())) {
// the value should be a Long number
String value = defaultValue + "L";
prop.getField().setLiteralInitializer(value);
} else if ("integer".equals(option.getType()) || "java.lang.Integer".equals(option.getJavaType()) || "boolean".equals(option.getType()) || "java.lang.Boolean".equals(option.getJavaType())) {
prop.getField().setLiteralInitializer(defaultValue.toString());
} else if (!isBlank(option.getEnums())) {
String enumShortName = type.substring(type.lastIndexOf(".") + 1);
prop.getField().setLiteralInitializer(enumShortName + "." + defaultValue);
javaClass.addImport(model.getJavaType());
}
}
}
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
writeSourceIfChanged(javaClass, fileName, true);
}
use of org.apache.camel.tooling.util.srcgen.JavaClass in project camel-spring-boot by apache.
the class SpringBootAutoConfigurationMojo method createComponentConverterSource.
private void createComponentConverterSource(String packageName, ComponentModel model) throws MojoFailureException {
final String name = model.getJavaType().substring(model.getJavaType().lastIndexOf(".") + 1).replace("Component", "ComponentConverter");
// create converter class and write source
JavaClass javaClass = new JavaClass(getProjectClassLoader());
javaClass.setPackage(packageName);
javaClass.setName(name);
javaClass.getJavaDoc().setFullText("Generated by camel-package-maven-plugin - do not edit this file!");
javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
javaClass.addAnnotation(Configuration.class).setLiteralValue("proxyBeanMethods", "false");
javaClass.addAnnotation("org.springframework.boot.context.properties.ConfigurationPropertiesBinding");
javaClass.addAnnotation("org.springframework.stereotype.Component");
javaClass.addImport("java.util.LinkedHashSet");
javaClass.addImport("java.util.Set");
javaClass.addImport("org.springframework.core.convert.TypeDescriptor");
javaClass.addImport("org.springframework.core.convert.converter.GenericConverter");
javaClass.implementInterface("GenericConverter");
javaClass.addField().setPrivate().setName("applicationContext").setType(loadClass("org.springframework.context.ApplicationContext")).addAnnotation(Autowired.class);
String body = createConverterPairBody(model);
javaClass.addMethod().setName("getConvertibleTypes").setPublic().setReturnType("Set<ConvertiblePair>").setBody(body);
body = createConvertBody(model);
javaClass.addMethod().setName("convert").setPublic().setReturnType("Object").addParameter("Object", "source").addParameter("TypeDescriptor", "sourceType").addParameter("TypeDescriptor", "targetType").setBody(body);
sortImports(javaClass);
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
writeSourceIfChanged(javaClass, fileName, false);
writeComponentSpringFactorySource(packageName, name);
}
Aggregations