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/17 07:44:03 UTC

[sling-whiteboard] branch master updated: [r2f] added initial prototype (not working) implementation of the InventoryPrinter which serializes the basic & runtime

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 114c751  [r2f] added initial prototype (not working) implementation of the InventoryPrinter which serializes the basic & runtime
114c751 is described below

commit 114c75118afe8ac639a40482b2affd523cabf160
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Mon Jun 17 09:43:55 2019 +0200

    [r2f] added initial prototype (not working) implementation of the
    InventoryPrinter which serializes the basic & runtime
---
 runtime2feature/pom.xml                            | 14 +++++
 ...actRuntimeEnvironment2FeatureModelPrinter.java} | 25 ++++++---
 .../impl/BaseFeature2CurrentRuntimePrinter.java    | 60 +++++++++++++++++++++
 .../RuntimeEnvironment2FeatureModelActivator.java  | 61 ++++++++++++++++------
 .../RuntimeEnvironment2FeatureModelPrinter.java    | 42 ++-------------
 5 files changed, 141 insertions(+), 61 deletions(-)

diff --git a/runtime2feature/pom.xml b/runtime2feature/pom.xml
index 1b262a6..85563a6 100644
--- a/runtime2feature/pom.xml
+++ b/runtime2feature/pom.xml
@@ -39,6 +39,9 @@
   </properties>
 
   <dependencies>
+    <!--
+     | core OSGi APIs
+    -->
     <dependency>
       <groupId>org.osgi</groupId>
       <artifactId>osgi.core</artifactId>
@@ -50,6 +53,9 @@
       <version>5.0.0</version>
       <scope>provided</scope>
     </dependency>
+    <!--
+     | Apache Sling Feature APIs
+    -->
     <dependency>
       <groupId>org.apache.sling</groupId>
       <artifactId>org.apache.sling.feature</artifactId>
@@ -63,6 +69,14 @@
       <scope>provided</scope>
     </dependency>
     <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.feature.diff</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
+    <!--
+     | Apache Felix Inventory Printer
+    -->
+    <dependency>
       <groupId>org.apache.felix</groupId>
       <artifactId>org.apache.felix.inventory</artifactId>
       <version>1.0.6</version>
diff --git a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelPrinter.java b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/AbstractRuntimeEnvironment2FeatureModelPrinter.java
similarity index 72%
copy from runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelPrinter.java
copy to runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/AbstractRuntimeEnvironment2FeatureModelPrinter.java
index abf7ca9..cb7a1cb 100644
--- a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelPrinter.java
+++ b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/AbstractRuntimeEnvironment2FeatureModelPrinter.java
@@ -30,19 +30,24 @@ import org.apache.sling.feature.r2f.DefaultConversionRequest;
 import org.apache.sling.feature.r2f.RuntimeEnvironment2FeatureModel;
 import org.osgi.framework.BundleContext;
 
