Search in sources :

Example 1 with LanguageOptionModel

use of org.apache.camel.tooling.model.LanguageModel.LanguageOptionModel in project camel-spring-boot by apache.

the class SpringBootAutoConfigurationMojo method createLanguageConfigurationSource.

private void createLanguageConfigurationSource(String packageName, LanguageModel model, String overrideLanguageName) throws MojoFailureException {
    final JavaClass javaClass = new JavaClass(getProjectClassLoader());
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Language", "LanguageConfiguration");
    javaClass.setPackage(packageName).setName(name);
    javaClass.extendSuperType("LanguageConfigurationPropertiesCommon");
    javaClass.addImport("org.apache.camel.spring.boot.LanguageConfigurationPropertiesCommon");
    // 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() + " language. 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.language." + (camelCaseToDash(overrideLanguageName != null ? overrideLanguageName : 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 (LanguageOptionModel option : model.getOptions()) {
        // need that and skip resultType as they are not global options
        if ("id".equals(option.getName()) || "expression".equals(option.getName()) || "resultType".equals(option.getName())) {
            continue;
        }
        // CHECKSTYLE:OFF
        if ("bean".equals(model.getName())) {
            // and skip following as they are not global options
            if ("bean".equals(option.getName()) || "ref".equals(option.getName()) || "method".equals(option.getName()) || "beanType".equals(option.getName())) {
                continue;
            }
        } else if ("tokenize".equals(model.getName())) {
            // and skip following as they are not global options
            if ("token".equals(option.getName()) || "endToken".equals(option.getName()) || "inheritNamespaceTagName".equals(option.getName()) || "headerName".equals(option.getName()) || "regex".equals(option.getName()) || "xml".equals(option.getName()) || "includeTokens".equals(option.getName()) || "group".equals(option.getName()) || "skipFirst".equals(option.getName())) {
                continue;
            }
        } else if ("xtokenize".equals(model.getName())) {
            // and skip following as they are not global options
            if ("headerName".equals(option.getName()) || "group".equals(option.getName())) {
                continue;
            }
        } else if ("xpath".equals(model.getName())) {
            // and skip following as they are not global options
            if ("headerName".equals(option.getName())) {
                continue;
            }
        } else if ("xquery".equals(model.getName())) {
            // and skip following as they are not global options
            if ("headerName".equals(option.getName())) {
                continue;
            }
        }
        // CHECKSTYLE:ON
        String type = option.getJavaType();
        type = getSimpleJavaType(type);
        // 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(option.getDefaultValue())) {
            if ("java.lang.String".equals(option.getJavaType())) {
                prop.getField().setStringInitializer(option.getDefaultValue().toString());
            } 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 : Generated(javax.annotation.Generated) JavaClass(org.apache.camel.tooling.util.srcgen.JavaClass) LanguageOptionModel(org.apache.camel.tooling.model.LanguageModel.LanguageOptionModel) Property(org.apache.camel.tooling.util.srcgen.Property) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty)

Aggregations

Generated (javax.annotation.Generated)1 LanguageOptionModel (org.apache.camel.tooling.model.LanguageModel.LanguageOptionModel)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