use of org.apache.camel.maven.packaging.model.DataFormatModel in project camel by apache.
the class PrepareReadmeMojo method generateDataFormatModel.
private DataFormatModel generateDataFormatModel(String json) {
List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("dataformat", json, false);
DataFormatModel dataFormat = new DataFormatModel();
dataFormat.setName(JSonSchemaHelper.getSafeValue("name", rows));
dataFormat.setTitle(JSonSchemaHelper.getSafeValue("title", rows));
dataFormat.setModelName(JSonSchemaHelper.getSafeValue("modelName", rows));
dataFormat.setDescription(JSonSchemaHelper.getSafeValue("description", rows));
dataFormat.setFirstVersion(JSonSchemaHelper.getSafeValue("firstVersion", rows));
dataFormat.setLabel(JSonSchemaHelper.getSafeValue("label", rows));
dataFormat.setDeprecated(JSonSchemaHelper.getSafeValue("deprecated", rows));
dataFormat.setJavaType(JSonSchemaHelper.getSafeValue("javaType", rows));
dataFormat.setGroupId(JSonSchemaHelper.getSafeValue("groupId", rows));
dataFormat.setArtifactId(JSonSchemaHelper.getSafeValue("artifactId", rows));
dataFormat.setVersion(JSonSchemaHelper.getSafeValue("version", rows));
return dataFormat;
}
use of org.apache.camel.maven.packaging.model.DataFormatModel in project camel by apache.
the class PrepareReadmeMojo method executeDataFormatsReadme.
protected void executeDataFormatsReadme(boolean coreOnly) throws MojoExecutionException, MojoFailureException {
Set<File> dataFormatFiles = new TreeSet<>();
if (dataFormatsDir != null && dataFormatsDir.isDirectory()) {
File[] files = dataFormatsDir.listFiles();
if (files != null) {
dataFormatFiles.addAll(Arrays.asList(files));
}
}
try {
List<DataFormatModel> models = new ArrayList<>();
for (File file : dataFormatFiles) {
String json = loadText(new FileInputStream(file));
DataFormatModel model = generateDataFormatModel(json);
// special for bindy as we have one common file
if (model.getName().startsWith("bindy")) {
model.setName("bindy");
}
models.add(model);
}
// sort the models
Collections.sort(models, new DataFormatComparator());
// how many different artifacts
int count = models.stream().map(DataFormatModel::getArtifactId).collect(toSet()).size();
// how many deprecated
long deprecated = models.stream().filter(m -> "true".equals(m.getDeprecated())).count();
// filter out camel-core
List<DataFormatModel> dataFormats = new ArrayList<>();
for (DataFormatModel model : models) {
if (coreOnly) {
if ("camel-core".equals(model.getArtifactId())) {
// only include core components
dataFormats.add(model);
}
} else {
// we want to include everything in the big file (also from camel-core)
dataFormats.add(model);
}
}
// 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 = templateDataFormats(dataFormats, count, deprecated);
boolean updated = updateDataFormats(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.DataFormatModel in project camel by apache.
the class SpringBootAutoConfigurationMojo method executeDataFormat.
private void executeDataFormat() throws MojoExecutionException, MojoFailureException {
// find the data format names
List<String> dataFormatNames = findDataFormatNames();
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 data formats
if (!dataFormatNames.isEmpty()) {
getLog().debug("Found " + dataFormatNames.size() + " dataformats");
List<DataFormatModel> allModels = new LinkedList<>();
for (String dataFormatName : dataFormatNames) {
String json = loadDataFormatJson(jsonFiles, dataFormatName);
if (json != null) {
DataFormatModel model = generateDataFormatModel(dataFormatName, json);
allModels.add(model);
}
}
// Group the models by implementing classes
Map<String, List<DataFormatModel>> grModels = allModels.stream().collect(Collectors.groupingBy(DataFormatModel::getJavaType));
for (String dataFormatClass : grModels.keySet()) {
List<DataFormatModel> dfModels = grModels.get(dataFormatClass);
// They should be equivalent
DataFormatModel model = dfModels.get(0);
List<String> aliases = dfModels.stream().map(DataFormatModel::getName).sorted().collect(Collectors.toList());
boolean hasOptions = !model.getDataFormatOptions().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 overrideDataFormatName = null;
if (aliases.size() > 1) {
// determine component name when there are multiple ones
overrideDataFormatName = model.getArtifactId().replace("camel-", "");
}
if (hasOptions) {
createDataFormatConfigurationSource(pkg, model, overrideDataFormatName);
}
createDataFormatAutoConfigurationSource(pkg, model, aliases, hasOptions, overrideDataFormatName);
createDataFormatSpringFactorySource(pkg, model);
}
}
}
use of org.apache.camel.maven.packaging.model.DataFormatModel in project camel by apache.
the class SpringBootAutoConfigurationMojo method generateDataFormatModel.
private static DataFormatModel generateDataFormatModel(String dataFormatName, String json) {
List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("dataformat", json, false);
DataFormatModel dataFormat = new DataFormatModel();
dataFormat.setTitle(getSafeValue("title", rows));
dataFormat.setName(getSafeValue("name", rows));
dataFormat.setModelName(getSafeValue("modelName", rows));
dataFormat.setDescription(getSafeValue("description", rows));
dataFormat.setFirstVersion(JSonSchemaHelper.getSafeValue("firstVersion", rows));
dataFormat.setLabel(getSafeValue("label", rows));
dataFormat.setDeprecated(getSafeValue("deprecated", rows));
dataFormat.setJavaType(getSafeValue("javaType", rows));
dataFormat.setGroupId(getSafeValue("groupId", rows));
dataFormat.setArtifactId(getSafeValue("artifactId", rows));
dataFormat.setVersion(getSafeValue("version", rows));
rows = JSonSchemaHelper.parseJsonSchema("properties", json, true);
for (Map<String, String> row : rows) {
DataFormatOptionModel option = new DataFormatOptionModel();
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.setEnumValues(getSafeValue("enum", row));
dataFormat.addDataFormatOption(option);
}
return dataFormat;
}
use of org.apache.camel.maven.packaging.model.DataFormatModel in project camel by apache.
the class SpringBootAutoConfigurationMojo method createDataFormatAutoConfigurationSource.
private void createDataFormatAutoConfigurationSource(String packageName, DataFormatModel model, List<String> dataFormatAliases, boolean hasOptions, String overrideDataFormatName) throws MojoFailureException {
final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
int pos = model.getJavaType().lastIndexOf(".");
String name = model.getJavaType().substring(pos + 1);
name = name.replace("DataFormat", "DataFormatAutoConfiguration");
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("DataFormatAutoConfiguration", "DataFormatConfiguration");
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");
javaClass.addImport("org.apache.camel.RuntimeCamelException");
javaClass.addImport("org.apache.camel.spi.DataFormat");
javaClass.addImport("org.apache.camel.spi.DataFormatFactory");
String body = createDataFormatBody(model.getShortJavaType(), hasOptions);
String methodName = "configure" + model.getShortJavaType() + "Factory";
MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType("org.apache.camel.spi.DataFormatFactory");
method.addParameter("CamelContext", "camelContext").setFinal(true);
if (hasOptions) {
method.addParameter(configurationName, "configuration").setFinal(true);
}
// Determine all the aliases
// adding the '-dataformat' suffix to prevent collision with component names
String[] springBeanAliases = dataFormatAliases.stream().map(alias -> alias + "-dataformat-factory").toArray(size -> new String[size]);
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.dataformat", (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US)));
sortImports(javaClass);
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
writeSourceIfChanged(javaClass, fileName);
writeAdditionalSpringMetaData("camel", "dataformat", (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US));
}
Aggregations