You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by an...@apache.org on 2019/01/04 19:28:24 UTC

[sling-org-apache-sling-tenant] 01/01: SLING-8197 - Migrated TenantProviderImpl to OSGi R6 and moved version to 1.1.3

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

andysch pushed a commit to branch org.apache.sling.tenant-1.1.3
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-tenant.git

commit 943723314cb5aea220c35e6c4a7398aa7082f34e
Author: Andreas Schaefer <sc...@Andreass-iMac.local>
AuthorDate: Fri Jan 4 11:23:01 2019 -0800

    SLING-8197 - Migrated TenantProviderImpl to OSGi R6 and moved version to 1.1.3
    
    This will re-introduce the OSGi services of Tenant Provider / Manager which was not created becuase the OSGi definition was still made with Felix SCR Annotations
---
 pom.xml                                            | 17 +++--
 .../sling/tenant/internal/TenantProviderImpl.java  | 88 ++++++++++++----------
 .../tenant/internal/TenantProviderImplTest.java    | 17 ++++-
 3 files changed, 75 insertions(+), 47 deletions(-)

diff --git a/pom.xml b/pom.xml
index 58827a7..81d6294 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,7 +27,7 @@
     </parent>
 
     <artifactId>org.apache.sling.tenant</artifactId>
-    <version>1.1.2</version>
+    <version>1.1.3-SNAPSHOT</version>
     <packaging>bundle</packaging>
 
     <name>Apache Sling Tenant</name>
@@ -67,11 +67,6 @@
     
     <dependencies>
         <dependency>
-            <groupId>org.apache.felix</groupId>
-            <artifactId>org.apache.felix.scr.annotations</artifactId>
-            <version>1.11.0</version>
-        </dependency>
-        <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
             <version>2.5.0</version>
@@ -84,6 +79,16 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.component.annotations</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.metatype.annotations</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>javax.jcr</groupId>
             <artifactId>jcr</artifactId>
         </dependency>
diff --git a/src/main/java/org/apache/sling/tenant/internal/TenantProviderImpl.java b/src/main/java/org/apache/sling/tenant/internal/TenantProviderImpl.java
index cff472e..8371471 100644
--- a/src/main/java/org/apache/sling/tenant/internal/TenantProviderImpl.java
+++ b/src/main/java/org/apache/sling/tenant/internal/TenantProviderImpl.java
@@ -29,16 +29,6 @@ import java.util.Map.Entry;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.PropertyUnbounded;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
-import org.apache.felix.scr.annotations.ReferencePolicy;
-import org.apache.felix.scr.annotations.References;
-import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.ModifiableValueMap;
 import org.apache.sling.api.resource.PersistenceException;
