Implemented FELIX-4998 : Declarative Service plugin might provide JSON format support for Inventory Printer
https://issues.apache.org/jira/browse/FELIX-4998

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1694680 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/Activator.java b/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/Activator.java
index b723d29..2270aeb 100644
--- a/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/Activator.java
+++ b/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/Activator.java
@@ -16,8 +16,10 @@
  */

 package org.apache.felix.webconsole.plugins.ds.internal;

 

+import java.util.Dictionary;

 import java.util.Hashtable;

 

+import org.apache.felix.inventory.Format;

 import org.apache.felix.inventory.InventoryPrinter;

 import org.apache.felix.webconsole.SimpleWebConsolePlugin;

 import org.osgi.framework.BundleActivator;

@@ -82,12 +84,17 @@
             this.plugin = plugin = new WebConsolePlugin().register(context);

             final Object service = context.getService(reference);

 

-            final Hashtable props = new Hashtable();

+            final Dictionary<String, Object> props = new Hashtable<String, Object>();

             final String name = "Declarative Services Components";

-            props.put(InventoryPrinter.NAME, name.replace(' ', '_'));

+            props.put(InventoryPrinter.NAME, "scr"); //$NON-NLS-1$

             props.put(InventoryPrinter.TITLE, name);

+            props.put(InventoryPrinter.FORMAT, new String[] {

+                    Format.TEXT.toString(),

+                    Format.JSON.toString()

+            });

             printerRegistration = context.registerService(InventoryPrinter.SERVICE,

-                new ComponentConfigurationPrinter(service), props);

+                new ComponentConfigurationPrinter(service, (WebConsolePlugin) plugin),

+                props);

 

             infoRegistration = new InfoProvider(context.getBundle(), service).register(context);

         }

diff --git a/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/ComponentConfigurationPrinter.java b/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/ComponentConfigurationPrinter.java
index 628a966..2744a29 100644
--- a/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/ComponentConfigurationPrinter.java
+++ b/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/ComponentConfigurationPrinter.java
@@ -32,6 +32,8 @@
 import org.apache.felix.inventory.Format;
 import org.apache.felix.inventory.InventoryPrinter;
 import org.apache.felix.webconsole.WebConsoleUtil;
+import org.json.JSONException;
+import org.json.JSONWriter;
 import org.osgi.framework.Constants;
 import org.osgi.framework.dto.ServiceReferenceDTO;
 import org.osgi.service.component.ComponentConstants;
@@ -48,16 +50,17 @@
 {
 
     private final ServiceComponentRuntime scrService;
+    private final WebConsolePlugin plugin;
 
-    ComponentConfigurationPrinter(Object scrService)
+    ComponentConfigurationPrinter(Object scrService, WebConsolePlugin plugin)
     {
         this.scrService = (ServiceComponentRuntime)scrService;
+        this.plugin = plugin;
     }
 
     /**
      * @see org.apache.felix.inventory.InventoryPrinter#print(java.io.PrintWriter, org.apache.felix.inventory.Format, boolean)
      */
-    @Override
     public void print(PrintWriter pw, Format format, boolean isZip)
     {
         final List<ComponentDescriptionDTO> descriptions = new ArrayList<ComponentDescriptionDTO>();
@@ -74,11 +77,43 @@
         }
         Collections.sort(configurations, Util.COMPONENT_COMPARATOR);
 
-        printComponents(pw, configurations);
+        if (Format.JSON.equals(format))
+        {
+            try
+            {
+                printComponentsJson(pw, configurations, isZip);
+            }
+            catch (JSONException t)
+            {
+                // ignore
+            }
+        }
+        else
+        {
+            printComponentsText(pw, configurations);
+        }
     }
 
+    private final void printComponentsJson(final PrintWriter pw,
+        final List<ComponentConfigurationDTO> configurations,
+        final boolean details) throws JSONException
+    {
+        final JSONWriter jw = new JSONWriter(pw);
+        jw.object();
+        jw.key("components"); //$NON-NLS-1$
+        jw.array();
+        
+        // render components
+        for (final ComponentConfigurationDTO cfg : configurations)
+        {
+            plugin.component(jw, cfg, details);
+        }
+        
+        jw.endArray();
+        jw.endObject();
+    }
 
-    private static final void printComponents(final PrintWriter pw,
+    private static final void printComponentsText(final PrintWriter pw,
             final List<ComponentConfigurationDTO> configurations)
     {
         if (configurations.size() == 0)
diff --git a/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/InfoProvider.java b/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/InfoProvider.java
index 0c8c4f3..0103662 100644
--- a/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/InfoProvider.java
+++ b/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/InfoProvider.java
@@ -53,7 +53,6 @@
     /**

      * @see org.apache.felix.webconsole.bundleinfo.BundleInfoProvider#getName(java.util.Locale)

      */

-    @Override

     public String getName(Locale locale)

     {

         return localization.getResourceBundle(locale).getString("info.name"); //$NON-NLS-1$;;

@@ -63,7 +62,6 @@
     * @see org.apache.felix.webconsole.bundleinfo.BundleInfoProvider#getBundleInfo(org.osgi.framework.Bundle,

     *      java.lang.String, java.util.Locale)

     */

-    @Override

     public BundleInfo[] getBundleInfo(Bundle bundle, String webConsoleRoot, Locale locale)

     {

         final List<ComponentDescriptionDTO> descriptions = new ArrayList<ComponentDescriptionDTO>();

diff --git a/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/Util.java b/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/Util.java
index e78cd74..164405d 100644
--- a/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/Util.java
+++ b/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/Util.java
@@ -27,7 +27,6 @@
 

     static final Comparator<ComponentConfigurationDTO> COMPONENT_COMPARATOR = new Comparator<ComponentConfigurationDTO>()

     {

-        @Override

         public int compare(ComponentConfigurationDTO c0, ComponentConfigurationDTO c1)

         {

             final int nameCmp = c0.description.name.compareTo(c1.description.name);

diff --git a/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/WebConsolePlugin.java b/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/WebConsolePlugin.java
index e04a3dd..3d14ba5 100644
--- a/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/WebConsolePlugin.java
+++ b/webconsole-plugins/ds/src/main/java/org/apache/felix/webconsole/plugins/ds/internal/WebConsolePlugin.java
@@ -243,7 +243,7 @@
         }
     }
 
-    private void component(JSONWriter jw, ComponentConfigurationDTO component, boolean details)
+    void component(JSONWriter jw, ComponentConfigurationDTO component, boolean details)
         throws JSONException
     {
         String id = String.valueOf(component.id);