Search in sources :

Example 1 with ComponentModel

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

the class PrepareReadmeMojo method executeComponentsReadme.

protected void executeComponentsReadme(boolean coreOnly) throws MojoExecutionException, MojoFailureException {
    Set<File> componentFiles = new TreeSet<>();
    if (componentsDir != null && componentsDir.isDirectory()) {
        File[] files = componentsDir.listFiles();
        if (files != null) {
            componentFiles.addAll(Arrays.asList(files));
        }
    }
    try {
        List<ComponentModel> models = new ArrayList<>();
        for (File file : componentFiles) {
            String json = loadText(new FileInputStream(file));
            ComponentModel model = generateComponentModel(json, coreOnly);
            // filter out alternative schemas which reuses documentation
            boolean add = true;
            if (!model.getAlternativeSchemes().isEmpty()) {
                String first = model.getAlternativeSchemes().split(",")[0];
                if (!model.getScheme().equals(first)) {
                    add = false;
                }
            }
            if (add) {
                models.add(model);
            }
        }
        // sort the models
        Collections.sort(models, new ComponentComparator());
        // filter out unwanted components
        List<ComponentModel> components = new ArrayList<>();
        for (ComponentModel model : models) {
            if (coreOnly) {
                if ("camel-core".equals(model.getArtifactId())) {
                    // only include core components
                    components.add(model);
                }
            } else {
                // we want to include everything in the big file (also from camel-core)
                components.add(model);
            }
        }
        // how many different artifacts
        int count = components.stream().map(ComponentModel::getArtifactId).collect(toSet()).size();
        // how many deprecated
        long deprecated = components.stream().filter(c -> "true".equals(c.getDeprecated())).count();
        // update the big readme file in the core/components dir
        File file;
        if (coreOnly) {
            file = new File(readmeCoreDir, "readme.adoc");
        } else {
            file = new File(readmeComponentsDir, "readme.adoc");
        }
        // update regular components
        boolean exists = file.exists();
        String changed = templateComponents(components, count, deprecated);
        boolean updated = updateComponents(file, changed);
        if (updated) {
            getLog().info("Updated readme.adoc file: " + file);
        } else if (exists) {
            getLog().debug("No changes to readme.adoc file: " + file);
        } else {
            getLog().warn("No readme.adoc file: " + file);
        }
    } catch (IOException e) {
        throw new MojoFailureException("Error due " + e.getMessage(), e);
    }
}
Also used : Arrays(java.util.Arrays) PackageHelper.writeText(org.apache.camel.maven.packaging.PackageHelper.writeText) MavenProjectHelper(org.apache.maven.project.MavenProjectHelper) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) ArrayList(java.util.ArrayList) OtherModel(org.apache.camel.maven.packaging.model.OtherModel) MavenProject(org.apache.maven.project.MavenProject) Map(java.util.Map) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TemplateRuntime(org.mvel2.templates.TemplateRuntime) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) Collections(edu.emory.mathcs.backport.java.util.Collections) EipModel(org.apache.camel.maven.packaging.model.EipModel) MojoFailureException(org.apache.maven.plugin.MojoFailureException) List(java.util.List) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) Comparator(java.util.Comparator) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) AbstractMojo(org.apache.maven.plugin.AbstractMojo) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TreeSet(java.util.TreeSet) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) File(java.io.File)

Example 2 with ComponentModel

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

the class SpringBootAutoConfigurationMojo method executeComponent.

