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-serviceusermapper.git


The following commit(s) were added to refs/heads/master by this push:
     new 2c55f10  SLING-10282 : Use singleton to register ServiceUserMapped 
services
2c55f10 is described below

commit 2c55f1095b2d4dc94ba983060ae53e0af411315e
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Fri Apr 9 07:07:03 2021 +0200

    SLING-10282 : Use singleton to register ServiceUserMapped services
---
 .../impl/ServiceUserMappedImpl.java                |  5 +++++
 .../impl/ServiceUserMapperImpl.java                |  4 ++--
 .../impl/ServiceUserMapperImplTest.java            | 22 +++++++++-------------
 3 files changed, 16 insertions(+), 15 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedImpl.java
 
b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedImpl.java
index 3a7d2d7..7061119 100644
--- 
a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedImpl.java
+++ 
b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedImpl.java
@@ -28,4 +28,9 @@ public class ServiceUserMappedImpl implements 
ServiceUserMapped {
 
     static String SERVICEUSERMAPPED = ServiceUserMapped.class.getName();
 
+    public static final ServiceUserMapped SINGLETON = new 
ServiceUserMappedImpl();
+
+    private ServiceUserMappedImpl() {
+        // private constructor
+    }
 }
diff --git 
a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
 
b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
index b980c7e..c8f3a8b 100644
--- 
a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
+++ 
b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
@@ -438,7 +438,7 @@ public class ServiceUserMapperImpl implements 
ServiceUserMapper {
             properties.put(Mapping.SERVICENAME, mapping.getServiceName());
             final ServiceRegistration serviceRegistration = 
bundleContext.registerService(
                     ServiceUserMappedImpl.SERVICEUSERMAPPED,
-                    new ServiceUserMappedImpl(), properties);
+                    ServiceUserMappedImpl.SINGLETON, properties);
 
             ServiceRegistration oldServiceRegistration = 
registration.setService(serviceRegistration);
             log.debug("Activated ServiceUserMapped {}", registration.mapping);
@@ -457,7 +457,7 @@ public class ServiceUserMapperImpl implements 
ServiceUserMapper {
             properties.put(Mapping.SERVICENAME, 
getServiceName(bundleContext.getBundle()));
             final ServiceRegistration serviceRegistration = bundleContext
                     .registerService(ServiceUserMappedImpl.SERVICEUSERMAPPED,
-                new ServiceUserMappedImpl(), properties);
+                ServiceUserMappedImpl.SINGLETON, properties);
             this.defaultRegistration.set(serviceRegistration);
         }
     }
diff --git 
a/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
 
b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
index 393a1e8..674058e 100644
--- 
a/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
+++ 
b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
@@ -28,11 +28,13 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ExecutorService;
@@ -620,7 +622,7 @@ public class ServiceUserMapperImplTest {
 
         final BundleContext bundleContext = mock(BundleContext.class);
         final Bundle bundle = mock(Bundle.class);
-        final Map<String, Map<Object, Dictionary>> registrations = new 
HashMap<>();
+        final Map<String, List<Map.Entry<Object, Dictionary<String, Object>>>> 
registrations = new HashMap<>();
 
         public ServiceRegistrationContextHelper() {
             when(bundleContext.registerService(any(String.class), 
any(Object.class), any(Dictionary.class)))
@@ -633,15 +635,11 @@ public class ServiceUserMapperImplTest {
             when(bundle.getSymbolicName()).thenReturn("mock");
         }
 
-        private ServiceRegistration registerService(String string, Object o, 
Dictionary dictionary) {
-            if (!registrations.containsKey(string)) {
-                registrations.put(string, new HashMap<>());
-            }
-            final Map<Object, Dictionary> serviceRegistrations = 
registrations.get(string);
-            serviceRegistrations.put(o, dictionary);
-
-            final Object registeredObject = o;
+        private ServiceRegistration registerService(String string, Object o, 
Dictionary<String, Object> dictionary) {
+            final List<Map.Entry<Object, Dictionary<String, Object>>> entries 
= registrations.computeIfAbsent(string, key -> new ArrayList<>());
+            final Map.Entry<Object, Dictionary<String, Object>> entry = 
Collections.singletonMap(o, dictionary).entrySet().iterator().next();
 
+            entries.add(entry);
 
             return new ServiceRegistration() {
                 @Override
@@ -656,19 +654,17 @@ public class ServiceUserMapperImplTest {
 
                 @Override
                 public void unregister() {
-                    serviceRegistrations.remove(registeredObject);
+                    entries.remove(entry);
                 }
             };
         }
 
-        public Map<Object, Dictionary> getRegistrations(String name) {
+        public List<Map.Entry<Object, Dictionary<String, Object>>> 
getRegistrations(String name) {
             return registrations.get(name);
         }
 
         public BundleContext getBundleContext() {
             return bundleContext;
         }
-
     }
-
 }

Reply via email to