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 14:54:00 UTC

[sling-whiteboard] 05/06: [r2f] refactored implementation according to OSGi best practice

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 6a2a99a260ba8f6cd1f953f631f723f76cff943f
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Wed Jun 19 16:49:39 2019 +0200

    [r2f] refactored implementation according to OSGi best practice
---
 runtime2feature/pom.xml                            | 27 ++++++++---
 .../sling/feature/r2f/ConversionRequest.java       | 28 -----------
 .../feature/r2f/DefaultConversionRequest.java      | 55 ----------------------
 .../r2f/RuntimeEnvironment2FeatureModel.java       |  5 +-
 .../RuntimeEnvironment2FeatureModelActivator.java  | 51 --------------------
 .../RuntimeEnvironment2FeatureModelService.java    | 41 ++++++++++++----
 .../org/apache/sling/feature/r2f/package-info.java |  2 +
 7 files changed, 55 insertions(+), 154 deletions(-)

diff --git a/runtime2feature/pom.xml b/runtime2feature/pom.xml
index 8ea9c87..8b344df 100644
--- a/runtime2feature/pom.xml
+++ b/runtime2feature/pom.xml
@@ -29,7 +29,6 @@
 
   <artifactId>org.apache.sling.feature.r2f</artifactId>
   <version>0.0.1-SNAPSHOT</version>
-  <packaging>bundle</packaging>
 
   <name>Apache Sling Feature Model runtime creator</name>
   <description>Feature Model runtime creator tool for Apache Sling</description>
@@ -54,6 +53,16 @@
       <version>5.0.0</version>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.annotation.versioning</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.service.component.annotations</artifactId>
+      <scope>provided</scope>
+    </dependency>
     <!--
      | Apache Sling Feature APIs
     -->
@@ -88,15 +97,19 @@
   <build>
     <plugins>
       <plugin>
-        <groupId>org.apache.felix</groupId>
-        <artifactId>maven-bundle-plugin</artifactId>
-        <extensions>true</extensions>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
         <configuration>
-          <instructions>
-              <Bundle-Activator>org.apache.sling.feature.r2f.impl.RuntimeEnvironment2FeatureModelActivator</Bundle-Activator>
-          </instructions>
+          <archive>
+            <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
+          </archive>
         </configuration>
       </plugin>
+      <plugin>
+        <groupId>biz.aQute.bnd</groupId>
+        <artifactId>bnd-maven-plugin</artifactId>
+        <version>4.1.0</version>
+      </plugin>
     </plugins>
   </build>
 
diff --git a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/ConversionRequest.java b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/ConversionRequest.java
deleted file mode 100644
index f0fea30..0000000
--- a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/ConversionRequest.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * 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;
-
-import org.apache.sling.feature.ArtifactId;
-import org.osgi.framework.BundleContext;
-
-public interface ConversionRequest {
-
-    ArtifactId getResultId();
-
-    BundleContext getBundleContext();
-
-}
diff --git a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/DefaultConversionRequest.java b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/DefaultConversionRequest.java
deleted file mode 100644
index 467aba3..0000000
--- a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/DefaultConversionRequest.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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;
-
-import static java.util.Objects.requireNonNull;
-
-import org.apache.sling.feature.ArtifactId;
-import org.osgi.framework.BundleContext;
-
-public class DefaultConversionRequest implements ConversionRequest {
-
-    private ArtifactId resultId;
-
-    private BundleContext bundleContext;
-
-    @Override
-    public ArtifactId getResultId() {
-        return resultId;
-    }
-
-    public DefaultConversionRequest setResultId(String resultId) {
-        resultId = requireNonNull(resultId, "Impossible to create the Feature diff with a null id");
-        return setResultId(ArtifactId.parse(resultId));
-    }
-
-    public DefaultConversionRequest setResultId(ArtifactId resultId) {
-        this.resultId = requireNonNull(resultId, "Impossible to create the Feature diff with a null id");
-        return this;
-    }
-
-    @Override
-    public BundleContext getBundleContext() {
-        return bundleContext;
-    }
-
-    public DefaultConversionRequest setBundleContext(BundleContext bundleContext) {
-        this.bundleContext = requireNonNull(bundleContext, "Impossible to create the Feature from a null BundleContext");
-        return this;
-    }
-
-}
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 c236708..c034a04 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,12 +17,11 @@
 package org.apache.sling.feature.r2f;
 
 import org.apache.sling.feature.Feature;
