Fixed FELIX-3429 UPnP plugin should also include a configuration printer
https://issues.apache.org/jira/browse/FELIX-3429

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1309278 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole-plugins/upnp/pom.xml b/webconsole-plugins/upnp/pom.xml
index bb091a4..3187742 100644
--- a/webconsole-plugins/upnp/pom.xml
+++ b/webconsole-plugins/upnp/pom.xml
@@ -64,31 +64,19 @@
             <plugin>
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
-                <version>2.0.1</version>
+                <version>2.3.4</version>
                 <extensions>true</extensions>
                 <configuration>
                     <instructions>
                         <Bundle-SymbolicName>
-                            ${artifactId}
+                            ${project.artifactId}
                         </Bundle-SymbolicName>
-                        <Bundle-Vendor>
-                            The Apache Software Foundation
-                        </Bundle-Vendor>
-                        <Bundle-Activator>
-                            org.apache.felix.webconsole.plugins.upnp.internal.Activator
-                        </Bundle-Activator>
                         <Private-Package>
                             org.apache.felix.webconsole.plugins.upnp.*
                         </Private-Package>
-                        <Import-Package>
-							org.osgi.framework;
-							org.osgi.service.upnp;version="1.1",
-							org.osgi.util.tracker; version="1.0.0",
-							org.apache.felix.webconsole; version="3.0.0",
-							javax.servlet; version="2.4",
-							javax.servlet.http; version="2.4",
-							org.json						
-                        </Import-Package>
+                        <Bundle-Activator>
+                            org.apache.felix.webconsole.plugins.upnp.internal.Activator
+                        </Bundle-Activator>
                     </instructions>
                 </configuration>
             </plugin>
@@ -118,7 +106,7 @@
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.webconsole</artifactId>
-            <version>3.0.0</version>
+            <version>3.1.0</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
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 81e3145..65b5d4d 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,10 +16,12 @@
  */

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

 

+import org.apache.felix.webconsole.ConfigurationPrinter;

 import org.apache.felix.webconsole.SimpleWebConsolePlugin;

 import org.osgi.framework.BundleActivator;

 import org.osgi.framework.BundleContext;

 import org.osgi.framework.ServiceReference;

+import org.osgi.framework.ServiceRegistration;

 import org.osgi.service.upnp.UPnPDevice;

 import org.osgi.util.tracker.ServiceTracker;

 import org.osgi.util.tracker.ServiceTrackerCustomizer;

@@ -34,6 +36,7 @@
     private BundleContext context;

 

     private SimpleWebConsolePlugin plugin;

+    private ServiceRegistration printerRegistration;

 

     /**

      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)

@@ -75,7 +78,10 @@
         if (plugin == null)

         {

             this.plugin = plugin = new WebConsolePlugin(tracker).register(context);

+            printerRegistration = context.registerService(ConfigurationPrinter.SERVICE,

+                new ConfigurationPrinterImpl(tracker), null);

         }

+

         // delegate event

         ControlServlet controller = ((WebConsolePlugin) plugin).controller;

         if (controller != null)

@@ -88,7 +94,8 @@
      * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference,

      *      java.lang.Object)

      */

-    public final synchronized void removedService(ServiceReference reference, Object service)

+    public final synchronized void removedService(ServiceReference reference,

+        Object service)

     {

         SimpleWebConsolePlugin plugin = this.plugin;

 

@@ -103,6 +110,14 @@
         {

             plugin.unregister();

             this.plugin = null;

+

+            // unregister configuration printer too

+            ServiceRegistration reg = printerRegistration;

+            if (reg != null)

+            {

+                reg.unregister();

+                printerRegistration = null;

+            }

         }

 

     }

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
new file mode 100644
index 0000000..f2a5a9f
--- /dev/null
+++ b/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ConfigurationPrinterImpl.java
@@ -0,0 +1,204 @@
+/*

+ * 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.felix.webconsole.plugins.upnp.internal;

+

+import java.io.PrintWriter;

+import java.util.Arrays;

+import java.util.Iterator;

+import java.util.TreeMap;

+

+import org.apache.felix.webconsole.ConfigurationPrinter;

+import org.apache.felix.webconsole.WebConsoleUtil;

+import org.osgi.framework.Constants;

+import org.osgi.framework.ServiceReference;

+import org.osgi.service.upnp.UPnPAction;

+import org.osgi.service.upnp.UPnPDevice;

+import org.osgi.service.upnp.UPnPService;

+import org.osgi.service.upnp.UPnPStateVariable;

+import org.osgi.util.tracker.ServiceTracker;

+

+/**

+ * Prints the available UPnP devices

+ *

+ */

