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/12 21:02:34 UTC

[sling-whiteboard] branch master updated: WhitelistServiceFactory takes place of configadmin based approach

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 b874c01  WhitelistServiceFactory takes place of configadmin based approach
b874c01 is described below

commit b874c01484a007ce1acae71d71b71a70862c4830
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Thu Jul 12 23:02:06 2018 +0200

    WhitelistServiceFactory takes place of configadmin based approach
---
 .../feature/whitelist/WhitelistServiceFactory.java | 26 +++++++++++
 .../sling/feature/whitelist/impl/Activator.java    | 11 ++---
 .../feature/whitelist/impl/WhitelistEnforcer.java  | 34 +++-----------
 .../impl/WhitelistServiceFactoryImpl.java          | 53 ++++++++++++++++++++++
 .../whitelist/impl/WhitelistEnforcerTest.java      | 21 +--------
 5 files changed, 93 insertions(+), 52 deletions(-)

diff --git a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/WhitelistServiceFactory.java b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/WhitelistServiceFactory.java
new file mode 100644
index 0000000..50f0193
--- /dev/null
+++ b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/WhitelistServiceFactory.java
@@ -0,0 +1,26 @@
+/*
+ * 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.whitelist;
+
+import java.util.Map;
+import java.util.Set;
+
+public interface WhitelistServiceFactory {
+    void initialize(Map<String, Map<String,Set<String>>> mappings);
+}
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 0c882e6..3767370 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
@@ -19,17 +19,12 @@
 package org.apache.sling.feature.whitelist.impl;
 
 import org.apache.sling.feature.service.Features;
+import org.apache.sling.feature.whitelist.WhitelistServiceFactory;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
-import org.osgi.framework.hooks.resolver.ResolverHookFactory;
-import org.osgi.service.cm.ManagedService;
 import org.osgi.util.tracker.ServiceTracker;
 
-import java.util.Dictionary;
-import java.util.Hashtable;
-
 public class Activator implements BundleActivator {
     private ServiceTracker<Features, Features> tracker;
     private ServiceRegistration<?> resolverHookServiceRegistration;
@@ -39,12 +34,16 @@ public class Activator implements BundleActivator {
         tracker = new ServiceTracker<>(context, Features.class, null);
         tracker.open();
 
+        WhitelistServiceFactory wsf = new WhitelistServiceFactoryImpl(context, tracker);
+        context.registerService(WhitelistServiceFactory.class, wsf, null);
+        /*
         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()},
                 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 23f5c69..3e5b550 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,53 +20,32 @@ 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;
-import org.osgi.service.cm.ConfigurationException;
-import org.osgi.service.cm.ManagedService;
 import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
-import java.util.Dictionary;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.stream.Collectors;
 
-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.";
+class WhitelistEnforcer implements ResolverHookFactory {
     static final Logger LOG = LoggerFactory.getLogger(WhitelistEnforcer.class);
 
-    final BundleContext bundleContext;
     final ServiceTracker<Features, Features> featureServiceTracker;
-    volatile WhitelistService whitelistService = null;
-    volatile ServiceRegistration<WhitelistService> wlsRegistration = null;
+    final WhitelistService whitelistService;
 
-    WhitelistEnforcer(BundleContext context, ServiceTracker<Features, Features> tracker) {
-        bundleContext = context;
+    WhitelistEnforcer(WhitelistService wls, ServiceTracker<Features, Features> tracker) {
+        whitelistService = wls;
         featureServiceTracker = tracker;
     }
 
     @Override
     public ResolverHook begin(Collection<BundleRevision> triggers) {
-        WhitelistService wls = whitelistService;
-        if (wls != null) {
-            return new ResolverHookImpl(featureServiceTracker, wls);
-        } else {
-            return null;
-        }
+        return new ResolverHookImpl(featureServiceTracker, whitelistService);
     }
 
+    /*
     @Override
     public synchronized void updated(Dictionary<String, ?> properties) throws ConfigurationException {
         if (wlsRegistration != null) {
@@ -112,4 +91,5 @@ class WhitelistEnforcer implements ManagedService, ResolverHookFactory {
         }
         return Collections.singleton(val.toString());
     }
+    */
 }
diff --git a/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceFactoryImpl.java b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceFactoryImpl.java
new file mode 100644
index 0000000..2d67428
--- /dev/null
+++ b/featuremodel/feature-whitelist/src/main/java/org/apache/sling/feature/whitelist/impl/WhitelistServiceFactoryImpl.java
@@ -0,0 +1,53 @@
+/*
+ * 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.whitelist.impl;
+
+import org.apache.sling.feature.service.Features;
+import org.apache.sling.feature.whitelist.WhitelistService;
+import org.apache.sling.feature.whitelist.WhitelistServiceFactory;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.hooks.resolver.ResolverHookFactory;
+import org.osgi.util.tracker.ServiceTracker;
+
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Set;
+
+public class WhitelistServiceFactoryImpl implements WhitelistServiceFactory {
+    private final BundleContext bundleContext;
+    private final ServiceTracker<Features, Features> featuresServiceTracker;
+
+    WhitelistServiceFactoryImpl(BundleContext context,
+            ServiceTracker<Features, Features> tracker) {
+        bundleContext = context;
+        featuresServiceTracker = tracker;
+    }
+
+    @Override
+    public void initialize(Map<String, Map<String, Set<String>>> mappings) {
+        Map<String, Set<String>> packages = mappings.get("packages");
+        Map<String, Set<String>> regions = mappings.get("regions");
+
+        WhitelistService wls = new WhitelistServiceImpl(packages, regions);
+        WhitelistEnforcer enforcer = new WhitelistEnforcer(wls, featuresServiceTracker);
+        Hashtable<String, Set<String>> props = new Hashtable<>(packages);
+        props.putAll(regions);
+        bundleContext.registerService(ResolverHookFactory.class, enforcer, props);
+    }
+}
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 b4d13e8..d008a9b 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
@@ -18,26 +18,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;
-import java.util.Collections;
-import java.util.Dictionary;
-import java.util.HashSet;
-import java.util.Hashtable;
-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 {
         BundleContext bc = Mockito.mock(BundleContext.class);
@@ -76,4 +58,5 @@ public class WhitelistEnforcerTest {
         enf.updated(null);
         assertNull("A null configuration should put back the null whitelist service", enf.whitelistService);
     }
+    */
 }