use of org.apache.camel.maven.connector.model.ComponentModel in project camel by apache.
the class SpringBootAutoConfigurationMojo method createConnectorConfigurationSource.
private void createConnectorConfigurationSource(String packageName, ComponentModel model, String javaType, String connectorScheme, List<String> componentOptions) throws MojoFailureException {
final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
int pos = javaType.lastIndexOf(".");
String name = javaType.substring(pos + 1);
name = name.replace("Component", "ConnectorConfiguration");
javaClass.setPackage(packageName).setName(name);
String doc = "Generated by camel-connector-maven-plugin - do not edit this file!";
if (!Strings.isBlank(model.getDescription())) {
doc = model.getDescription() + "\n\n" + doc;
}
// replace Component with Connector
doc = doc.replaceAll("Component", "Connector");
doc = doc.replaceAll("component", "connector");
javaClass.getJavaDoc().setFullText(doc);
// compute the configuration prefix to use with spring boot configuration
String prefix = "";
if (!"false".equalsIgnoreCase(configurationPrefix)) {
// make sure prefix is in lower case
prefix = configurationPrefix.toLowerCase(Locale.US);
if (!prefix.endsWith(".")) {
prefix += ".";
}
}
prefix += connectorScheme.toLowerCase(Locale.US);
javaClass.addAnnotation("org.springframework.boot.context.properties.ConfigurationProperties").setStringValue("prefix", prefix);
for (ComponentOptionModel option : model.getComponentOptions()) {
// only include the options that has been explicit configured in the camel-connector.json file
boolean accepted = false;
if (componentOptions != null) {
accepted = componentOptions.stream().anyMatch(o -> o.equals(option.getName()));
}
if (accepted) {
String type = option.getJavaType();
PropertySource<JavaClassSource> prop = javaClass.addProperty(type, option.getName());
if ("true".equals(option.getDeprecated())) {
prop.getField().addAnnotation(Deprecated.class);
prop.getAccessor().addAnnotation(Deprecated.class);
prop.getMutator().addAnnotation(Deprecated.class);
// DeprecatedConfigurationProperty must be on getter when deprecated
prop.getAccessor().addAnnotation(DeprecatedConfigurationProperty.class);
}
if (!Strings.isBlank(option.getDescription())) {
prop.getField().getJavaDoc().setFullText(option.getDescription());
}
if (!Strings.isBlank(option.getDefaultValue())) {
if ("java.lang.String".equals(option.getJavaType())) {
prop.getField().setStringInitializer(option.getDefaultValue());
} else if ("long".equals(option.getJavaType()) || "java.lang.Long".equals(option.getJavaType())) {
// the value should be a Long number
String value = option.getDefaultValue() + "L";
prop.getField().setLiteralInitializer(value);
} else if ("integer".equals(option.getType()) || "boolean".equals(option.getType())) {
prop.getField().setLiteralInitializer(option.getDefaultValue());
} else if (!Strings.isBlank(option.getEnums())) {
String enumShortName = type.substring(type.lastIndexOf(".") + 1);
prop.getField().setLiteralInitializer(enumShortName + "." + option.getDefaultValue());
javaClass.addImport(model.getJavaType());
}
}
}
}
sortImports(javaClass);
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
writeSourceIfChanged(javaClass, fileName);
}
use of org.apache.camel.maven.connector.model.ComponentModel in project camel by apache.
the class SpringBootAutoConfigurationMojo method generateComponentModel.
private static ComponentModel generateComponentModel(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(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);
}
return component;
}
use of org.apache.camel.maven.connector.model.ComponentModel in project camel by apache.
the class SpringBootAutoConfigurationMojo method executeConnector.
@SuppressWarnings("unchecked")
private void executeConnector() throws Exception {
String javaType = null;
String connectorScheme = null;
List<String> componentOptions = null;
File file = new File(classesDirectory, "camel-connector.json");
if (file.exists()) {
ObjectMapper mapper = new ObjectMapper();
Map dto = mapper.readValue(file, Map.class);
javaType = (String) dto.get("javaType");
connectorScheme = (String) dto.get("scheme");
componentOptions = (List) dto.get("componentOptions");
}
// find the component dependency and get its .json file
file = new File(classesDirectory, "camel-component-schema.json");
if (file.exists() && javaType != null && connectorScheme != null) {
String json = loadText(new FileInputStream(file));
ComponentModel model = generateComponentModel(json);
// 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 = javaType.lastIndexOf(".");
String pkg = javaType.substring(0, pos) + ".springboot";
// we only create spring boot auto configuration if there is options to configure
if (hasOptions) {
getLog().info("Generating Spring Boot AutoConfiguration for Connector: " + model.getScheme());
createConnectorConfigurationSource(pkg, model, javaType, connectorScheme, componentOptions);
createConnectorAutoConfigurationSource(pkg, hasOptions, javaType, connectorScheme);
createConnectorSpringFactorySource(pkg, javaType);
}
}
}
Aggregations