Search in sources :

Example 1 with ComponentOptionModel

use of org.apache.camel.maven.packaging.model.ComponentOptionModel in project camel by apache.

the class SpringBootAutoConfigurationMojo method generateComponentModel.

private static ComponentModel generateComponentModel(String componentName, String json) {
    List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("component", json, false);
    ComponentModel component = new ComponentModel(true);
    component.setScheme(getSafeValue("scheme", rows));
    component.setSyntax(getSafeValue("syntax", rows));
    component.setAlternativeSyntax(getSafeValue("alternativeSyntax", rows));
    component.setTitle(getSafeValue("title", rows));
    component.setDescription(getSafeValue("description", rows));
    component.setFirstVersion(JSonSchemaHelper.getSafeValue("firstVersion", rows));
    component.setLabel(getSafeValue("label", rows));
    component.setDeprecated(getSafeValue("deprecated", rows));
    component.setConsumerOnly(getSafeValue("consumerOnly", rows));
    component.setProducerOnly(getSafeValue("producerOnly", rows));
    component.setJavaType(getSafeValue("javaType", rows));
    component.setGroupId(getSafeValue("groupId", rows));
    component.setArtifactId(getSafeValue("artifactId", rows));
    component.setVersion(getSafeValue("version", rows));
    rows = JSonSchemaHelper.parseJsonSchema("componentProperties", json, true);
    for (Map<String, String> row : rows) {
        ComponentOptionModel option = new ComponentOptionModel();
        option.setName(getSafeValue("name", row));
        option.setDisplayName(getSafeValue("displayName", row));
        option.setKind(getSafeValue("kind", row));
        option.setType(getSafeValue("type", row));
        option.setJavaType(getSafeValue("javaType", row));
        option.setDeprecated(getSafeValue("deprecated", row));
        option.setDescription(getSafeValue("description", row));
        option.setDefaultValue(getSafeValue("defaultValue", row));
        option.setEnums(getSafeValue("enum", row));
        component.addComponentOption(option);
    }
    rows = JSonSchemaHelper.parseJsonSchema("properties", json, true);
    for (Map<String, String> row : rows) {
        EndpointOptionModel option = new EndpointOptionModel();
        option.setName(getSafeValue("name", row));
        option.setDisplayName(getSafeValue("displayName", row));
        option.setKind(getSafeValue("kind", row));
        option.setGroup(getSafeValue("group", row));
        option.setRequired(getSafeValue("required", row));
        option.setType(getSafeValue("type", row));
        option.setJavaType(getSafeValue("javaType", row));
        option.setEnums(getSafeValue("enum", row));
        option.setPrefix(getSafeValue("prefix", row));
        option.setMultiValue(getSafeValue("multiValue", row));
        option.setDeprecated(getSafeValue("deprecated", row));
        option.setDefaultValue(getSafeValue("defaultValue", row));
        option.setDescription(getSafeValue("description", row));
        option.setEnumValues(getSafeValue("enum", row));
        component.addEndpointOption(option);
    }
    return component;
}
Also used : EndpointOptionModel(org.apache.camel.maven.packaging.model.EndpointOptionModel) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) Map(java.util.Map) HashMap(java.util.HashMap) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel)

Example 2 with ComponentOptionModel

use of org.apache.camel.maven.packaging.model.ComponentOptionModel in project camel by apache.

the class SpringBootAutoConfigurationMojo method createComponentConfigurationSource.

