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:14:15 UTC

[sling-org-apache-sling-serviceusermapper] 08/17: SLING-4930 : InventoryPrinter for Service User Mappings

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

rombert pushed a commit to annotated tag org.apache.sling.serviceusermapper-1.2.2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-serviceusermapper.git

commit 1029fe540044bc2593ccebf2ccadcdb5e3f7d16f
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Aug 7 12:39:34 2015 +0000

    SLING-4930 : InventoryPrinter for Service User Mappings
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/serviceusermapper@1694670 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  5 +-
 .../impl/MappingInventoryPrinter.java              | 59 ++++++++--------------
 .../impl/ServiceUserMapperImpl.java                | 42 +++++++++++----
 3 files changed, 56 insertions(+), 50 deletions(-)

diff --git a/pom.xml b/pom.xml
index 418621b..5bff61c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -61,7 +61,10 @@
                             org.apache.felix.inventory;resolution:=optional,
                             *
                         </Import-Package>
-                    </instructions>
+                       <DynamicImport-Package>
+                            org.apache.felix.inventory
+                        </DynamicImport-Package>
+                     </instructions>
                 </configuration>
             </plugin>
         </plugins>
diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java b/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java
index cc1228d..f078887 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java
@@ -20,8 +20,6 @@ package org.apache.sling.serviceusermapping.impl;
 
 import java.io.PrintWriter;
 import java.util.ArrayList;
-import java.util.Dictionary;
-import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
 import java.util.SortedMap;
