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 00:01:40 UTC

[sling-whiteboard] 02/02: [r2f] added runtime feature generatror InventoryPrinter implementation, DS replaced by a simple bundle activator

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

commit 5a4feef605da560ce3ed165a1da94b18cc2086cb
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Mon Jun 17 02:01:31 2019 +0200

    [r2f] added runtime feature generatror InventoryPrinter implementation,
    DS replaced by a simple bundle activator
---
 runtime2feature/pom.xml                            | 26 ++++++---
 .../RuntimeEnvironment2FeatureModelActivator.java  | 61 +++++++++++++++++++
 .../RuntimeEnvironment2FeatureModelPrinter.java    | 68 ++++++++++++++++++++++
 .../RuntimeEnvironment2FeatureModelService.java    |  2 -
 4 files changed, 148 insertions(+), 9 deletions(-)

diff --git a/runtime2feature/pom.xml b/runtime2feature/pom.xml
index 92ccc31..1b262a6 100644
--- a/runtime2feature/pom.xml
+++ b/runtime2feature/pom.xml
@@ -45,12 +45,6 @@
       <scope>provided</scope>
     </dependency>
     <dependency>
-      <groupId>org.apache.felix</groupId>
-      <artifactId>org.apache.felix.scr.ds-annotations</artifactId>
-      <version>1.2.10</version>
-      <scope>provided</scope>
-    </dependency>
-    <dependency>
       <groupId>org.osgi</groupId>
       <artifactId>org.osgi.compendium</artifactId>
       <version>5.0.0</version>
@@ -62,6 +56,18 @@
       <version>1.0.2</version>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.feature.io</artifactId>
+      <version>1.0.2</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.inventory</artifactId>
+      <version>1.0.6</version>
+      <scope>provided</scope>
+    </dependency>
 
     <!--
      | Test only dependencies
@@ -77,7 +83,13 @@
     <plugins>
       <plugin>
         <groupId>org.apache.felix</groupId>
-        <artifactId>maven-scr-plugin</artifactId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+              <Bundle-Activator>org.apache.sling.feature.r2f.impl.RuntimeEnvironment2FeatureModelActivator</Bundle-Activator>
+          </instructions>
+        </configuration>
       </plugin>
     </plugins>
   </build>
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
new file mode 100644
index 0000000..9128b54
--- /dev/null
+++ b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelActivator.java
@@ -0,0 +1,61 @@
+/*
+ * 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 java.util.Dictionary;
+import java.util.Hashtable;
+
+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 {
+
+    private static final String SERVICE_TITLE = "Apache Sling Runtime Environment to Feature Model converter";
+
+    private static final String SERVICE_NAME = "r2f";
+
+    private ServiceRegistration<RuntimeEnvironment2FeatureModel> converterRegistration;
+
+    private ServiceRegistration<InventoryPrinter> printerRegistration;
+
+    @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);
+
+        RuntimeEnvironment2FeatureModel converter = new RuntimeEnvironment2FeatureModelService();
+
+        properties.put(InventoryPrinter.NAME, SERVICE_NAME);
+        properties.put(InventoryPrinter.TITLE, SERVICE_TITLE);
+        InventoryPrinter printer = new RuntimeEnvironment2FeatureModelPrinter(converter, context);
+
+        converterRegistration = context.registerService(RuntimeEnvironment2FeatureModel.class, converter, properties);
+        printerRegistration = context.registerService(InventoryPrinter.class, printer, properties);
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception {
+        converterRegistration.unregister();
+        printerRegistration.unregister();
+    }
+
+}
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
new file mode 100644
index 0000000..abf7ca9
--- /dev/null
+++ b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelPrinter.java
@@ -0,0 +1,68 @@
+/*
+ * 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.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 RuntimeEnvironment2FeatureModelPrinter(RuntimeEnvironment2FeatureModel converter, BundleContext bundleContext) {
+        this.converter = converter;
+        this.bundleContext = 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);
+        }
+    }
+
+}
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 569b339..077969e 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
@@ -29,9 +29,7 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
-import org.osgi.service.component.annotations.Component;
 
-@Component
 public final class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironment2FeatureModel {
 
     public Feature scanAndAssemble(ConversionRequest conversionRequest) {