private void createComponentConfigurationSource(String packageName, ComponentModel model, String overrideComponentName) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Component", "ComponentConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    if (!Strings.isBlank(model.getDescription())) {
        doc = model.getDescription() + "\n\n" + doc;
    }
    javaClass.getJavaDoc().setFullText(doc);
    String prefix = "camel.component." + (overrideComponentName != null ? overrideComponentName : model.getScheme());
    // make sure prefix is in lower case
    prefix = prefix.toLowerCase(Locale.US);
    javaClass.addAnnotation("org.springframework.boot.context.properties.ConfigurationProperties").setStringValue("prefix", prefix);
    Set<JavaClassSource> nestedTypes = new HashSet<>();
    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);
        JavaClassSource javaClassSource = readJavaType(type);
        if (isNestedProperty(nestedTypes, javaClassSource)) {
            type = option.getShortJavaType() + INNER_TYPE_SUFFIX;
        }
        PropertySource<JavaClassSource> prop = javaClass.addProperty(type, option.getName());
        if (!type.endsWith(INNER_TYPE_SUFFIX) && type.indexOf('[') == -1 && !EXCLUDE_INNER_PATTERN.matcher(type).matches() && Strings.isBlank(option.getEnums()) && (javaClassSource == null || (javaClassSource.isClass() && !javaClassSource.isAbstract()))) {
            // add nested configuration annotation for complex properties
            prop.getField().addAnnotation(NestedConfigurationProperty.class);
        }
        if ("true".equals(option.getDeprecated())) {
            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.isBlank(option.getDescription())) {
            prop.getField().getJavaDoc().setFullText(option.getDescription());
        }
        if (!Strings.isBlank(option.getDefaultValue())) {
            if ("java.lang.String".equals(option.getJavaType())) {
                prop.getField().setStringInitializer(option.getDefaultValue());
            } 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()) || "boolean".equals(option.getType())) {
                prop.getField().setLiteralInitializer(option.getDefaultValue());
            } else if (!Strings.isBlank(option.getEnums())) {
                String enumShortName = type.substring(type.lastIndexOf(".") + 1);
                prop.getField().setLiteralInitializer(enumShortName + "." + option.getDefaultValue());
                javaClass.addImport(model.getJavaType());
            }
        }
    }
    // add inner classes for nested AutoConfiguration options
    ClassLoader projectClassLoader = getProjectClassLoader();
    for (JavaClassSource nestedType : nestedTypes) {
        final JavaClassSource innerClass = javaClass.addNestedType("public static class " + nestedType.getName() + INNER_TYPE_SUFFIX);
        // add source class name as a static field
        innerClass.addField().setPublic().setStatic(true).setFinal(true).setType(Class.class).setName("CAMEL_NESTED_CLASS").setLiteralInitializer(nestedType.getCanonicalName() + ".class");
        // parse option type
        for (ResolvedProperty resolvedProperty : getProperties(nestedType)) {
            String optionType = resolvedProperty.propertyType;
            PropertySource<JavaClassSource> sourceProp = resolvedProperty.propertySource;
            Type<JavaClassSource> propType = sourceProp.getType();
            final PropertySource<JavaClassSource> prop = innerClass.addProperty(optionType, sourceProp.getName());
            boolean anEnum;
            Class optionClass;
            if (!propType.isArray()) {
                optionClass = loadClass(projectClassLoader, optionType);
                anEnum = optionClass.isEnum();
            } else {
                optionClass = null;
                anEnum = false;
            }
            // add nested configuration annotation for complex properties
            if (!EXCLUDE_INNER_PATTERN.matcher(optionType).matches() && !propType.isArray() && !anEnum && optionClass != null && !optionClass.isInterface() && !optionClass.isAnnotation() && !Modifier.isAbstract(optionClass.getModifiers())) {
                prop.getField().addAnnotation(NestedConfigurationProperty.class);
            }
            if (sourceProp.hasAnnotation(Deprecated.class)) {
                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);
            }
            String description = null;
            final MethodSource<JavaClassSource> mutator = sourceProp.getMutator();
            if (mutator.hasJavaDoc()) {
                description = mutator.getJavaDoc().getFullText();
            } else if (sourceProp.hasField()) {
                description = sourceProp.getField().getJavaDoc().getFullText();
            }
            if (!Strings.isBlank(description)) {
                prop.getField().getJavaDoc().setFullText(description);
            }
            // as the annotation can refer to a constant field which we wont have accessible at this point
            if (sourceProp.hasAnnotation(UriParam.class) || sourceProp.hasAnnotation(UriPath.class)) {
                String defaultValue = null;
                String javaType = null;
                String type = null;
                String fileName = model.getJavaType();
                fileName = fileName.substring(0, fileName.lastIndexOf("."));
                fileName = fileName.replace('.', '/');
                File jsonFile = new File(classesDir, fileName + "/" + model.getScheme() + ".json");
                if (jsonFile.isFile() && jsonFile.exists()) {
                    try {
                        String json = FileUtils.readFileToString(jsonFile);
                        List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("properties", json, true);
                        // grab name from annotation
                        String optionName;
                        if (sourceProp.hasAnnotation(UriParam.class)) {
                            optionName = sourceProp.getAnnotation(UriParam.class).getStringValue("name");
                        } else {
                            optionName = sourceProp.getAnnotation(UriPath.class).getStringValue("name");
                        }
                        if (optionName == null) {
                            optionName = sourceProp.hasField() ? sourceProp.getField().getName() : null;
                        }
                        if (optionName != null) {
                            javaType = getPropertyJavaType(rows, optionName);
                            type = getPropertyType(rows, optionName);
                            defaultValue = getPropertyDefaultValue(rows, optionName);
                        }
                    } catch (IOException e) {
                    // ignore
                    }
                }
                if (!Strings.isBlank(defaultValue)) {
                    // roaster can create the wrong type for some options so use the correct type we found in the json schema
                    String wrapperType = getSimpleJavaType(javaType);
                    if (wrapperType.startsWith("java.lang.")) {
                        // skip java.lang. as prefix for wrapper type
                        wrapperType = wrapperType.substring(10);
                        prop.setType(wrapperType);
                    }
                    if ("long".equals(javaType) || "java.lang.Long".equals(javaType)) {
                        // the value should be a Long number
                        String value = defaultValue + "L";
                        prop.getField().setLiteralInitializer(value);
                    } else if ("integer".equals(type) || "boolean".equals(type)) {
                        prop.getField().setLiteralInitializer(defaultValue);
                    } else if ("string".equals(type)) {
                        prop.getField().setStringInitializer(defaultValue);
                    } else if (anEnum) {
                        String enumShortName = optionClass.getSimpleName();
                        prop.getField().setLiteralInitializer(enumShortName + "." + defaultValue);
                        javaClass.addImport(model.getJavaType());
                    }
                }
            }
        }
    }
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) IOException(java.io.IOException) UriParam(org.apache.camel.spi.UriParam) UriPath(org.apache.camel.spi.UriPath) URLClassLoader(java.net.URLClassLoader) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel) HashSet(java.util.HashSet)

