You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2019/08/14 08:45:37 UTC

[sling-org-apache-sling-feature-launcher] branch master updated: SLING-8629 : ConfigurationAdmin does not get registered without configurations

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

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-launcher.git


The following commit(s) were added to refs/heads/master by this push:
     new e9e01ee  SLING-8629 : ConfigurationAdmin does not get registered without configurations
e9e01ee is described below

commit e9e01eed05a97818f8ffe8b05f8d444b8e69e8f1
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Aug 14 10:45:29 2019 +0200

    SLING-8629 : ConfigurationAdmin does not get registered without configurations
---
 .../launcher/impl/launchers/AbstractRunner.java    | 162 ++++++++++-----------
 1 file changed, 79 insertions(+), 83 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java b/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java
index d3c35b8..d5f952b 100644
--- a/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java
+++ b/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java
@@ -88,94 +88,53 @@ public abstract class AbstractRunner implements Callable<Integer> {
     }
 
     protected void setupFramework(final Framework framework, final Map<Integer, List<URL>> bundlesMap) throws BundleException {
-        if ( !configurations.isEmpty() ) {
-            // check for Apache Felix CM persistence manager config
-            final String pm = framework.getBundleContext().getProperty(CM_CONFIG_PM);
-            if (PM_FEATURE_LAUNCHER.equals(pm)) {
-                logger.info("Using feature launcher configuration admin persistence manager");
-                try {
-                    // we start a tracker for the memory PM
-                    this.configAdminTracker = new ServiceTracker<>(framework.getBundleContext(),
-                            framework.getBundleContext().createFilter(PM_MEMORY_FILTER),
-
-                            new ServiceTrackerCustomizer<Object, Object>() {
-                                private volatile ServiceRegistration<?> reg;
-
-                                @Override
-                                public Object addingService(final ServiceReference<Object> reference) {
-                                    // get memory pm
-                                    final Object memoryPM = framework.getBundleContext().getService(reference);
-                                    if (memoryPM != null) {
-                                        try {
-                                            // we re use the memory PM (it is not used anyway)
-                                            // and simply store the configs there using reflection
-                                            final Method storeMethod = memoryPM.getClass().getDeclaredMethod("store",
-                                                    String.class, Dictionary.class);
-                                            for (final Object[] obj : configurations) {
-                                                @SuppressWarnings("unchecked")
-                                                final Dictionary<String, Object> props = (Dictionary<String, Object>) obj[2];
-                                                final String pid;
-                                                if (obj[1] != null) {
-                                                    final String factoryPid = (String) obj[1];
-                                                    pid = factoryPid.concat("~").concat((String) obj[0]);
-                                                    props.put("service.factoryPid", factoryPid);
-                                                } else {
-                                                    pid = (String) obj[0];
-                                            }
-                                                props.put(Constants.SERVICE_PID, pid);
-                                                storeMethod.invoke(memoryPM, pid, props);
-                                            }
-                                            // register feature launcher pm
-                                            final Dictionary<String, Object> properties = new Hashtable<>();
-                                            properties.put("name", PM_FEATURE_LAUNCHER);
-                                            reg = reference.getBundle().getBundleContext().registerService(
-                                                    "org.apache.felix.cm.PersistenceManager", memoryPM, properties);
-                                        } catch (IllegalAccessException | IllegalArgumentException
-                                                | InvocationTargetException | NoSuchMethodException
-                                                | SecurityException e) {
-                                            throw new RuntimeException(e);
-                                        }
-                                    }
-                                    return memoryPM;
-                                }
-
-                                @Override
-                                public void modifiedService(ServiceReference<Object> reference, Object service) {
-                                    // nothing to do
-                                }
-
-                                @Override
-                                public void removedService(ServiceReference<Object> reference, Object service) {
-                                    if (reg != null) {
-                                        reg.unregister();
-                                        reg = null;
-                                }
-                                    reference.getBundle().getBundleContext().ungetService(reference);
-                                }
-                            });
-                } catch (final InvalidSyntaxException e) {
-                    // the filter is constant so this should really not happen
-                    throw new RuntimeException(e);
-                }
-                this.configAdminTracker.open();
-
-            } else {
+        // check for Apache Felix CM persistence manager config
+        final String pm = framework.getBundleContext().getProperty(CM_CONFIG_PM);
+        if (PM_FEATURE_LAUNCHER.equals(pm)) {
+            logger.info("Using feature launcher configuration admin persistence manager");
+            try {
+                // we start a tracker for the memory PM
                 this.configAdminTracker = new ServiceTracker<>(framework.getBundleContext(),
-                        "org.osgi.service.cm.ConfigurationAdmin",
+                        framework.getBundleContext().createFilter(PM_MEMORY_FILTER),
+
                         new ServiceTrackerCustomizer<Object, Object>() {
+                            private volatile ServiceRegistration<?> reg;
 
                             @Override
                             public Object addingService(final ServiceReference<Object> reference) {
-                                // get config admin
-                                final Object cm = framework.getBundleContext().getService(reference);
-                                if ( cm != null ) {
+                                // get memory pm
+                                final Object memoryPM = framework.getBundleContext().getService(reference);
+                                if (memoryPM != null) {
                                     try {
-                                        configure(cm);
-                                    } finally {
-                                        framework.getBundleContext().ungetService(reference);
+                                        // we re use the memory PM (it is not used anyway)
+                                        // and simply store the configs there using reflection
+                                        final Method storeMethod = memoryPM.getClass().getDeclaredMethod("store",
+                                                String.class, Dictionary.class);
+                                        for (final Object[] obj : configurations) {
+                                            @SuppressWarnings("unchecked")
+                                            final Dictionary<String, Object> props = (Dictionary<String, Object>) obj[2];
+                                            final String pid;
+                                            if (obj[1] != null) {
+                                                final String factoryPid = (String) obj[1];
+                                                pid = factoryPid.concat("~").concat((String) obj[0]);
+                                                props.put("service.factoryPid", factoryPid);
+                                            } else {
+                                                pid = (String) obj[0];
+                                            }
+                                            props.put(Constants.SERVICE_PID, pid);
+                                            storeMethod.invoke(memoryPM, pid, props);
+                                        }
+                                        // register feature launcher pm
+                                        final Dictionary<String, Object> properties = new Hashtable<>();
+                                        properties.put("name", PM_FEATURE_LAUNCHER);
+                                        reg = reference.getBundle().getBundleContext().registerService(
+                                                "org.apache.felix.cm.PersistenceManager", memoryPM, properties);
+                                    } catch (IllegalAccessException | IllegalArgumentException
+                                            | InvocationTargetException | NoSuchMethodException | SecurityException e) {
+                                        throw new RuntimeException(e);
                                     }
                                 }
-                                return null;
+                                return memoryPM;
                             }
 
                             @Override
@@ -185,11 +144,48 @@ public abstract class AbstractRunner implements Callable<Integer> {
 
                             @Override
                             public void removedService(ServiceReference<Object> reference, Object service) {
-                                // nothing to do
+                                if (reg != null) {
+                                    reg.unregister();
+                                    reg = null;
+                                }
+                                reference.getBundle().getBundleContext().ungetService(reference);
                             }
-                });
-                this.configAdminTracker.open();
+                        });
+            } catch (final InvalidSyntaxException e) {
+                // the filter is constant so this should really not happen
+                throw new RuntimeException(e);
             }
+            this.configAdminTracker.open();
+        } else if (!configurations.isEmpty()) {
+
+            this.configAdminTracker = new ServiceTracker<>(framework.getBundleContext(),
+                    "org.osgi.service.cm.ConfigurationAdmin", new ServiceTrackerCustomizer<Object, Object>() {
+
+                        @Override
+                        public Object addingService(final ServiceReference<Object> reference) {
+                            // get config admin
+                            final Object cm = framework.getBundleContext().getService(reference);
+                            if (cm != null) {
+                                try {
+                                    configure(cm);
+                                } finally {
+                                    framework.getBundleContext().ungetService(reference);
+                                }
+                            }
+                            return null;
+                        }
+
+                        @Override
+                        public void modifiedService(ServiceReference<Object> reference, Object service) {
+                            // nothing to do
+                        }
+
+                        @Override
+                        public void removedService(ServiceReference<Object> reference, Object service) {
+                            // nothing to do
+                        }
+                    });
+            this.configAdminTracker.open();
         }
         if ( !installables.isEmpty() ) {
             this.installerTracker = new ServiceTracker<>(framework.getBundleContext(),