From 893ed0a9ba9a564c4f1dedb1ca4028a2ed3c7332 Mon Sep 17 00:00:00 2001
From: Robert Munteanu <rombert@apache.org>
Date: Fri, 14 Jul 2017 11:08:09 +0300
Subject: [PATCH] OAK-6450 - Stop relying on the service.pid property in
 SecurityProviderRegistration

Use the comoponent.name component property if the service.pid
is not available. The SecurityProviderRegistration property name is
unchanged, for backwards compatibility reasons.

The objectClass property may not be used as it points to the service
name(s) under which the component is registered. Using a custom property
name such as oak.security.name was considered but discarded as:

* all services would need to set this service property
* the component.name property is guaranteed to be added by the SCR
implementation, according to the OSGi Declarative Services spec 1.3 (
see OSGi Compendium R6, section 112.6 - Component Properties )
---
 .../internal/SecurityProviderRegistration.java     | 31 +++++++++++++---------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/internal/SecurityProviderRegistration.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/internal/SecurityProviderRegistration.java
index c21021bbcc..07a8e6f081 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/internal/SecurityProviderRegistration.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/internal/SecurityProviderRegistration.java
@@ -68,6 +68,7 @@ import org.slf4j.LoggerFactory;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Lists.newCopyOnWriteArrayList;
+import static org.osgi.framework.Constants.OBJECTCLASS;
 
 @Component(
         immediate = true,
@@ -78,11 +79,13 @@ import static com.google.common.collect.Lists.newCopyOnWriteArrayList;
 @Properties({
         @Property(
                 name = "requiredServicePids",
-                label = "Required Service PIDs",
+                label = "Required Services",
                 description = "The SecurityProvider will not register itself " +
-                        "unless the services identified by these PIDs are " +
-                        "registered first. Only the PIDs of implementations of " +
-                        "the following interfaces are checked: " +
+                        "unless the services identified by the following service pids " +
+                        "or component names are registered first. The class name is " +
+                        "identified by checking the service.pid property. If that property " +
+                        "does not exist, the component.name property is used as a fallback." +
+                        "Only implementations of the following interfaces are checked :" +
                         "AuthorizationConfiguration, PrincipalConfiguration, " +
                         "TokenConfiguration, AuthorizableActionProvider, " +
                         "RestrictionProvider and UserAuthenticationFactory.",
@@ -567,27 +570,31 @@ public class SecurityProviderRegistration {
     }
 
     private void addCandidate(Map<String, Object> properties) {
-        String pid = getServicePid(properties);
+        String pidOrName = getServicePidOrComponentName(properties);
 
-        if (pid == null) {
+        if (pidOrName == null) {
             return;
         }
 
-        preconditions.addCandidate(pid);
+        preconditions.addCandidate(pidOrName);
     }
 
     private void removeCandidate(Map<String, Object> properties) {
-        String pid = getServicePid(properties);
+        String pidOrName = getServicePidOrComponentName(properties);
 
-        if (pid == null) {
+        if (pidOrName == null) {
             return;
         }
 
-        preconditions.removeCandidate(pid);
+        preconditions.removeCandidate(pidOrName);
     }
 
-    private static String getServicePid(Map<String, Object> properties) {
-        return PropertiesUtil.toString(properties.get(Constants.SERVICE_PID), null);
+    private static String getServicePidOrComponentName(Map<String, Object> properties) {
+        String servicePid = PropertiesUtil.toString(properties.get(Constants.SERVICE_PID), null);
+        if ( servicePid != null ) {
+            return servicePid;
+        }
+        return PropertiesUtil.toString(properties.get("component.name"), null);
     }
 
     private static String[] getRequiredServicePids(Map<String, Object> configuration) {
-- 
2.13.2

