You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2017/09/05 19:03:26 UTC

svn commit: r1807388 - in /felix/trunk/osgi-r7/configurator/src: main/java/org/apache/felix/configurator/impl/Configurator.java test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java

Author: cziegeler
Date: Tue Sep  5 19:03:26 2017
New Revision: 1807388

URL: http://svn.apache.org/viewvc?rev=1807388&view=rev
Log:
Fix NPE when config admin is started later

Modified:
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
    felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java

Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java?rev=1807388&r1=1807387&r2=1807388&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java (original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java Tue Sep  5 19:03:26 2017
@@ -529,22 +529,28 @@ public class Configurator {
         Long configAdminServiceBundleId = this.state.getConfigAdminBundleId(cfg.getBundleId());
         if ( configAdminServiceBundleId == null ) {
             final Bundle configBundle = cfg.getBundleId() == -1 ? this.bundleContext.getBundle() : this.bundleContext.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext().getBundle(cfg.getBundleId());
-            if ( configBundle != null ) {
+            if ( configBundle != null && configBundle.getState() == Bundle.ACTIVE ) {
                 if ( System.getSecurityManager() == null
                      || configBundle.hasPermission( new ServicePermission(ConfigurationAdmin.class.getName(), ServicePermission.GET)) ) {
                     try {
-                        final Collection<ServiceReference<ConfigurationAdmin>> refs = configBundle.getBundleContext().getServiceReferences(ConfigurationAdmin.class, null);
-                        final List<ServiceReference<ConfigurationAdmin>> sortedRefs = new ArrayList<>(refs);
-                        Collections.sort(sortedRefs);
-                        for(int i=sortedRefs.size();i>0;i--) {
-                            final ServiceReference<ConfigurationAdmin> r = sortedRefs.get(i-1);
-                            synchronized ( this.configAdminReferences ) {
-                                if ( this.configAdminReferences.contains(r) ) {
-                                    configAdminServiceBundleId = r.getBundle().getBundleId();
-                                    break;
+                        final BundleContext ctx = configBundle.getBundleContext();
+                        if ( ctx != null ) {
+                            final Collection<ServiceReference<ConfigurationAdmin>> refs = ctx.getServiceReferences(ConfigurationAdmin.class, null);
+                            final List<ServiceReference<ConfigurationAdmin>> sortedRefs = new ArrayList<>(refs);
+                            Collections.sort(sortedRefs);
+                            for(int i=sortedRefs.size();i>0;i--) {
+                                final ServiceReference<ConfigurationAdmin> r = sortedRefs.get(i-1);
+                                synchronized ( this.configAdminReferences ) {
+                                    if ( this.configAdminReferences.contains(r) ) {
+                                        configAdminServiceBundleId = r.getBundle().getBundleId();
+                                        break;
+                                    }
                                 }
                             }
                         }
+                    } catch ( final IllegalStateException e) {
+                        // this might happen if the config admin bundle gets deactivated while we use it
+                        // we can ignore this and retry later on
                     } catch (final InvalidSyntaxException e) {
                         // this can never happen as we pass {@code null} as the filter
                     }

Modified: felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java?rev=1807388&r1=1807387&r2=1807388&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java (original)
+++ felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/ConfiguratorTest.java Tue Sep  5 19:03:26 2017
@@ -61,6 +61,7 @@ public class ConfiguratorTest {
     @Before public void setup() throws IOException {
         bundle = mock(Bundle.class);
         when(bundle.getBundleId()).thenReturn(42L);
+        when(bundle.getState()).thenReturn(Bundle.ACTIVE);
         bundleContext = mock(BundleContext.class);
         when(bundle.getBundleContext()).thenReturn(bundleContext);
         when(bundleContext.getBundle()).thenReturn(bundle);
@@ -81,6 +82,7 @@ public class ConfiguratorTest {
         final Bundle b = mock(Bundle.class);
         when(b.getBundleId()).thenReturn(id);
         when(b.getLastModified()).thenReturn(5L);
+        when(b.getState()).thenReturn(Bundle.ACTIVE);
         final BundleWiring wiring = mock(BundleWiring.class);
         when(b.adapt(BundleWiring.class)).thenReturn(wiring);
         final BundleRequirement req = mock(BundleRequirement.class);