@@ -31,37 +29,20 @@ import org.apache.felix.inventory.Format;
 import org.apache.felix.inventory.InventoryPrinter;
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.io.JSONWriter;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /** InventoryPrinter for service user mappings */
 public class MappingInventoryPrinter implements InventoryPrinter {
 
-    private final ServiceUserMapperImpl mapper;
-    private ServiceRegistration serviceReg;
-    
-    private final Logger log = LoggerFactory.getLogger(getClass());
-    
-    MappingInventoryPrinter(BundleContext ctx, ServiceUserMapperImpl mapper) {
+    private volatile ServiceUserMapperImpl mapper;
+
+    MappingInventoryPrinter(final ServiceUserMapperImpl mapper) {
         this.mapper = mapper;
-        
-        final Dictionary<String, Object> props = new Hashtable<String, Object>();
-        props.put(InventoryPrinter.FORMAT, new String[] { Format.JSON.toString(), Format.TEXT.toString() });
-        props.put(InventoryPrinter.NAME, "serviceusers");
-        props.put(InventoryPrinter.TITLE, "Service User Mappings");
-        props.put(InventoryPrinter.WEBCONSOLE, true);
-        serviceReg = ctx.registerService(InventoryPrinter.class, this, props);
     }
-    
+
     void deactivate() {
-        if(serviceReg != null) {
-            serviceReg.unregister();
-            serviceReg = null;
-        }
+        this.mapper = null;
     }
-    
+
     @Override
     public void print(PrintWriter out, Format format, boolean isZip) {
         try {
@@ -74,11 +55,11 @@ public class MappingInventoryPrinter implements InventoryPrinter {
             e.printStackTrace(out);
         }
     }
-    
+
     private String getMappedUser(Mapping m) {
         return m.map(m.getServiceName(), m.getSubServiceName());
     }
-    
+
     private SortedMap<String, List<Mapping>> getMappingsByUser(List<Mapping> mappings) {
         SortedMap<String, List<Mapping>> result = new TreeMap<String, List<Mapping>>();
         for(Mapping m : mappings) {
@@ -92,7 +73,7 @@ public class MappingInventoryPrinter implements InventoryPrinter {
         }
         return result;
     }
-    
+
     private void asJSON(JSONWriter w, Mapping m) throws JSONException {
         w.object();
         w.key("serviceName").value(m.getServiceName());
@@ -100,18 +81,18 @@ public class MappingInventoryPrinter implements InventoryPrinter {
         w.key("user").value(getMappedUser(m));
         w.endObject();
     }
-    
+
     private void renderJson(PrintWriter out) throws JSONException {
         final List<Mapping> data = mapper.getActiveMappings();
         final Map<String, List<Mapping>> byUser = getMappingsByUser(data);
-        
+
         final JSONWriter w = new JSONWriter(out);
         w.setTidy(true);
         w.object();
         w.key("title").value("Service User Mappings");
         w.key("mappingsCount").value(data.size());
         w.key("uniqueUsersCount").value(byUser.keySet().size());
-        
+
         w.key("mappingsByUser");
         w.object();
         for(Map.Entry<String, List<Mapping>> e : byUser.entrySet()) {
@@ -123,32 +104,32 @@ public class MappingInventoryPrinter implements InventoryPrinter {
             w.endArray();
         }
         w.endObject();
-        
+
         w.endObject();
     }
-    
+
     private void asText(PrintWriter w, Mapping m, String indent) {
         final String SEP = " / ";
         w.print(indent);
         w.print(m.getServiceName());
         w.print(SEP);
-        final String sub = m.getSubServiceName(); 
+        final String sub = m.getSubServiceName();
         w.print(sub == null ? "" : sub);
         w.print(SEP);
         w.println(getMappedUser(m));
     }
-    
+
     private void renderText(PrintWriter out) {
         final List<Mapping> data = mapper.getActiveMappings();
         final Map<String, List<Mapping>> byUser = getMappingsByUser(data);
-        
+
         final String formatInfo = " (format: service name / sub service name / user)";
-        
+
         out.print("*** Mappings by user (");
         out.print(byUser.keySet().size());
         out.print(" users):");
         out.println(formatInfo);
-        
+
         for(Map.Entry<String, List<Mapping>> e : byUser.entrySet()) {
             out.print("  ");
             out.println(e.getKey());
@@ -156,5 +137,5 @@ public class MappingInventoryPrinter implements InventoryPrinter {
                 asText(out, m, "    ");
             }
         }
-   }   
+   }
 }
\ No newline at end of file
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 97f55f4..555de03 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
@@ -51,6 +51,7 @@ import org.apache.sling.serviceusermapping.ServiceUserValidator;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -110,8 +111,8 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
     private SortedMap<Mapping, ServiceRegistration> activeMappingRegistrations = new TreeMap<Mapping, ServiceRegistration>();
 
     private BundleContext bundleContext;
-    
-    private MappingInventoryPrinter mip;
+
+    private ServiceRegistration invPrinterReg;
 
     @Activate
     @Modified
@@ -137,19 +138,39 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
             this.bundleContext = bundleContext;
             this.updateMappings();
         }
-        
-        if(bundleContext != null && mip == null) {
-            mip = new MappingInventoryPrinter(bundleContext, this);
+
+        // check for reconfiguration (bundleContext is only null during tests!)
+        if ( invPrinterReg == null && bundleContext != null ) {
+            final Dictionary<String, Object> serviceProps = new Hashtable<String, Object>();
+            serviceProps.put("felix.inventory.printer.format", new String[] { "JSON", "TEXT" });
+            serviceProps.put("felix.inventory.printer.name", "serviceusers");
+            serviceProps.put("felix.inventory.printer.title", "Service User Mappings");
+            serviceProps.put("felix.inventory.printer.webconsole", true);
+            invPrinterReg = bundleContext.registerService("org.apache.felix.inventory.InventoryPrinter",
+                    new ServiceFactory() {
+
+                        @Override
+                        public Object getService(final Bundle bundle, final ServiceRegistration registration) {
+                            return new MappingInventoryPrinter(ServiceUserMapperImpl.this);
+                        }
+
+                        @Override
+                        public void ungetService(final Bundle bundle, final ServiceRegistration registration, final Object service) {
+                            if ( service instanceof MappingInventoryPrinter) {
+                                ((MappingInventoryPrinter)service).deactivate();
+                            }
+                        }
+                    }, serviceProps);
         }
     }
 
     @Deactivate
     void deactivate() {
-        if(mip != null) {
-            mip.deactivate();
-            mip = null;
+        if (invPrinterReg != null) {
+            invPrinterReg.unregister();
+            invPrinterReg = null;
         }
-        
+
         synchronized ( this.amendments) {
             updateServiceMappings(new ArrayList<Mapping>());
             bundleContext = null;
@@ -175,6 +196,7 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
     /**
      * @see org.apache.sling.serviceusermapping.ServiceUserMapper#getServiceUserID(org.osgi.framework.Bundle, java.lang.String)
      */
+    @Override
     public String getServiceUserID(final Bundle bundle, final String subServiceName) {
         final String serviceName = getServiceName(bundle);
         final String userId = internalGetUserId(serviceName, subServiceName);
@@ -300,7 +322,7 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
     static String getServiceName(final Bundle bundle) {
         return bundle.getSymbolicName();
     }
-    
+
     List<Mapping> getActiveMappings() {
         return Collections.unmodifiableList(Arrays.asList(activeMappings));
     }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.