use of org.apache.camel.tooling.model.DataFormatModel.DataFormatOptionModel 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);
}
Aggregations