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/19 08:28:14 UTC

[sling-whiteboard] branch master updated: [r2f] added a method to retrieve the (assembled) Feature used to launch the platform

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 10d50bd  [r2f] added a method to retrieve the (assembled) Feature used to launch the platform
10d50bd is described below

commit 10d50bd0a29c2b45f14fdd272be184693b1fd3f0
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Wed Jun 19 10:28:06 2019 +0200

    [r2f] added a method to retrieve the (assembled) Feature used to launch
    the platform
---
 runtime2feature/README.md                          | 19 ++++++++++++-
 .../r2f/RuntimeEnvironment2FeatureModel.java       |  5 +++-
 .../RuntimeEnvironment2FeatureModelService.java    | 31 +++++++++++++++++++++-
 3 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/runtime2feature/README.md b/runtime2feature/README.md
index 3c972d4..72de5b4 100644
--- a/runtime2feature/README.md
+++ b/runtime2feature/README.md
@@ -19,9 +19,26 @@ RuntimeEnvironment2FeatureModel generator;
 ConversionRequest conversionRequest = new DefaultConversionRequest()
                                       .setResultId("org.apache.sling:org.apache.sling.r2e:jar:RUNTIME:1.0.0")
                                       .setBundleContext(bundleContext);
-Feature runtimeFeature = generator.scanAndAssemble(conversionRequest)
+Feature runtimeFeature = generator.getRuntimeFeature(conversionRequest)
 ```
 
 ## Please Note
 
 Currently version will include in the generated Feature Model `bundles` and `configurations` only, which are the only informations that can be extracted from a `BundleContext` instance.
+
+## Launch Feature
+
+The `RuntimeEnvironment2FeatureModel` OSGi service is also able to retrieve the (assembled) Feature used to launch the platform:
+
+```
+import org.apache.sling.feature.r2f.*;
+
+@Activate
+BundleContext bundleContext;
+
+@Reference
+RuntimeEnvironment2FeatureModel generator;
+
+...
+Feature launchFeature = generator.getLaunchFeature(bundleContext)
+```
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 e5862a5..c236708 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
@@ -17,9 +17,12 @@
 package org.apache.sling.feature.r2f;
 
 import org.apache.sling.feature.Feature;
+import org.osgi.framework.BundleContext;
 
 public interface RuntimeEnvironment2FeatureModel {
 
-    Feature scanAndAssemble(ConversionRequest conversionRequest);
+    Feature getLaunchFeature(BundleContext bundleContext);
+
+    Feature getRuntimeFeature(ConversionRequest conversionRequest);
 
 }
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 077969e..e1f6356 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
@@ -16,8 +16,16 @@
  */
 package org.apache.sling.feature.r2f.impl;
 
+import static java.nio.file.Files.newBufferedReader;
 import static java.util.Objects.requireNonNull;
+import static org.apache.sling.feature.io.json.FeatureJSONReader.read;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.URI;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.stream.Stream;
 
 import org.apache.sling.feature.ArtifactId;
@@ -32,7 +40,28 @@ import org.osgi.service.cm.ConfigurationAdmin;
 
 public final class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironment2FeatureModel {
 
-    public Feature scanAndAssemble(ConversionRequest conversionRequest) {
+    private static final String SLING_FEATURE_PROPERTY_NAME = "sling.feature";
+
+    @Override
+    public Feature getLaunchFeature(BundleContext bundleContext) {
+        String launchFeatureLocation = bundleContext.getProperty(SLING_FEATURE_PROPERTY_NAME);
+
+        if (launchFeatureLocation == null) {
+            throw new IllegalStateException("Framework property 'sling.feature' is not set, impossible to assemble the launch Feature");
+        }
+
+        URI launchFeatureURI = URI.create(launchFeatureLocation);
+        Path launchFeaturePath = Paths.get(launchFeatureURI);
+
+        try (BufferedReader reader = newBufferedReader(launchFeaturePath)) {
+            return read(reader, launchFeatureLocation);
+        } catch (IOException cause) {
+            throw new UncheckedIOException(cause);
+        }
+    }
+
+    @Override
+    public Feature getRuntimeFeature(ConversionRequest conversionRequest) {
         ArtifactId resultId = requireNonNull(conversionRequest.getResultId(), "Impossible to create the Feature with a null id");
         BundleContext bundleContext = requireNonNull(conversionRequest.getBundleContext(), "Impossible to create the Feature from a null BundleContext");