You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by si...@apache.org on 2019/06/20 15:33:36 UTC

[sling-whiteboard] branch master updated: [r2f] added API which return the runtime feature assembling the prototype and the launch2running feature

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

simonetripodi 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 d806d8c  [r2f] added API which return the runtime feature assembling the prototype and the launch2running feature
d806d8c is described below

commit d806d8c950f3fdd42c62bbb41bb44a3b40954a5e
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Thu Jun 20 17:33:09 2019 +0200

    [r2f] added API which return the runtime feature assembling the
    prototype and the launch2running feature
---
 runtime2feature/README.md                          | 19 ++++++-
 .../r2f/RuntimeEnvironment2FeatureModel.java       |  6 +-
 .../RuntimeEnvironment2FeatureModelService.java    | 64 ++++++++++++++++++----
 3 files changed, 74 insertions(+), 15 deletions(-)

diff --git a/runtime2feature/README.md b/runtime2feature/README.md
index 75798c8..ad8dc43 100644
--- a/runtime2feature/README.md
+++ b/runtime2feature/README.md
@@ -2,7 +2,7 @@
 
 # Apache Sling OSGi runtime environment to Feature Model generator
 
-## Runtime Feature
+## Running Feature
 
 This is a simple OSGi service which is able to convert, given a `BundleContext` instance, a currently running OSGi container to an Apache Sling Feature Model definition.
 
@@ -15,7 +15,7 @@ import org.apache.sling.feature.r2f.*;
 RuntimeEnvironment2FeatureModel generator;
 
 ...
-Feature runtimeFeature = generator.getRuntimeFeature();
+Feature runtimeFeature = generator.getRunningFeature();
 ```
 
 ## Please Note
@@ -49,3 +49,18 @@ RuntimeEnvironment2FeatureModel generator;
 ...
 Feature launchFeature = generator.getLaunch2RuntimeUpgradingFeature();
 ```
+
+## The effective Runtime Feature
+
+Finally, the `RuntimeEnvironment2FeatureModel` OSGi service is also able to compute the real runtime Feature which is assembled from the Feature used to launch the platform and that targets the runtime Feature:
+
+```
+import org.apache.sling.feature.r2f.*;
+
+@Reference
+RuntimeEnvironment2FeatureModel generator;
+
+...
+Feature launchFeature = generator.getLaunch2RuntimeUpgradingFeature();
+```
+
diff --git a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/RuntimeEnvironment2FeatureModel.java b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/RuntimeEnvironment2FeatureModel.java
index f6ae696..9c52006 100644
--- a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/RuntimeEnvironment2FeatureModel.java
+++ b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/RuntimeEnvironment2FeatureModel.java
@@ -22,8 +22,10 @@ public interface RuntimeEnvironment2FeatureModel {
 
     Feature getLaunchFeature();
 
-    Feature getRuntimeFeature();
+    Feature getRunningFeature();
+
+    Feature getLaunch2RunningUpgradingFeature();
 
-    Feature getLaunch2RuntimeUpgradingFeature();
+    Feature getRuntimeFeature();
 
 }
diff --git a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelService.java b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelService.java
index 778a2c8..484aec1 100644
--- a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelService.java
+++ b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelService.java
@@ -17,6 +17,7 @@
 package org.apache.sling.feature.r2f.impl;
 
 import static java.nio.file.Files.newBufferedReader;
+import static org.apache.sling.feature.builder.FeatureBuilder.assemble;
 import static org.apache.sling.feature.diff.FeatureDiff.compareFeatures;
 import static org.apache.sling.feature.io.json.FeatureJSONReader.read;
 
@@ -30,6 +31,9 @@ import java.util.stream.Stream;
 
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.Prototype;
+import org.apache.sling.feature.builder.BuilderContext;
+import org.apache.sling.feature.builder.FeatureProvider;
 import org.apache.sling.feature.diff.DiffRequest;
 import org.apache.sling.feature.r2f.RuntimeEnvironment2FeatureModel;
 import org.osgi.framework.Bundle;
@@ -42,10 +46,16 @@ import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Deactivate;
 
 @Component(service = RuntimeEnvironment2FeatureModel.class)
-public class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironment2FeatureModel {
+public class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironment2FeatureModel, FeatureProvider {
 
     private static final String SLING_FEATURE_PROPERTY_NAME = "sling.feature";
 
+    private static final String RUNNING_CLASSIFIER = "running";
+
+    private static final String RUNTIME_CLASSIFIER = "runtime";
+
+    private static final String PACKAGING_FEATURE = "slingosgifeature";
+
     protected BundleContext bundleContext;
 
     private Feature launchFeature;
@@ -82,13 +92,8 @@ public class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironmen
     }
 
     @Override
-    public Feature getRuntimeFeature() {
-        String groupId = launchFeature.getId().getGroupId();
-        String artifactId = launchFeature.getId().getArtifactId();
-        String version = launchFeature.getId().getArtifactId();
-        String classifier = launchFeature.getId().getArtifactId() + "-RUNTIME";
-
-        Feature targetFeature = new Feature(new ArtifactId(groupId, artifactId, version, classifier, null));
+    public Feature getRunningFeature() {
+        Feature targetFeature = new Feature(newId(RUNNING_CLASSIFIER));
 
         // collect all bundles
 
@@ -124,16 +129,53 @@ public class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironmen
     }
 
     @Override
-    public Feature getLaunch2RuntimeUpgradingFeature() {
-        Feature runtimeFeature = getRuntimeFeature();
+    public Feature getLaunch2RunningUpgradingFeature() {
+        Feature runningFeature = getRunningFeature();
 
         // framework-properties can not be scanned in the BundleContext ATM
         // extensions can not be computed at runtime
         return compareFeatures(new DiffRequest()
                                .setPrevious(launchFeature)
-                               .setCurrent(runtimeFeature)
+                               .setCurrent(runningFeature)
                                .addIncludeComparator("bundles")
                                .addIncludeComparator("configurations"));
     }
 
+    @Override
+    public Feature getRuntimeFeature() {
+        Feature launch2RunningUpgradingFeature = getLaunch2RunningUpgradingFeature();
+        Prototype prototype = launch2RunningUpgradingFeature.getPrototype();
+
+        // if there are no differences, no need to assemble the new Feature, it is a vanilla Feature
+
+        if (launch2RunningUpgradingFeature.getBundles().isEmpty()
+                && launch2RunningUpgradingFeature.getConfigurations().isEmpty()
+                && prototype.getBundleRemovals().isEmpty()
+                && prototype.getConfigurationRemovals().isEmpty()) {
+            return launchFeature;
+        }
+
+        ArtifactId runtimeId = newId(RUNTIME_CLASSIFIER);
+
+        BuilderContext context = new BuilderContext(this);
+
+        return assemble(runtimeId, context, launch2RunningUpgradingFeature);
+    }
+
+    @Override
+    public Feature provide(ArtifactId id) {
+        if (launchFeature.getId().equals(id)) {
+            return launchFeature;
+        }
+        return null;
+    }
+
+    private ArtifactId newId(String classifier) {
+        String groupId = launchFeature.getId().getGroupId();
+        String artifactId = launchFeature.getId().getArtifactId();
+        String version = launchFeature.getId().getVersion();
+
+        return new ArtifactId(groupId, artifactId, version, classifier, PACKAGING_FEATURE);
+    }
+
 }