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);
}
}
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;
}
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");
}
}
}
}
}
}
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));
}
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);
}
}
}
Aggregations