Search in sources :

Example 1 with LanguageModel

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

the class PrepareReadmeMojo method executeLanguagesReadme.

protected void executeLanguagesReadme(boolean coreOnly) throws MojoExecutionException, MojoFailureException {
    Set<File> languageFiles = new TreeSet<>();
    if (languagesDir != null && languagesDir.isDirectory()) {
        File[] files = languagesDir.listFiles();
        if (files != null) {
            languageFiles.addAll(Arrays.asList(files));
        }
    }
    try {
        List<LanguageModel> models = new ArrayList<>();
        for (File file : languageFiles) {
            String json = loadText(new FileInputStream(file));
            LanguageModel model = generateLanguageModel(json);
            models.add(model);
        }
        // sort the models
        Collections.sort(models, new LanguageComparator());
        // filter out camel-core
        List<LanguageModel> languages = new ArrayList<>();
        for (LanguageModel model : models) {
            if (coreOnly) {
                if ("camel-core".equals(model.getArtifactId())) {
                    // only include core components
                    languages.add(model);
                }
            } else {
                // we want to include everything in the big file (also from camel-core)
                languages.add(model);
            }
        }
        // how many different artifacts
        int count = languages.stream().map(LanguageModel::getArtifactId).collect(toSet()).size();
        // how many deprecated
        long deprecated = languages.stream().filter(l -> "true".equals(l.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 data formats
        boolean exists = file.exists();
        String changed = templateLanguages(languages, count, deprecated);
        boolean updated = updateLanguages(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) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) File(java.io.File)

Example 2 with LanguageModel

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

the class PrepareUserGuideMojo method generateLanguageModel.

private LanguageModel generateLanguageModel(String json) {
    List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("language", json, false);
    LanguageModel language = new LanguageModel();
    language.setTitle(JSonSchemaHelper.getSafeValue("title", rows));
    language.setName(JSonSchemaHelper.getSafeValue("name", rows));
    language.setModelName(JSonSchemaHelper.getSafeValue("modelName", rows));
    language.setDescription(JSonSchemaHelper.getSafeValue("description", rows));
    language.setFirstVersion(JSonSchemaHelper.getSafeValue("firstVersion", rows));
    language.setLabel(JSonSchemaHelper.getSafeValue("label", rows));
    language.setDeprecated(JSonSchemaHelper.getSafeValue("deprecated", rows));
    language.setJavaType(JSonSchemaHelper.getSafeValue("javaType", rows));
    language.setGroupId(JSonSchemaHelper.getSafeValue("groupId", rows));
    language.setArtifactId(JSonSchemaHelper.getSafeValue("artifactId", rows));
    language.setVersion(JSonSchemaHelper.getSafeValue("version", rows));
    return language;
}
Also used : LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) Map(java.util.Map)

Example 3 with LanguageModel

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

the class UpdateReadmeMojo method executeLanguage.

private void executeLanguage() throws MojoExecutionException, MojoFailureException {
    // find the language names
    List<String> languageNames = findLanguageNames();
    final Set<File> jsonFiles = new TreeSet<File>();
    PackageHelper.findJsonFiles(buildDir, jsonFiles, new PackageHelper.CamelComponentsModelFilter());
    // only if there is language we should update the documentation files
    if (!languageNames.isEmpty()) {
        getLog().debug("Found " + languageNames.size() + " languages");
        for (String languageName : languageNames) {
            String json = loadLanguageJson(jsonFiles, languageName);
            if (json != null) {
                File file = new File(docDir, languageName + "-language.adoc");
                LanguageModel model = generateLanguageModel(languageName, json);
                String docTitle = model.getTitle() + " Language";
                boolean deprecated = "true".equals(model.getDeprecated());
                if (deprecated) {
                    docTitle += " (deprecated)";
                }
                boolean exists = file.exists();
                boolean updated;
                updated = updateTitles(file, docTitle);
                updated |= updateAvailableFrom(file, model.getFirstVersion());
                String options = templateLanguageOptions(model);
                updated |= updateLanguageOptions(file, options);
                if (updated) {
                    getLog().info("Updated doc file: " + file);
                } else if (exists) {
                    getLog().debug("No changes to doc file: " + file);
                } else {
                    getLog().warn("No language doc file: " + file);
                    if (isFailFast()) {
                        throw new MojoExecutionException("Failed build due failFast=true");
                    }
                }
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) File(java.io.File)

Example 4 with LanguageModel

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

the class SpringBootAutoConfigurationMojo method createLanguageAutoConfigurationSource.

private void createLanguageAutoConfigurationSource(String packageName, LanguageModel model, List<String> languageAliases, boolean hasOptions, String overrideLanguageName) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Language", "LanguageAutoConfiguration");
    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("LanguageAutoConfiguration", "LanguageConfiguration");
    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("org.apache.camel.CamelContextAware");
    javaClass.addImport(model.getJavaType());
    javaClass.addImport("org.apache.camel.CamelContext");
    String body = createLanguageBody(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");
    method.addParameter(configurationName, "configuration");
    // Determine all the aliases
    // adding the '-language' suffix to prevent collision with component names
    String[] springBeanAliases = languageAliases.stream().map(alias -> alias + "-language").toArray(size -> new String[size]);
    method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
    method.addAnnotation(Scope.class).setStringValue("prototype");
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
    // Generate Condition
    javaClass.addNestedType(createConditionType(javaClass, "camel.language", (overrideLanguageName != null ? overrideLanguageName : model.getName()).toLowerCase(Locale.US)));
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
    writeAdditionalSpringMetaData("camel", "language", (overrideLanguageName != null ? overrideLanguageName : model.getName()).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) Scope(org.springframework.context.annotation.Scope)

Example 5 with LanguageModel

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

the class SpringBootAutoConfigurationMojo method executeLanguage.

private void executeLanguage() throws MojoExecutionException, MojoFailureException {
    // find the language names
    List<String> languageNames = findLanguageNames();
    final Set<File> jsonFiles = new TreeSet<File>();
    // we can reuse the component model filter
    PackageHelper.findJsonFiles(buildDir, jsonFiles, new PackageHelper.CamelComponentsModelFilter());
    // create auto configuration for the languages
    if (!languageNames.isEmpty()) {
        getLog().debug("Found " + languageNames.size() + " languages");
        List<LanguageModel> allModels = new LinkedList<>();
        for (String languageName : languageNames) {
            String json = loadLanguageJson(jsonFiles, languageName);
            if (json != null) {
                LanguageModel model = generateLanguageModel(languageName, json);
                allModels.add(model);
            }
        }
        // Group the models by implementing classes
        Map<String, List<LanguageModel>> grModels = allModels.stream().collect(Collectors.groupingBy(LanguageModel::getJavaType));
        for (String languageClass : grModels.keySet()) {
            List<LanguageModel> dfModels = grModels.get(languageClass);
            // They should be equivalent
            LanguageModel model = dfModels.get(0);
            List<String> aliases = dfModels.stream().map(LanguageModel::getName).sorted().collect(Collectors.toList());
            boolean hasOptions = !model.getLanguageOptions().isEmpty();
            // 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 overrideLanguageName = null;
            if (aliases.size() > 1) {
                // determine language name when there are multiple ones
                overrideLanguageName = model.getArtifactId().replace("camel-", "");
            }
            if (hasOptions) {
                createLanguageConfigurationSource(pkg, model, overrideLanguageName);
            }
            createLanguageAutoConfigurationSource(pkg, model, aliases, hasOptions, overrideLanguageName);
            createLanguageSpringFactorySource(pkg, model);
        }
    }
}
Also used : LinkedList(java.util.LinkedList) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) File(java.io.File)

Aggregations

LanguageModel (org.apache.camel.maven.packaging.model.LanguageModel)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 LanguageOptionModel (org.apache.camel.maven.packaging.model.LanguageOptionModel)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 ComponentModel (org.apache.camel.maven.packaging.model.ComponentModel)2 DataFormatModel (org.apache.camel.maven.packaging.model.DataFormatModel)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1