You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by mp...@apache.org on 2015/03/12 15:02:21 UTC
svn commit: r1666196 - in
/sling/trunk/bundles/extensions/serviceusermapper/src:
main/java/org/apache/sling/serviceusermapping/
main/java/org/apache/sling/serviceusermapping/impl/
test/java/org/apache/sling/serviceusermapping/impl/
Author: mpetria
Date: Thu Mar 12 14:02:20 2015
New Revision: 1666196
URL: http://svn.apache.org/r1666196
Log:
SLING-4312: changed name from ServiceUserMapping to ServiceUserMapped and removed all metohds
Added:
sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapped.java
sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilter.java
sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedImpl.java
sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilterTest.java
Removed:
sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapping.java
sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappingBundleFilter.java
sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappingBundleFilterTest.java
Modified:
sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java
sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
Added: sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapped.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapped.java?rev=1666196&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapped.java (added)
+++ sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapped.java Thu Mar 12 14:02:20 2015
@@ -0,0 +1,22 @@
+package org.apache.sling.serviceusermapping;
+
+import aQute.bnd.annotation.ProviderType;
+
+/**
+ * The <code>ServiceUserMapped</code> is a marker service that can be used to ensure that there is an already registered mapping for a certain service/subService.
+ * A service reference targeting a <code>ServiceUserMapped</code> will be satisfied only if <code>ServiceUserMapper.getServiceUserID</code>
+ * will resolve the subService to an userID.
+ * For example setting the reference target to "(subServiceName=mySubService)" ensures that your component only starts when the subService is available.
+ * The subServiceName will not be set for mappings that do not have one, and those can be referenced with a negating target "(!(subServiceName=*))".
+ * Trying to reference a sub service from a bundle for which it was not registered for will not work.
+ */
+@ProviderType
+public interface ServiceUserMapped {
+
+
+ /**
+ * The name of the osgi property holding the sub service name.
+ */
+ static String SUBSERVICENAME = "subServiceName";
+
+}
Modified: sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java?rev=1666196&r1=1666195&r2=1666196&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java (original)
+++ sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java Thu Mar 12 14:02:20 2015
@@ -18,19 +18,17 @@
*/
package org.apache.sling.serviceusermapping.impl;
-import org.apache.sling.serviceusermapping.ServiceUserMapping;
-
/**
* The <code>Mapping</code> class defines the mapping of a service's name and
* optional service information to a user name.
*/
-class Mapping implements ServiceUserMapping, Comparable<Mapping> {
+class Mapping implements Comparable<Mapping> {
/**
* The name of the osgi property holding the service name.
*/
- static String SERVICENAME = "serviceName";
+ static String SERVICENAME = ".serviceName";
private final String serviceName;
Added: sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilter.java?rev=1666196&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilter.java (added)
+++ sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilter.java Thu Mar 12 14:02:20 2015
@@ -0,0 +1,93 @@
+/*
+ * 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 org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.serviceusermapping.ServiceUserMapped;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.hooks.service.EventListenerHook;
+import org.osgi.framework.hooks.service.FindHook;
+import org.osgi.framework.hooks.service.ListenerHook;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+@Component
+@Service(value = {EventListenerHook.class, FindHook.class} )
+/**
+ * The <code>ServiceUserMappingBundleFilter</code> only allows the bundle for which the service mapping is available to see it.
+ */
+public class ServiceUserMappedBundleFilter implements EventListenerHook, FindHook {
+
+ public void event(ServiceEvent serviceEvent, Map map) {
+
+ ServiceReference serviceReference = serviceEvent.getServiceReference();
+ if (isServiceMappingReference(serviceReference)) {
+ Object serviceName = serviceReference.getProperty(Mapping.SERVICENAME);
+
+ if (serviceName != null && serviceName instanceof String) {
+ Iterator<Map.Entry<BundleContext, Collection<ListenerHook.ListenerInfo>>> it = map.entrySet().iterator();
+ while (it.hasNext()) {
+ BundleContext ctx = it.next().getKey();
+
+ String bundleServiceName = ServiceUserMapperImpl.getServiceName(ctx.getBundle());
+ if (!serviceName.equals(bundleServiceName)) {
+ it.remove();
+ }
+ }
+ }
+ }
+ }
+
+ public void find(BundleContext bundleContext, String name, String filter, boolean allServices,
+ Collection references) {
+ String bundleServiceName = ServiceUserMapperImpl.getServiceName(bundleContext.getBundle());
+
+ Iterator<ServiceReference> it = references.iterator();
+ while (it.hasNext()) {
+ ServiceReference serviceReference = it.next();
+ if (isServiceMappingReference(serviceReference)) {
+ Object serviceName = serviceReference.getProperty(Mapping.SERVICENAME);
+
+ if (serviceName != null && !serviceName.equals(bundleServiceName)) {
+ it.remove();
+ }
+ }
+ }
+ }
+
+ private static boolean isServiceMappingReference(ServiceReference serviceReference) {
+ Object objectClass = serviceReference.getProperty(Constants.OBJECTCLASS);
+ for (Object o : (Object[]) objectClass) {
+ if (ServiceUserMappedImpl.SERVICEUSERMAPPED.equals(o)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+
+
+}
Added: sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedImpl.java?rev=1666196&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedImpl.java (added)
+++ sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedImpl.java Thu Mar 12 14:02:20 2015
@@ -0,0 +1,31 @@
+/*
+ * 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 org.apache.sling.serviceusermapping.ServiceUserMapped;
+
+/**
+ * This is a trivial implementation of the marker interface <code>ServiceUserMapped</code>
+ */
+public class ServiceUserMappedImpl implements ServiceUserMapped {
+
+ static String SERVICEUSERMAPPED = ServiceUserMapped.class.getName();
+
+}
Modified: sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java?rev=1666196&r1=1666195&r2=1666196&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java (original)
+++ sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java Thu Mar 12 14:02:20 2015
@@ -45,9 +45,9 @@ import org.apache.felix.scr.annotations.
import org.apache.felix.scr.annotations.References;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.serviceusermapping.ServiceUserMapped;
import org.apache.sling.serviceusermapping.ServiceUserMapper;
import org.apache.sling.serviceusermapping.ServiceUserValidator;
-import org.apache.sling.serviceusermapping.ServiceUserMapping;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
@@ -167,7 +167,7 @@ public class ServiceUserMapperImpl imple
* @see org.apache.sling.serviceusermapping.ServiceUserMapper#getServiceUserID(org.osgi.framework.Bundle, java.lang.String)
*/
public String getServiceUserID(final Bundle bundle, final String subServiceName) {
- final String serviceName = bundle.getSymbolicName();
+ final String serviceName = getServiceName(bundle);
final String userId = internalGetUserId(serviceName, subServiceName);
return isValidUser(userId, serviceName, subServiceName) ? userId : null;
}
@@ -244,11 +244,12 @@ public class ServiceUserMapperImpl imple
if (!activeMappingRegistrations.containsKey(mapping)) {
Dictionary<String, Object> properties = new Hashtable<String, Object>();
if (mapping.getSubServiceName() != null) {
- properties.put(ServiceUserMapping.SUBSERVICENAME, mapping.getSubServiceName());
+ properties.put(ServiceUserMapped.SUBSERVICENAME, mapping.getSubServiceName());
}
properties.put(Mapping.SERVICENAME, mapping.getServiceName());
- ServiceRegistration registration = bundleContext.registerService(ServiceUserMapping.class.getName(), mapping, properties);
+ ServiceRegistration registration = bundleContext.registerService(ServiceUserMappedImpl.SERVICEUSERMAPPED,
+ new ServiceUserMappedImpl(), properties);
activeMappingRegistrations.put(mapping, registration);
}
}
@@ -289,5 +290,9 @@ public class ServiceUserMapperImpl imple
}
return true;
}
+
+ static String getServiceName(Bundle bundle) {
+ return bundle.getSymbolicName();
+ }
}
Added: sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilterTest.java?rev=1666196&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilterTest.java (added)
+++ sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilterTest.java Thu Mar 12 14:02:20 2015
@@ -0,0 +1,115 @@
+/*
+ * 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 junit.framework.TestCase;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.hooks.service.EventListenerHook;
+import org.osgi.framework.hooks.service.FindHook;
+import org.osgi.framework.hooks.service.ListenerHook;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test reference and bundle filtering based on <code>Mapping.SERVICENAME</code>
+ */
+public class ServiceUserMappedBundleFilterTest {
+
+ final static String BUNDLE1 = "bundle1";
+ final static String BUNDLE2 = "bundle2";
+
+
+ final static BundleContext bundleContext1;
+ final static BundleContext bundleContext2;
+
+ static {
+ bundleContext1 = mock(BundleContext.class);
+ Bundle bundle1 = mock(Bundle.class);
+ when(bundleContext1.getBundle()).thenReturn(bundle1);
+ when(bundle1.getSymbolicName()).thenReturn(BUNDLE1);
+
+
+ bundleContext2 = mock(BundleContext.class);
+ Bundle bundle2 = mock(Bundle.class);
+ when(bundleContext2.getBundle()).thenReturn(bundle2);
+ when(bundle2.getSymbolicName()).thenReturn(BUNDLE2);
+
+ }
+
+
+
+
+ @Test
+ public void testEvent() {
+ Map<BundleContext, Collection<ListenerHook.ListenerInfo>> map = new HashMap<BundleContext, Collection<ListenerHook.ListenerInfo>>();
+
+ map.put(bundleContext1, new ArrayList<ListenerHook.ListenerInfo>());
+ map.put(bundleContext2, new ArrayList<ListenerHook.ListenerInfo>());
+
+ ServiceEvent serviceEvent = mock(ServiceEvent.class);
+ ServiceReference serviceReference = mock(ServiceReference.class);
+ when(serviceEvent.getServiceReference()).thenReturn(serviceReference);
+ when(serviceReference.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[]{ServiceUserMappedImpl.SERVICEUSERMAPPED});
+ when(serviceReference.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE1);
+
+
+ EventListenerHook eventListenerHook = new ServiceUserMappedBundleFilter();
+ eventListenerHook.event(serviceEvent, map);
+
+ TestCase.assertEquals(1, map.size());
+ TestCase.assertTrue(map.containsKey(bundleContext1));
+
+ }
+
+ @Test
+ public void testFind() {
+ List collection = new ArrayList<ServiceReference>();
+
+ ServiceReference serviceReference1 = mock(ServiceReference.class);
+ ServiceReference serviceReference2 = mock(ServiceReference.class);
+ collection.add(serviceReference1);
+ collection.add(serviceReference2);
+
+ when(serviceReference1.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE1);
+ when(serviceReference1.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[]{ServiceUserMappedImpl.SERVICEUSERMAPPED});
+
+ when(serviceReference2.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE2);
+ when(serviceReference2.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[]{ServiceUserMappedImpl.SERVICEUSERMAPPED});
+
+ FindHook findHook = new ServiceUserMappedBundleFilter();
+ findHook.find(bundleContext1, null, null, false, collection);
+
+ TestCase.assertEquals(1, collection.size());
+ TestCase.assertTrue(collection.contains(serviceReference1));
+ }
+}
Modified: sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java?rev=1666196&r1=1666195&r2=1666196&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java (original)
+++ sling/trunk/bundles/extensions/serviceusermapper/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java Thu Mar 12 14:02:20 2015
@@ -24,7 +24,6 @@ import java.util.Map;
import junit.framework.TestCase;
-import org.apache.sling.serviceusermapping.ServiceUserMapping;
import org.apache.sling.serviceusermapping.ServiceUserValidator;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
@@ -237,7 +236,7 @@ public class ServiceUserMapperImplTest {
final ServiceRegistrationContextHelper context = new ServiceRegistrationContextHelper();
sum.configure(context.getBundleContext(), config);
- TestCase.assertEquals(2, context.getRegistrations(ServiceUserMapping.class.getName()).size());
+ TestCase.assertEquals(2, context.getRegistrations(ServiceUserMappedImpl.SERVICEUSERMAPPED).size());
final MappingConfigAmendment mca1 = new MappingConfigAmendment();
@SuppressWarnings("serial")
@@ -251,7 +250,7 @@ public class ServiceUserMapperImplTest {
mca1.configure(mca1Config);
sum.bindAmendment(mca1, mca1Config);
- TestCase.assertEquals(3, context.getRegistrations(ServiceUserMapping.class.getName()).size());
+ TestCase.assertEquals(3, context.getRegistrations(ServiceUserMappedImpl.SERVICEUSERMAPPED).size());
final MappingConfigAmendment mca2 = new MappingConfigAmendment();
@SuppressWarnings("serial")
@@ -265,11 +264,11 @@ public class ServiceUserMapperImplTest {
mca2.configure(mca2Config);
sum.bindAmendment(mca2, mca2Config);
- TestCase.assertEquals(4, context.getRegistrations(ServiceUserMapping.class.getName()).size());
+ TestCase.assertEquals(4, context.getRegistrations(ServiceUserMappedImpl.SERVICEUSERMAPPED).size());
sum.unbindAmendment(mca1, mca1Config);
- TestCase.assertEquals(3, context.getRegistrations(ServiceUserMapping.class.getName()).size());
+ TestCase.assertEquals(3, context.getRegistrations(ServiceUserMappedImpl.SERVICEUSERMAPPED).size());
}