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/07/03 20:08:20 UTC

svn commit: r1499501 - in /sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src: main/java/org/apache/sling/serviceusermapping/ main/java/org/apache/sling/serviceusermapping/impl/ test/java/org/apache/sling/serviceusermapping/...

Author: fmeschbe
Date: Wed Jul  3 18:08:20 2013
New Revision: 1499501

URL: http://svn.apache.org/r1499501
Log:
Implement support for service based ResourceResolver and Session access

- Clarify empty serviceInfo to be the same as null
- Clarify empty serviceInfo in configuration is an error
  (error sample: service:=user)
- Add missing service label and description for Metatype Service
- Add unit tests for ServiceUserMapperImpl service

Added:
    sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.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/Mapping.java
    sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
    sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.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=1499501&r1=1499500&r2=1499501&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 Wed Jul  3 18:08:20 2013
@@ -32,6 +32,14 @@ import aQute.bnd.annotation.ProviderType
  * service appropriate access without requiring administrative level access to
  * the storage.
  * <p>
+ * In general a service is implement in a single bundle such as the JSP compiler
+ * bundle. Other services may be implemented in multiple bundles. In certain
+ * cases there may be sub-services requiring different access levels. For
+ * example a couple of bundles may implement a "mail" service where each bundle
+ * implement implements a part of the service such as the "smtp", "queuing", and
+ * "delivery" sub services. Such sub services are identified with the
+ * {@code serviceInfo} parameter on the method calls.
+ * <p>
  * In addition to allowing to phase out the use of
  * {@code ResourceResolver.getAdministrativeResourceResolver} and
  * {@code SlingRepository.loginAdministrative} it also allows to better account
@@ -58,13 +66,13 @@ public interface ServiceUserMapper {
      * {@code serviceInfo}.
      * <p>
      * The service name consists of a name derived from the bundle and the
-     * {@code serviceInfo} value if not {@code null}.
+     * {@code serviceInfo} value if not {@code null} or empty.
      *
      * @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}.
+     *            an empty string or {@code null}.
      * @return The name of the service represented by the bundle along with the
      *         additional service information.
      */
