You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:13:28 UTC

[sling-org-apache-sling-serviceusermapper] 03/12: Switch back to using a regular ServiceUserName service tacking the service bundle as an argument.

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.serviceusermapper-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-serviceusermapper.git

commit 7d98561ec3029e18f29c631321194c0d3a2bb6fb
Author: Felix Meschberger <fm...@apache.org>
AuthorDate: Thu Mar 21 14:48:49 2013 +0000

    Switch back to using a regular ServiceUserName service tacking the service bundle as an argument.
    
    Reason: It makes no sense to impose on the use of the ServiceUserName serive on the using bundle. Rather the use of this service is a property of the ResourceResolverFactory and the SlingRepository service which want to derive the actual service name from its using bundle.
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper@1459341 13f79535-47bb-0310-9956-ffa450edef68
---
 .../serviceusermapping/ServiceUserMapper.java      |  39 +++++-
 .../impl/ServiceUserMapperController.java          | 142 ---------------------
 .../impl/ServiceUserMapperImpl.java                | 106 +++++++++++++--
 3 files changed, 128 insertions(+), 159 deletions(-)

diff --git a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
index 36e9181..e95d96d 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
@@ -18,27 +18,61 @@
  */
 package org.apache.sling.serviceusermapping;
 
+import org.osgi.framework.Bundle;
+
 import aQute.bnd.annotation.ProviderType;
 
+/**
+ * The <code>ServiceUserMapper</code> service can be used to map a service
+ * provided by a bundle to the name of a user account used to access the
+ * ResourceResolver used by the service to access its data.
+ * <p>
+ * The goal of this service is to allow services to be implemented accessing the
+ * storage with service-specific accounts which are tailored to allow the
+ * service appropriate access without requiring administrative level access to
+ * the storage.
+ * <p>
+ * In addition to allowing to phase out the use of
+ * {@code ResourceResolver.getAdministrativeResourceResolver} and
+ * {@code SlingRepository.loginAdministrative} it also allows to better account
+ * for changes to the storage by the different services.
+ * <p>
+ * This service is not intended to be used by the general user but by
+ * implementations of the {@code ResourceResolverFactory} and
+ * {@code SlingRepository} services.
+ * <p>
+ * This service is not intended to be implemented by clients.
+ */
 @ProviderType
 public interface ServiceUserMapper {
 
     /**
+     * The name of the Bundle manifest header providing the name of the service
+     * provided by the bundle. If this header is missing or empty, the bundle's
+     * symbolic name is used instead to name the service.
+     */
+    String BUNDLE_HEADER_SERVICE_NAME = "Sling-Service";
+
+    /**
      * Returns the name of the service represented by the {@code bundle} and the
      * {@code serviceInfo}.
      *
+     * @param bundle The bundle implementing the service request access to
+     *            resources.
      * @param serviceInfo Additional information about the concrete service
      *            requesting access. This parameter is optional and may be
      *            {@code null}.
      * @return The name of the service represented by the bundle along with the
      *         additional service information.
      */
-    String getServiceName(String serviceInfo);
+    String getServiceName(Bundle bundle, String serviceInfo);
 
     /**
      * Returns the name of a user to the be used to access the Sling Resource
      * tree or the JCR Repository.
      *
+     * @param bundle The bundle implementing the service request access to
+     *            resources.
      * @param serviceInfo Additional information about the concrete service
      *            requesting access. This parameter is optional and may be
      *            {@code null}.
@@ -46,6 +80,5 @@ public interface ServiceUserMapper {
      *         for the service. This may be {@code null} to only grant guest
      *         level (or anonymous level) access to the resources.
      */
-    String getUserForService(String serviceInfo);
-
+    String getUserForService(Bundle bundle, String serviceInfo);
 }
diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java
deleted file mode 100644
index 682c43d..0000000
--- a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * 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.serviceusermapping.impl;
-
-import java.util.ArrayList;
-import java.util.Map;
-
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.ConfigurationPolicy;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Modified;
-import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.PropertyUnbounded;
-import org.apache.sling.commons.osgi.PropertiesUtil;
-import org.apache.sling.serviceusermapping.ServiceUserMapper;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.ServiceFactory;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.component.ComponentContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-@Component(metatype = true, ds = true, policy = ConfigurationPolicy.REQUIRE)
-public class ServiceUserMapperController {
-
-    @Property(
-            label = "Service Mappings",
-            description = "Provides mappings from service name to user names. "
-                + "Each entry is of the form 'serviceName [ \":\" serviceInfo ] \"=\" userName' "
-                + "where serviceName and serviceInfo identify the service and userName would "
-                + "defines the name of the user to provide to the service. Invalid entries are logged and ignored.",
-            unbounded = PropertyUnbounded.ARRAY)
-    private static final String PROP_SERVICE2USER_MAPPING = "user.mapping";
-
-    private static final String[] PROP_SERVICE2USER_MAPPING_DEFAULT = {};
-
-    private static final String PROP_DEFAULT_USER = "user.default";
-
-    @Property(
-            name = PROP_DEFAULT_USER,
-            label = "Default User",
-            description = "The name of the user to use as the default if no service mapping"
-                + "applies. If this property is missing or empty the default user name reflects "
-                + "an anonymous user.")
-    private static final String PROP_DEFAULT_USER_DEFAULT = "";
-
-    /** default log */
-    private final Logger log = LoggerFactory.getLogger(getClass());
-
-    private Mapping[] serviceUserMappings;
-
-    private String defaultUser;
-
-    private ServiceRegistration serviceUserMapper;
-
-    @Activate
-    private void activate(ComponentContext ctx, final Map<String, Object> config) {
-        this.configure(config);
-        this.serviceUserMapper = ctx.getBundleContext().registerService(ServiceUserMapper.class.getName(),
-            new ServiceFactory() {
-
-                public Object getService(Bundle bundle, ServiceRegistration registration) {
-                    return new ServiceUserMapperImpl(bundle, ServiceUserMapperController.this);
-                }
-
-                public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
-                    // no cleanup required
-                }
-            }, ctx.getProperties());
-    }
-
-    @Modified
-    private void configure(Map<String, Object> config) {
-        final String[] props = PropertiesUtil.toStringArray(config.get(PROP_SERVICE2USER_MAPPING),
-            PROP_SERVICE2USER_MAPPING_DEFAULT);
-
-        ArrayList<Mapping> mappings = new ArrayList<Mapping>(props.length);
-        for (String prop : props) {
-            if (prop != null) {
-                try {
-                    Mapping mapping = new Mapping(prop);
-                    mappings.add(mapping);
-                } catch (IllegalArgumentException iae) {
-                    log.info("configure: Ignoring '{}': {}", prop, iae.getMessage());
-                }
-            }
-        }
-
-        this.serviceUserMappings = mappings.toArray(new Mapping[mappings.size()]);
-        this.defaultUser = PropertiesUtil.toString(config.get(PROP_DEFAULT_USER), PROP_DEFAULT_USER_DEFAULT);
-    }
-
-    @Deactivate
-    private void deactivate() {
-        if (this.serviceUserMapper != null) {
-            this.serviceUserMapper.unregister();
-            this.serviceUserMapper = null;
-        }
-    }
-
-    String getServiceName(final String bundleServiceName, final String serviceInfo) {
-        return (serviceInfo == null) ? bundleServiceName : bundleServiceName + ":" + serviceInfo;
-    }
-
-    String getUserForService(final String bundleServiceName, final String serviceInfo) {
-        // try with serviceInfo first
-        for (Mapping mapping : this.serviceUserMappings) {
-            final String user = mapping.map(bundleServiceName, serviceInfo);
-            if (user != null) {
-                return user;
-            }
-        }
-
-        // second round without serviceInfo
-        for (Mapping mapping : this.serviceUserMappings) {
-            final String user = mapping.map(bundleServiceName, null);
-            if (user != null) {
-                return user;
-            }
-        }
-
-        // finally, fall back to default user
-        return this.defaultUser;
-    }
-}
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 ca9cb13..9109cab 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
@@ -18,32 +18,110 @@
  */
 package org.apache.sling.serviceusermapping.impl;
 
