You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2018/01/19 07:08:20 UTC

[sling-whiteboard] branch master updated: Refactor start order handling

This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 6ee87c2  Refactor start order handling
6ee87c2 is described below

commit 6ee87c27772afa7cd41a21f6b751028fac4f156a
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Jan 19 08:07:53 2018 +0100

    Refactor start order handling
---
 .../sling/feature/modelconverter/impl/Main.java    | 12 ++--
 .../java/org/apache/sling/feature/Artifact.java    | 41 ++++++++++++
 .../java/org/apache/sling/feature/Bundles.java     | 76 +++-------------------
 .../apache/sling/feature/process/BuilderUtil.java  |  4 +-
 .../apache/sling/feature/maven/Preprocessor.java   |  5 +-
 5 files changed, 62 insertions(+), 76 deletions(-)

diff --git a/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/Main.java b/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/Main.java
index fedd964..9f5aaf9 100644
--- a/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/Main.java
+++ b/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/Main.java
@@ -656,13 +656,17 @@ public class Main {
         final Feature f = new Feature("application");
 
         // bundles
-        for(final Map.Entry<Integer, org.apache.sling.feature.Artifact> bundle : app.getBundles().getAllBundles()) {
-            final ArtifactId id = bundle.getValue().getId();
+        for(final org.apache.sling.feature.Artifact bundle : app.getBundles()) {
+            final ArtifactId id = bundle.getId();
             final Artifact newBundle = new Artifact(id.getGroupId(), id.getArtifactId(), id.getVersion(), id.getClassifier(), id.getType());
-            for(final Map.Entry<String, String> prop : bundle.getValue().getMetadata()) {
+            for(final Map.Entry<String, String> prop : bundle.getMetadata()) {
                 newBundle.getMetadata().put(prop.getKey(), prop.getValue());
             }
-            f.getOrCreateRunMode(null).getOrCreateArtifactGroup(bundle.getKey()).add(newBundle);
+            int startLevel = bundle.getStartOrder();
+            if ( startLevel == 0 ) {
+                startLevel = 20;
+            }
+            f.getOrCreateRunMode(null).getOrCreateArtifactGroup(startLevel).add(newBundle);
         }
 
         // configurations
diff --git a/featuremodel/feature/src/main/java/org/apache/sling/feature/Artifact.java b/featuremodel/feature/src/main/java/org/apache/sling/feature/Artifact.java
index 6821e26..81ff660 100644
--- a/featuremodel/feature/src/main/java/org/apache/sling/feature/Artifact.java
+++ b/featuremodel/feature/src/main/java/org/apache/sling/feature/Artifact.java
@@ -63,6 +63,47 @@ public class Artifact implements Comparable<Artifact> {
         return this.metadata;
     }
 
+    /**
+     * Get the start order of the artifact.
+     * This is a convenience method which gets the value for the property named
+     * {@code #KEY_START_ORDER} from the metadata.
+     * @return The start order, if no start order is defined, {@code 0} is returned.
+     * @throws NumberFormatException If the stored metadata is not a number
+     * @throws IllegalStateException If the stored metadata is a negative number
+     */
+    public int getStartOrder() {
+        final String order = this.getMetadata().get(Artifact.KEY_START_ORDER);
+        final int startOrder;
+        if ( order != null ) {
+            startOrder = Integer.parseInt(order);
+            if ( startOrder < 0 ) {
+                throw new IllegalStateException("Start order must be >= 0 but is " + order);
+            }
+        } else {
+            startOrder = 0;
+        }
+
+        return startOrder;
+    }
+
+    /**
+     * Set the start order of the artifact
+     * This is a convenience method which sets the value of the property named
+     * {@code #KEY_START_ORDER} from the metadata.
+     * @param startOrder The start order
+     * @throws IllegalArgumentException If the number is negative
+     */
+    public void setStartOrder(final int startOrder) {
+        if ( startOrder < 0 ) {
+            throw new IllegalArgumentException("Start order must be >= 0 but is " + startOrder);
+        }
+        if ( startOrder == 0 ) {
+            this.getMetadata().remove(KEY_START_ORDER);
+        } else {
+            this.getMetadata().put(KEY_START_ORDER, String.valueOf(startOrder));
+        }
+    }
+
     @Override
     public int compareTo(final Artifact o) {
         return this.id.compareTo(o.id);
diff --git a/featuremodel/feature/src/main/java/org/apache/sling/feature/Bundles.java b/featuremodel/feature/src/main/java/org/apache/sling/feature/Bundles.java
index 269d39f..12b5b52 100644
--- a/featuremodel/feature/src/main/java/org/apache/sling/feature/Bundles.java
+++ b/featuremodel/feature/src/main/java/org/apache/sling/feature/Bundles.java
@@ -34,25 +34,9 @@ public class Bundles implements Iterable<Artifact> {
     private final List<Artifact> bundles = new ArrayList<>();
 
     /**
-     * Get the start order of a bundle.
-     * @param bundle The bundle
-     * @return The start order, if no start order is defined, {@code 0} is returned.
-     */
-    public static int getStartOrder(final Artifact bundle) {
-        final String order = bundle.getMetadata().get(Artifact.KEY_START_ORDER);
-        final int startOrder;
-        if ( order != null ) {
-            startOrder = Integer.parseInt(order);
-        } else {
-            startOrder = 0;
-        }
-
-        return startOrder;
-    }
-
-    /**
      * Get the map of all bundles sorted by start order. The map is sorted
-     * and iterating over the keys is done in start level order.
+     * and iterating over the keys is done in start order. Bundles without a start
+     * order (having the value 0) are returned last.
      * @return The map of bundles. The map is unmodifiable.
      */
     public Map<Integer, List<Artifact>> getBundlesByStartOrder() {
@@ -74,7 +58,7 @@ public class Bundles implements Iterable<Artifact> {
         });
 
         for(final Artifact bundle : this.bundles) {
-            final int startOrder = getStartOrder(bundle);
+            final int startOrder = bundle.getStartOrder();
             List<Artifact> list = startOrderMap.get(startOrder);
             if ( list == null ) {
                 list = new ArrayList<>();
@@ -85,31 +69,6 @@ public class Bundles implements Iterable<Artifact> {
         return Collections.unmodifiableMap(startOrderMap);
     }
 
-    public List<Map.Entry<Integer, Artifact>> getAllBundles() {
-        final List<Map.Entry<Integer, Artifact>> list = new ArrayList<>();
-        for(final Artifact a : this) {
-            list.add(new Map.Entry<Integer, Artifact>() {
-
-                @Override
-                public Artifact setValue(Artifact value) {
-                    return null;
-                }
-
-                @Override
-                public Artifact getValue() {
-                    // TODO Auto-generated method stub
-                    return a;
-                }
-
-                @Override
-                public Integer getKey() {
-                    return getStartOrder(a);
-                }
-            });
-        }
-        return list;
-    }
-
     /**
      * Add an artifact as a bundle.
      * @param bundle The bundle
@@ -158,31 +117,14 @@ public class Bundles implements Iterable<Artifact> {
     }
 
     /**
-     * Get start order and artifact for the given id, neglecting the version
+     * Get the artifact for the given id, neglecting the version
      * @param id The artifact id
      * @return A map entry with start order and artifact, {@code null} otherwise
      */
-    public Map.Entry<Integer, Artifact> getSame(final ArtifactId id) {
-        for(final Artifact bundle : this) {
+    public Artifact getSame(final ArtifactId id) {
+        for(final Artifact bundle : this.bundles) {
             if ( bundle.getId().isSame(id)) {
-                final int startOrder = getStartOrder(bundle);
-                return new Map.Entry<Integer, Artifact>() {
-
-                    @Override
-                    public Integer getKey() {
-                        return startOrder;
-                    }
-
-                    @Override
-                    public Artifact getValue() {
-                        return bundle;
-                    }
-
-                    @Override
-                    public Artifact setValue(final Artifact value) {
-                        throw new IllegalStateException();
-                    }
-                };
+                return bundle;
             }
         }
         return null;
@@ -194,7 +136,7 @@ public class Bundles implements Iterable<Artifact> {
      * @return {@code true} if the artifact exists
      */
     public boolean containsExact(final ArtifactId id) {
-        for(final Artifact entry : this) {
+        for(final Artifact entry : this.bundles) {
             if ( entry.getId().equals(id)) {
                 return true;
             }
@@ -208,7 +150,7 @@ public class Bundles implements Iterable<Artifact> {
      * @return {@code true} if the artifact exists
      */
     public boolean containsSame(final ArtifactId id) {
-        for(final Artifact entry : this) {
+        for(final Artifact entry : this.bundles) {
             if ( entry.getId().isSame(id)) {
                 return true;
             }
diff --git a/featuremodel/feature/src/main/java/org/apache/sling/feature/process/BuilderUtil.java b/featuremodel/feature/src/main/java/org/apache/sling/feature/process/BuilderUtil.java
index 1db7bf8..377a5be 100644
--- a/featuremodel/feature/src/main/java/org/apache/sling/feature/process/BuilderUtil.java
+++ b/featuremodel/feature/src/main/java/org/apache/sling/feature/process/BuilderUtil.java
@@ -58,8 +58,8 @@ class BuilderUtil {
                 // version handling - use provided algorithm
                 boolean replace = true;
                 if ( artifactMergeAlg == ArtifactMerge.HIGHEST ) {
-                    final Map.Entry<Integer, Artifact> existing = target.getSame(a.getId());
-                    if ( existing != null && existing.getValue().getId().getOSGiVersion().compareTo(a.getId().getOSGiVersion()) > 0 ) {
+                    final Artifact existing = target.getSame(a.getId());
+                    if ( existing != null && existing.getId().getOSGiVersion().compareTo(a.getId().getOSGiVersion()) > 0 ) {
                         replace = false;
                     }
                 }
diff --git a/featuremodel/osgifeature-maven-plugin/src/main/java/org/apache/sling/feature/maven/Preprocessor.java b/featuremodel/osgifeature-maven-plugin/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
index f7c011d..1435e4b 100644
--- a/featuremodel/osgifeature-maven-plugin/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
+++ b/featuremodel/osgifeature-maven-plugin/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
@@ -23,7 +23,6 @@ import java.io.Reader;
 import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
 import java.util.stream.Collectors;
 
 import org.apache.maven.model.Dependency;
@@ -296,8 +295,8 @@ public class Preprocessor {
             final ProjectInfo info,
             final Feature assembledFeature,
             final String scope) {
-        for(final Map.Entry<Integer, org.apache.sling.feature.Artifact> entry : assembledFeature.getBundles().getAllBundles()) {
-            final ArtifactId a = entry.getValue().getId();
+        for(final org.apache.sling.feature.Artifact entry : assembledFeature.getBundles()) {
+            final ArtifactId a = entry.getId();
             if ( a.getGroupId().equals(info.project.getGroupId())
                  && a.getArtifactId().equals(info.project.getArtifactId())
                  && a.getVersion().equals(info.project.getVersion()) ) {

-- 
To stop receiving notification emails like this one, please contact
['"commits@sling.apache.org" <co...@sling.apache.org>'].