-import org.osgi.framework.BundleContext;
 
 public interface RuntimeEnvironment2FeatureModel {
 
-    Feature getLaunchFeature(BundleContext bundleContext);
+    Feature getLaunchFeature();
 
-    Feature getRuntimeFeature(ConversionRequest conversionRequest);
+    Feature getRuntimeFeature();
 
 }
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
deleted file mode 100644
index c34a64b..0000000
--- a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelActivator.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.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 org.apache.sling.feature.r2f.RuntimeEnvironment2FeatureModel;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-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 ServiceRegistration<RuntimeEnvironment2FeatureModel> registration;
-
-    @Override
-    public void start(BundleContext bundleContext) throws Exception {
-        Dictionary<String, Object> properties = new Hashtable<>();
-        properties.put(SERVICE_VENDOR, bundleContext.getBundle().getHeaders(BUNDLE_VENDOR));
-        properties.put(SERVICE_DESCRIPTION, SERVICE_TITLE);
-
-        registration = bundleContext.registerService(RuntimeEnvironment2FeatureModel.class, new RuntimeEnvironment2FeatureModelService(), properties);
-    }
-
-    @Override
-    public void stop(BundleContext context) throws Exception {
-        registration.unregister();
-    }
-
-}
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 e1f6356..f727261 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,7 +17,6 @@
 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;
@@ -30,20 +29,29 @@ import java.util.stream.Stream;
 
 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.RuntimeEnvironment2FeatureModel;
 import org.osgi.framework.Bundle;
 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.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
 
-public final class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironment2FeatureModel {
+@Component(service = RuntimeEnvironment2FeatureModel.class)
+public class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironment2FeatureModel {
 
     private static final String SLING_FEATURE_PROPERTY_NAME = "sling.feature";
 
-    @Override
-    public Feature getLaunchFeature(BundleContext bundleContext) {
+    protected BundleContext bundleContext;
+
+    private Feature launchFeature;
+
+    @Activate
+    public void start(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+
         String launchFeatureLocation = bundleContext.getProperty(SLING_FEATURE_PROPERTY_NAME);
 
         if (launchFeatureLocation == null) {
@@ -54,18 +62,31 @@ public final class RuntimeEnvironment2FeatureModelService implements RuntimeEnvi
         Path launchFeaturePath = Paths.get(launchFeatureURI);
 
         try (BufferedReader reader = newBufferedReader(launchFeaturePath)) {
-            return read(reader, launchFeatureLocation);
+            launchFeature = read(reader, launchFeatureLocation);
         } catch (IOException cause) {
             throw new UncheckedIOException(cause);
         }
     }
 
+    @Deactivate
+    public void stop() {
+        bundleContext = null;
+        launchFeature = null;
+    }
+
+    @Override
+    public Feature getLaunchFeature() {
+        return launchFeature;
+    }
+
     @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");
+    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(resultId);
+        Feature targetFeature = new Feature(new ArtifactId(groupId, artifactId, version, classifier, null));
 
         // collect all bundles
 
diff --git a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/package-info.java b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/package-info.java
index 15549c4..151085c 100644
--- a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/package-info.java
+++ b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/package-info.java
@@ -14,4 +14,6 @@
  * License for the specific language governing permissions and limitations under
  * the License.
  */
+
+@org.osgi.annotation.versioning.Version("1.0.0")
 package org.apache.sling.feature.r2f;