Example 3 with ComponentOptionModel

use of org.apache.camel.maven.packaging.model.ComponentOptionModel in project camel by apache.

the class UpdateReadmeMojo method generateComponentModel.

private ComponentModel generateComponentModel(String componentName, String json) {
    List<Map<String, String>> rows = parseJsonSchema("component", json, false);
    ComponentModel component = new ComponentModel(true);
    component.setScheme(getSafeValue("scheme", rows));
    component.setSyntax(getSafeValue("syntax", rows));
    component.setAlternativeSyntax(getSafeValue("alternativeSyntax", rows));
    component.setAlternativeSchemes(getSafeValue("alternativeSchemes", rows));
    component.setTitle(getSafeValue("title", rows));
    component.setDescription(getSafeValue("description", rows));
    component.setFirstVersion(getSafeValue("firstVersion", rows));
    component.setLabel(getSafeValue("label", rows));
    component.setDeprecated(getSafeValue("deprecated", rows));
    component.setConsumerOnly(getSafeValue("consumerOnly", rows));
    component.setProducerOnly(getSafeValue("producerOnly", rows));
    component.setJavaType(getSafeValue("javaType", rows));
    component.setGroupId(getSafeValue("groupId", rows));
    component.setArtifactId(getSafeValue("artifactId", rows));
    component.setVersion(getSafeValue("version", rows));
    String oldGroup = null;
    rows = parseJsonSchema("componentProperties", json, true);
    for (Map<String, String> row : rows) {
        ComponentOptionModel option = new ComponentOptionModel();
        option.setName(getSafeValue("name", row));
        option.setDisplayName(getSafeValue("displayName", row));
        option.setKind(getSafeValue("kind", row));
        option.setGroup(getSafeValue("group", row));
        option.setRequired(getSafeValue("required", row));
        option.setType(getSafeValue("type", row));
        option.setJavaType(getSafeValue("javaType", row));
        option.setEnums(getSafeValue("enum", row));
        option.setDeprecated(getSafeValue("deprecated", row));
        option.setSecret(getSafeValue("secret", row));
        option.setDefaultValue(getSafeValue("defaultValue", row));
        option.setDescription(getSafeValue("description", row));
        // lets put required in the description
        if ("true".equals(option.getRequired())) {
            String desc = "*Required* " + option.getDescription();
            option.setDescription(desc);
        }
        component.addComponentOption(option);
        // group separate between different options
        if (oldGroup == null || !oldGroup.equals(option.getGroup())) {
            option.setNewGroup(true);
        }
        oldGroup = option.getGroup();
    }
    oldGroup = null;
    rows = parseJsonSchema("properties", json, true);
    for (Map<String, String> row : rows) {
        EndpointOptionModel option = new EndpointOptionModel();
        option.setName(getSafeValue("name", row));
        option.setDisplayName(getSafeValue("displayName", row));
        option.setKind(getSafeValue("kind", row));
        option.setGroup(getSafeValue("group", row));
        option.setRequired(getSafeValue("required", row));
        option.setType(getSafeValue("type", row));
        option.setJavaType(getSafeValue("javaType", row));
        option.setEnums(getSafeValue("enum", row));
        option.setPrefix(getSafeValue("prefix", row));
        option.setMultiValue(getSafeValue("multiValue", row));
        option.setDeprecated(getSafeValue("deprecated", row));
        option.setSecret(getSafeValue("secret", row));
        option.setDefaultValue(getSafeValue("defaultValue", row));
        option.setDescription(getSafeValue("description", row));
        // lets put required in the description
        if ("true".equals(option.getRequired())) {
            String desc = "*Required* " + option.getDescription();
            option.setDescription(desc);
        }
        // separate the options in path vs parameter so we can generate two different tables
        if ("path".equals(option.getKind())) {
            component.addEndpointPathOption(option);
        } else {
            component.addEndpointOption(option);
        }
        // group separate between different options
        if (oldGroup == null || !oldGroup.equals(option.getGroup())) {
            option.setNewGroup(true);
        }
        oldGroup = option.getGroup();
    }
    return component;
}
Also used : EndpointOptionModel(org.apache.camel.maven.packaging.model.EndpointOptionModel) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) Map(java.util.Map) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel)

Aggregations

Map (java.util.Map)3 ComponentOptionModel (org.apache.camel.maven.packaging.model.ComponentOptionModel)3 HashMap (java.util.HashMap)2 ComponentModel (org.apache.camel.maven.packaging.model.ComponentModel)2 EndpointOptionModel (org.apache.camel.maven.packaging.model.EndpointOptionModel)2 File (java.io.File)1 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1 HashSet (java.util.HashSet)1 UriParam (org.apache.camel.spi.UriParam)1 UriPath (org.apache.camel.spi.UriPath)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1