You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2015/08/06 16:53:01 UTC

svn commit: r1694513 - 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: bdelacretaz
Date: Thu Aug  6 14:53:01 2015
New Revision: 1694513

URL: http://svn.apache.org/r1694513
Log:
SLING-4930 - InventoryPrinter for service user mappings

Added:
    sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java
Modified:
    sling/trunk/bundles/extensions/serviceusermapper/pom.xml
    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=1694513&r1=1694512&r2=1694513&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/pom.xml (original)
+++ sling/trunk/bundles/extensions/serviceusermapper/pom.xml Thu Aug  6 14:53:01 2015
@@ -57,6 +57,10 @@
                 <extensions>true</extensions>
                 <configuration>
                     <instructions>
+                        <Import-Package>
+                            org.apache.felix.inventory;resolution:=optional,
+                            *
+                        </Import-Package>
                     </instructions>
                 </configuration>
             </plugin>
@@ -75,8 +79,19 @@
             <version>2.1.0</version>
             <scope>provided</scope>
         </dependency>
-
-
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.json</artifactId>
+            <version>2.0.6</version>
+            <scope>provided</scope>
+        </dependency>
+       <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.inventory</artifactId>
+            <version>1.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+  
         <dependency>
             <groupId>javax.jcr</groupId>
             <artifactId>jcr</artifactId>

Added: 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=1694513&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java (added)
+++ sling/trunk/bundles/extensions/serviceusermapper/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java Thu Aug  6 14:53:01 2015
@@ -0,0 +1,176 @@
+/*
+ * 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 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;
+import java.util.TreeMap;
+
+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) {
+        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;
+        }
+    }
+    
+    @Override
+    public void print(PrintWriter out, Format format, boolean isZip) {
+        try {
+            if(format.equals(Format.JSON)) {
+                renderJson(out);
+            } else if(format.equals(Format.TEXT)) {
+                renderText(out);
+            }
+        } catch(Exception e) {
+            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) {
+            final String user = getMappedUser(m);
+            List<Mapping> list = result.get(user);
+            if(list == null) {
+                list = new ArrayList<Mapping>();
+                result.put(user, list);
+            }
+            list.add(m);
+        }
+        return result;
+    }
+    
+    private void asJSON(JSONWriter w, Mapping m) throws JSONException {
+        w.object();
+        w.key("serviceName").value(m.getServiceName());
+        w.key("subServiceName").value(m.getSubServiceName());
+        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("rawMappings");
+        w.array();
+        for(Mapping m : data) {
+            asJSON(w, m);
+        }
+        w.endArray();
+        
+        w.key("mappingsByUser");
+        w.object();
+        for(Map.Entry<String, List<Mapping>> e : byUser.entrySet()) {
+            w.key(e.getKey());
+            w.array();
+            for(Mapping m : e.getValue()) {
+                asJSON(w,m);
+            }
+            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(); 
+        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("*** Raw Mappings (");
+        out.print(data.size());
+        out.print("):");
+        out.println(formatInfo);
+        
+        for(Mapping m : data) {
+            asText(out, m, "  ");
+        }
+        
+        out.println();
+        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());
+            for(Mapping m : e.getValue()) {
+                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=1694513&r1=1694512&r2=1694513&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 Aug  6 14:53:01 2015
@@ -19,6 +19,7 @@
 package org.apache.sling.serviceusermapping.impl;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
@@ -109,6 +110,8 @@ public class ServiceUserMapperImpl imple
     private SortedMap<Mapping, ServiceRegistration> activeMappingRegistrations = new TreeMap<Mapping, ServiceRegistration>();
 
     private BundleContext bundleContext;
+    
+    private MappingInventoryPrinter mip;
 
     @Activate
     @Modified
@@ -134,10 +137,19 @@ public class ServiceUserMapperImpl imple
             this.bundleContext = bundleContext;
             this.updateMappings();
         }
+        
+        if(bundleContext != null && mip == null) {
+            mip = new MappingInventoryPrinter(bundleContext, this);
+        }
     }
 
     @Deactivate
     void deactivate() {
+        if(mip != null) {
+            mip.deactivate();
+            mip = null;
+        }
+        
         synchronized ( this.amendments) {
             updateServiceMappings(new ArrayList<Mapping>());
             bundleContext = null;
@@ -288,5 +300,9 @@ public class ServiceUserMapperImpl imple
     static String getServiceName(final Bundle bundle) {
         return bundle.getSymbolicName();
     }
+    
+    List<Mapping> getActiveMappings() {
+        return Collections.unmodifiableList(Arrays.asList(activeMappings));
+    }
 }