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 9a7ee78  Whitelisting service implementation
9a7ee78 is described below

commit 9a7ee78d59caedf666bd7ec7b851309b82273612
Author: David Bosschaert <[email protected]>
AuthorDate: Tue Jul 10 16:26:18 2018 +0100

    Whitelisting service implementation
---
 featuremodel/feature-whitelist/pom.xml             |  4 +++
 .../sling/feature/whitelist/impl/Activator.java    |  4 +--
 .../feature/whitelist/impl/WhitelistEnforcer.java  | 41 ++++++++++++----------
 .../whitelist/impl/WhitelistEnforcerTest.java      | 16 +++++----
 4 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/featuremodel/feature-whitelist/pom.xml 
b/featuremodel/feature-whitelist/pom.xml
index ef59d00..7840f27 100644
--- a/featuremodel/feature-whitelist/pom.xml
+++ b/featuremodel/feature-whitelist/pom.xml
@@ -41,6 +41,10 @@
                 <artifactId>maven-bundle-plugin</artifactId>
                 <extensions>true</extensions>
                 <configuration>
+                    <instructions>
+                        
<Bundle-Activator>org.apache.sling.feature.whitelist.impl.Activator</Bundle-Activator>
+                    </instructions>
+                
                     <!--  Skip baselining for 0.x version -->
                     <skip>true</skip>
                 </configuration>
diff --git 
a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/Activator.java
 
b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/Activator.java
index 27caf1e..0c882e6 100644
--- 
a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/Activator.java
+++ 
b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/Activator.java
@@ -39,12 +39,12 @@ public class Activator implements BundleActivator {
         tracker = new ServiceTracker<>(context, Features.class, null);
         tracker.open();
 
-        WhitelistEnforcer whitelistEnforcer = new WhitelistEnforcer(tracker);
+        WhitelistEnforcer enforcer = new WhitelistEnforcer(context, tracker);
         Dictionary<String, Object> resHookProps = new Hashtable<>();
         resHookProps.put(Constants.SERVICE_PID, 
WhitelistEnforcer.class.getName());
         resolverHookServiceRegistration = context.registerService(
                 new String[] {ManagedService.class.getName(), 
ResolverHookFactory.class.getName()},
-                whitelistEnforcer, resHookProps);
+                enforcer, resHookProps);
     }
 
     @Override
diff --git 
a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcer.java
 
b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcer.java
index 26b68c2..23f5c69 100644
--- 
a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcer.java
+++ 
b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcer.java
@@ -20,6 +20,8 @@ package org.apache.sling.feature.whitelist.impl;
 
 import org.apache.sling.feature.service.Features;
 import org.apache.sling.feature.whitelist.WhitelistService;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.hooks.resolver.ResolverHook;
 import org.osgi.framework.hooks.resolver.ResolverHookFactory;
 import org.osgi.framework.wiring.BundleRevision;
