From 3cb7a44be5a1b51e58c5ffc4f0c6f8cb2377024d Mon Sep 17 00:00:00 2001 From: Beat Strasser Date: Thu, 25 Aug 2016 10:41:41 +0200 Subject: [PATCH] Added support for karaf feature files #41 --- .../internal/impl/KarafFeature.java | 76 +++++++++++++++++++ .../internal/impl/MavenProjectImpl.java | 40 ++++++++++ 2 files changed, 116 insertions(+) create mode 100644 com.inventage.tools.versiontiger/src/main/java/com/inventage/tools/versiontiger/internal/impl/KarafFeature.java diff --git a/com.inventage.tools.versiontiger/src/main/java/com/inventage/tools/versiontiger/internal/impl/KarafFeature.java b/com.inventage.tools.versiontiger/src/main/java/com/inventage/tools/versiontiger/internal/impl/KarafFeature.java new file mode 100644 index 0000000..f6ee4a6 --- /dev/null +++ b/com.inventage.tools.versiontiger/src/main/java/com/inventage/tools/versiontiger/internal/impl/KarafFeature.java @@ -0,0 +1,76 @@ +package com.inventage.tools.versiontiger.internal.impl; + +import java.io.File; +import java.util.LinkedHashSet; +import java.util.Set; + +import com.inventage.tools.versiontiger.MavenVersion; +import com.inventage.tools.versiontiger.util.FileHandler; +import com.inventage.tools.versiontiger.util.XmlHandler; + +import de.pdark.decentxml.Element; + +class KarafFeature { + + private static final String ELEMENT_ROOT = "features"; + private static final String ELEMENT_BUNDLE = "bundle"; + private static final String BUNDLE_MVN_PREFIX = "mvn:"; + private static final String GAV_SEPARATOR = "/"; + + private final File filePath; + + private String content; + + KarafFeature(File filePath) { + this.filePath = filePath; + } + + Set updateReferencesFor(String id, MavenVersion oldVersion, MavenVersion newVersion) { + XmlHandler xmlHandler = new XmlHandler(); + Set result = new LinkedHashSet(); + + Element features = xmlHandler.getElement(getFileContent(), ELEMENT_ROOT); + for (Element feature : features.getChildren()) { + for (Element bundle : feature.getChildren()) { + if (ELEMENT_BUNDLE.equals(bundle.getName())) { + String bundleReference = updateBundleReferencesFor(id, oldVersion, newVersion, bundle); + if (bundleReference != null) { + result.add(bundleReference); + } + } + } + } + + if (!result.isEmpty()) { + content = features.getDocument().toXML(); + new FileHandler().writeFileContent(filePath, content); + } + + return result; + } + + private String updateBundleReferencesFor(String id, MavenVersion oldVersion, MavenVersion newVersion, Element bundle) { + String bundleReference = bundle.getTrimmedText(); + if (bundleReference.startsWith(BUNDLE_MVN_PREFIX)) { + bundleReference = bundleReference.substring(4); + String[] gav = bundleReference.split(GAV_SEPARATOR, 3); + if (gav != null && gav.length == 3) { + if (id.equals(gav[1]) && (oldVersion == null || gav[2].equals(oldVersion.toString()))) { + bundleReference = BUNDLE_MVN_PREFIX + gav[0] + GAV_SEPARATOR + gav[1] + GAV_SEPARATOR + newVersion; + bundle.setText(bundleReference); + return bundle.getChildPath() + " = " + bundleReference; + } + } + } + return null; + } + + private String getFileContent() { + if (content == null) { + content = new FileHandler().readFileContent(filePath); + } + + return content; + } + +} diff --git a/com.inventage.tools.versiontiger/src/main/java/com/inventage/tools/versiontiger/internal/impl/MavenProjectImpl.java b/com.inventage.tools.versiontiger/src/main/java/com/inventage/tools/versiontiger/internal/impl/MavenProjectImpl.java index 84d1bf8..b927bd7 100644 --- a/com.inventage.tools.versiontiger/src/main/java/com/inventage/tools/versiontiger/internal/impl/MavenProjectImpl.java +++ b/com.inventage.tools.versiontiger/src/main/java/com/inventage/tools/versiontiger/internal/impl/MavenProjectImpl.java @@ -1,6 +1,8 @@ package com.inventage.tools.versiontiger.internal.impl; import java.io.File; +import java.util.LinkedHashSet; +import java.util.Set; import com.inventage.tools.versiontiger.MavenProject; import com.inventage.tools.versiontiger.MavenVersion; @@ -85,6 +87,7 @@ public void setVersion(MavenVersion newVersion) { version = newVersion; setProjectVersionInPom(newVersion); + setVersionInKarafFiles(oldVersion, newVersion); } private void setProjectVersionInPom(MavenVersion newVersion) { @@ -151,6 +154,13 @@ public void updateReferencesFor(String id, MavenVersion oldVersion, MavenVersion pomContent = projectElement.getDocument().toXML(); new FileHandler().writeFileContent(getPomXmlFile(), pomContent); } + + for (File karafFile : getKarafFiles()) { + Set updatedReferences = new KarafFeature(karafFile).updateReferencesFor(id, oldVersion, newVersion); + for (String updatedKarafReference : updatedReferences) { + logReferenceSuccess(karafFile + ": " + updatedKarafReference, oldVersion, newVersion, id); + } + } } } @@ -199,6 +209,36 @@ private boolean updateParent(Element projectElement, String id, MavenVersion old return hasModifications; } + + private Set getKarafFiles() { + Set result = new LinkedHashSet(); + + Element propertyElement = new XmlHandler().getElement(getPomContent(), "project/properties/versionTigerFiles"); + if (propertyElement != null) { + String propertyValue = propertyElement.getText(); + if (propertyValue != null) { + for (String fileDef : propertyValue.trim().split("\\s*,\\s*")) { + if (fileDef.startsWith("karaf:")) { + File karafFile = new FileHandler().createFileFromPath(projectPath, fileDef.substring(6)); + if (karafFile.exists()) { + result.add(karafFile); + } + } + } + } + } + + return result; + } + + private void setVersionInKarafFiles(MavenVersion oldVersion, MavenVersion newVersion) { + for (File karafFile : getKarafFiles()) { + Set updatedReferences = new KarafFeature(karafFile).updateReferencesFor(id, oldVersion, newVersion); + for (String updatedKarafReference : updatedReferences) { + logSuccess(karafFile + ": " + updatedKarafReference, oldVersion, newVersion); + } + } + } private File getPomXmlFile() { return new File(projectPath, "pom.xml");