You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/04/27 09:51:32 UTC

[sling-org-apache-sling-feature] 12/22: SLING-7521 Order bundles in the generated app based on feature order and start order

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

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

commit 5ec3548e14f1ebfb383ec32e427cf131202bb119
Author: David Bosschaert <bo...@adobe.com>
AuthorDate: Mon Mar 5 15:05:23 2018 +0000

    SLING-7521 Order bundles in the generated app based on feature order and start order
    
    Order resource (bundles and features) in the resulting application based on the order
    of resolved features and then also in the order of the start order within the feature.
---
 .../org/apache/sling/feature/FeatureResource.java  | 50 ++++++++++++++++++++++
 .../sling/feature/process/ApplicationBuilder.java  | 43 +++++++++++++------
 .../sling/feature/process/FeatureResolver.java     | 13 +++---
 3 files changed, 88 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/FeatureResource.java b/src/main/java/org/apache/sling/feature/FeatureResource.java
new file mode 100644
index 0000000..38b0b43
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/FeatureResource.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.feature;
+
+import org.osgi.framework.Version;
+import org.osgi.resource.Resource;
+
+/**
+ * A Resource that is associated with an Maven Artifact and belongs to a Feature.
+ */
+public interface FeatureResource extends Resource {
+    /**
+     * Obtain the ID of the resource. If the resource is a bundle then this
+     * is the bundle symbolic name.
+     * @return The ID of the resource.
+     */
+    String getId();
+
+    /**
+     * Obtain the version of the resource.
+     * @return The version of the resource.
+     */
+    Version getVersion();
+
+    /**
+     * Obtain the associated (Maven) Artifact.
+     * @return The artifact for this Resource.
+     */
+    Artifact getArtifact();
+
+    /**
+     * Obtain the feature that contains this resource.
+     * @return The feature that contains the resource.
+     */
+    Feature getFeature();
+}
diff --git a/src/main/java/org/apache/sling/feature/process/ApplicationBuilder.java b/src/main/java/org/apache/sling/feature/process/ApplicationBuilder.java
index 49c17b8..2a9698a 100644
--- a/src/main/java/org/apache/sling/feature/process/ApplicationBuilder.java
+++ b/src/main/java/org/apache/sling/feature/process/ApplicationBuilder.java
@@ -16,12 +16,14 @@
  */
 package org.apache.sling.feature.process;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import org.apache.sling.feature.Application;
+import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.FeatureResource;
+
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Build an application based on features.
@@ -89,12 +91,11 @@ public class ApplicationBuilder {
             app = new Application();
         }
 
-        // Created sorted feature list
         // Remove duplicate features by selecting the one with the highest version
-        List<Feature> sortedFeatureList = new ArrayList<>();
+        final List<Feature> featureList = new ArrayList<>();
         for(final Feature f : features) {
             Feature found = null;
-            for(final Feature s : sortedFeatureList) {
+            for(final Feature s : featureList) {
                 if ( s.getId().isSame(f.getId()) ) {
                     found = s;
                     break;
@@ -108,21 +109,29 @@ public class ApplicationBuilder {
                     add = false;
                 } else {
                     // remove lower version, higher version will be added
-                    app.getFeatureIds().remove(found.getId());
-                    sortedFeatureList.remove(found);
+                    featureList.remove(found);
                 }
             }
             if ( add ) {
-                app.getFeatureIds().add(f.getId());
-                sortedFeatureList.add(f);
+                featureList.add(f);
             }
         }
 
         // order by dependency chain
-        sortedFeatureList = resolver.orderFeatures(sortedFeatureList);
+        final List<FeatureResource> sortedResources = resolver.orderResources(featureList);
+
+        final List<Feature> sortedFeatures = new ArrayList<>();
+        for (final FeatureResource fr : sortedResources) {
+            Feature f = fr.getFeature();
+            if (!sortedFeatures.contains(f)) {
+                sortedFeatures.add(f);
+            }
+        }
 
         // assemble
-        for(final Feature f : sortedFeatureList) {
+        int featureStartOrder = 5; // begin with start order a little higher than 0
+        for(final Feature f : sortedFeatures) {
+            app.getFeatureIds().add(f.getId());
             final Feature assembled = FeatureBuilder.assemble(f, context.clone(new FeatureProvider() {
 
                 @Override
@@ -136,6 +145,16 @@ public class ApplicationBuilder {
                 }
             }));
 
+            int globalStartOrder = featureStartOrder;
+            for (Artifact a : assembled.getBundles()) {
+                int so = a.getStartOrder() + featureStartOrder;
+                if (so > globalStartOrder)
+                    globalStartOrder = so;
+                a.setStartOrder(so);
+            }
+            // Next feature will have a higher start order than the previous
+            featureStartOrder = globalStartOrder + 1;
+
             merge(app, assembled);
         }
 
diff --git a/src/main/java/org/apache/sling/feature/process/FeatureResolver.java b/src/main/java/org/apache/sling/feature/process/FeatureResolver.java
index b3eaa35..5fbba7c 100644
--- a/src/main/java/org/apache/sling/feature/process/FeatureResolver.java
+++ b/src/main/java/org/apache/sling/feature/process/FeatureResolver.java
@@ -19,20 +19,21 @@ package org.apache.sling.feature.process;
 import java.util.List;
 
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.FeatureResource;
 
 /**
  * A resolver that can perform operations on the feature model.
  */
 public interface FeatureResolver extends AutoCloseable {
     /**
-     * Order the features by their dependency chain. Each feature and its
-     * components are resolved and each other feature providing the capabilities
-     * needed by the feature is placed before the requiring feature in the
-     * result.
+     * Order the resources in list of features by their dependency chain.
+     * Each feature and its components are resolved. Then all the resources
+     * in the feature are ordered so that each resource is placed before
+     * the requiring feature/resources in the result.
      *
      * @param features
      *            The features to order.
-     * @return The ordered features.
+     * @return The ordered resources from the features.
      */
-    List<Feature> orderFeatures(List<Feature> features);
+    List<FeatureResource> orderResources(List<Feature> features);
 }

-- 
To stop receiving notification emails like this one, please contact
davidb@apache.org.