@@ -78,7 +86,7 @@ public interface ServiceUserMapper {
      *            resources.
      * @param serviceInfo Additional information about the concrete service
      *            requesting access. This parameter is optional and may be
-     *            {@code null}.
+     *            an empty string or {@code null}.
      * @return The name of the user to use to provide access to the resources
      *         for the service. This may be {@code null} if no particular user
      *         can be derived for the service identified by the bundle and the

Modified: sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java?rev=1499501&r1=1499500&r2=1499501&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java (original)
+++ sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java Wed Jul  3 18:08:20 2013
@@ -47,15 +47,15 @@ class Mapping {
         final int colon = spec.indexOf(':');
         final int equals = spec.indexOf('=');
 
-        if (equals <= 0) {
-            throw new IllegalArgumentException("serviceName missing");
+        if (colon == 0 || equals <= 0) {
+            throw new IllegalArgumentException("serviceName is required");
         } else if (equals == spec.length() - 1) {
-            throw new IllegalArgumentException("userName missing");
+            throw new IllegalArgumentException("userName is required");
+        } else if (colon + 1 == equals) {
+            throw new IllegalArgumentException("serviceInfo must not be empty");
         }
 
-        if (colon == 0) {
-            throw new IllegalArgumentException("serviceName missing");
-        } else if (colon < 0 || colon > equals) {
+        if (colon < 0 || colon > equals) {
             this.serviceName = spec.substring(0, equals);
             this.serviceInfo = null;
         } else {

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=1499501&r1=1499500&r2=1499501&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 Wed Jul  3 18:08:20 2013
@@ -33,7 +33,11 @@ import org.osgi.framework.Bundle;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@Component(metatype = true, ds = true)
+@Component(
+        metatype = true,
+        ds = true,
+        label = "Apache Sling Service User Mapper Service",
+        description = "Configuration for the service mapping service names to names of users.")
 @Service()
 public class ServiceUserMapperImpl implements ServiceUserMapper {
 
@@ -55,7 +59,7 @@ public class ServiceUserMapperImpl imple
             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 no default user is defined.")
-    private static final String PROP_DEFAULT_USER_DEFAULT = "";
+    private static final String PROP_DEFAULT_USER_DEFAULT = null;
 
     /** default log */
     private final Logger log = LoggerFactory.getLogger(getClass());
@@ -66,7 +70,7 @@ public class ServiceUserMapperImpl imple
 
     @Activate
     @Modified
-    private void configure(Map<String, Object> config) {
+    void configure(Map<String, Object> config) {
         final String[] props = PropertiesUtil.toStringArray(config.get(PROP_SERVICE2USER_MAPPING),
             PROP_SERVICE2USER_MAPPING_DEFAULT);
 
@@ -88,7 +92,7 @@ public class ServiceUserMapperImpl imple
 
     public String getServiceName(Bundle bundle, String serviceInfo) {
         final String serviceName = getServiceName(bundle);
-        return (serviceInfo == null) ? serviceName : serviceName +  ":" + serviceInfo;
+        return (serviceInfo == null || serviceInfo.length() == 0) ? serviceName : serviceName + ":" + serviceInfo;
     }
 
     public String getUserForService(Bundle bundle, String serviceInfo) {

Modified: sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.java?rev=1499501&r1=1499500&r2=1499501&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.java (original)
+++ sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.java Wed Jul  3 18:08:20 2013
@@ -75,10 +75,24 @@ public class MappingTest {
     }
 
     @Test
+    public void test_constructor_empty_service_info() {
+        try {
+            new Mapping("srv:=user");
+            TestCase.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException iae) {
+            // expected
+        }
+    }
+
+    @Test
+    public void test_constructor_user_with_colon() {
+        new Mapping("srv=jcr:user");
+    }
+
+    @Test
     public void test_constructor_and_map() {
         assertMapping("service", null, "user");
         assertMapping("service", "serviceInfo", "user");
-        assertMapping("service", "", "user");
     }
 
     private void assertMapping(final String serviceName, final String serviceInfo, final String userName) {

Added: sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java?rev=1499501&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java (added)
+++ sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java Wed Jul  3 18:08:20 2013
@@ -0,0 +1,135 @@
+/*
+ * 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.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.sling.commons.testing.osgi.MockBundle;
+import org.apache.sling.serviceusermapping.ServiceUserMapper;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+
+public class ServiceUserMapperImplTest {
+    private static final String BUNDLE_SYMBOLIC1 = "bundle1";
+
+    private static final String BUNDLE_SYMBOLIC2 = "bundle2";
+
+    private static final String SRV = "srv";
+
+    private static final String SUB = "sub";
+
+    private static final String NONE = "none";
+
+    private static final String SAMPLE = "sample";
+
+    private static final String ANOTHER = "another";
+
+    private static final String SAMPLE_SUB = "sample_sub";
+
+    private static final String ANOTHER_SUB = "another_sub";
+
+    private static final Bundle BUNDLE1 = new MockBundle(10) {
+        public String getSymbolicName() {
+            return BUNDLE_SYMBOLIC1;
+        };
+
+        public java.util.Dictionary<?, ?> getHeaders() {
+            return new Hashtable<String, Object>();
+        };
+
+        public java.util.Dictionary<?, ?> getHeaders(String locale) {
+            return getHeaders();
+        };
+    };
+
+    private static final Bundle BUNDLE2 = new MockBundle(10) {
+        public String getSymbolicName() {
+            return BUNDLE_SYMBOLIC2;
+        };
+
+        @SuppressWarnings("serial")
+        public java.util.Dictionary<?, ?> getHeaders() {
+            return new Hashtable<String, Object>() {
+                {
+                    put(ServiceUserMapper.BUNDLE_HEADER_SERVICE_NAME, SRV);
+                }
+            };
+        };
+
+        public java.util.Dictionary<?, ?> getHeaders(String locale) {
+            return getHeaders();
+        };
+    };
+
+    @Test
+    public void test_getServiceName() {
+        @SuppressWarnings("serial")
+        Map<String, Object> config = new HashMap<String, Object>() {
+            {
+                put("user.mapping", new String[] {
+                    BUNDLE_SYMBOLIC1 + "=" + SAMPLE, //
+                    SRV + "=" + ANOTHER, //
+                    BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, //
+                    SRV + ":" + SUB + "=" + ANOTHER_SUB //
+                });
+                put("user.default", NONE);
+            }
+        };
+
+        final ServiceUserMapperImpl sum = new ServiceUserMapperImpl();
+        sum.configure(config);
+
+        TestCase.assertEquals(BUNDLE_SYMBOLIC1, sum.getServiceName(BUNDLE1, null));
+        TestCase.assertEquals(SRV, sum.getServiceName(BUNDLE2, null));
+        TestCase.assertEquals(BUNDLE_SYMBOLIC1, sum.getServiceName(BUNDLE1, ""));
+        TestCase.assertEquals(SRV, sum.getServiceName(BUNDLE2, ""));
+        TestCase.assertEquals(BUNDLE_SYMBOLIC1 + ":" + SUB, sum.getServiceName(BUNDLE1, SUB));
+        TestCase.assertEquals(SRV + ":" + SUB, sum.getServiceName(BUNDLE2, SUB));
+    }
+
+    @Test
+    public void test_getUserForService() {
+        @SuppressWarnings("serial")
+        Map<String, Object> config = new HashMap<String, Object>() {
+            {
+                put("user.mapping", new String[] {
+                    BUNDLE_SYMBOLIC1 + "=" + SAMPLE, //
+                    SRV + "=" + ANOTHER, //
+                    BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, //
+                    SRV + ":" + SUB + "=" + ANOTHER_SUB //
+                });
+                put("user.default", NONE);
+            }
+        };
+
+        final ServiceUserMapperImpl sum = new ServiceUserMapperImpl();
+        sum.configure(config);
+
+        TestCase.assertEquals(SAMPLE, sum.getUserForService(BUNDLE1, null));
+        TestCase.assertEquals(ANOTHER, sum.getUserForService(BUNDLE2, null));
+        TestCase.assertEquals(SAMPLE, sum.getUserForService(BUNDLE1, ""));
+        TestCase.assertEquals(ANOTHER, sum.getUserForService(BUNDLE2, ""));
+        TestCase.assertEquals(SAMPLE_SUB, sum.getUserForService(BUNDLE1, SUB));
+        TestCase.assertEquals(ANOTHER_SUB, sum.getUserForService(BUNDLE2, SUB));
+    }
+}