Fixed FELIX-4997 : UPnP plugin should use Inventory instead of deprecated ConfigurationPrinter
https://issues.apache.org/jira/browse/FELIX-4997
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1694640 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole-plugins/upnp/pom.xml b/webconsole-plugins/upnp/pom.xml
index 4088cad..4d481c6 100644
--- a/webconsole-plugins/upnp/pom.xml
+++ b/webconsole-plugins/upnp/pom.xml
@@ -126,6 +126,12 @@
<version>3.1.0</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>org.json</groupId>
<artifactId>json</artifactId>
diff --git a/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/Activator.java b/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/Activator.java
index 6e11114..538a7b0 100644
--- a/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/Activator.java
+++ b/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/Activator.java
@@ -16,7 +16,11 @@
*/
package org.apache.felix.webconsole.plugins.upnp.internal;
-import org.apache.felix.webconsole.ConfigurationPrinter;
+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;
import org.osgi.framework.BundleContext;
@@ -78,8 +82,16 @@
if (plugin == null)
{
this.plugin = plugin = new WebConsolePlugin(tracker).register(context);
- printerRegistration = context.registerService(ConfigurationPrinter.SERVICE,
- new ConfigurationPrinterImpl(tracker), null);
+
+ // register configuration printer
+ final Dictionary/*<String, Object>*/ props = new Hashtable/*<String, Object>*/();
+ props.put(InventoryPrinter.NAME, "upnp"); //$NON-NLS-1$
+ props.put(InventoryPrinter.TITLE, "UPnP Devices"); //$NON-NLS-1$
+ props.put(InventoryPrinter.FORMAT,
+ new String[] { Format.TEXT.toString(), Format.JSON.toString() });
+
+ printerRegistration = context.registerService(InventoryPrinter.SERVICE,
+ new ConfigurationPrinterImpl(tracker), props);
}
return context.getService(reference);
diff --git a/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ConfigurationPrinterImpl.java b/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ConfigurationPrinterImpl.java
index 05b65c5..51076d8 100644
--- a/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ConfigurationPrinterImpl.java
+++ b/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ConfigurationPrinterImpl.java
@@ -21,8 +21,12 @@
import java.util.Iterator;
import java.util.TreeMap;
-import org.apache.felix.webconsole.ConfigurationPrinter;
+import org.apache.felix.inventory.Format;
+import org.apache.felix.inventory.InventoryPrinter;
import org.apache.felix.webconsole.WebConsoleUtil;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.service.upnp.UPnPAction;
@@ -35,7 +39,7 @@
* Prints the available UPnP devices
*
*/
-class ConfigurationPrinterImpl implements ConfigurationPrinter, Constants
+class ConfigurationPrinterImpl implements InventoryPrinter, Constants
{
private final ServiceTracker tracker;
@@ -46,18 +50,12 @@
}
/**
- * @see org.apache.felix.webconsole.ConfigurationPrinter#getTitle()
+ * @see org.apache.felix.inventory.InventoryPrinter
+ * #print(java.io.PrintWriter, org.apache.felix.inventory.Format, boolean)
*/
- public String getTitle()
+ public void print(PrintWriter pw, Format format, boolean isZip)
{
- return "UPnP Devices"; //$NON-NLS-1$
- }
- /**
- * @see org.apache.felix.webconsole.ConfigurationPrinter#printConfiguration(java.io.PrintWriter)
- */
- public void printConfiguration(PrintWriter pw)
- {
TreeMap componentMap = new TreeMap();
ServiceReference[] refs = tracker.getServiceReferences();
@@ -67,10 +65,51 @@
if (null != ref.getProperty(UPnPDevice.UDN)) // make sure device is valid
{
// order components by friendly name
- componentMap.put(nameOf(ref).toString() + ref.getProperty(SERVICE_ID), ref);
+ componentMap.put(nameOf(ref).toString() + ref.getProperty(SERVICE_ID),
+ ref);
}
}
+ if (Format.JSON.equals(format))
+ {
+ try
+ {
+ printJSON(componentMap, pw);
+ }
+ catch (JSONException e)
+ {
+ printText(componentMap, pw);
+ }
+ }
+ else
+ {
+ printText(componentMap, pw);
+ }
+ }
+
+ private void printJSON(TreeMap componentMap, PrintWriter pw) throws JSONException
+ {
+ final JSONObject ret = new JSONObject();
+ final JSONArray jDevices = new JSONArray();
+ ret.put("devices", jDevices); //$NON-NLS-1$
+
+ // render components
+ for (Iterator ci = componentMap.values().iterator(); ci.hasNext();)
+ {
+ final ServiceReference ref = (ServiceReference) ci.next();
+ final UPnPDevice device = (UPnPDevice) tracker.getService(ref);
+ if (device != null)
+ {
+ jDevices.put(Serializer.deviceToJSON(ref, device));
+ }
+ }
+
+ ret.write(pw);
+ }
+
+ private void printText(TreeMap componentMap, PrintWriter pw)
+ {
+
if (componentMap.isEmpty())
{
pw.println("Status: No UPnP devices found");
@@ -201,4 +240,6 @@
pw.println();
}
+
+
}