+class ConfigurationPrinterImpl implements ConfigurationPrinter, Constants

+{

+

+    private final ServiceTracker tracker;

+

+    ConfigurationPrinterImpl(ServiceTracker tracker)

+    {

+        this.tracker = tracker;

+    }

+

+    /**

+     * @see org.apache.felix.webconsole.ConfigurationPrinter#getTitle()

+     */

+    public String getTitle()

+    {

+        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();

+        for (int i = 0; refs != null && i < refs.length; i++)

+        {

+            ServiceReference ref = refs[i];

+            if (null != ref.getProperty(UPnPDevice.UDN)) // make sure device is valid

+            {

+                // order components by friendly name

+                componentMap.put(nameOf(ref), ref);

+            }

+        }

+

+        if (componentMap.isEmpty())

+        {

+            pw.println("Status: No UPnP devices found");

+            return;

+        }

+

+        pw.println("Status: " + componentMap.size() + " UPnP devices found");

+        pw.println();

+

+        // render components

+        for (Iterator ci = componentMap.values().iterator(); ci.hasNext();)

+        {

+            print(pw, (ServiceReference) ci.next());

+        }

+    }

+

+    private static final Object nameOf(ServiceReference ref)

+    {

+        Object name = ref.getProperty(UPnPDevice.FRIENDLY_NAME);

+        if (null == name)

+        {

+            name = ref.getProperty(UPnPDevice.UDN);

+        }

+        return name;

+    }

+

+    private void print(PrintWriter pw, ServiceReference ref)

+    {

+

+        pw.println(nameOf(ref));

+        pw.println("  Properties:");

+

+        String[] properties = ref.getPropertyKeys();

+        Arrays.sort(properties);

+        for (int i = 0; i < properties.length; i++)

+        {

+            String key = properties[i];

+            if (OBJECTCLASS.equals(key) || SERVICE_ID.equals(key))

+            {

+                continue;

+            }

+            Object val = ref.getProperty(key);

+            val = WebConsoleUtil.toString(val);

+            pw.print("    "); //$NON-NLS-1$

+            pw.print(key);

+            pw.print(" = "); //$NON-NLS-1$

+            pw.println(val);

+        }

+

+        UPnPDevice dev = (UPnPDevice) tracker.getService(ref);

+        if (null == dev)

+            return;

+

+        UPnPService services[] = dev.getServices();

+        if (null != services && services.length > 0)

+        {

+            pw.println("  Services:");

+            for (int i = 0; i < services.length; i++)

+            {

+                print(pw, services[i]);

+            }

+        }

+

+        pw.println();

+    }

+

+    private void print(PrintWriter pw, UPnPService serv)

+    {

+        pw.print("    Service: ");

+        pw.print(serv.getId());

+        pw.print(", ver ");

+        pw.print(serv.getVersion());

+        pw.print(" (type = ");

+        pw.print(serv.getType());

+        pw.println(')');

+        UPnPAction[] actions = serv.getActions();

+        if (actions != null && actions.length > 0)

+        {

+            pw.print("    Actions: ");

+            for (int i = 0; i < actions.length; i++)

+            {

+                pw.print(actions[i].getName());

+                if (i < actions.length - 1)

+                {

+                    pw.print(", ");

+                }

+            }

+            pw.println();

+        }

+

+        UPnPStateVariable[] vars = serv.getStateVariables();

+        if (vars != null && vars.length > 0)

+        {

+            pw.println("    State Vars:");

+            for (int i = 0; i < vars.length; i++)

+            {

+                print(pw, vars[i]);

+            }

+        } 

+    }

+

+    private void print(PrintWriter pw, UPnPStateVariable var)

+    {

+        pw.print("       ");

+        pw.print(var.getName());

+        pw.print(" (");

+        pw.print(var.getUPnPDataType());

+        pw.print(')');

+        if (var.sendsEvents())

+        {

+            pw.print(", sends events");

+        }

+        if (null != var.getMinimum())

+        {

+            pw.print(", min = ");

+            pw.print(var.getMinimum());

+        }

+        if (null != var.getMaximum())

+        {

+            pw.print(", max = ");

+            pw.print(var.getMaximum());

+        }

+        if (null != var.getStep())

+        {

+            pw.print(", step = ");

+            pw.print(var.getStep());

+        }

+        pw.println();

+    }

+

+}