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