You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2013/03/21 15:14:18 UTC

svn commit: r1459326 - in /sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping: ServiceUserMapper.java impl/ServiceUserMapperController.java impl/ServiceUserMapperImpl.java

Author: fmeschbe
Date: Thu Mar 21 14:14:17 2013
New Revision: 1459326

URL: http://svn.apache.org/r1459326
Log:
change api: use ServiceFactory approach and service bundle is actually the using bundle

Added:
    sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java
Modified:
    sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
    sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java

Modified: sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java?rev=1459326&r1=1459325&r2=1459326&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java (original)
+++ sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java Thu Mar 21 14:14:17 2013
@@ -18,19 +18,27 @@
  */
 package org.apache.sling.serviceusermapping;
 
-import org.osgi.framework.Bundle;
-
 import aQute.bnd.annotation.ProviderType;
 
 @ProviderType
 public interface ServiceUserMapper {
 
     /**
+     * Returns the name of the service represented by the {@code bundle} and the
+     * {@code serviceInfo}.
+     *
+     * @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);
+
+    /**
      * 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}.
@@ -38,6 +46,6 @@ 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(Bundle bundle, String serviceInfo);
+    String getUserForService(String serviceInfo);
 
 }

Added: sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java?rev=1459326&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java (added)
+++ sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java Thu Mar 21 14:14:17 2013
@@ -0,0 +1,142 @@
+/*
+ * 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;
+    }
+}

Modified: sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java?rev=1459326&r1=1459325&r2=1459326&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java (original)
+++ sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java Thu Mar 21 14:14:17 2013
@@ -18,105 +18,32 @@
  */
 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;
-
-@Component(metatype = true, ds = true, policy = ConfigurationPolicy.REQUIRE)
-@Service()
-public class ServiceUserMapperImpl implements ServiceUserMapper {
-
-    @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;
-
-    @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.serviceUserMappings = mappings.toArray(new Mapping[mappings.size()]);
-        this.defaultUser = PropertiesUtil.toString(config.get(PROP_DEFAULT_USER), PROP_DEFAULT_USER_DEFAULT);
-    }
+class ServiceUserMapperImpl implements ServiceUserMapper {
 
-    public String getUserForService(Bundle bundle, String serviceInfo) {
-        final String serviceName = getServiceName(bundle);
+    private String bundleServiceName;
 
-        // try with serviceInfo first
-        for (Mapping mapping : this.serviceUserMappings) {
-            final String user = mapping.map(serviceName, serviceInfo);
-            if (user != null) {
-                return user;
-            }
-        }
+    private final ServiceUserMapperController controller;
 
-        // second round without serviceInfo
-        for (Mapping mapping : this.serviceUserMappings) {
-            final String user = mapping.map(serviceName, null);
-            if (user != null) {
-                return user;
-            }
+    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();
         }
 
-        // finally, fall back to default user
-        return this.defaultUser;
+        this.controller = controller;
     }
 
-    private String getServiceName(final Bundle bundle) {
-        final String name = (String) bundle.getHeaders().get("Sling-ResourceResolver-Service");
-        if (name != null && name.trim().length() > 0) {
-            return name.trim();
-        }
+    public String getServiceName(String serviceInfo) {
+        return controller.getServiceName(this.bundleServiceName, serviceInfo);
+    }
 
-        return bundle.getSymbolicName();
+    public String getUserForService(String serviceInfo) {
+        return controller.getUserForService(this.bundleServiceName, serviceInfo);
     }
+
 }