You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/07/09 16:57:25 UTC

[sling-whiteboard] branch master updated: Feature Service configurable via configadmin

This is an automated email from the ASF dual-hosted git repository.

davidb 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 e131158  Feature Service configurable via configadmin
e131158 is described below

commit e13115842def0d424d831b7f331c8026d0863b21
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Mon Jul 9 17:56:56 2018 +0100

    Feature Service configurable via configadmin
---
 featuremodel/feature-service/pom.xml               |  9 +++
 .../sling/feature/service/impl/Activator.java      | 41 ++++++++++
 .../service/impl/FeaturesServiceManager.java       | 88 ++++++++++++++++++++++
 3 files changed, 138 insertions(+)

diff --git a/featuremodel/feature-service/pom.xml b/featuremodel/feature-service/pom.xml
index d18eeee..35fdcb4 100644
--- a/featuremodel/feature-service/pom.xml
+++ b/featuremodel/feature-service/pom.xml
@@ -41,6 +41,10 @@
                 <artifactId>maven-bundle-plugin</artifactId>
                 <extensions>true</extensions>
                 <configuration>
+                    <instructions>
+                        <Bundle-Activator>org.apache.sling.feature.service.impl.Activator</Bundle-Activator>
+                    </instructions>
+                
                     <!--  Skip baselining for 0.x version -->
                     <skip>true</skip>
                 </configuration>
@@ -73,6 +77,11 @@
             <artifactId>osgi.core</artifactId>
             <scope>provided</scope>
         </dependency>        
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.cmpn</artifactId>
+            <scope>provided</scope>
+        </dependency>        
 
         <!-- Testing -->
         <dependency>
diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/Activator.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/Activator.java
new file mode 100644
index 0000000..98c10b1
--- /dev/null
+++ b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/Activator.java
@@ -0,0 +1,41 @@
+/*
+ * 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.service.impl;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.ManagedService;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+public class Activator implements BundleActivator {
+    @Override
+    public void start(BundleContext context) throws Exception {
+        Dictionary<String, Object> props = new Hashtable<>();
+        props.put(Constants.SERVICE_PID, FeatureServiceImpl.class.getName());
+        context.registerService(ManagedService.class,
+                new FeaturesServiceManager(context), props);
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception {
+    }
+}
diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceManager.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceManager.java
new file mode 100644
index 0000000..4925bbb
--- /dev/null
+++ b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceManager.java
@@ -0,0 +1,88 @@
+/*
+ * 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.service.impl;
+
+import org.apache.sling.feature.service.Features;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
+
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+class FeaturesServiceManager implements ManagedService {
+    private final BundleContext bundleContext;
+    private ServiceRegistration<Features> reg;
+
+    FeaturesServiceManager(BundleContext context) {
+        bundleContext = context;
+    }
+
+    @Override
+    public synchronized void updated(Dictionary<String, ?> properties) throws ConfigurationException {
+        System.out.println("######******* Updated " + properties);
+        if (reg != null)
+            reg.unregister();
+
+        if (properties == null)
+            return;
+
+        Map<String, Long> bsnVerToID = getBundleToID();
+
+        Map<Long, String> bundleIDFeatures = new HashMap<>();
+        for(Enumeration<String> e = properties.keys(); e.hasMoreElements(); ) {
+            String key = e.nextElement();
+            Long bid = bsnVerToID.get(key);
+            if (bid != null) {
+                bundleIDFeatures.put(bid, getStringPlus(properties.get(key)));
+            }
+        }
+
+        FeatureServiceImpl fs = new FeatureServiceImpl(bundleIDFeatures);
+        reg = bundleContext.registerService(Features.class, fs, null);
+    }
+
+    private Map<String, Long> getBundleToID() {
+        Map<String, Long> m = new HashMap<>();
+
+        for (Bundle b : bundleContext.getBundles()) {
+            m.put(b.getSymbolicName() + ":" + b.getVersion(), b.getBundleId());
+        }
+
+        return m;
+    }
+
+    private String getStringPlus(Object obj) {
+        if (obj instanceof String) {
+            return (String) obj;
+        }
+        if (obj instanceof Collection) {
+            Iterator<?> it = ((Collection<?>) obj).iterator();
+            if (it.hasNext())
+                return it.next().toString();
+        }
+        return null;
+    }
+}