Search in sources :

Example 1 with EipOptionModel

use of org.apache.camel.tooling.model.EipModel.EipOptionModel in project camel-spring-boot by apache.

the class SpringBootAutoConfigurationMojo method createRestConfigurationSource.

private void createRestConfigurationSource(String packageName, EipModel model, String propertiesPrefix) throws MojoFailureException {
    final int pos = model.getJavaType().lastIndexOf(".");
    final String className = model.getJavaType().substring(pos + 1) + "Properties";
    generateDummyClass(packageName + "." + className);
    // Common base class
    JavaClass javaClass = new JavaClass(getProjectClassLoader());
    javaClass.setPackage(packageName);
    javaClass.setName(className);
    javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
    javaClass.addAnnotation("org.springframework.boot.context.properties.ConfigurationProperties").setStringValue("prefix", propertiesPrefix);
    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);
    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>";
        } else if ("java.util.List<org.apache.camel.model.rest.RestPropertyDefinition>".equalsIgnoreCase(type)) {
            type = "java.util.Map<java.lang.String, java.lang.Object>";
        }
        // to avoid ugly names such as c-o-r-s
        if ("enableCORS".equalsIgnoreCase(name)) {
            name = "enableCors";
        }
        // generate inner class for non-primitive options
        Property prop = javaClass.addProperty(type, name);
        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());
                javaClass.addImport(model.getJavaType());
            }
        }
    }
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + className + ".java";
    writeSourceIfChanged(javaClass, fileName, true);
}
Also used : EipOptionModel(org.apache.camel.tooling.model.EipModel.EipOptionModel) Generated(javax.annotation.Generated) 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)

Example 2 with EipOptionModel

use of org.apache.camel.tooling.model.EipModel.EipOptionModel 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);
    }
}
Also used : EipOptionModel(org.apache.camel.tooling.model.EipModel.EipOptionModel) Generated(javax.annotation.Generated) JavaClass(org.apache.camel.tooling.util.srcgen.JavaClass) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) JavaClass(org.apache.camel.tooling.util.srcgen.JavaClass) Method(org.apache.camel.tooling.util.srcgen.Method) 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)2 EipOptionModel (org.apache.camel.tooling.model.EipModel.EipOptionModel)2 JavaClass (org.apache.camel.tooling.util.srcgen.JavaClass)2 Property (org.apache.camel.tooling.util.srcgen.Property)2 ConditionalOnProperty (org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)2 DeprecatedConfigurationProperty (org.springframework.boot.context.properties.DeprecatedConfigurationProperty)2 Method (org.apache.camel.tooling.util.srcgen.Method)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1