This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-kafka-connector.git
commit 2344f275b9b7207604db6d822285465b30a40567 Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Tue Oct 6 08:22:04 2020 +0200 Generator a JSON metadata file for each connector, fixed CS and cleanup --- .../maven/CamelKafkaConnectorUpdateMojo.java | 1043 ++++++++++---------- .../maven/dto/CamelKafkaConnectorModel.java | 12 +- .../maven/utils/JsonMapperKafkaConnector.java | 10 +- 3 files changed, 520 insertions(+), 545 deletions(-) diff --git a/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/CamelKafkaConnectorUpdateMojo.java b/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/CamelKafkaConnectorUpdateMojo.java index 03f1c35..a0b3bf9 100644 --- a/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/CamelKafkaConnectorUpdateMojo.java +++ b/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/CamelKafkaConnectorUpdateMojo.java @@ -89,292 +89,283 @@ import static org.apache.camel.tooling.util.PackageHelper.writeText; /** * Generate Camel Kafka Connector for the component */ -@Mojo(name = "camel-kafka-connector-update", threadSafe = true, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, defaultPhase = LifecyclePhase.GENERATE_RESOURCES) +@Mojo(name = "camel-kafka-connector-update", threadSafe = true, +requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME, +requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, +defaultPhase = LifecyclePhase.GENERATE_RESOURCES) public class CamelKafkaConnectorUpdateMojo extends AbstractCamelKafkaConnectorMojo { - private static final String GENERATED_SECTION_START = "START OF GENERATED CODE"; - private static final String GENERATED_SECTION_START_COMMENT = "<!--" + GENERATED_SECTION_START + "-->"; - private static final String GENERATED_SECTION_END = "END OF GENERATED CODE"; - private static final String GENERATED_SECTION_END_COMMENT = "<!--" + GENERATED_SECTION_END + "-->"; - - private static final String EXCLUDE_DEPENDENCY_PROPERTY_PREFIX = "exclude_"; - private static final String ADDITIONAL_COMMON_PROPERTIES_PROPERTY_PREFIX = "additional_properties_"; - private static final String XML_FEATURES_DISALLOW_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl"; - - private static final Map<String, Class<?>> PRIMITIVE_TYPES_TO_CLASS_MAP; - private static final Map<String, String> PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP; - - private static final Map<String, String> RESERVED_WORDS_SUBSTITUTION_MAP; - - static { - PRIMITIVE_TYPES_TO_CLASS_MAP = new HashMap<>(); - PRIMITIVE_TYPES_TO_CLASS_MAP.put("boolean", Boolean.class); - PRIMITIVE_TYPES_TO_CLASS_MAP.put("long", Long.class); - PRIMITIVE_TYPES_TO_CLASS_MAP.put("int", Integer.class); - PRIMITIVE_TYPES_TO_CLASS_MAP.put("short", Short.class); - PRIMITIVE_TYPES_TO_CLASS_MAP.put("double", Double.class); - PRIMITIVE_TYPES_TO_CLASS_MAP.put("float", Float.class); - - PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP = new HashMap<>(); - PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("boolean", "ConfigDef.Type.BOOLEAN"); - PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("long", "ConfigDef.Type.LONG"); - PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("int", "ConfigDef.Type.INT"); - PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("short", "ConfigDef.Type.SHORT"); - PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("double", "ConfigDef.Type.DOUBLE"); - PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("float", "ConfigDef.Type.DOUBLE"); - - RESERVED_WORDS_SUBSTITUTION_MAP = new HashMap<>(); - RESERVED_WORDS_SUBSTITUTION_MAP.put("class", "clazz"); - } - - protected DynamicClassLoader projectClassLoader; - - @Parameter(property = "name", required = true) - protected String name; - - @Parameter(property = "componentJson", required = true) - protected String componentJson; - - /** - * The maven session. - */ - @Parameter(defaultValue = "${session}", readonly = true) - private MavenSession session; - - /** - * A comma separated list of column separated GAV to include as dependencies to - * the generated camel kafka connector. (i.e. - * groupId:ArtifactId:version,groupId_2:ArtifactId_2:version_2) - */ - @Parameter(defaultValue = "", readonly = true) - private String additionalDependencies; - - @Override - protected String getMainDepArtifactId() { - return "camel-" + name; - } - - @Override - protected void executeAll() throws MojoFailureException { - if (name == null || name.isEmpty()) { - throw new MojoFailureException("Connector name must be specified as the parameter"); - } - if (name.startsWith("camel-")) { - name = name.substring("camel-".length()); - } - if (name.endsWith(KAFKA_CONNECTORS_SUFFIX)) { - name = name.substring(0, name.length() - KAFKA_CONNECTORS_SUFFIX.length()); - } - try { - updateConnector(); - } catch (Exception e) { - throw new MojoFailureException("Fail to update connector " + name, e); - } - } - - protected DynamicClassLoader getProjectClassLoader() { - if (projectClassLoader == null) { - final List<String> classpathElements; - try { - classpathElements = project.getTestClasspathElements(); - } catch (org.apache.maven.artifact.DependencyResolutionRequiredException e) { - throw new RuntimeException(e.getMessage(), e); - } - projectClassLoader = DynamicClassLoader.createDynamicClassLoader(classpathElements); - } - return projectClassLoader; - } - - private void updateConnector() throws Exception { - String sanitizedName = sanitizeMavenArtifactId(name); - // create the starter directory - File connectorDir = new File(projectDir, "camel-" + sanitizedName + KAFKA_CONNECTORS_SUFFIX); - if (!connectorDir.exists() || !connectorDir.isDirectory()) { - getLog().info("Connector " + name + " can not be updated since directory " + connectorDir.getAbsolutePath() - + " dose not exist."); - throw new MojoFailureException("Directory already exists: " + connectorDir); - } - - // create the base pom.xml - Document pom = createBasePom(connectorDir); - - // Apply changes to the starter pom - fixExcludedDependencies(pom); - fixAdditionalDependencies(pom, additionalDependencies); - fixAdditionalRepositories(pom); - - // Write the starter pom - File pomFile = new File(connectorDir, "pom.xml"); - writeXmlFormatted(pom, pomFile, getLog()); - - // write package - Document pkg = createPackageFile(); - File pkgFile = new File(connectorDir, "src/main/assembly/package.xml"); - writeXmlFormatted(pkg, pkgFile, getLog()); - - // write LICENSE, USAGE - writeStaticFiles(connectorDir); - - // generate classes - ComponentModel model = JsonMapper.generateComponentModel(componentJson); - if (model.isConsumerOnly()) { - createClasses(sanitizedName, connectorDir, model, ConnectorType.SOURCE); - } else if (model.isProducerOnly()) { - createClasses(sanitizedName, connectorDir, model, ConnectorType.SINK); - } else { - createClasses(sanitizedName, connectorDir, model, ConnectorType.SOURCE); - createClasses(sanitizedName, connectorDir, model, ConnectorType.SINK); - } - } - - private void fixExcludedDependencies(Document pom) throws Exception { - // add dependencies to be excluded form camel component dependency - Set<String> loggingImpl = new HashSet<>(); - - // excluded dependencies - Set<String> configExclusions = new HashSet<>(); - Properties properties = new Properties(); - properties.load(new FileInputStream(rm.getResourceAsFile(fixDependenciesProperties))); - String artExcl = properties.getProperty(EXCLUDE_DEPENDENCY_PROPERTY_PREFIX + getMainDepArtifactId()); - getLog().debug("Configured exclusions: " + artExcl); - if (artExcl != null && artExcl.trim().length() > 0) { - for (String dep : artExcl.split(",")) { - getLog().debug("Adding configured exclusion: " + dep); - configExclusions.add(dep); - } - } - - Set<String> libsToRemove = new TreeSet<>(); - libsToRemove.addAll(loggingImpl); - libsToRemove.addAll(configExclusions); - - if (libsToRemove.size() > 0) { - getLog().info("Camel-kafka-connector: the following dependencies will be removed from the connector: " - + libsToRemove); - MavenUtils.addExclusionsToDependency(pom, getMainDepArtifactId(), libsToRemove, GENERATED_SECTION_START, - GENERATED_SECTION_END); - } - } - - private void fixAdditionalDependencies(Document pom, String additionalDependencies) throws Exception { - Properties properties = new Properties(); - properties.load(new FileInputStream(rm.getResourceAsFile(fixDependenciesProperties))); - - Set<String> deps = new TreeSet<>(); - deps.addAll(MavenUtils.csvToSet(properties.getProperty(getMainDepArtifactId()))); - deps.addAll(MavenUtils.csvToSet(additionalDependencies)); - - Set<String> globalProps = MavenUtils.csvToSet(properties.getProperty("global")); - boolean inGlobal = false; - for (String gp : globalProps) { - String camelGav = getMainDepGroupId() + ":" + getMainDepArtifactId(); - String camelKafkaConnectorGav = project.getGroupId() + ":" + project.getArtifactId(); - if (gp.equals(camelGav) || gp.equals(camelKafkaConnectorGav)) { - inGlobal = true; - break; - } - } - - if (!inGlobal) { - // add global properties for all modules not in global properties - deps.addAll(globalProps); - } - - if (deps.size() > 0) { - getLog().debug("The following dependencies will be added to the starter: " + deps); - MavenUtils.addDependencies(pom, deps, GENERATED_SECTION_START, GENERATED_SECTION_END); - } - } - - private void fixAdditionalRepositories(Document pom) throws Exception { - if (project.getFile() != null) { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); - dbf.setFeature(XML_FEATURES_DISALLOW_DOCTYPE_DECL, true); - DocumentBuilder builder = dbf.newDocumentBuilder(); - Document originalPom = builder.parse(project.getFile()); - - XPath xpath = XPathFactory.newInstance().newXPath(); - Node repositories = (Node) xpath.compile("/project/repositories").evaluate(originalPom, - XPathConstants.NODE); - if (repositories != null) { - pom.getDocumentElement().appendChild(pom.createComment(GENERATED_SECTION_START)); - pom.getDocumentElement().appendChild(pom.importNode(repositories, true)); - pom.getDocumentElement().appendChild(pom.createComment(GENERATED_SECTION_END)); - } - } else { - getLog().warn("Cannot access the project pom file to retrieve repositories"); - } - } - - private Document createPackageFile() throws ResourceNotFoundException, FileResourceCreationException, IOException { - getLog().info("Creating a new package.xml for the connector."); - Template packageTemplate = MavenUtils.getTemplate(rm.getResourceAsFile(packageFileTemplate)); - Map<String, String> props = new HashMap<>(); - try { - return MavenUtils.createCrateXmlDocumentFromTemplate(packageTemplate, props); - } catch (Exception e) { - getLog().error( - "Cannot create package.xml file from Template: " + packageTemplate + " with properties: " + props, - e); - } - return null; - } - - private Document createBasePom(File connectorDir) throws IOException, SAXException, ParserConfigurationException { - File pomFile = new File(connectorDir, "pom.xml"); - if (pomFile.exists()) { - try (InputStream in = new FileInputStream(pomFile)) { - String content = IOUtils.toString(in, StandardCharsets.UTF_8); - boolean editablePom = content.contains(GENERATED_SECTION_START_COMMENT); - if (editablePom) { - content = MavenUtils.removeGeneratedSections(content, GENERATED_SECTION_START_COMMENT, - GENERATED_SECTION_END_COMMENT, 10); - DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - - Document pom; - try (InputStream contentIn = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) { - pom = builder.parse(contentIn); - } - - getLog().debug("Reusing the existing pom.xml for the starter"); - return pom; - } else { - getLog().error("Cannot use the existing pom.xml file since it is not editable. It does not contain " - + GENERATED_SECTION_START_COMMENT); - throw new UnsupportedOperationException( - "Cannot use the existing pom.xml file since it is not editable. It does not contain " - + GENERATED_SECTION_START_COMMENT); - } - } - } else { - getLog().error("The pom.xml file is not present, please use camel-kafka-connector-create first."); - throw new UnsupportedOperationException( - "The pom.xml file is not present, please use camel-kafka-connector-create first."); - } - } - - private void writeStaticFiles(File connectorDir) - throws IOException, ResourceNotFoundException, FileResourceCreationException { - String notice; - String license; - try (InputStream isNotice = new FileInputStream(rm.getResourceAsFile(noticeTemplate)); - InputStream isLicense = new FileInputStream(rm.getResourceAsFile(licenseTemplate))) { - notice = IOUtils.toString(isNotice, StandardCharsets.UTF_8); - license = IOUtils.toString(isLicense, StandardCharsets.UTF_8); - } - - writeFileIfChanged(notice, new File(connectorDir, "src/main/resources/META-INF/NOTICE.txt"), getLog()); - writeFileIfChanged(license, new File(connectorDir, "src/main/resources/META-INF/LICENSE.txt"), getLog()); - } - - private String getComponentId() { - String componentName = getMainDepArtifactId(); - String componentId = componentName.replace("camel-", ""); - return componentId; - } - - private void createClasses(String sanitizedName, File connectorDir, ComponentModel model, ConnectorType ct) + private static final String GENERATED_SECTION_START = "START OF GENERATED CODE"; + private static final String GENERATED_SECTION_START_COMMENT = "<!--" + GENERATED_SECTION_START + "-->"; + private static final String GENERATED_SECTION_END = "END OF GENERATED CODE"; + private static final String GENERATED_SECTION_END_COMMENT = "<!--" + GENERATED_SECTION_END + "-->"; + + private static final String EXCLUDE_DEPENDENCY_PROPERTY_PREFIX = "exclude_"; + private static final String ADDITIONAL_COMMON_PROPERTIES_PROPERTY_PREFIX = "additional_properties_"; + private static final String XML_FEATURES_DISALLOW_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl"; + + private static final Map<String, Class<?>> PRIMITIVE_TYPES_TO_CLASS_MAP; + private static final Map<String, String> PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP; + + private static final Map<String, String> RESERVED_WORDS_SUBSTITUTION_MAP; + + static { + PRIMITIVE_TYPES_TO_CLASS_MAP = new HashMap<>(); + PRIMITIVE_TYPES_TO_CLASS_MAP.put("boolean", Boolean.class); + PRIMITIVE_TYPES_TO_CLASS_MAP.put("long", Long.class); + PRIMITIVE_TYPES_TO_CLASS_MAP.put("int", Integer.class); + PRIMITIVE_TYPES_TO_CLASS_MAP.put("short", Short.class); + PRIMITIVE_TYPES_TO_CLASS_MAP.put("double", Double.class); + PRIMITIVE_TYPES_TO_CLASS_MAP.put("float", Float.class); + + PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP = new HashMap<>(); + PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("boolean", "ConfigDef.Type.BOOLEAN"); + PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("long", "ConfigDef.Type.LONG"); + PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("int", "ConfigDef.Type.INT"); + PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("short", "ConfigDef.Type.SHORT"); + PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("double", "ConfigDef.Type.DOUBLE"); + PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.put("float", "ConfigDef.Type.DOUBLE"); + + RESERVED_WORDS_SUBSTITUTION_MAP = new HashMap<>(); + RESERVED_WORDS_SUBSTITUTION_MAP.put("class", "clazz"); + } + + protected DynamicClassLoader projectClassLoader; + + @Parameter(property = "name", required = true) + protected String name; + + @Parameter(property = "componentJson", required = true) + protected String componentJson; + + /** + * The maven session. + */ + @Parameter(defaultValue = "${session}", readonly = true) + private MavenSession session; + + /** + * A comma separated list of column separated GAV to include as dependencies + * to the generated camel kafka connector. (i.e. + * groupId:ArtifactId:version,groupId_2:ArtifactId_2:version_2) + */ + @Parameter(defaultValue = "", readonly = true) + private String additionalDependencies; + + @Override + protected String getMainDepArtifactId() { + return "camel-" + name; + } + + @Override + protected void executeAll() throws MojoFailureException { + if (name == null || name.isEmpty()) { + throw new MojoFailureException("Connector name must be specified as the parameter"); + } + if (name.startsWith("camel-")) { + name = name.substring("camel-".length()); + } + if (name.endsWith(KAFKA_CONNECTORS_SUFFIX)) { + name = name.substring(0, name.length() - KAFKA_CONNECTORS_SUFFIX.length()); + } + try { + updateConnector(); + } catch (Exception e) { + throw new MojoFailureException("Fail to update connector " + name, e); + } + } + + protected DynamicClassLoader getProjectClassLoader() { + if (projectClassLoader == null) { + final List<String> classpathElements; + try { + classpathElements = project.getTestClasspathElements(); + } catch (org.apache.maven.artifact.DependencyResolutionRequiredException e) { + throw new RuntimeException(e.getMessage(), e); + } + projectClassLoader = DynamicClassLoader.createDynamicClassLoader(classpathElements); + } + return projectClassLoader; + } + + private void updateConnector() throws Exception { + String sanitizedName = sanitizeMavenArtifactId(name); + // create the starter directory + File connectorDir = new File(projectDir, "camel-" + sanitizedName + KAFKA_CONNECTORS_SUFFIX); + if (!connectorDir.exists() || !connectorDir.isDirectory()) { + getLog().info("Connector " + name + " can not be updated since directory " + connectorDir.getAbsolutePath() + " dose not exist."); + throw new MojoFailureException("Directory already exists: " + connectorDir); + } + + // create the base pom.xml + Document pom = createBasePom(connectorDir); + + // Apply changes to the starter pom + fixExcludedDependencies(pom); + fixAdditionalDependencies(pom, additionalDependencies); + fixAdditionalRepositories(pom); + + // Write the starter pom + File pomFile = new File(connectorDir, "pom.xml"); + writeXmlFormatted(pom, pomFile, getLog()); + + // write package + Document pkg = createPackageFile(); + File pkgFile = new File(connectorDir, "src/main/assembly/package.xml"); + writeXmlFormatted(pkg, pkgFile, getLog()); + + // write LICENSE, USAGE + writeStaticFiles(connectorDir); + + // generate classes + ComponentModel model = JsonMapper.generateComponentModel(componentJson); + if (model.isConsumerOnly()) { + createClasses(sanitizedName, connectorDir, model, ConnectorType.SOURCE); + } else if (model.isProducerOnly()) { + createClasses(sanitizedName, connectorDir, model, ConnectorType.SINK); + } else { + createClasses(sanitizedName, connectorDir, model, ConnectorType.SOURCE); + createClasses(sanitizedName, connectorDir, model, ConnectorType.SINK); + } + } + + private void fixExcludedDependencies(Document pom) throws Exception { + // add dependencies to be excluded form camel component dependency + Set<String> loggingImpl = new HashSet<>(); + + // excluded dependencies + Set<String> configExclusions = new HashSet<>(); + Properties properties = new Properties(); + properties.load(new FileInputStream(rm.getResourceAsFile(fixDependenciesProperties))); + String artExcl = properties.getProperty(EXCLUDE_DEPENDENCY_PROPERTY_PREFIX + getMainDepArtifactId()); + getLog().debug("Configured exclusions: " + artExcl); + if (artExcl != null && artExcl.trim().length() > 0) { + for (String dep : artExcl.split(",")) { + getLog().debug("Adding configured exclusion: " + dep); + configExclusions.add(dep); + } + } + + Set<String> libsToRemove = new TreeSet<>(); + libsToRemove.addAll(loggingImpl); + libsToRemove.addAll(configExclusions); + + if (libsToRemove.size() > 0) { + getLog().info("Camel-kafka-connector: the following dependencies will be removed from the connector: " + libsToRemove); + MavenUtils.addExclusionsToDependency(pom, getMainDepArtifactId(), libsToRemove, GENERATED_SECTION_START, GENERATED_SECTION_END); + } + } + + private void fixAdditionalDependencies(Document pom, String additionalDependencies) throws Exception { + Properties properties = new Properties(); + properties.load(new FileInputStream(rm.getResourceAsFile(fixDependenciesProperties))); + + Set<String> deps = new TreeSet<>(); + deps.addAll(MavenUtils.csvToSet(properties.getProperty(getMainDepArtifactId()))); + deps.addAll(MavenUtils.csvToSet(additionalDependencies)); + + Set<String> globalProps = MavenUtils.csvToSet(properties.getProperty("global")); + boolean inGlobal = false; + for (String gp : globalProps) { + String camelGav = getMainDepGroupId() + ":" + getMainDepArtifactId(); + String camelKafkaConnectorGav = project.getGroupId() + ":" + project.getArtifactId(); + if (gp.equals(camelGav) || gp.equals(camelKafkaConnectorGav)) { + inGlobal = true; + break; + } + } + + if (!inGlobal) { + // add global properties for all modules not in global properties + deps.addAll(globalProps); + } + + if (deps.size() > 0) { + getLog().debug("The following dependencies will be added to the starter: " + deps); + MavenUtils.addDependencies(pom, deps, GENERATED_SECTION_START, GENERATED_SECTION_END); + } + } + + private void fixAdditionalRepositories(Document pom) throws Exception { + if (project.getFile() != null) { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); + dbf.setFeature(XML_FEATURES_DISALLOW_DOCTYPE_DECL, true); + DocumentBuilder builder = dbf.newDocumentBuilder(); + Document originalPom = builder.parse(project.getFile()); + + XPath xpath = XPathFactory.newInstance().newXPath(); + Node repositories = (Node)xpath.compile("/project/repositories").evaluate(originalPom, XPathConstants.NODE); + if (repositories != null) { + pom.getDocumentElement().appendChild(pom.createComment(GENERATED_SECTION_START)); + pom.getDocumentElement().appendChild(pom.importNode(repositories, true)); + pom.getDocumentElement().appendChild(pom.createComment(GENERATED_SECTION_END)); + } + } else { + getLog().warn("Cannot access the project pom file to retrieve repositories"); + } + } + + private Document createPackageFile() throws ResourceNotFoundException, FileResourceCreationException, IOException { + getLog().info("Creating a new package.xml for the connector."); + Template packageTemplate = MavenUtils.getTemplate(rm.getResourceAsFile(packageFileTemplate)); + Map<String, String> props = new HashMap<>(); + try { + return MavenUtils.createCrateXmlDocumentFromTemplate(packageTemplate, props); + } catch (Exception e) { + getLog().error("Cannot create package.xml file from Template: " + packageTemplate + " with properties: " + props, e); + } + return null; + } + + private Document createBasePom(File connectorDir) throws IOException, SAXException, ParserConfigurationException { + File pomFile = new File(connectorDir, "pom.xml"); + if (pomFile.exists()) { + try (InputStream in = new FileInputStream(pomFile)) { + String content = IOUtils.toString(in, StandardCharsets.UTF_8); + boolean editablePom = content.contains(GENERATED_SECTION_START_COMMENT); + if (editablePom) { + content = MavenUtils.removeGeneratedSections(content, GENERATED_SECTION_START_COMMENT, GENERATED_SECTION_END_COMMENT, 10); + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + + Document pom; + try (InputStream contentIn = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) { + pom = builder.parse(contentIn); + } + + getLog().debug("Reusing the existing pom.xml for the starter"); + return pom; + } else { + getLog().error("Cannot use the existing pom.xml file since it is not editable. It does not contain " + GENERATED_SECTION_START_COMMENT); + throw new UnsupportedOperationException("Cannot use the existing pom.xml file since it is not editable. It does not contain " + + GENERATED_SECTION_START_COMMENT); + } + } + } else { + getLog().error("The pom.xml file is not present, please use camel-kafka-connector-create first."); + throw new UnsupportedOperationException("The pom.xml file is not present, please use camel-kafka-connector-create first."); + } + } + + private void writeStaticFiles(File connectorDir) throws IOException, ResourceNotFoundException, FileResourceCreationException { + String notice; + String license; + try (InputStream isNotice = new FileInputStream(rm.getResourceAsFile(noticeTemplate)); InputStream isLicense = new FileInputStream(rm.getResourceAsFile(licenseTemplate))) { + notice = IOUtils.toString(isNotice, StandardCharsets.UTF_8); + license = IOUtils.toString(isLicense, StandardCharsets.UTF_8); + } + + writeFileIfChanged(notice, new File(connectorDir, "src/main/resources/META-INF/NOTICE.txt"), getLog()); + writeFileIfChanged(license, new File(connectorDir, "src/main/resources/META-INF/LICENSE.txt"), getLog()); + } + + private String getComponentId() { + String componentName = getMainDepArtifactId(); + String componentId = componentName.replace("camel-", ""); + return componentId; + } + + 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(); @@ -416,7 +407,7 @@ public class CamelKafkaConnectorUpdateMojo extends AbstractCamelKafkaConnectorMo @Override public boolean test(BaseOptionModel optionModel) { return optionModel.getLabel() == null || optionModel.getLabel().contains("producer") - || (!optionModel.getLabel().contains("producer") && !optionModel.getLabel().contains("consumer")); + || (!optionModel.getLabel().contains("producer") && !optionModel.getLabel().contains("consumer")); } }; break; @@ -508,32 +499,32 @@ public class CamelKafkaConnectorUpdateMojo extends AbstractCamelKafkaConnectorMo Collection aggStrategiesElements = FileUtils.listFiles(file, new RegexFileFilter(".*AggregationStrategy.java"), DirectoryFileFilter.DIRECTORY); if (!convertersElements.isEmpty()) { for (Iterator iterator = convertersElements.iterator(); iterator.hasNext();) { - File p = (File) iterator.next(); + File p = (File)iterator.next(); String filePath = p.getCanonicalPath(); String f = StringUtils.removeStart(filePath, connectorDir.getAbsolutePath().toString() + "/src/main/java/"); String finalElement = StringUtils.replace(f, File.separator, "."); String finalPath = StringUtils.removeEnd(finalElement, ".java"); - convertersList.add(finalPath); + convertersList.add(finalPath); } } if (!transformElements.isEmpty()) { for (Iterator iterator = transformElements.iterator(); iterator.hasNext();) { - File p = (File) iterator.next(); + File p = (File)iterator.next(); String filePath = p.getCanonicalPath(); String f = StringUtils.removeStart(filePath, connectorDir.getAbsolutePath().toString() + "/src/main/java/"); String finalElement = StringUtils.replace(f, File.separator, "."); String finalPath = StringUtils.removeEnd(finalElement, ".java"); - transformsList.add(finalPath); + transformsList.add(finalPath); } } if (!aggStrategiesElements.isEmpty()) { for (Iterator iterator = aggStrategiesElements.iterator(); iterator.hasNext();) { - File p = (File) iterator.next(); + File p = (File)iterator.next(); String filePath = p.getCanonicalPath(); String f = StringUtils.removeStart(filePath, connectorDir.getAbsolutePath().toString() + "/src/main/java/"); String finalElement = StringUtils.replace(f, File.separator, "."); String finalPath = StringUtils.removeEnd(finalElement, ".java"); - aggregationStrategiesList.add(finalPath); + aggregationStrategiesList.add(finalPath); } } } @@ -571,12 +562,14 @@ public class CamelKafkaConnectorUpdateMojo extends AbstractCamelKafkaConnectorMo throw new MojoExecutionException("Error processing mvel examples properties template. Reason: " + e, e); } - // Generate documentation in src/main/docs and docs/modules/ROOT/pages/connectors + // Generate documentation in src/main/docs and + // docs/modules/ROOT/pages/connectors File docFolder = new File(connectorDir, "src/main/docs/"); File docFile = new File(docFolder, getMainDepArtifactId() + "-kafka-" + ct.name().toLowerCase() + "-connector.adoc"); File docFolderWebsite = new File(projectBaseDir, "docs/modules/ROOT/pages/connectors/"); File docFileWebsite = new File(docFolderWebsite, getMainDepArtifactId() + "-kafka-" + ct.name().toLowerCase() + "-connector.adoc"); - String changed = templateAutoConfigurationOptions(listOptions, getMainDepArtifactId(), connectorDir, ct, packageName + "." + javaClassConnectorName, convertersList, transformsList, aggregationStrategiesList); + String changed = templateAutoConfigurationOptions(listOptions, getMainDepArtifactId(), connectorDir, ct, packageName + "." + javaClassConnectorName, convertersList, + transformsList, aggregationStrategiesList); boolean updated = updateAutoConfigureOptions(docFile, changed); if (updated) { getLog().info("Updated doc file: " + docFile); @@ -592,247 +585,229 @@ public class CamelKafkaConnectorUpdateMojo extends AbstractCamelKafkaConnectorMo writeJson(listOptions, getMainDepArtifactId(), connectorDir, ct, packageName + "." + javaClassConnectorName, convertersList, transformsList, aggregationStrategiesList); } - private void addProperties(Map<String, String> additionalProperties, String additionalProp) { - if (additionalProp != null && additionalProp.trim().length() > 0) { - for (String prop : additionalProp.split(",")) { - getLog().debug("Additional property before key value split: " + prop); - String[] keyValue = prop.split("="); - getLog().debug("Additional property key value: " + keyValue); - additionalProperties.put(keyValue[0], keyValue[1]); - } - } - } - - private void addConnectorOptions(String sanitizedName, ConnectorType ct, JavaClass javaClass, Method confMethod, - String propertyQualifier, String firstNamespace, String secondNamespace, BaseOptionModel epo, - List<CamelKafkaConnectorOptionModel> listOptions) { - String propertyName = epo.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." + firstNamespace + "." + secondNamespace + "." + epo.getName(); - - String confFieldName = propertyPrefix + "CONF"; - javaClass.addField().setFinal(true).setPublic().setStatic(true).setName(confFieldName).setType(String.class) - .setStringInitializer(propertyValue); - - String docFieldName = propertyPrefix + "DOC"; - String docLiteralInitializer = epo.getDescription(); - if (epo.getEnums() != null && !epo.getEnums().isEmpty()) { - docLiteralInitializer = docLiteralInitializer + " One of:"; - String enumOptionListing = epo.getEnums().stream().reduce("", (s, s2) -> s + " [" + s2 + "]"); - docLiteralInitializer = docLiteralInitializer + enumOptionListing; - } - 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(epo.getShortJavaType(), String.class); - String type = epo.getType(); - String defaultValueClassLiteralInitializer = epo.getDefaultValue() == null ? "null" - : epo.getDefaultValue().toString(); - if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(String.class)) { - defaultValueClassLiteralInitializer = "\"" + defaultValueClassLiteralInitializer + "\""; - } else if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(Long.class)) { - if (!type.equalsIgnoreCase("duration")) { - defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + "L"; - } else { - if (defaultValueClassLiteralInitializer.endsWith("ms")) { - defaultValueClassLiteralInitializer = StringUtils.removeEnd(defaultValueClassLiteralInitializer, - "ms") + "L"; - } else { - defaultValueClassLiteralInitializer = TimeUtils.toMilliSeconds(defaultValueClassLiteralInitializer) - + "L"; - } - } - } else if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(Integer.class)) { - if (!type.equalsIgnoreCase("duration")) { - defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + ""; - } else { - if (defaultValueClassLiteralInitializer.endsWith("ms")) { - defaultValueClassLiteralInitializer = StringUtils.removeEnd(defaultValueClassLiteralInitializer, - "ms") + ""; - } else { - defaultValueClassLiteralInitializer = TimeUtils.toMilliSeconds(defaultValueClassLiteralInitializer) - + ""; - } - } - } else if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(int.class)) { - if (!type.equalsIgnoreCase("duration")) { - defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + ""; - } else { - if (defaultValueClassLiteralInitializer.endsWith("ms")) { - defaultValueClassLiteralInitializer = StringUtils.removeEnd(defaultValueClassLiteralInitializer, - "ms") + ""; - } else { - defaultValueClassLiteralInitializer = TimeUtils.toMilliSeconds(defaultValueClassLiteralInitializer) - + ""; - } - } - } else if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(Float.class)) { - defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + "F"; - } else if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(Double.class)) { - defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + "D"; - } - javaClass.addField().setFinal(true).setPublic().setStatic(true).setName(defaultFieldName) - .setType(defaultValueClass).setLiteralInitializer(defaultValueClassLiteralInitializer); - - String confType = PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.getOrDefault(epo.getShortJavaType(), - "ConfigDef.Type.STRING"); - String confPriority = epo.isDeprecated() ? "ConfigDef.Importance.LOW" : "ConfigDef.Importance.MEDIUM"; - confPriority = epo.isRequired() ? "ConfigDef.Importance.HIGH" : confPriority; - 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, "ConfigDef.Importance.")); - optionModel.setDefaultValue(defaultValueClassLiteralInitializer); - listOptions.add(optionModel); - - } - - private String templateAutoConfigurationOptions(List<CamelKafkaConnectorOptionModel> options, String componentName, - File connectorDir, ConnectorType ct, String connectorClass, List<String> convertersList, - List<String> transformsList, List<String> aggregationStrategiesList) throws MojoExecutionException { - - CamelKafkaConnectorModel model = new CamelKafkaConnectorModel(); - model.setOptions(options); - model.setArtifactId(getMainDepArtifactId()); - model.setGroupId(getMainDepGroupId()); - model.setVersion(getMainDepVersion()); - model.setConnectorClass(connectorClass); - model.setConverters(convertersList); - model.setTransforms(transformsList); - model.setAggregationStrategies(aggregationStrategiesList); - if (getMainDepArtifactId().equalsIgnoreCase("camel-coap+tcp")) { - model.setTitle("camel-coap-tcp"); - } else if (getMainDepArtifactId().equalsIgnoreCase("camel-coaps+tcp")) { - model.setTitle("camel-coaps-tcp"); - } else { - model.setTitle(getMainDepArtifactId()); - } - - try { - String template = null; - if (ct.name().equals(ConnectorType.SINK.name())) { - template = loadText(CamelKafkaConnectorUpdateMojo.class.getClassLoader() - .getResourceAsStream("camel-kafka-connector-sink-options.mvel")); - } else if (ct.name().equals(ConnectorType.SOURCE.name())) { - template = loadText(CamelKafkaConnectorUpdateMojo.class.getClassLoader() - .getResourceAsStream("camel-kafka-connector-source-options.mvel")); - } - String out = (String) TemplateRuntime.eval(template, model, - Collections.singletonMap("util", MvelHelper.INSTANCE)); - return out; - } catch (Exception e) { - throw new MojoExecutionException("Error processing mvel template. Reason: " + e, e); - } - } - - private void writeJson(List<CamelKafkaConnectorOptionModel> options, String componentName, - File connectorDir, ConnectorType ct, String connectorClass, List<String> convertersList, - List<String> transformsList, List<String> aggregationStrategiesList) throws MojoExecutionException { - - CamelKafkaConnectorModel model = new CamelKafkaConnectorModel(); - model.setOptions(options); - model.setArtifactId(getMainDepArtifactId()); - model.setGroupId(getMainDepGroupId()); - model.setVersion(getMainDepVersion()); - model.setConnectorClass(connectorClass); - model.setType(ct.name().toLowerCase()); - model.setConverters(convertersList); - model.setTransforms(transformsList); - model.setAggregationStrategies(aggregationStrategiesList); - if (getMainDepArtifactId().equalsIgnoreCase("camel-coap+tcp")) { - model.setTitle("camel-coap-tcp"); - } else if (getMainDepArtifactId().equalsIgnoreCase("camel-coaps+tcp")) { - model.setTitle("camel-coaps-tcp"); - } else { - model.setTitle(getMainDepArtifactId()); - } + private void addProperties(Map<String, String> additionalProperties, String additionalProp) { + if (additionalProp != null && additionalProp.trim().length() > 0) { + for (String prop : additionalProp.split(",")) { + getLog().debug("Additional property before key value split: " + prop); + String[] keyValue = prop.split("="); + getLog().debug("Additional property key value: " + keyValue); + additionalProperties.put(keyValue[0], keyValue[1]); + } + } + } + + private void addConnectorOptions(String sanitizedName, ConnectorType ct, JavaClass javaClass, Method confMethod, String propertyQualifier, String firstNamespace, + String secondNamespace, BaseOptionModel epo, List<CamelKafkaConnectorOptionModel> listOptions) { + String propertyName = epo.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." + firstNamespace + "." + secondNamespace + "." + epo.getName(); + + String confFieldName = propertyPrefix + "CONF"; + javaClass.addField().setFinal(true).setPublic().setStatic(true).setName(confFieldName).setType(String.class).setStringInitializer(propertyValue); + + String docFieldName = propertyPrefix + "DOC"; + String docLiteralInitializer = epo.getDescription(); + if (epo.getEnums() != null && !epo.getEnums().isEmpty()) { + docLiteralInitializer = docLiteralInitializer + " One of:"; + String enumOptionListing = epo.getEnums().stream().reduce("", (s, s2) -> s + " [" + s2 + "]"); + docLiteralInitializer = docLiteralInitializer + enumOptionListing; + } + 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(epo.getShortJavaType(), String.class); + String type = epo.getType(); + String defaultValueClassLiteralInitializer = epo.getDefaultValue() == null ? "null" : epo.getDefaultValue().toString(); + if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(String.class)) { + defaultValueClassLiteralInitializer = "\"" + defaultValueClassLiteralInitializer + "\""; + } else if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(Long.class)) { + if (!type.equalsIgnoreCase("duration")) { + defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + "L"; + } else { + if (defaultValueClassLiteralInitializer.endsWith("ms")) { + defaultValueClassLiteralInitializer = StringUtils.removeEnd(defaultValueClassLiteralInitializer, "ms") + "L"; + } else { + defaultValueClassLiteralInitializer = TimeUtils.toMilliSeconds(defaultValueClassLiteralInitializer) + "L"; + } + } + } else if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(Integer.class)) { + if (!type.equalsIgnoreCase("duration")) { + defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + ""; + } else { + if (defaultValueClassLiteralInitializer.endsWith("ms")) { + defaultValueClassLiteralInitializer = StringUtils.removeEnd(defaultValueClassLiteralInitializer, "ms") + ""; + } else { + defaultValueClassLiteralInitializer = TimeUtils.toMilliSeconds(defaultValueClassLiteralInitializer) + ""; + } + } + } else if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(int.class)) { + if (!type.equalsIgnoreCase("duration")) { + defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + ""; + } else { + if (defaultValueClassLiteralInitializer.endsWith("ms")) { + defaultValueClassLiteralInitializer = StringUtils.removeEnd(defaultValueClassLiteralInitializer, "ms") + ""; + } else { + defaultValueClassLiteralInitializer = TimeUtils.toMilliSeconds(defaultValueClassLiteralInitializer) + ""; + } + } + } else if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(Float.class)) { + defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + "F"; + } else if (!defaultValueClassLiteralInitializer.equals("null") && defaultValueClass.equals(Double.class)) { + defaultValueClassLiteralInitializer = defaultValueClassLiteralInitializer + "D"; + } + javaClass.addField().setFinal(true).setPublic().setStatic(true).setName(defaultFieldName).setType(defaultValueClass) + .setLiteralInitializer(defaultValueClassLiteralInitializer); + + String confType = PRIMITIVE_TYPES_TO_KAFKA_CONFIG_DEF_MAP.getOrDefault(epo.getShortJavaType(), "ConfigDef.Type.STRING"); + String confPriority = epo.isDeprecated() ? "ConfigDef.Importance.LOW" : "ConfigDef.Importance.MEDIUM"; + confPriority = epo.isRequired() ? "ConfigDef.Importance.HIGH" : confPriority; + 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, "ConfigDef.Importance.")); + optionModel.setDefaultValue(defaultValueClassLiteralInitializer); + listOptions.add(optionModel); + + } + + private String templateAutoConfigurationOptions(List<CamelKafkaConnectorOptionModel> options, String componentName, File connectorDir, ConnectorType ct, String connectorClass, + List<String> convertersList, List<String> transformsList, List<String> aggregationStrategiesList) + throws MojoExecutionException { + + CamelKafkaConnectorModel model = new CamelKafkaConnectorModel(); + model.setOptions(options); + model.setArtifactId(getMainDepArtifactId()); + model.setGroupId(getMainDepGroupId()); + model.setVersion(getMainDepVersion()); + model.setConnectorClass(connectorClass); + model.setConverters(convertersList); + model.setTransforms(transformsList); + model.setAggregationStrategies(aggregationStrategiesList); + if (getMainDepArtifactId().equalsIgnoreCase("camel-coap+tcp")) { + model.setTitle("camel-coap-tcp"); + } else if (getMainDepArtifactId().equalsIgnoreCase("camel-coaps+tcp")) { + model.setTitle("camel-coaps-tcp"); + } else { + model.setTitle(getMainDepArtifactId()); + } + + try { + String template = null; + if (ct.name().equals(ConnectorType.SINK.name())) { + template = loadText(CamelKafkaConnectorUpdateMojo.class.getClassLoader().getResourceAsStream("camel-kafka-connector-sink-options.mvel")); + } else if (ct.name().equals(ConnectorType.SOURCE.name())) { + template = loadText(CamelKafkaConnectorUpdateMojo.class.getClassLoader().getResourceAsStream("camel-kafka-connector-source-options.mvel")); + } + String out = (String)TemplateRuntime.eval(template, model, Collections.singletonMap("util", MvelHelper.INSTANCE)); + return out; + } catch (Exception e) { + throw new MojoExecutionException("Error processing mvel template. Reason: " + e, e); + } + } + + private void writeJson(List<CamelKafkaConnectorOptionModel> options, String componentName, File connectorDir, ConnectorType ct, String connectorClass, + List<String> convertersList, List<String> transformsList, List<String> aggregationStrategiesList) + throws MojoExecutionException { + + CamelKafkaConnectorModel model = new CamelKafkaConnectorModel(); + model.setOptions(options); + model.setArtifactId(getMainDepArtifactId()); + model.setGroupId(getMainDepGroupId()); + model.setVersion(getMainDepVersion()); + model.setConnectorClass(connectorClass); + model.setType(ct.name().toLowerCase()); + model.setConverters(convertersList); + model.setTransforms(transformsList); + model.setAggregationStrategies(aggregationStrategiesList); + if (getMainDepArtifactId().equalsIgnoreCase("camel-coap+tcp")) { + model.setTitle("camel-coap-tcp"); + } else if (getMainDepArtifactId().equalsIgnoreCase("camel-coaps+tcp")) { + model.setTitle("camel-coaps-tcp"); + } else { + model.setTitle(getMainDepArtifactId()); + } File docFolder = new File(connectorDir, "src/generated/resources/"); File docFile = new File(docFolder, getMainDepArtifactId() + "-kafka-" + ct.name().toLowerCase() + "-connector.json"); - JsonObject j = JsonMapperKafkaConnector.asJsonObject(model); - updateJsonFile(docFile, Jsoner.prettyPrint(j.toJson())); - } - - private boolean updateAutoConfigureOptions(File file, String changed) throws MojoExecutionException { - try { - if (!file.exists()) { - // include markers for new files - changed = "// kafka-connector options: START\n" + changed + "\n// kafka-connector options: END\n"; - writeText(file, changed); - return true; - } - - String text = loadText(new FileInputStream(file)); - - String existing = Strings.between(text, "// kafka-connector options: START", - "// kafka-connector options: END"); - if (existing != null) { - // remove leading line breaks etc - existing = existing.trim(); - changed = changed.trim(); - if (existing.equals(changed)) { - return false; - } else { - String before = Strings.before(text, "// kafka-connector options: START"); - String after = Strings.after(text, "// kafka-connector options: END"); - text = before + "// kafka-connector options: START\n" + changed - + "\n// kafka-connector options: END" + after; - writeText(file, text); - return true; - } - } else { - getLog().warn("Cannot find markers in file " + file); - getLog().warn("Add the following markers"); - getLog().warn("\t// kafka-connector options: START"); - getLog().warn("\t// kafka-connector options: END"); - return false; - } - } catch (Exception e) { - throw new MojoExecutionException("Error reading file " + file + " Reason: " + e, e); - } - } - - private boolean updateJsonFile(File file, String changed) throws MojoExecutionException { - try { - if (!file.exists()) { - writeText(file, changed); - return true; - } - - String text = loadText(new FileInputStream(file)); - - String existing = text; - if (existing != null) { - // remove leading line breaks etc - existing = existing.trim(); - changed = changed.trim(); - if (existing.equals(changed)) { - System.err.println("content is equal"); - return false; - } else { - System.err.println("content is different"); - writeText(file, changed); - return true; - } - } else { - System.err.println("existsing is null"); - return false; - } - } catch (Exception e) { - throw new MojoExecutionException("Error reading file " + file + " Reason: " + e, e); - } - } - - private enum ConnectorType { - SINK, SOURCE - } + JsonObject j = JsonMapperKafkaConnector.asJsonObject(model); + updateJsonFile(docFile, Jsoner.prettyPrint(j.toJson())); + } + + private boolean updateAutoConfigureOptions(File file, String changed) throws MojoExecutionException { + try { + if (!file.exists()) { + // include markers for new files + changed = "// kafka-connector options: START\n" + changed + "\n// kafka-connector options: END\n"; + writeText(file, changed); + return true; + } + + String text = loadText(new FileInputStream(file)); + + String existing = Strings.between(text, "// kafka-connector options: START", "// kafka-connector options: END"); + if (existing != null) { + // remove leading line breaks etc + existing = existing.trim(); + changed = changed.trim(); + if (existing.equals(changed)) { + return false; + } else { + String before = Strings.before(text, "// kafka-connector options: START"); + String after = Strings.after(text, "// kafka-connector options: END"); + text = before + "// kafka-connector options: START\n" + changed + "\n// kafka-connector options: END" + after; + writeText(file, text); + return true; + } + } else { + getLog().warn("Cannot find markers in file " + file); + getLog().warn("Add the following markers"); + getLog().warn("\t// kafka-connector options: START"); + getLog().warn("\t// kafka-connector options: END"); + return false; + } + } catch (Exception e) { + throw new MojoExecutionException("Error reading file " + file + " Reason: " + e, e); + } + } + + private boolean updateJsonFile(File file, String changed) throws MojoExecutionException { + try { + if (!file.exists()) { + writeText(file, changed); + return true; + } + + String text = loadText(new FileInputStream(file)); + + String existing = text; + if (existing != null) { + // remove leading line breaks etc + existing = existing.trim(); + changed = changed.trim(); + if (existing.equals(changed)) { + System.err.println("content is equal"); + return false; + } else { + System.err.println("content is different"); + writeText(file, changed); + return true; + } + } else { + System.err.println("existsing is null"); + return false; + } + } catch (Exception e) { + throw new MojoExecutionException("Error reading file " + file + " Reason: " + e, e); + } + } + + private enum ConnectorType { + SINK, SOURCE + } } diff --git a/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/dto/CamelKafkaConnectorModel.java b/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/dto/CamelKafkaConnectorModel.java index 00b7420..d14a9b2 100644 --- a/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/dto/CamelKafkaConnectorModel.java +++ b/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/dto/CamelKafkaConnectorModel.java @@ -102,11 +102,11 @@ public class CamelKafkaConnectorModel { this.aggregationStrategies = aggregationStrategies; } - public String getType() { - return type; - } + public String getType() { + return type; + } - public void setType(String type) { - this.type = type; - } + public void setType(String type) { + this.type = type; + } } diff --git a/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/utils/JsonMapperKafkaConnector.java b/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/utils/JsonMapperKafkaConnector.java index 352eeb0..669c601 100644 --- a/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/utils/JsonMapperKafkaConnector.java +++ b/tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/utils/JsonMapperKafkaConnector.java @@ -23,9 +23,9 @@ import org.apache.camel.kafkaconnector.maven.dto.CamelKafkaConnectorOptionModel; import org.apache.camel.util.json.JsonObject; public final class JsonMapperKafkaConnector { - - private static final String KAFKA_CONNECTOR_GROUPID_SUFFIX = ".kafkaconnector"; - private static final String KAFKA_CONNECTOR_ARTIFACTID_SUFFIX = "-kafka-connector"; + + private static final String KAFKA_CONNECTOR_GROUPID_SUFFIX = ".kafkaconnector"; + private static final String KAFKA_CONNECTOR_ARTIFACTID_SUFFIX = "-kafka-connector"; private JsonMapperKafkaConnector() { } @@ -43,13 +43,13 @@ public final class JsonMapperKafkaConnector { wrapper.put("properties", asJsonObject(model.getOptions())); return wrapper; } - + public static JsonObject asJsonObject(List<CamelKafkaConnectorOptionModel> options) { JsonObject json = new JsonObject(); options.forEach(option -> json.put(option.getName(), asJsonObject(option))); return json; } - + public static JsonObject asJsonObject(CamelKafkaConnectorOptionModel model) { JsonObject obj = new JsonObject(); obj.put("name", model.getName());