-public final class RuntimeEnvironment2FeatureModelPrinter implements InventoryPrinter {
+abstract class AbstractRuntimeEnvironment2FeatureModelPrinter implements InventoryPrinter {
 
-    private final RuntimeEnvironment2FeatureModel converter;
+    private final RuntimeEnvironment2FeatureModel generator;
 
     private final BundleContext bundleContext;
 
-    public RuntimeEnvironment2FeatureModelPrinter(RuntimeEnvironment2FeatureModel converter, BundleContext bundleContext) {
-        this.converter = converter;
+    public AbstractRuntimeEnvironment2FeatureModelPrinter(RuntimeEnvironment2FeatureModel generator,
+                                                          BundleContext bundleContext) {
+        this.generator = generator;
         this.bundleContext = bundleContext;
     }
 
+    protected final BundleContext getBundleContext() {
+        return bundleContext;
+    }
+
     @Override
-    public void print(PrintWriter printWriter, Format format, boolean isZip) {
+    public final void print(PrintWriter printWriter, Format format, boolean isZip) {
         // TODO
         String groupId = bundleContext.getProperty(null);
         String artifactId = bundleContext.getProperty(null);
@@ -52,17 +57,21 @@ public final class RuntimeEnvironment2FeatureModelPrinter implements InventoryPr
         ConversionRequest request = new DefaultConversionRequest()
                                     .setBundleContext(bundleContext)
                                     .setResultId(new ArtifactId(groupId, artifactId, version, classifier, null));
-        Feature currentFeature = converter.scanAndAssemble(request);
+        Feature currentFeature = generator.scanAndAssemble(request);
+
+        Feature computedFeature = compute(currentFeature);
 
         try {
-            write(printWriter, currentFeature);
+            write(printWriter, computedFeature);
         } catch (IOException e) {
             printWriter.append("An error occured while searlizing ")
-                       .append(currentFeature.toString())
+                       .append(computedFeature.toString())
                        .append(":\n");
 
             e.printStackTrace(printWriter);
         }
     }
 
+    protected abstract Feature compute(Feature currentFeature);
+
 }
diff --git a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/BaseFeature2CurrentRuntimePrinter.java b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/BaseFeature2CurrentRuntimePrinter.java
new file mode 100644
index 0000000..a083969
--- /dev/null
+++ b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/BaseFeature2CurrentRuntimePrinter.java
@@ -0,0 +1,60 @@
+/*
+ * 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.r2f.impl;
+
+import static org.apache.sling.feature.diff.FeatureDiff.compareFeatures;
+
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.diff.DefaultDiffRequest;
+import org.apache.sling.feature.r2f.RuntimeEnvironment2FeatureModel;
+import org.osgi.framework.BundleContext;
+
+public class BaseFeature2CurrentRuntimePrinter extends AbstractRuntimeEnvironment2FeatureModelPrinter {
+
+    public BaseFeature2CurrentRuntimePrinter(RuntimeEnvironment2FeatureModel generator, BundleContext bundleContext) {
+        super(generator, bundleContext);
+    }
+
+    @Override
+    protected Feature compute(Feature currentFeature) {
+        // TODO
+        Feature previousFeature = null;
+
+        StringBuilder classifier = new StringBuilder()
+                                   .append(previousFeature.getId().getVersion())
+                                   .append("-to-")
+                                   .append(currentFeature.getId().getVersion())
+                                   .append('-')
+                                   .append(currentFeature.getId().getClassifier())
+                                   .append("-upgrade");
+
+        Feature featureDiff = compareFeatures(new DefaultDiffRequest()
+                                              .setPrevious(previousFeature)
+                                              .setCurrent(currentFeature)
+                                              .addIncludeComparator("artifacts")
+                                              .addIncludeComparator("configurations")
+                                              .setResultId(new ArtifactId(currentFeature.getId().getGroupId(),
+                                                           currentFeature.getId().getArtifactId(), 
+                                                           currentFeature.getId().getVersion(),
+                                                           classifier.toString(),
+                                                           currentFeature.getId().getType())));
+
+        return featureDiff;
+    }
+
+}
diff --git a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelActivator.java b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelActivator.java
index 9128b54..6e26d76 100644
--- a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelActivator.java
+++ b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelActivator.java
@@ -16,14 +16,21 @@
  */
 package org.apache.sling.feature.r2f.impl;
 
+import static org.apache.felix.inventory.InventoryPrinter.NAME;
+import static org.apache.felix.inventory.InventoryPrinter.TITLE;
+import static org.osgi.framework.Constants.BUNDLE_VENDOR;
+import static org.osgi.framework.Constants.SERVICE_DESCRIPTION;
+import static org.osgi.framework.Constants.SERVICE_VENDOR;
+
 import java.util.Dictionary;
 import java.util.Hashtable;
+import java.util.LinkedList;
+import java.util.List;
 
 import org.apache.felix.inventory.InventoryPrinter;
 import org.apache.sling.feature.r2f.RuntimeEnvironment2FeatureModel;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 
 public final class RuntimeEnvironment2FeatureModelActivator implements BundleActivator {
@@ -32,30 +39,54 @@ public final class RuntimeEnvironment2FeatureModelActivator implements BundleAct
 
     private static final String SERVICE_NAME = "r2f";
 
-    private ServiceRegistration<RuntimeEnvironment2FeatureModel> converterRegistration;
+    private static final String RUNTIME_GENERATOR = " - Runtime Generator";
+
+    private static final String BASE_2_RUNTIME_DIFF_GENERATOR = " - Base 2 Runtime diff Generator";
 
-    private ServiceRegistration<InventoryPrinter> printerRegistration;
+    private final List<ServiceRegistration<?>> registrations = new LinkedList<>();
 
     @Override
-    public void start(BundleContext context) throws Exception {
-        final Dictionary<String, Object> properties = new Hashtable<>();
-        properties.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders(Constants.BUNDLE_VENDOR));
-        properties.put(Constants.SERVICE_DESCRIPTION, SERVICE_TITLE);
+    public void start(BundleContext bundleContext) throws Exception {
+        RuntimeEnvironment2FeatureModel generator = new RuntimeEnvironment2FeatureModelService();
+        registerService(bundleContext, RuntimeEnvironment2FeatureModel.class, generator, null);
 
-        RuntimeEnvironment2FeatureModel converter = new RuntimeEnvironment2FeatureModelService();
+        InventoryPrinter runtimePrinter = new RuntimeEnvironment2FeatureModelPrinter(generator, bundleContext);
+        registerService(bundleContext, InventoryPrinter.class, runtimePrinter, RUNTIME_GENERATOR);
 
-        properties.put(InventoryPrinter.NAME, SERVICE_NAME);
-        properties.put(InventoryPrinter.TITLE, SERVICE_TITLE);
-        InventoryPrinter printer = new RuntimeEnvironment2FeatureModelPrinter(converter, context);
+        InventoryPrinter base2RuntimePrinter = new BaseFeature2CurrentRuntimePrinter(generator, bundleContext);
+        registerService(bundleContext, InventoryPrinter.class, base2RuntimePrinter, BASE_2_RUNTIME_DIFF_GENERATOR);
+    }
 
-        converterRegistration = context.registerService(RuntimeEnvironment2FeatureModel.class, converter, properties);
-        printerRegistration = context.registerService(InventoryPrinter.class, printer, properties);
+    private <S> void registerService(BundleContext bundleContext, Class<S> type, S service, String classifier) {
+        Dictionary<String, Object> properties = new Hashtable<>();
+        properties.put(SERVICE_VENDOR, bundleContext.getBundle().getHeaders(BUNDLE_VENDOR));
+        putProperty(SERVICE_DESCRIPTION, SERVICE_TITLE, classifier, properties);
+        putProperty(SERVICE_DESCRIPTION, SERVICE_TITLE, classifier, properties);
+        putProperty(NAME, SERVICE_NAME, classifier, properties);
+        putProperty(TITLE, SERVICE_TITLE, classifier, properties);
+
+        registrations.add(bundleContext.registerService(type, service, properties));
+    }
+
+    private static void putProperty(String key, String value, String classifier, Dictionary<String, Object> properties) {
+        String finalValue;
+
+        if (classifier != null && !classifier.isEmpty()) {
+            finalValue = value.concat(classifier);
+        } else {
+            finalValue = value;
+        }
+
+        properties.put(key, finalValue);
     }
 
     @Override
     public void stop(BundleContext context) throws Exception {
-        converterRegistration.unregister();
-        printerRegistration.unregister();
+        for (ServiceRegistration<?> registration : registrations) {
+            registration.unregister();
+        }
+
+        registrations.clear();
     }
 
 }
diff --git a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelPrinter.java b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelPrinter.java
index abf7ca9..82eec3d 100644
--- a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelPrinter.java
+++ b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelPrinter.java
@@ -16,53 +16,19 @@
  */
 package org.apache.sling.feature.r2f.impl;
 
-import static org.apache.sling.feature.io.json.FeatureJSONWriter.write;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-
-import org.apache.felix.inventory.Format;
-import org.apache.felix.inventory.InventoryPrinter;
-import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
-import org.apache.sling.feature.r2f.ConversionRequest;
-import org.apache.sling.feature.r2f.DefaultConversionRequest;
 import org.apache.sling.feature.r2f.RuntimeEnvironment2FeatureModel;
 import org.osgi.framework.BundleContext;
 
-public final class RuntimeEnvironment2FeatureModelPrinter implements InventoryPrinter {
-
-    private final RuntimeEnvironment2FeatureModel converter;
-
-    private final BundleContext bundleContext;
+public final class RuntimeEnvironment2FeatureModelPrinter extends AbstractRuntimeEnvironment2FeatureModelPrinter {
 
     public RuntimeEnvironment2FeatureModelPrinter(RuntimeEnvironment2FeatureModel converter, BundleContext bundleContext) {
-        this.converter = converter;
-        this.bundleContext = bundleContext;
+        super(converter, bundleContext);
     }
 
     @Override
-    public void print(PrintWriter printWriter, Format format, boolean isZip) {
-        // TODO
-        String groupId = bundleContext.getProperty(null);
-        String artifactId = bundleContext.getProperty(null);
-        String version = bundleContext.getProperty(null);
-        String classifier = bundleContext.getProperty(null);
-
-        ConversionRequest request = new DefaultConversionRequest()
-                                    .setBundleContext(bundleContext)
-                                    .setResultId(new ArtifactId(groupId, artifactId, version, classifier, null));
-        Feature currentFeature = converter.scanAndAssemble(request);
-
-        try {
-            write(printWriter, currentFeature);
-        } catch (IOException e) {
-            printWriter.append("An error occured while searlizing ")
-                       .append(currentFeature.toString())
-                       .append(":\n");
-
-            e.printStackTrace(printWriter);
-        }
+    protected Feature compute(Feature currentFeature) {
+        return currentFeature;
     }
 
 }