@@ -40,27 +42,40 @@ import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 
-class WhitelistEnforcer implements ResolverHookFactory, ManagedService {
+class WhitelistEnforcer implements ManagedService, ResolverHookFactory {
     private static final String CONFIG_REGION_MAPPING_PREFIX = 
"whitelist.region.";
     private static final String CONFIG_FEATURE_MAPPING_PREFIX = 
"whitelist.feature.";
     static final Logger LOG = LoggerFactory.getLogger(WhitelistEnforcer.class);
 
+    final BundleContext bundleContext;
     final ServiceTracker<Features, Features> featureServiceTracker;
-    volatile WhitelistService whitelistService = new NullWhitelistService();
+    volatile WhitelistService whitelistService = null;
+    volatile ServiceRegistration<WhitelistService> wlsRegistration = null;
 
-    WhitelistEnforcer(ServiceTracker<Features, Features> tracker) {
+    WhitelistEnforcer(BundleContext context, ServiceTracker<Features, 
Features> tracker) {
+        bundleContext = context;
         featureServiceTracker = tracker;
     }
 
     @Override
     public ResolverHook begin(Collection<BundleRevision> triggers) {
-        return new ResolverHookImpl(featureServiceTracker, whitelistService);
+        WhitelistService wls = whitelistService;
+        if (wls != null) {
+            return new ResolverHookImpl(featureServiceTracker, wls);
+        } else {
+            return null;
+        }
     }
 
     @Override
-    public void updated(Dictionary<String, ?> properties) throws 
ConfigurationException {
+    public synchronized void updated(Dictionary<String, ?> properties) throws 
ConfigurationException {
+        if (wlsRegistration != null) {
+            wlsRegistration.unregister();
+            wlsRegistration = null;
+        }
+
         if (properties == null) {
-            whitelistService = new NullWhitelistService();
+            whitelistService = null;
             return;
         }
 
@@ -82,6 +97,7 @@ class WhitelistEnforcer implements ResolverHookFactory, 
ManagedService {
         }
 
         whitelistService = new WhitelistServiceImpl(rpm, frm);
+        wlsRegistration = 
bundleContext.registerService(WhitelistService.class, whitelistService, null);
     }
 
     Set<String> getStringPlusValue(Object val) {
@@ -96,17 +112,4 @@ class WhitelistEnforcer implements ResolverHookFactory, 
ManagedService {
         }
         return Collections.singleton(val.toString());
     }
-
-    static class NullWhitelistService implements WhitelistService {
-        @Override
-        public Set<String> listRegions(String feature) {
-            return null;
-        }
-
-        @Override
-        public Boolean regionWhitelistsPackage(String region, String 
packageName) {
-            return null;
-        }
-    }
-
 }
diff --git 
a/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcerTest.java
 
b/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcerTest.java
index 6173511..b4d13e8 100644
--- 
a/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcerTest.java
+++ 
b/featuremodel/feature-whitelist/src/test/java/org/apache/sling/feature/whitelist/impl/WhitelistEnforcerTest.java
@@ -20,6 +20,8 @@ package org.apache.sling.feature.whitelist.impl;
 
 import org.apache.sling.feature.whitelist.WhitelistService;
 import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.BundleContext;
 import org.osgi.service.cm.ConfigurationException;
 
 import java.util.Arrays;
@@ -31,16 +33,17 @@ import java.util.Set;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 public class WhitelistEnforcerTest {
     @Test
     public void testWhitelistEnforcerConfigUpdate() throws 
ConfigurationException {
-        WhitelistEnforcer enf = new WhitelistEnforcer(null);
+        BundleContext bc = Mockito.mock(BundleContext.class);
+        WhitelistEnforcer enf = new WhitelistEnforcer(bc, null);
 
-        assertTrue("Precondition",
-                enf.whitelistService instanceof 
WhitelistEnforcer.NullWhitelistService);
+        assertNull("Precondition", enf.whitelistService);
 
         Dictionary<String, Object> props = new Hashtable<>();
         props.put("ignored", "ignored-too");
@@ -51,7 +54,9 @@ public class WhitelistEnforcerTest {
         props.put("whitelist.feature.gid:myfeature:1.0.0", new String [] 
{"region1", "region2"});
         enf.updated(props);
 
-        assertFalse(enf.whitelistService instanceof 
WhitelistEnforcer.NullWhitelistService);
+        assertNotNull(enf.whitelistService);
+        Mockito.verify(bc, Mockito.times(1)).registerService(
+                Mockito.eq(WhitelistService.class), 
Mockito.isA(WhitelistService.class), Mockito.any());
 
         // check that the configuration parsing worked
         assertTrue(enf.whitelistService.regionWhitelistsPackage("region1", 
"org.foo.pkg1"));
@@ -69,7 +74,6 @@ public class WhitelistEnforcerTest {
         assertNull(enf.whitelistService.listRegions("unknown"));
 
         enf.updated(null);
-        assertTrue("A null configuration should put back the null whitelist 
service",
-                enf.whitelistService instanceof 
WhitelistEnforcer.NullWhitelistService);
+        assertNull("A null configuration should put back the null whitelist 
service", enf.whitelistService);
     }
 }

Reply via email to