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:27 UTC
[sling-org-apache-sling-serviceusermapper] 02/12: change api: use
ServiceFactory approach and service bundle is actually the using bundle
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 c5e0963f334093059b519694c7477b7b9b471451
Author: Felix Meschberger <fm...@apache.org>
AuthorDate: Thu Mar 21 14:14:17 2013 +0000
change api: use ServiceFactory approach and service bundle is actually the using bundle
git-svn-id: https://svn.apache.org/repos/asf/sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper@1459326 13f79535-47bb-0310-9956-ffa450edef68
---
.../serviceusermapping/ServiceUserMapper.java | 18 +++-
...rImpl.java => ServiceUserMapperController.java} | 52 +++++++----
.../impl/ServiceUserMapperImpl.java | 103 +++------------------
3 files changed, 64 insertions(+), 109 deletions(-)
diff --git a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
index 04025b4..36e9181 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
@@ -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);
}
diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java
similarity index 72%
copy from src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
copy to src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java
index b0a966d..682c43d 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperController.java
@@ -24,19 +24,21 @@ 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.felix.scr.annotations.Service;
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)
-@Service()
-public class ServiceUserMapperImpl implements ServiceUserMapper {
+public class ServiceUserMapperController {
@Property(
label = "Service Mappings",
@@ -66,7 +68,24 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
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),
@@ -88,12 +107,22 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
this.defaultUser = PropertiesUtil.toString(config.get(PROP_DEFAULT_USER), PROP_DEFAULT_USER_DEFAULT);
}
- public String getUserForService(Bundle bundle, String serviceInfo) {
- final String serviceName = getServiceName(bundle);
+ @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(serviceName, serviceInfo);
+ final String user = mapping.map(bundleServiceName, serviceInfo);
if (user != null) {
return user;
}
@@ -101,7 +130,7 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
// second round without serviceInfo
for (Mapping mapping : this.serviceUserMappings) {
- final String user = mapping.map(serviceName, null);
+ final String user = mapping.map(bundleServiceName, null);
if (user != null) {
return user;
}
@@ -110,13 +139,4 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
// finally, fall back to default user
return this.defaultUser;
}
-
- 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();
- }
-
- return bundle.getSymbolicName();
- }
}
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 b0a966d..ca9cb13 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
@@ -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 = "";
+class ServiceUserMapperImpl implements ServiceUserMapper {
- /** default log */
- private final Logger log = LoggerFactory.getLogger(getClass());
+ private String bundleServiceName;
- private Mapping[] serviceUserMappings;
+ private final ServiceUserMapperController controller;
- 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());
- }
- }
+ 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();
}
- this.serviceUserMappings = mappings.toArray(new Mapping[mappings.size()]);
- this.defaultUser = PropertiesUtil.toString(config.get(PROP_DEFAULT_USER), PROP_DEFAULT_USER_DEFAULT);
+ this.controller = controller;
}
- 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;
+ public String getServiceName(String serviceInfo) {
+ return controller.getServiceName(this.bundleServiceName, serviceInfo);
}
- 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();
- }
-
- return bundle.getSymbolicName();
+ public String getUserForService(String serviceInfo) {
+ return controller.getUserForService(this.bundleServiceName, serviceInfo);
}
+
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.