+import java.util.ArrayList;
+import java.util.Map;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.PropertyUnbounded;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.apache.sling.serviceusermapping.ServiceUserMapper;
 import org.osgi.framework.Bundle;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-class ServiceUserMapperImpl implements ServiceUserMapper {
+@Component(metatype = true, ds = true, policy = ConfigurationPolicy.REQUIRE)
+@Service()
+public class ServiceUserMapperImpl implements ServiceUserMapper {
 
-    private String bundleServiceName;
+    @Property(
+            label = "Service Mappings",
+            description = "Provides mappings from service name to user names. "
+                + "Each entry is of the form 'serviceName [ \":\" serviceInfo ] \"=\" userName' "
+                + "where serviceName and serviceInfo identify the service and userName would "
+                + "defines the name of the user to provide to the service. Invalid entries are logged and ignored.",
+            unbounded = PropertyUnbounded.ARRAY)
+    private static final String PROP_SERVICE2USER_MAPPING = "user.mapping";
 
-    private final ServiceUserMapperController controller;
+    private static final String[] PROP_SERVICE2USER_MAPPING_DEFAULT = {};
 
-    ServiceUserMapperImpl(final Bundle bundle, final ServiceUserMapperController controller) {
-        final String name = (String) bundle.getHeaders().get("Sling-ResourceResolver-Service");
-        if (name != null && name.trim().length() > 0) {
-            this.bundleServiceName = name.trim();
-        } else {
-            this.bundleServiceName = bundle.getSymbolicName();
+    private static final String PROP_DEFAULT_USER = "user.default";
+
+    @Property(
+            name = PROP_DEFAULT_USER,
+            label = "Default User",
+            description = "The name of the user to use as the default if no service mapping"
+                + "applies. If this property is missing or empty the default user name reflects "
+                + "an anonymous user.")
+    private static final String PROP_DEFAULT_USER_DEFAULT = "";
+
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private Mapping[] serviceUserMappings;
+
+    private String defaultUser;
+
+    @Activate
+    @Modified
+    private void configure(Map<String, Object> config) {
+        final String[] props = PropertiesUtil.toStringArray(config.get(PROP_SERVICE2USER_MAPPING),
+            PROP_SERVICE2USER_MAPPING_DEFAULT);
+
+        ArrayList<Mapping> mappings = new ArrayList<Mapping>(props.length);
+        for (String prop : props) {
+            if (prop != null) {
+                try {
+                    Mapping mapping = new Mapping(prop);
+                    mappings.add(mapping);
+                } catch (IllegalArgumentException iae) {
+                    log.info("configure: Ignoring '{}': {}", prop, iae.getMessage());
+                }
+            }
         }
 
-        this.controller = controller;
+        this.serviceUserMappings = mappings.toArray(new Mapping[mappings.size()]);
+        this.defaultUser = PropertiesUtil.toString(config.get(PROP_DEFAULT_USER), PROP_DEFAULT_USER_DEFAULT);
     }
 
-    public String getServiceName(String serviceInfo) {
-        return controller.getServiceName(this.bundleServiceName, serviceInfo);
+    public String getServiceName(Bundle bundle, String serviceInfo) {
+        final String serviceName = getServiceName(bundle);
+        return (serviceInfo == null) ? serviceName : serviceName +  ":" + serviceInfo;
     }
 
-    public String getUserForService(String serviceInfo) {
-        return controller.getUserForService(this.bundleServiceName, serviceInfo);
+    public String getUserForService(Bundle bundle, String serviceInfo) {
+        final String serviceName = getServiceName(bundle);
+
+        // try with serviceInfo first
+        for (Mapping mapping : this.serviceUserMappings) {
+            final String user = mapping.map(serviceName, serviceInfo);
+            if (user != null) {
+                return user;
+            }
+        }
+
+        // second round without serviceInfo
+        for (Mapping mapping : this.serviceUserMappings) {
+            final String user = mapping.map(serviceName, null);
+            if (user != null) {
+                return user;
+            }
+        }
+
+        // finally, fall back to default user
+        return this.defaultUser;
     }
 
+    private String getServiceName(final Bundle bundle) {
+        final String name = (String) bundle.getHeaders().get(BUNDLE_HEADER_SERVICE_NAME);
+        if (name != null && name.trim().length() > 0) {
+            return name.trim();
+        }
+
+        return bundle.getSymbolicName();
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.