Search in sources :

Example 1 with ComponentOptionModel

use of org.apache.camel.tooling.model.ComponentModel.ComponentOptionModel in project camel-spring-boot by apache.

the class SpringBootAutoConfigurationMojo method createComponentConfigurationSource.

private void createComponentConfigurationSource(String packageName, ComponentModel model, String overrideComponentName) throws MojoFailureException {
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Component", "ComponentConfiguration");
    final JavaClass javaClass = new JavaClass(getProjectClassLoader());
    javaClass.setPackage(packageName);
    javaClass.setName(name);
    javaClass.extendSuperType("ComponentConfigurationPropertiesCommon");
    javaClass.addImport("org.apache.camel.spring.boot.ComponentConfigurationPropertiesCommon");
    // 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");
    String scheme = overrideComponentName != null ? overrideComponentName : model.getScheme();
    bogus.getField().getJavaDoc().setText("Whether to enable auto configuration of the " + scheme + " component. 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().setText(doc);
    String prefix = "camel.component." + camelCaseToDash(overrideComponentName != null ? overrideComponentName : model.getScheme());
    // make sure prefix is in lower case
    prefix = prefix.toLowerCase(Locale.US);
    javaClass.addAnnotation(Generated.class.getName()).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
    javaClass.addAnnotation("org.springframework.boot.context.properties.ConfigurationProperties").setStringValue("prefix", prefix);
    for (ComponentOptionModel option : model.getComponentOptions()) {
        if (skipComponentOption(model, option)) {
            // some component options should be skipped
            continue;
        }
        String type = option.getJavaType();
        // generate inner class for non-primitive options
        type = getSimpleJavaType(type);
        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();
            boolean complex = isComplexTypeOrDuration(option) && isBlank(option.getEnums());
            if (complex) {
                if (!desc.endsWith(".")) {
                    desc = desc + ".";
                }
                desc = desc + " The option is a " + option.getJavaType() + " type.";
            }
            prop.getField().getJavaDoc().setFullText(desc);
        }
        if (!isBlank(option.getDefaultValue())) {
            if ("java.lang.String".equals(option.getJavaType())) {
                prop.getField().setStringInitializer(option.getDefaultValue().toString());
            } else if ("duration".equals(option.getType())) {
                String value = convertDurationToMills(option.getDefaultValue().toString());
                // duration is either long or int java type
                if ("long".equals(option.getJavaType()) || "java.lang.Long".equals(option.getJavaType())) {
                    value = value + "L";
                }
                prop.getField().setLiteralInitializer(value);
            } else if ("long".equals(option.getJavaType()) || "java.lang.Long".equals(option.getJavaType())) {
                // 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());
                javaClass.addImport(model.getJavaType());
            }
        }
    }
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName, true);
}
Also used : JavaClass(org.apache.camel.tooling.util.srcgen.JavaClass) Property(org.apache.camel.tooling.util.srcgen.Property) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) ComponentOptionModel(org.apache.camel.tooling.model.ComponentModel.ComponentOptionModel)

Aggregations

ComponentOptionModel (org.apache.camel.tooling.model.ComponentModel.ComponentOptionModel)1 JavaClass (org.apache.camel.tooling.util.srcgen.JavaClass)1 Property (org.apache.camel.tooling.util.srcgen.Property)1 ConditionalOnProperty (org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)1 DeprecatedConfigurationProperty (org.springframework.boot.context.properties.DeprecatedConfigurationProperty)1