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/07/15 21:53:06 UTC

[sling-org-apache-sling-feature-r2f] branch master updated: ConfigurationAdmin reference managed via OSGi declarative services, avoid to manually look for it in the BundleContext

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-org-apache-sling-feature-r2f.git


The following commit(s) were added to refs/heads/master by this push:
     new 620a7e7  ConfigurationAdmin reference managed via OSGi declarative services, avoid to manually look for it in the BundleContext
620a7e7 is described below

commit 620a7e7466ba8de333cb4372c6a4dc01b9130585
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Mon Jul 15 23:52:59 2019 +0200

    ConfigurationAdmin reference managed via OSGi declarative services,
    avoid to manually look for it in the BundleContext
---
 pom.xml                                            |  8 +++---
 .../RuntimeEnvironment2FeatureModelService.java    | 32 ++++++++++------------
 2 files changed, 18 insertions(+), 22 deletions(-)

diff --git a/pom.xml b/pom.xml
index c754675..d6ad852 100644
--- a/pom.xml
+++ b/pom.xml
@@ -55,18 +55,18 @@
     </dependency>
     <dependency>
       <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.compendium</artifactId>
-      <version>5.0.0</version>
+      <artifactId>org.osgi.annotation.versioning</artifactId>
       <scope>provided</scope>
     </dependency>
     <dependency>
       <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.annotation.versioning</artifactId>
+      <artifactId>org.osgi.service.component.annotations</artifactId>
       <scope>provided</scope>
     </dependency>
     <dependency>
       <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.service.component.annotations</artifactId>
+      <artifactId>org.osgi.compendium</artifactId>
+      <version>5.0.0</version>
       <scope>provided</scope>
     </dependency>
 
diff --git a/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelService.java b/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelService.java
index 04b661c..61decf8 100644
--- a/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelService.java
+++ b/src/main/java/org/apache/sling/feature/r2f/impl/RuntimeEnvironment2FeatureModelService.java
@@ -45,13 +45,13 @@ import org.apache.sling.feature.diff.DiffRequest;
 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.framework.Version;
 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;
+import org.osgi.service.component.annotations.Reference;
 
 @Component(service = RuntimeEnvironment2FeatureModel.class)
 public class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironment2FeatureModel, FeatureProvider {
@@ -76,9 +76,12 @@ public class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironmen
 
     private static final String PACKAGING_FEATURE = "slingosgifeature";
 
+    private final Map<Entry<String, Version>, ArtifactId> bvm = new HashMap<>();
+
     protected BundleContext bundleContext;
 
-    private final Map<Entry<String, Version>, ArtifactId> bvm = new HashMap<>();
+    @Reference
+    protected ConfigurationAdmin configurationAdmin;
 
     private Feature launchFeature;
 
@@ -188,23 +191,16 @@ public class RuntimeEnvironment2FeatureModelService implements RuntimeEnvironmen
 
         // collect all configurations
 
-        ServiceReference<ConfigurationAdmin> configurationAdminReference = bundleContext.getServiceReference(ConfigurationAdmin.class);
-        if (configurationAdminReference != null) {
-            ConfigurationAdmin configurationAdmin = bundleContext.getService(configurationAdminReference);
-
-            if (configurationAdmin != null) {
-                try {
-                    Configuration[] configurations = configurationAdmin.listConfigurations(null);
-                    if (configurations != null) {
-                        OSGiConfiguration2FeatureConfigurationMapper mapper = new OSGiConfiguration2FeatureConfigurationMapper(targetFeature);
-
-                        Stream.of(configurations).map(mapper).forEach(mapper);
-                    }
-                } catch (Exception e) {
-                    // that should not happen
-                    throw new RuntimeException("Something went wrong while iterating over all available Configurations", e);
-                }
+        try {
+            Configuration[] configurations = configurationAdmin.listConfigurations(null);
+            if (configurations != null && configurations.length > 0) {
+                OSGiConfiguration2FeatureConfigurationMapper mapper = new OSGiConfiguration2FeatureConfigurationMapper(targetFeature);
+
+                Stream.of(configurations).map(mapper).forEach(mapper);
             }
+        } catch (Exception e) {
+            // that should not happen
+            throw new RuntimeException("Something went wrong while iterating over all available Configurations", e);
         }
 
         return targetFeature;