use of org.apache.camel.tooling.model.BaseOptionModel 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.tooling.model.BaseOptionModel in project camel-quarkus by apache.
the class CamelUnremovableBeansProcessor method unremovableCamelBeans.
@BuildStep
UnremovableBeanBuildItem unremovableCamelBeans(BuildTimeCamelCatalogBuildItem camelCatalogBuildItem) {
BuildTimeCamelCatalog catalog = camelCatalogBuildItem.getCatalog();
Set<DotName> unremovableClasses = catalog.getAllOptions().stream().filter(option -> option.getType().equals("object")).filter(option -> !option.getJavaType().startsWith("java.lang")).map(BaseOptionModel::getJavaType).map(DotName::createSimple).collect(Collectors.toSet());
if (LOGGER.isDebugEnabled()) {
unremovableClasses.stream().forEach(unremovableClass -> LOGGER.debug("Registering camel unremovable bean class: {}", unremovableClass));
}
return UnremovableBeanBuildItem.beanTypes(unremovableClasses);
}
Aggregations