Search in sources :

Example 1 with BaseOptionModel

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);
}
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 2 with BaseOptionModel

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);
}
Also used : IndexView(org.jboss.jandex.IndexView) SchemaResource(org.apache.camel.quarkus.core.deployment.catalog.SchemaResource) Logger(org.slf4j.Logger) UnremovableBeanBuildItem(io.quarkus.arc.deployment.UnremovableBeanBuildItem) Collection(java.util.Collection) DotName(org.jboss.jandex.DotName) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) ClassInfo(org.jboss.jandex.ClassInfo) BuildTimeCamelCatalog(org.apache.camel.quarkus.core.deployment.catalog.BuildTimeCamelCatalog) Collectors(java.util.stream.Collectors) CombinedIndexBuildItem(io.quarkus.deployment.builditem.CombinedIndexBuildItem) HashSet(java.util.HashSet) BuildStep(io.quarkus.deployment.annotations.BuildStep) List(java.util.List) Stream(java.util.stream.Stream) BuildTimeCamelCatalogBuildItem(org.apache.camel.quarkus.core.deployment.spi.BuildTimeCamelCatalogBuildItem) Dataformat(org.apache.camel.spi.annotations.Dataformat) InterceptStrategy(org.apache.camel.spi.InterceptStrategy) AnnotationInstance(org.jboss.jandex.AnnotationInstance) Component(org.apache.camel.spi.annotations.Component) Language(org.apache.camel.spi.annotations.Language) BuildTimeJsonSchemaResolver(org.apache.camel.quarkus.core.deployment.catalog.BuildTimeJsonSchemaResolver) BaseOptionModel(org.apache.camel.tooling.model.BaseOptionModel) BuildTimeCamelCatalog(org.apache.camel.quarkus.core.deployment.catalog.BuildTimeCamelCatalog) BaseOptionModel(org.apache.camel.tooling.model.BaseOptionModel) DotName(org.jboss.jandex.DotName) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

BaseOptionModel (org.apache.camel.tooling.model.BaseOptionModel)2 UnremovableBeanBuildItem (io.quarkus.arc.deployment.UnremovableBeanBuildItem)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 CombinedIndexBuildItem (io.quarkus.deployment.builditem.CombinedIndexBuildItem)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Properties (java.util.Properties)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Generated (javax.annotation.Generated)1