@@ -59,6 +49,15 @@ import org.osgi.framework.Constants;
 import org.osgi.framework.Filter;
 import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -66,24 +65,26 @@ import org.slf4j.LoggerFactory;
  * Resource based Tenant Provider implementation.
  */
 @Component(
-        metatype = true,
-        label = "Apache Sling Tenant Provider",
-        description = "Service responsible for providing Tenants",
-        immediate = true)
-@Service(value = {TenantProvider.class, TenantManager.class})
-@Property(name = Constants.SERVICE_DESCRIPTION, value = "Apache Sling Tenant Provider")
-@References({
-    @Reference(
-        name = "tenantSetup",
-        referenceInterface = TenantCustomizer.class,
-        cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,
-        policy = ReferencePolicy.DYNAMIC),
-    @Reference(
-            name = "hook",
-            referenceInterface = TenantManagerHook.class,
-            cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,
-            policy = ReferencePolicy.DYNAMIC)
-})
+    service = {TenantProvider.class, TenantManager.class},
+    property = {
+            Constants.SERVICE_VENDOR + "=The Apache Software Foundation",
+            Constants.SERVICE_DESCRIPTION + "=Apache Sling Tenant Provider"
+    },
+    immediate = true,
+    reference = {
+        @Reference(
+            name = "tenantSetup",
+            service = TenantCustomizer.class,
+            cardinality = ReferenceCardinality.MULTIPLE,
+            policy = ReferencePolicy.DYNAMIC),
+        @Reference(
+                name = "hook",
+                service = TenantManagerHook.class,
+                cardinality = ReferenceCardinality.MULTIPLE,
+                policy = ReferencePolicy.DYNAMIC)
+    }
+)
+@Designate(ocd = TenantProviderImpl.Configuration.class)
 public class TenantProviderImpl implements TenantProvider, TenantManager {
 
     /** default log */
@@ -94,8 +95,22 @@ public class TenantProviderImpl implements TenantProvider, TenantManager {
      */
     private static final String RESOURCE_TENANT_ROOT = "/etc/tenants";
 
-    @Property(value = RESOURCE_TENANT_ROOT, label = "Tenants Root Path", description = "Defines tenants root path")
-    private static final String TENANT_ROOT = "tenant.root";
+    @ObjectClassDefinition(name = "Apache Sling Tenant Provider",
+            description = "Service responsible for providing Tenants.")
+    public @interface Configuration {
+
+        @AttributeDefinition(
+            name = "Tenants Root Path",
+            description = "Defines tenants root path",
+            defaultValue = RESOURCE_TENANT_ROOT
+        )
+        String tenant_root();
+        @AttributeDefinition(
+            name = "Tenants Path Matcher",
+            description = "Defines tenants path matcher i.e. /content/sample/([^/]+)/*, used while resolving path to tenant"
+        )
+        String[] tenant_path_matcher();
+    }
 
     private static final String[] DEFAULT_PATH_MATCHER = {};
 
@@ -105,13 +120,6 @@ public class TenantProviderImpl implements TenantProvider, TenantManager {
     private SortedMap<Comparable<Object>, TenantManagerHook> registeredHooks = new TreeMap<Comparable<Object>, TenantManagerHook>(
             Collections.reverseOrder());
 
-    @Property(
-            value = {},
-            unbounded = PropertyUnbounded.ARRAY,
-            label = "Tenants Path Matcher",
-            description = "Defines tenants path matcher i.e. /content/sample/([^/]+)/*, used while resolving path to tenant")
-    private static final String TENANT_PATH_MATCHER = "tenant.path.matcher";
-
     private String tenantRootPath = RESOURCE_TENANT_ROOT;
 
     @Reference
@@ -122,9 +130,9 @@ public class TenantProviderImpl implements TenantProvider, TenantManager {
     private WebConsolePlugin plugin;
 
     @Activate
-    protected void activate(final BundleContext bundleContext, final Map<String, Object> properties) {
-        this.tenantRootPath = PropertiesUtil.toString(properties.get(TENANT_ROOT), RESOURCE_TENANT_ROOT);
-        this.adapterFactory = new TenantAdapterFactory(bundleContext, this, PropertiesUtil.toStringArray(properties.get(TENANT_PATH_MATCHER), DEFAULT_PATH_MATCHER));
+    protected void activate(final BundleContext bundleContext, final Configuration configuration) {
+        this.tenantRootPath = PropertiesUtil.toString(configuration.tenant_root(), RESOURCE_TENANT_ROOT);
+        this.adapterFactory = new TenantAdapterFactory(bundleContext, this, PropertiesUtil.toStringArray(configuration.tenant_path_matcher(), DEFAULT_PATH_MATCHER));
         this.plugin = new WebConsolePlugin(bundleContext, this);
     }
 
diff --git a/src/test/java/org/apache/sling/tenant/internal/TenantProviderImplTest.java b/src/test/java/org/apache/sling/tenant/internal/TenantProviderImplTest.java
index c3ff612..1628fe5 100644
--- a/src/test/java/org/apache/sling/tenant/internal/TenantProviderImplTest.java
+++ b/src/test/java/org/apache/sling/tenant/internal/TenantProviderImplTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.tenant.internal;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -41,7 +42,21 @@ public class TenantProviderImplTest {
         Mockito.when(rrf.getServiceResourceResolver(
                 Mockito.anyMapOf(String.class, Object.class))).thenReturn(rr);
         set(provider, "factory", rrf);
-        provider.activate(context, new HashMap<String, Object>());
+        TenantProviderImpl.Configuration configuration = new TenantProviderImpl.Configuration() {
+            @Override
+            public Class<? extends Annotation> annotationType() {
+                return null;
+            }
+            @Override
+            public String tenant_root() {
+                return "/etc/tenants";
+            }
+            @Override
+            public String[] tenant_path_matcher() {
+                return new String[] {};
+            }
+        };
+        provider.activate(context, configuration);
         Iterator<Tenant> tenants = provider.getTenants();
         TestCase.assertNotNull(tenants);
         TestCase.assertFalse(tenants.hasNext());