Refactor URL replacement in karaf-plugin 40/85140/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 16 Oct 2019 08:57:29 +0000 (10:57 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 16 Oct 2019 09:03:15 +0000 (11:03 +0200)
We need to be mindful of file separators when dealing with URI/URLs.
While File-based strings are using platform-dependend separator,
either '/' (Unices) or '\' (Windows et al.), file: URI scheme is
using '/' to separate path components (as per RFC8089).

Make sure we preserve the semantic context and perform proper
conversion between the two representations.

JIRA: ODLPARENT-214
Change-Id: I8d20ee1bdb4bfb3c1f675423a3849082c21fe26c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
karaf-plugin/src/main/java/org/opendaylight/odlparent/PopulateLocalRepoMojo.java

index f3f7880a11b73a62a843fc65fe134b7d8603499e..24dfdac7c38d89050a78d1aa9b1d44a0c3625b71 100644 (file)
@@ -143,16 +143,23 @@ public class PopulateLocalRepoMojo extends AbstractMojo {
 
     private void readFeatureCfg(AetherUtil aetherUtil, FeatureUtil featureUtil, Set<Artifact> artifacts,
             Set<Features> features) {
-        String karafHome = localRepo.getParent();
-        File file = new File(karafHome + "/etc/org.apache.karaf.features.cfg");
-        Properties prop = new Properties();
+        // Create file structure
+        final File karafHome = localRepo.getParentFile();
+        final File karafEtc = new File(karafHome, "etc");
+        final File file = new File(karafEtc, "org.apache.karaf.features.cfg");
+
+        final Properties prop = new Properties();
         try (InputStream is = new FileInputStream(file)) {
             prop.load(is);
-            String featuresRepositories = prop.getProperty("featuresRepositories");
+
+            // Note this performs path seaparator translation
+            final String karafHomePath = karafHome.toURI().getPath();
+            final String karafEtcPath = karafEtc.toURI().getPath();
+            final String featuresRepositories = prop.getProperty("featuresRepositories");
             for (String mvnUrl : featuresRepositories.split(",")) {
-                String fixedUrl = mvnUrl
-                        .replace("${karaf.home}", karafHome)
-                        .replace("${karaf.etc}", karafHome + File.pathSeparatorChar + "etc");
+                final String fixedUrl = mvnUrl
+                        .replace("${karaf.home}", karafHomePath)
+                        .replace("${karaf.etc}", karafEtcPath);
                 if (fixedUrl.startsWith("file:")) {
                     try {
                         // Local feature file
@@ -173,7 +180,7 @@ public class PopulateLocalRepoMojo extends AbstractMojo {
 
     private Set<Artifact> readStartupProperties(AetherUtil aetherUtil) {
         Set<Artifact> artifacts = new LinkedHashSet<>();
-        File file = new File(localRepo.getParentFile().toString() + "/etc/startup.properties");
+        File file = new File(new File(localRepo.getParentFile(), "etc"), "startup.properties");
         Properties prop = new Properties();
         try (InputStream is = new FileInputStream(file)) {
             prop.load(is);