use of org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel in project camel-kafka-connector by apache.
the class CamelKafkaConnectorKameletUpdateMojo method addConnectorOptions.
private void addConnectorOptions(String sanitizedName, ConnectorType ct, JavaClass javaClass, Method confMethod, String propertyQualifier, KameletPropertyModel kameletProperty, List<CamelKafkaConnectorOptionModel> listOptions, Set<String> kameletRequiredProperties) {
String propertyName = kameletProperty.getName();
String regex = "([A-Z][a-z]+)";
String replacement = "$1_";
String propertyPrefix = "CAMEL_" + ct + "_" + sanitizedName.replace("-", "").toUpperCase() + "_" + propertyQualifier.toUpperCase() + "_" + StringUtils.capitalize(propertyName).replaceAll(regex, replacement).toUpperCase();
String propertyValue = "camel.kamelet." + name + "." + propertyName;
String confFieldName = propertyPrefix + "CONF";
javaClass.addField().setFinal(true).setPublic().setStatic(true).setName(confFieldName).setType(String.class).setStringInitializer(propertyValue);
String docFieldName = propertyPrefix + "DOC";
String docLiteralInitializer = kameletProperty.getDescription().replace("\n", " ");
if (kameletProperty.getExample() != null) {
docLiteralInitializer = docLiteralInitializer + " Example: " + kameletProperty.getExample().replace("\n", " ");
}
javaClass.addField().setFinal(true).setPublic().setStatic(true).setName(docFieldName).setType(String.class).setStringInitializer(docLiteralInitializer);
String defaultFieldName = propertyPrefix + "DEFAULT";
Class<?> defaultValueClass = PRIMITIVE_TYPES_TO_CLASS_MAP.getOrDefault(kameletProperty.getType(), String.class);
String type = defaultValueClass.getSimpleName().toLowerCase();
String defaultValueClassLiteralInitializer;
if (kameletProperty.getDefaultValue() == null) {
// Handling null default camel options values (that means there is no default value).
defaultValueClassLiteralInitializer = "null";
} else {
defaultValueClassLiteralInitializer = kameletProperty.getDefaultValue();
if (defaultValueClass.equals(String.class)) {
defaultValueClassLiteralInitializer = "\"" + defaultValueClassLiteralInitializer + "\"";
}
if (defaultValueClass.equals(Long.class) || defaultValueClass.equals(Integer.class) || defaultValueClass.equals(int.class)) {
if (type.equalsIgnoreCase("duration")) {
if (defaultValueClassLiteralInitializer.endsWith("ms")) {
defaultValueClassLiteralInitializer = StringUtils.removeEnd(defaultValueClassLiteralInitializer, "ms");
} else {
defaultValueClassLiteralInitializer = Long.toString(TimeUtils.toMilliSeconds(defaultValueClassLiteralInitializer));
}
}
if (defaultValueClass.equals(Long.class) && !defaultValueClassLiteralInitializer.endsWith("L")) {
defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + "L";
}
}
if (defaultValueClass.equals(Float.class)) {
defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + "F";
}
if (defaultValueClass.equals(Double.class)) {
defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + "D";
}
}
javaClass.addField().setFinal(true).setPublic().setStatic(true).setName(defaultFieldName).setType(defaultValueClass).setLiteralInitializer(defaultValueClassLiteralInitializer);
String confType;
if ("password".equals(kameletProperty.getFormat())) {
confType = PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.getOrDefault(type, CONFIG_DEF_TYPE_PASSWORD);
} else {
confType = PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.getOrDefault(type, CONFIG_DEF_TYPE_STRING);
}
boolean isRequired = kameletRequiredProperties.contains(kameletProperty.getName());
String confPriority = isRequired ? CONFIG_DEF_IMPORTANCE_HIGH : CONFIG_DEF_IMPORTANCE_MEDIUM;
confMethod.setBody(confMethod.getBody() + "conf.define(" + confFieldName + ", " + confType + ", " + defaultFieldName + ", " + confPriority + ", " + docFieldName + ");\n");
CamelKafkaConnectorOptionModel optionModel = new CamelKafkaConnectorOptionModel();
optionModel.setName(propertyValue);
optionModel.setDescription(docLiteralInitializer);
optionModel.setPriority(StringUtils.removeStart(confPriority, CONFIG_DEF_IMPORTANCE_PREFIX));
optionModel.setDefaultValue(defaultValueClassLiteralInitializer.equals("null") ? null : defaultValueClassLiteralInitializer);
optionModel.setRequired(String.valueOf(isRequired));
// XXX: kamelets dose not support enum like properties type yet.
listOptions.add(optionModel);
}
use of org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel in project camel-kafka-connector by apache.
the class CamelKafkaConnectorUpdateMojo method createClasses.
private void createClasses(String sanitizedName, File connectorDir, ComponentModel model, ConnectorType ct) throws MojoFailureException, ResourceNotFoundException, FileResourceCreationException, IOException, MojoExecutionException {
String ctCapitalizedName = StringUtils.capitalize(ct.name().toLowerCase());
String ctLowercaseName = ct.name().toLowerCase();
String packageName = "org.apache.camel.kafkaconnector." + RESERVED_WORDS_SUBSTITUTION_MAP.getOrDefault(sanitizedName.replace("-", ""), sanitizedName.replace("-", ""));
Map<String, String> additionalProperties = new HashMap<>();
Properties properties = new Properties();
try (InputStream stream = new FileInputStream(rm.getResourceAsFile(fixDependenciesProperties))) {
properties.load(stream);
}
String commonPropertyValue = properties.getProperty(ADDITIONAL_COMMON_PROPERTIES_PROPERTY_PREFIX + getMainDepArtifactId());
getLog().debug("Additional common connector properties: " + commonPropertyValue);
addProperties(additionalProperties, commonPropertyValue);
String sourceOrSinkPropertyValue = properties.getProperty(ADDITIONAL_COMMON_PROPERTIES_PROPERTY_PREFIX + ctLowercaseName + "_" + getMainDepArtifactId());
getLog().debug("Additional " + ctLowercaseName + " connector properties: " + sourceOrSinkPropertyValue);
addProperties(additionalProperties, sourceOrSinkPropertyValue);
// Camel{sanitizedName}{Sink,Source}ConnectorConfig.java
String javaClassConnectorConfigName = "Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "ConnectorConfig";
final JavaClass javaClassConnectorConfig = new JavaClass(getProjectClassLoader());
javaClassConnectorConfig.setPackage(packageName);
javaClassConnectorConfig.setName(javaClassConnectorConfigName);
javaClassConnectorConfig.addAnnotation(Generated.class).setStringValue("value", "This class has been generated by camel-kafka-connector-generator-maven-plugin, remove this annotation to prevent it from being generated.");
javaClassConnectorConfig.extendSuperType("Camel" + ctCapitalizedName + "ConnectorConfig");
javaClassConnectorConfig.addImport("java.util.Map");
javaClassConnectorConfig.addImport("org.apache.camel.kafkaconnector.Camel" + ctCapitalizedName + "ConnectorConfig");
javaClassConnectorConfig.addImport("org.apache.kafka.common.config.ConfigDef");
javaClassConnectorConfig.addMethod().setConstructor(true).setName(javaClassConnectorConfigName).addParameter("ConfigDef", "config").addParameter("Map<String, String>", "parsedConfig").setPublic().setBody("super(config, parsedConfig);");
javaClassConnectorConfig.addMethod().setConstructor(true).setName(javaClassConnectorConfigName).addParameter("Map<String, String>", "parsedConfig").setPublic().setBody("this(conf(), parsedConfig);");
Method confMethod = javaClassConnectorConfig.addMethod().setConstructor(false).setName("conf").setReturnType("ConfigDef").setPublic().setStatic().setBody("ConfigDef conf = new ConfigDef(Camel" + ctCapitalizedName + "ConnectorConfig.conf());\n");
Predicate<? super BaseOptionModel> filterEndpointOptions;
switch(ct) {
case SINK:
filterEndpointOptions = new Predicate<BaseOptionModel>() {
@Override
public boolean test(BaseOptionModel optionModel) {
return optionModel.getLabel() == null || optionModel.getLabel().contains("producer") || (!optionModel.getLabel().contains("producer") && !optionModel.getLabel().contains("consumer"));
}
};
break;
case SOURCE:
filterEndpointOptions = new Predicate<BaseOptionModel>() {
@Override
public boolean test(BaseOptionModel optionModel) {
return optionModel.getLabel() == null || optionModel.getLabel().contains("consumer") || (!optionModel.getLabel().contains("producer") && !optionModel.getLabel().contains("consumer"));
}
};
break;
default:
throw new UnsupportedOperationException("Connector type not supported: " + ct + " must be one of " + ConnectorType.SINK + ", " + ConnectorType.SOURCE);
}
List<CamelKafkaConnectorOptionModel> listOptions = new ArrayList<>();
model.getEndpointPathOptions().stream().filter(filterEndpointOptions).forEachOrdered(epo -> addConnectorOptions(sanitizedName, ct, javaClassConnectorConfig, confMethod, "PATH", ctLowercaseName, "path", epo, listOptions));
model.getEndpointParameterOptions().stream().filter(filterEndpointOptions).forEachOrdered(epo -> addConnectorOptions(sanitizedName, ct, javaClassConnectorConfig, confMethod, "ENDPOINT", ctLowercaseName, "endpoint", epo, listOptions));
model.getComponentOptions().stream().filter(filterEndpointOptions).forEachOrdered(co -> addConnectorOptions(sanitizedName, ct, javaClassConnectorConfig, confMethod, "COMPONENT", "component", sanitizedName, co, listOptions));
confMethod.setBody(confMethod.getBody() + "return conf;");
String javaClassConnectorConfigFileName = packageName.replaceAll("\\.", "\\/") + File.separator + javaClassConnectorConfigName + ".java";
MavenUtils.writeSourceIfChanged(javaClassConnectorConfig, javaClassConnectorConfigFileName, false, connectorDir, rm.getResourceAsFile(javaFilesHeader));
// Camel{sanitizedName}{Sink,Source}Task.java
String javaClassTaskName = "Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "Task";
final JavaClass javaClassTask = new JavaClass(getProjectClassLoader());
javaClassTask.setPackage(packageName);
javaClassTask.setName(javaClassTaskName);
javaClassTask.addAnnotation(Generated.class).setStringValue("value", "This class has been generated by camel-kafka-connector-generator-maven-plugin, remove this annotation to prevent it from being generated.");
javaClassTask.extendSuperType("Camel" + ctCapitalizedName + "Task");
javaClassTask.addImport("java.util.HashMap");
javaClassTask.addImport("java.util.Map");
javaClassTask.addImport("org.apache.camel.kafkaconnector.Camel" + ctCapitalizedName + "ConnectorConfig");
javaClassTask.addImport("org.apache.camel.kafkaconnector.Camel" + ctCapitalizedName + "Task");
javaClassTask.addMethod().setConstructor(false).setName("getCamel" + ctCapitalizedName + "ConnectorConfig").setProtected().addParameter("Map<String, String>", "props").setReturnType("Camel" + ctCapitalizedName + "ConnectorConfig").setBody("return new Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "ConnectorConfig(props);").addAnnotation(Override.class);
Method getDefaultConfigMethod = javaClassTask.addMethod().setConstructor(false).setName("getDefaultConfig").setProtected().setReturnType("Map<String, String>").setBody("return new HashMap<String, String>() {{\n");
getDefaultConfigMethod.setBody(getDefaultConfigMethod.getBody() + " put(Camel" + ctCapitalizedName + "ConnectorConfig.CAMEL_" + ct + "_COMPONENT_CONF, \"" + model.getScheme() + "\");\n");
for (String key : new TreeSet<String>(additionalProperties.keySet())) {
getDefaultConfigMethod.setBody(getDefaultConfigMethod.getBody() + " put(\"" + key + "\", \"" + additionalProperties.get(key) + "\");\n");
}
getDefaultConfigMethod.setBody(getDefaultConfigMethod.getBody() + "}};\n");
getDefaultConfigMethod.addAnnotation(Override.class);
String javaClassTaskFileName = packageName.replaceAll("\\.", "\\/") + File.separator + javaClassTaskName + ".java";
MavenUtils.writeSourceIfChanged(javaClassTask, javaClassTaskFileName, false, connectorDir, rm.getResourceAsFile(javaFilesHeader));
// Camel{sanitizedName}{Sink,Source}Connector.java
String javaClassConnectorName = "Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "Connector";
final JavaClass javaClassConnector = new JavaClass(getProjectClassLoader());
javaClassConnector.setPackage(packageName);
javaClassConnector.setName(javaClassConnectorName);
javaClassConnector.addAnnotation(Generated.class).setStringValue("value", "This class has been generated by camel-kafka-connector-generator-maven-plugin, remove this annotation to prevent it from being generated.");
javaClassConnector.extendSuperType("Camel" + ctCapitalizedName + "Connector");
javaClassConnector.addImport("org.apache.camel.kafkaconnector.Camel" + ctCapitalizedName + "Connector");
javaClassConnector.addImport("org.apache.kafka.common.config.ConfigDef");
javaClassConnector.addImport("org.apache.kafka.connect.connector.Task");
javaClassConnector.addMethod().setConstructor(false).setName("config").setPublic().setReturnType("ConfigDef").setBody("return Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "ConnectorConfig.conf();").addAnnotation(Override.class);
javaClassConnector.addMethod().setConstructor(false).setName("taskClass").setPublic().setReturnType("Class<? extends Task>").setBody("return Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "Task.class;").addAnnotation(Override.class);
String javaClassConnectorFileName = packageName.replaceAll("\\.", "\\/") + File.separator + javaClassConnectorName + ".java";
MavenUtils.writeSourceIfChanged(javaClassConnector, javaClassConnectorFileName, false, connectorDir, rm.getResourceAsFile(javaFilesHeader));
List<String> convertersList = new ArrayList<>();
List<String> transformsList = new ArrayList<>();
List<String> aggregationStrategiesList = new ArrayList<>();
if (connectorDir != null && connectorDir.isDirectory()) {
File[] files = connectorDir.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory()) {
Collection<File> convertersElements = FileUtils.listFiles(file, new RegexFileFilter(".*Converter.java"), DirectoryFileFilter.DIRECTORY);
Collection<File> transformElements = FileUtils.listFiles(file, new RegexFileFilter(".*Transforms.java"), DirectoryFileFilter.DIRECTORY);
Collection<File> aggStrategiesElements = FileUtils.listFiles(file, new RegexFileFilter(".*AggregationStrategy.java"), DirectoryFileFilter.DIRECTORY);
for (File p : convertersElements) {
String filePath = p.getCanonicalPath();
String f = StringUtils.removeStart(filePath, connectorDir.getAbsolutePath() + "/src/main/java/");
String finalElement = StringUtils.replace(f, File.separator, ".");
String finalPath = StringUtils.removeEnd(finalElement, ".java");
convertersList.add(finalPath);
}
for (File p : transformElements) {
String filePath = p.getCanonicalPath();
String f = StringUtils.removeStart(filePath, connectorDir.getAbsolutePath() + "/src/main/java/");
String finalElement = StringUtils.replace(f, File.separator, ".");
String finalPath = StringUtils.removeEnd(finalElement, ".java");
transformsList.add(finalPath);
}
for (File p : aggStrategiesElements) {
String filePath = p.getCanonicalPath();
String f = StringUtils.removeStart(filePath, connectorDir.getAbsolutePath() + "/src/main/java/");
String finalElement = StringUtils.replace(f, File.separator, ".");
String finalPath = StringUtils.removeEnd(finalElement, ".java");
aggregationStrategiesList.add(finalPath);
}
}
}
}
}
// docs/examples/Camel{sanitizedName}{Sink,Source}.properties
try {
String examplesPropertiestemplate = null;
switch(ct) {
case SOURCE:
examplesPropertiestemplate = loadText(rm.getResourceAsFile(exampleSourcePropertiesFileTemplate));
break;
case SINK:
examplesPropertiestemplate = loadText(rm.getResourceAsFile(exampleSinkPropertiesFileTemplate));
break;
default:
break;
}
HashMap<String, Object> templateParams = new HashMap<>();
templateParams.put("connectorName", StringUtils.capitalize(sanitizedName));
templateParams.put("connectorClass", packageName + "." + javaClassConnectorName);
List<CamelKafkaConnectorOptionModel> mandatoryOptions = listOptions.stream().filter(o -> "HIGH".equalsIgnoreCase(o.getPriority())).sorted(Comparator.comparing(CamelKafkaConnectorOptionModel::getName, String.CASE_INSENSITIVE_ORDER)).collect(Collectors.toList());
templateParams.put("options", mandatoryOptions);
String examplePropertiesFileContent = (String) TemplateRuntime.eval(examplesPropertiestemplate, templateParams);
writeFileIfChanged(examplePropertiesFileContent, new File(connectorDir, "src/main/docs/examples/" + javaClassConnectorName + ".properties"), getLog());
} catch (Exception e) {
throw new MojoExecutionException("Error processing mvel examples properties template. Reason: " + e, e);
}
// generate json descriptor src/generated/resources/<connector-name>.json
writeJson(listOptions, model.getDescription(), connectorDir, ct, packageName + "." + javaClassConnectorName, convertersList, transformsList, aggregationStrategiesList);
// generate descriptor src/generated/descriptors/connector-{sink,source}.properties
writeDescriptors(connectorDir, ct);
}
use of org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel in project camel-kafka-connector by apache.
the class CamelKafkaConnectorCatalog method getConnectorOptionModel.
private List<CamelKafkaConnectorOptionModel> getConnectorOptionModel(JsonObject obj) {
List<CamelKafkaConnectorOptionModel> model = new ArrayList<>();
JsonObject wrapper = (JsonObject) obj.get("properties");
Set<String> options = wrapper.keySet();
for (String string : options) {
JsonObject object = (JsonObject) wrapper.get(string);
CamelKafkaConnectorOptionModel singleModel = new CamelKafkaConnectorOptionModel();
singleModel.setDefaultValue((String) object.get("defaultValue"));
singleModel.setPriority((String) object.get("priority"));
singleModel.setDescription((String) object.get("description"));
singleModel.setName((String) object.get("name"));
singleModel.setRequired((String) object.get("required"));
singleModel.setPossibleEnumValues((List<String>) object.get("enum"));
model.add(singleModel);
}
return model;
}
use of org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel in project camel-kafka-connector by apache.
the class CamelKafkaConnectorCatalogTest method checkAddedConnectorContainsCorrectPropertyValues.
private void checkAddedConnectorContainsCorrectPropertyValues(String connectorName) {
CamelKafkaConnectorOptionModel camelKafkaConnectorOptionModel = catalog.getConnectorsModel().get(connectorName).getOptions().get(0);
assertEquals("\"firstValue\"", camelKafkaConnectorOptionModel.getDefaultValue());
assertEquals("camel.component.my-test-connector.demo", camelKafkaConnectorOptionModel.getName());
assertEquals("MEDIUM", camelKafkaConnectorOptionModel.getPriority());
assertEquals("A demo description of the component", camelKafkaConnectorOptionModel.getDescription());
assertEquals(Arrays.asList("firstValue", "secondValue"), camelKafkaConnectorOptionModel.getPossibleEnumValues());
}
use of org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel in project camel-kafka-connector by apache.
the class CamelKafkaConnectorKameletUpdateMojo method createClassesAndDocumentation.
private void createClassesAndDocumentation(String sanitizedName, File connectorDir, KameletModel kamelet, ConnectorType ct) throws MojoFailureException, ResourceNotFoundException, FileResourceCreationException, IOException, MojoExecutionException {
String ctCapitalizedName = StringUtils.capitalize(ct.name().toLowerCase());
String ctLowercaseName = ct.name().toLowerCase();
String packageName = "org.apache.camel.kafkaconnector." + RESERVED_WORDS_SUBSTITUTION_MAP.getOrDefault(sanitizedName.replace("-", ""), sanitizedName.replace("-", ""));
Map<String, String> additionalProperties = new HashMap<>();
Properties properties = new Properties();
try (InputStream stream = new FileInputStream(rm.getResourceAsFile(fixKameletDependenciesProperties))) {
properties.load(stream);
}
String commonPropertyValue = properties.getProperty(ADDITIONAL_COMMON_PROPERTIES_PROPERTY_PREFIX + getMainDepArtifactId());
getLog().debug("Additional common connector properties: " + commonPropertyValue);
addProperties(additionalProperties, commonPropertyValue);
String sourceOrSinkPropertyValue = properties.getProperty(ADDITIONAL_COMMON_PROPERTIES_PROPERTY_PREFIX + ctLowercaseName + "_" + getMainDepArtifactId());
getLog().debug("Additional " + ctLowercaseName + " connector properties: " + sourceOrSinkPropertyValue);
addProperties(additionalProperties, sourceOrSinkPropertyValue);
// Camel{sanitizedName}{Sink,Source}ConnectorConfig.java
String javaClassConnectorConfigName = "Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "ConnectorConfig";
final JavaClass javaClassConnectorConfig = new JavaClass(getProjectClassLoader());
javaClassConnectorConfig.setPackage(packageName);
javaClassConnectorConfig.setName(javaClassConnectorConfigName);
javaClassConnectorConfig.addAnnotation(Generated.class).setStringValue("value", "This class has been generated by camel-kafka-connector-generator-maven-plugin, remove this annotation to prevent it from being generated.");
javaClassConnectorConfig.extendSuperType("Camel" + ctCapitalizedName + "ConnectorConfig");
javaClassConnectorConfig.addImport("java.util.Map");
javaClassConnectorConfig.addImport("org.apache.camel.kafkaconnector.Camel" + ctCapitalizedName + "ConnectorConfig");
javaClassConnectorConfig.addImport("org.apache.kafka.common.config.ConfigDef");
javaClassConnectorConfig.addMethod().setConstructor(true).setName(javaClassConnectorConfigName).addParameter("ConfigDef", "config").addParameter("Map<String, String>", "parsedConfig").setPublic().setBody("super(config, parsedConfig);");
javaClassConnectorConfig.addMethod().setConstructor(true).setName(javaClassConnectorConfigName).addParameter("Map<String, String>", "parsedConfig").setPublic().setBody("this(conf(), parsedConfig);");
Method confMethod = javaClassConnectorConfig.addMethod().setConstructor(false).setName("conf").setReturnType("ConfigDef").setPublic().setStatic().setBody("ConfigDef conf = new ConfigDef(Camel" + ctCapitalizedName + "ConnectorConfig.conf());\n");
// instantiate CamelKafkaConnectorOptionModel for further use during documentation generation
List<CamelKafkaConnectorOptionModel> listOptions = new ArrayList<>();
List<KameletPropertyModel> kameletProperties = kamelet.getProperties();
kameletProperties.forEach(kameletProperty -> addConnectorOptions(sanitizedName, ct, javaClassConnectorConfig, confMethod, "KAMELET", kameletProperty, listOptions, kamelet.getRequiredProperties()));
confMethod.setBody(confMethod.getBody() + "return conf;");
String javaClassConnectorConfigFileName = packageName.replaceAll("\\.", "\\/") + File.separator + javaClassConnectorConfigName + ".java";
MavenUtils.writeSourceIfChanged(javaClassConnectorConfig, javaClassConnectorConfigFileName, false, connectorDir, rm.getResourceAsFile(javaFilesHeader));
// Camel{sanitizedName}{Sink,Source}Task.java
String javaClassTaskName = "Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "Task";
final JavaClass javaClassTask = new JavaClass(getProjectClassLoader());
javaClassTask.setPackage(packageName);
javaClassTask.setName(javaClassTaskName);
javaClassTask.addAnnotation(Generated.class).setStringValue("value", "This class has been generated by camel-kafka-connector-generator-maven-plugin, remove this annotation to prevent it from being generated.");
javaClassTask.extendSuperType("Camel" + ctCapitalizedName + "Task");
javaClassTask.addImport("java.util.HashMap");
javaClassTask.addImport("java.util.Map");
javaClassTask.addImport("org.apache.camel.kafkaconnector.Camel" + ctCapitalizedName + "ConnectorConfig");
javaClassTask.addImport("org.apache.camel.kafkaconnector.Camel" + ctCapitalizedName + "Task");
javaClassTask.addMethod().setConstructor(false).setName("getCamel" + ctCapitalizedName + "ConnectorConfig").setProtected().addParameter("Map<String, String>", "props").setReturnType("Camel" + ctCapitalizedName + "ConnectorConfig").setBody("return new Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "ConnectorConfig(props);").addAnnotation(Override.class);
if (!additionalProperties.keySet().isEmpty()) {
Method getDefaultConfigMethod = javaClassTask.addMethod().setConstructor(false).setName("getDefaultConfig").setProtected().setReturnType("Map<String, String>").setBody("return new HashMap<String, String>() {{\n");
for (String key : new TreeSet<String>(additionalProperties.keySet())) {
getDefaultConfigMethod.setBody(getDefaultConfigMethod.getBody() + " put(\"" + key + "\", \"" + additionalProperties.get(key) + "\");\n");
}
getDefaultConfigMethod.setBody(getDefaultConfigMethod.getBody() + "}};\n");
getDefaultConfigMethod.addAnnotation(Override.class);
}
Method getSinkOrSourceKameletMethod = javaClassTask.addMethod().setConstructor(false).setName("get" + ctCapitalizedName + "Kamelet").setProtected().setReturnType("String").setBody("return \"kamelet:" + name + "\";");
getSinkOrSourceKameletMethod.addAnnotation(Override.class);
String javaClassTaskFileName = packageName.replaceAll("\\.", "\\/") + File.separator + javaClassTaskName + ".java";
MavenUtils.writeSourceIfChanged(javaClassTask, javaClassTaskFileName, false, connectorDir, rm.getResourceAsFile(javaFilesHeader));
// Camel{sanitizedName}{Sink,Source}Connector.java
String javaClassConnectorName = "Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "Connector";
final JavaClass javaClassConnector = new JavaClass(getProjectClassLoader());
javaClassConnector.setPackage(packageName);
javaClassConnector.setName(javaClassConnectorName);
javaClassConnector.addAnnotation(Generated.class).setStringValue("value", "This class has been generated by camel-kafka-connector-generator-maven-plugin, remove this annotation to prevent it from being generated.");
javaClassConnector.extendSuperType("Camel" + ctCapitalizedName + "Connector");
javaClassConnector.addImport("org.apache.camel.kafkaconnector.Camel" + ctCapitalizedName + "Connector");
javaClassConnector.addImport("org.apache.kafka.common.config.ConfigDef");
javaClassConnector.addImport("org.apache.kafka.connect.connector.Task");
javaClassConnector.addMethod().setConstructor(false).setName("config").setPublic().setReturnType("ConfigDef").setBody("return Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "ConnectorConfig.conf();").addAnnotation(Override.class);
javaClassConnector.addMethod().setConstructor(false).setName("taskClass").setPublic().setReturnType("Class<? extends Task>").setBody("return Camel" + StringUtils.capitalize(sanitizedName.replace("-", "")) + ctCapitalizedName + "Task.class;").addAnnotation(Override.class);
String javaClassConnectorFileName = packageName.replaceAll("\\.", "\\/") + File.separator + javaClassConnectorName + ".java";
MavenUtils.writeSourceIfChanged(javaClassConnector, javaClassConnectorFileName, false, connectorDir, rm.getResourceAsFile(javaFilesHeader));
List<String> convertersList = new ArrayList<>();
List<String> transformsList = new ArrayList<>();
List<String> aggregationStrategiesList = new ArrayList<>();
if (connectorDir != null && connectorDir.isDirectory()) {
File[] files = connectorDir.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory()) {
Collection<File> convertersElements = FileUtils.listFiles(file, new RegexFileFilter(".*Converter.java"), DirectoryFileFilter.DIRECTORY);
Collection<File> transformElements = FileUtils.listFiles(file, new RegexFileFilter(".*Transforms.java"), DirectoryFileFilter.DIRECTORY);
Collection<File> aggStrategiesElements = FileUtils.listFiles(file, new RegexFileFilter(".*AggregationStrategy.java"), DirectoryFileFilter.DIRECTORY);
for (File p : convertersElements) {
String filePath = p.getCanonicalPath();
String f = StringUtils.removeStart(filePath, connectorDir.getAbsolutePath() + "/src/main/java/");
String finalElement = StringUtils.replace(f, File.separator, ".");
String finalPath = StringUtils.removeEnd(finalElement, ".java");
convertersList.add(finalPath);
}
for (File p : transformElements) {
String filePath = p.getCanonicalPath();
String f = StringUtils.removeStart(filePath, connectorDir.getAbsolutePath() + "/src/main/java/");
String finalElement = StringUtils.replace(f, File.separator, ".");
String finalPath = StringUtils.removeEnd(finalElement, ".java");
transformsList.add(finalPath);
}
for (File p : aggStrategiesElements) {
String filePath = p.getCanonicalPath();
String f = StringUtils.removeStart(filePath, connectorDir.getAbsolutePath() + "/src/main/java/");
String finalElement = StringUtils.replace(f, File.separator, ".");
String finalPath = StringUtils.removeEnd(finalElement, ".java");
aggregationStrategiesList.add(finalPath);
}
}
}
}
}
// docs/examples/Camel{sanitizedName}{Sink,Source}.properties
try {
String examplesPropertiestemplate = null;
switch(ct) {
case SOURCE:
examplesPropertiestemplate = loadText(rm.getResourceAsFile(exampleSourcePropertiesFileTemplate));
break;
case SINK:
examplesPropertiestemplate = loadText(rm.getResourceAsFile(exampleSinkPropertiesFileTemplate));
break;
default:
break;
}
HashMap<String, Object> templateParams = new HashMap<>();
templateParams.put("connectorName", StringUtils.capitalize(sanitizedName));
templateParams.put("connectorClass", packageName + "." + javaClassConnectorName);
List<CamelKafkaConnectorOptionModel> mandatoryOptions = listOptions.stream().filter(o -> "HIGH".equalsIgnoreCase(o.getPriority())).sorted(Comparator.comparing(CamelKafkaConnectorOptionModel::getName, String.CASE_INSENSITIVE_ORDER)).collect(Collectors.toList());
templateParams.put("options", mandatoryOptions);
String examplePropertiesFileContent = (String) TemplateRuntime.eval(examplesPropertiestemplate, templateParams);
writeFileIfChanged(examplePropertiesFileContent, new File(connectorDir, "src/main/docs/examples/" + javaClassConnectorName + ".properties"), getLog());
} catch (Exception e) {
throw new MojoExecutionException("Error processing mvel examples properties template. Reason: " + e, e);
}
// generate json descriptor src/generated/resources/<connector-name>.json
writeJson(listOptions, kamelet.getDescription(), connectorDir, ct, packageName + "." + javaClassConnectorName, convertersList, transformsList, aggregationStrategiesList);
// generate descriptor src/generated/descriptors/connector-{sink,source}.properties
writeDescriptors(connectorDir, ct);
}
Aggregations