SLING-11003 : Provide option to store bundle symbolic name and version in metadata
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/launcher/LauncherProperties.java b/src/main/java/org/apache/sling/feature/extension/apiregions/launcher/LauncherProperties.java
index 9cfc1f7..047ac0a 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/launcher/LauncherProperties.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/launcher/LauncherProperties.java
@@ -48,32 +48,29 @@
     private static final String REGION_ORDER = "__region.order__";
 
     public static Properties getBundleIDtoBSNandVersionMap(Feature app, ArtifactProvider artifactProvider) {
-        Map<ArtifactId, String> map = new HashMap<>();
-
-        for (Artifact bundle : app.getBundles())
-        {
-            map.computeIfAbsent(bundle.getId(), id ->
-            {
-                try(JarFile jarFile = IOUtils.getJarFileFromURL(artifactProvider.provide(id), true, null)) {
-                    Attributes manifest = jarFile.getManifest().getMainAttributes();
-                    String bsn = manifest.getValue(Constants.BUNDLE_SYMBOLICNAME);
-                    if (bsn != null && !bsn.trim().isEmpty())
-                    {
-                        return bsn.trim() + "~" + Version.parseVersion(manifest.getValue(Constants.BUNDLE_VERSION));
-                    }
-                    else {
-                        return null;
-                    }
-                } catch (IOException ex) {
-                    throw new UncheckedIOException(ex);
-                }
-            });
-        }
-
         Properties result = new Properties();
 
-        for (Map.Entry<ArtifactId, String> entry : map.entrySet()) {
-            result.setProperty(entry.getKey().toMvnId(), entry.getValue());
+        for (Artifact bundle : app.getBundles()) {
+            final String key = bundle.getId().toMvnId();
+            if ( result.getProperty(key) == null ) {
+                String bsn = bundle.getMetadata().get(Constants.BUNDLE_SYMBOLICNAME);
+                String version = bundle.getMetadata().get(Constants.BUNDLE_VERSION);
+                if ( bsn == null || version == null ) {
+                    try(JarFile jarFile = IOUtils.getJarFileFromURL(artifactProvider.provide(bundle.getId()), true, null)) {
+                        Attributes manifest = jarFile.getManifest().getMainAttributes();
+                        bsn = manifest.getValue(Constants.BUNDLE_SYMBOLICNAME);
+                        if (bsn != null) {
+                            version = manifest.getValue(Constants.BUNDLE_VERSION);
+                        }
+                    } catch (IOException ex) {
+                        throw new UncheckedIOException(ex);
+                    }
+    
+                }
+                if ( bsn != null && version != null ) {
+                    result.setProperty(key, bsn.concat("~").concat(Version.parseVersion(version).toString()));
+                }
+            }
         }
 
         return result;
diff --git a/src/test/java/org/apache/sling/feature/extension/apiregions/launcher/LauncherPropertiesTest.java b/src/test/java/org/apache/sling/feature/extension/apiregions/launcher/LauncherPropertiesTest.java
index ba2a355..ff71541 100644
--- a/src/test/java/org/apache/sling/feature/extension/apiregions/launcher/LauncherPropertiesTest.java
+++ b/src/test/java/org/apache/sling/feature/extension/apiregions/launcher/LauncherPropertiesTest.java
@@ -25,6 +25,7 @@
 import org.apache.sling.feature.extension.apiregions.api.ApiRegions;
 import org.junit.Assert;
 import org.junit.Test;
+import org.osgi.framework.Constants;
 
 public class LauncherPropertiesTest
 {
@@ -94,4 +95,20 @@
         Assert.assertEquals(1, prop.size());
         Assert.assertEquals(featureId.toMvnId(), prop.get(artifactId.toMvnId()));
     }
+
+    @Test
+    public void testGetBundleIDtoBSNandVersionMap() {
+        final ArtifactId featureId = ArtifactId.parse("g:f:1");
+        final ArtifactId artifactId = ArtifactId.parse("g:a:2");
+
+        final Feature f = new Feature(featureId);
+        final Artifact a = new Artifact(artifactId);
+        a.getMetadata().put(Constants.BUNDLE_SYMBOLICNAME, "my.bundle");
+        a.getMetadata().put(Constants.BUNDLE_VERSION, "1.0");
+        f.getBundles().add(a);
+
+        final Properties prop = LauncherProperties.getBundleIDtoBSNandVersionMap(f, null);
+        Assert.assertEquals(1, prop.size());
+        Assert.assertEquals("my.bundle~1.0.0", prop.get(a.getId().toMvnId()));
+    }
 }