private void executeComponent() throws MojoExecutionException, MojoFailureException {
    // find the component names
    List<String> componentNames = findComponentNames();
    final Set<File> jsonFiles = new TreeSet<File>();
    PackageHelper.findJsonFiles(buildDir, jsonFiles, new PackageHelper.CamelComponentsModelFilter());
    // create auto configuration for the components
    if (!componentNames.isEmpty()) {
        getLog().debug("Found " + componentNames.size() + " components");
        List<ComponentModel> allModels = new LinkedList<>();
        for (String componentName : componentNames) {
            String json = loadComponentJson(jsonFiles, componentName);
            if (json != null) {
                ComponentModel model = generateComponentModel(componentName, json);
                allModels.add(model);
            }
        }
        // Group the models by implementing classes
        Map<String, List<ComponentModel>> grModels = allModels.stream().collect(Collectors.groupingBy(ComponentModel::getJavaType));
        for (String componentClass : grModels.keySet()) {
            List<ComponentModel> compModels = grModels.get(componentClass);
            // They should be equivalent
            ComponentModel model = compModels.get(0);
            List<String> aliases = compModels.stream().map(ComponentModel::getScheme).sorted().collect(Collectors.toList());
            // resolvePropertyPlaceholders is an option which only make sense to use if the component has other options
            boolean hasOptions = model.getComponentOptions().stream().anyMatch(o -> !o.getName().equals("resolvePropertyPlaceholders"));
            // use springboot as sub package name so the code is not in normal
            // package so the Spring Boot JARs can be optional at runtime
            int pos = model.getJavaType().lastIndexOf(".");
            String pkg = model.getJavaType().substring(0, pos) + ".springboot";
            String overrideComponentName = null;
            if (aliases.size() > 1) {
                // determine component name when there are multiple ones
                overrideComponentName = model.getArtifactId().replace("camel-", "");
            }
            if (hasOptions) {
                createComponentConfigurationSource(pkg, model, overrideComponentName);
            }
            createComponentAutoConfigurationSource(pkg, model, aliases, hasOptions, overrideComponentName);
            createComponentSpringFactorySource(pkg, model);
        }
    }
}
Also used : LinkedList(java.util.LinkedList) TreeSet(java.util.TreeSet) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) File(java.io.File)

Example 3 with ComponentModel

use of org.apache.camel.maven.packaging.model.ComponentModel 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 4 with ComponentModel

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

the class PrepareUserGuideMojo method generateComponentModel.

private ComponentModel generateComponentModel(String json) {
    List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("component", json, false);
    ComponentModel component = new ComponentModel(true);
    component.setScheme(JSonSchemaHelper.getSafeValue("scheme", rows));
    component.setSyntax(JSonSchemaHelper.getSafeValue("syntax", rows));
    component.setAlternativeSyntax(JSonSchemaHelper.getSafeValue("alternativeSyntax", rows));
    component.setAlternativeSchemes(JSonSchemaHelper.getSafeValue("alternativeSchemes", rows));
    component.setTitle(JSonSchemaHelper.getSafeValue("title", rows));
    component.setDescription(JSonSchemaHelper.getSafeValue("description", rows));
    component.setFirstVersion(JSonSchemaHelper.getSafeValue("firstVersion", rows));
    component.setLabel(JSonSchemaHelper.getSafeValue("label", rows));
    component.setDeprecated(JSonSchemaHelper.getSafeValue("deprecated", rows));
    component.setConsumerOnly(JSonSchemaHelper.getSafeValue("consumerOnly", rows));
    component.setProducerOnly(JSonSchemaHelper.getSafeValue("producerOnly", rows));
    component.setJavaType(JSonSchemaHelper.getSafeValue("javaType", rows));
    component.setGroupId(JSonSchemaHelper.getSafeValue("groupId", rows));
    component.setArtifactId(JSonSchemaHelper.getSafeValue("artifactId", rows));
    component.setVersion(JSonSchemaHelper.getSafeValue("version", rows));
    return component;
}
Also used : ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) Map(java.util.Map)

Example 5 with ComponentModel

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

the class SpringBootAutoConfigurationMojo method createComponentAutoConfigurationSource.

