Search in sources :

Example 1 with CamelKafkaConnectorOptionModel

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);
}
Also used : CamelKafkaConnectorOptionModel(org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel)

Example 2 with CamelKafkaConnectorOptionModel

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Properties(java.util.Properties) TreeSet(java.util.TreeSet) BaseOptionModel(org.apache.camel.tooling.model.BaseOptionModel) Generated(javax.annotation.Generated) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RegexFileFilter(org.apache.commons.io.filefilter.RegexFileFilter) Method(org.apache.camel.tooling.util.srcgen.Method) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) ResourceNotFoundException(org.codehaus.plexus.resource.loader.ResourceNotFoundException) IOException(java.io.IOException) FileResourceCreationException(org.codehaus.plexus.resource.loader.FileResourceCreationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) JavaClass(org.apache.camel.tooling.util.srcgen.JavaClass) CamelKafkaConnectorOptionModel(org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel) JsonObject(org.apache.camel.util.json.JsonObject) File(java.io.File)

Example 3 with CamelKafkaConnectorOptionModel

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;
}
Also used : CamelKafkaConnectorOptionModel(org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel) ArrayList(java.util.ArrayList) JsonObject(org.apache.camel.util.json.JsonObject)

Example 4 with CamelKafkaConnectorOptionModel

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());
}
Also used : CamelKafkaConnectorOptionModel(org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel)

Example 5 with CamelKafkaConnectorOptionModel

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Properties(java.util.Properties) KameletPropertyModel(org.apache.camel.kafkaconnector.maven.model.KameletPropertyModel) TreeSet(java.util.TreeSet) Generated(javax.annotation.Generated) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RegexFileFilter(org.apache.commons.io.filefilter.RegexFileFilter) Method(org.apache.camel.tooling.util.srcgen.Method) FileInputStream(java.io.FileInputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SAXException(org.xml.sax.SAXException) ResourceNotFoundException(org.codehaus.plexus.resource.loader.ResourceNotFoundException) IOException(java.io.IOException) FileResourceCreationException(org.codehaus.plexus.resource.loader.FileResourceCreationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) JavaClass(org.apache.camel.tooling.util.srcgen.JavaClass) CamelKafkaConnectorOptionModel(org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel) JsonObject(org.apache.camel.util.json.JsonObject) File(java.io.File)

Aggregations

CamelKafkaConnectorOptionModel (org.apache.camel.kafkaconnector.model.CamelKafkaConnectorOptionModel)6 ArrayList (java.util.ArrayList)3 JsonObject (org.apache.camel.util.json.JsonObject)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 TreeSet (java.util.TreeSet)2 Generated (javax.annotation.Generated)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 JavaClass (org.apache.camel.tooling.util.srcgen.JavaClass)2 Method (org.apache.camel.tooling.util.srcgen.Method)2 RegexFileFilter (org.apache.commons.io.filefilter.RegexFileFilter)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 FileResourceCreationException (org.codehaus.plexus.resource.loader.FileResourceCreationException)2 ResourceNotFoundException (org.codehaus.plexus.resource.loader.ResourceNotFoundException)2