You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2015/08/07 14:39:34 UTC

svn commit: r1694670 - in /sling/trunk/bundles/extensions/serviceusermapper: pom.xml src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java

Author: cziegeler
Date: Fri Aug  7 12:39:34 2015
New Revision: 1694670

URL: http://svn.apache.org/r1694670
Log:
SLING-4930 : InventoryPrinter for Service User Mappings

Modified:
    sling/trunk/bundles/extensions/serviceusermapper/pom.xml
    sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java
    sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java

Modified: sling/trunk/bundles/extensions/serviceusermapper/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/serviceusermapper/pom.xml?rev=1694670&r1=1694669&r2=1694670&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/pom.xml (original)
+++ sling/trunk/bundles/extensions/serviceusermapper/pom.xml Fri Aug  7 12:39:34 2015
@@ -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>

Modified: sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java?rev=1694670&r1=1694669&r2=1694670&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java (original)
+++ sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java Fri Aug  7 12:39:34 2015
@@ -20,8 +20,6 @@ package org.apache.sling.serviceusermapp
 
 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 imp
             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 imp
         }
         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 imp
         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 imp
             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 imp
                 asText(out, m, "    ");
             }
         }
-   }   
+   }
 }
\ No newline at end of file

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=1694670&r1=1694669&r2=1694670&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 Fri Aug  7 12:39:34 2015
@@ -51,6 +51,7 @@ import org.apache.sling.serviceusermappi
 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 imple
     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 imple
             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 imple
     /**
      * @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 imple
     static String getServiceName(final Bundle bundle) {
         return bundle.getSymbolicName();
     }
-    
+
     List<Mapping> getActiveMappings() {
         return Collections.unmodifiableList(Arrays.asList(activeMappings));
     }