private void createComponentAutoConfigurationSource(String packageName, ComponentModel model, List<String> componentAliases, boolean hasOptions, 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", "ComponentAutoConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    javaClass.getJavaDoc().setFullText(doc);
    javaClass.addAnnotation(Configuration.class);
    javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class");
    javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    String configurationName = name.replace("ComponentAutoConfiguration", "ComponentConfiguration");
    if (hasOptions) {
        AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
        ann.setLiteralValue("value", configurationName + ".class");
        javaClass.addImport("java.util.HashMap");
        javaClass.addImport("java.util.Map");
        javaClass.addImport("org.apache.camel.util.IntrospectionSupport");
    }
    javaClass.addImport(model.getJavaType());
    javaClass.addImport("org.apache.camel.CamelContext");
    // add method for auto configure
    String body = createComponentBody(model.getShortJavaType(), hasOptions);
    String methodName = "configure" + model.getShortJavaType();
    MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType(model.getShortJavaType()).addThrows(Exception.class);
    method.addParameter("CamelContext", "camelContext");
    if (hasOptions) {
        method.addParameter(configurationName, "configuration");
    }
    // Determine all the aliases
    String[] springBeanAliases = componentAliases.stream().map(alias -> alias + "-component").toArray(size -> new String[size]);
    method.addAnnotation(Lazy.class);
    method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
    // Generate Condition
    javaClass.addNestedType(createConditionType(javaClass, "camel.component", (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US)));
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
    writeAdditionalSpringMetaData("camel", "component", (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US));
}
Also used : NestedConfigurationProperty(org.springframework.boot.context.properties.NestedConfigurationProperty) Arrays(java.util.Arrays) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) URL(java.net.URL) Type(org.jboss.forge.roaster.model.Type) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) GsonBuilder(com.google.gson.GsonBuilder) UriParam(org.apache.camel.spi.UriParam) ConditionContext(org.springframework.context.annotation.ConditionContext) URLClassLoader(java.net.URLClassLoader) Matcher(java.util.regex.Matcher) Roaster(org.jboss.forge.roaster.Roaster) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Gson(com.google.gson.Gson) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Configuration(org.springframework.context.annotation.Configuration) JSonSchemaHelper.getPropertyJavaType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyJavaType) List(java.util.List) RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) Resource(org.apache.maven.model.Resource) Formatter(org.jboss.forge.roaster.model.util.Formatter) DataFormatOptionModel(org.apache.camel.maven.packaging.model.DataFormatOptionModel) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Modifier(java.lang.reflect.Modifier) Strings(org.jboss.forge.roaster.model.util.Strings) Lazy(org.springframework.context.annotation.Lazy) JSonSchemaHelper.getSafeValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getSafeValue) Pattern(java.util.regex.Pattern) Conditional(org.springframework.context.annotation.Conditional) AbstractMojo(org.apache.maven.plugin.AbstractMojo) Import(org.jboss.forge.roaster.model.source.Import) LanguageOptionModel(org.apache.camel.maven.packaging.model.LanguageOptionModel) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) UriPath(org.apache.camel.spi.UriPath) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) HashSet(java.util.HashSet) JavaType(org.jboss.forge.roaster.model.JavaType) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) EndpointOptionModel(org.apache.camel.maven.packaging.model.EndpointOptionModel) LinkedList(java.util.LinkedList) PropertySource(org.jboss.forge.roaster.model.source.PropertySource) JSonSchemaHelper.getPropertyDefaultValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyDefaultValue) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SpringBootCondition(org.springframework.boot.autoconfigure.condition.SpringBootCondition) ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) JSonSchemaHelper.getPropertyType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyType) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Bean(org.springframework.context.annotation.Bean) AnnotatedTypeMetadata(org.springframework.core.type.AnnotatedTypeMetadata) Collections(java.util.Collections) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) InputStream(java.io.InputStream) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Conditional(org.springframework.context.annotation.Conditional) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

ComponentModel (org.apache.camel.maven.packaging.model.ComponentModel)9 Map (java.util.Map)6 File (java.io.File)5 TreeSet (java.util.TreeSet)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 ComponentOptionModel (org.apache.camel.maven.packaging.model.ComponentOptionModel)3 EndpointOptionModel (org.apache.camel.maven.packaging.model.EndpointOptionModel)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 Arrays (java.util.Arrays)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 PackageHelper.loadText (org.apache.camel.maven.packaging.PackageHelper.loadText)2 DataFormatModel (org.apache.camel.maven.packaging.model.DataFormatModel)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1