You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2017/02/13 19:22:25 UTC

svn commit: r1782837 - in /felix/trunk/webconsole-plugins/upnp: ./ src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/

Author: cziegeler
Date: Mon Feb 13 19:22:25 2017
New Revision: 1782837

URL: http://svn.apache.org/viewvc?rev=1782837&view=rev
Log:
FELIX-5510 : Remove usage of org.json form web console plugins

Modified:
    felix/trunk/webconsole-plugins/upnp/changelog.txt
    felix/trunk/webconsole-plugins/upnp/pom.xml
    felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ConfigurationPrinterImpl.java
    felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ControlServlet.java
    felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/Serializer.java

Modified: felix/trunk/webconsole-plugins/upnp/changelog.txt
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/upnp/changelog.txt?rev=1782837&r1=1782836&r2=1782837&view=diff
==============================================================================
--- felix/trunk/webconsole-plugins/upnp/changelog.txt (original)
+++ felix/trunk/webconsole-plugins/upnp/changelog.txt Mon Feb 13 19:22:25 2017
@@ -1,3 +1,9 @@
+Changes from 1.0.6 to 1.0.8
+---------------------------
+** Improvement
+    * [FELIX-5510] - Remove usage of org.json form web console plugins
+
+
 Changes from 1.0.4 to 1.0.6
 ---------------------------
 ** Bug

Modified: felix/trunk/webconsole-plugins/upnp/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/upnp/pom.xml?rev=1782837&r1=1782836&r2=1782837&view=diff
==============================================================================
--- felix/trunk/webconsole-plugins/upnp/pom.xml (original)
+++ felix/trunk/webconsole-plugins/upnp/pom.xml Mon Feb 13 19:22:25 2017
@@ -14,7 +14,7 @@
 	<parent>
 		<groupId>org.apache.felix</groupId>
 		<artifactId>felix-parent</artifactId>
-		<version>2.1</version>
+		<version>4</version>
 		<relativePath>../../../pom/pom.xml</relativePath>
 	</parent>
 
@@ -64,7 +64,7 @@
 			<plugin>
 				<groupId>org.apache.felix</groupId>
 				<artifactId>maven-bundle-plugin</artifactId>
-				<version>2.3.4</version>
+				<version>3.2.0</version>
 				<extensions>true</extensions>
 				<configuration>
 					<instructions>
@@ -78,6 +78,9 @@
 							org.apache.felix.webconsole.plugins.upnp.internal.Activator
                         </Bundle-Activator>
                         <Include-Resource>{maven-resources},OSGI-INF=target/classes/OSGI-INF</Include-Resource>
+                        <Embed-Dependency>
+                             org.apache.felix.utils;inline=org/apache/felix/utils/json/JSONWriter**
+                        </Embed-Dependency>
 					</instructions>
 				</configuration>
 			</plugin>
@@ -126,18 +129,17 @@
 			<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.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>
-			<version>20070829</version>
-			<scope>compile</scope>
-			<optional>true</optional>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.utils</artifactId>
+			<version>1.9.0</version>
+			<scope>provided</scope>
 		</dependency>
 	</dependencies>
 </project>

Modified: felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ConfigurationPrinterImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ConfigurationPrinterImpl.java?rev=1782837&r1=1782836&r2=1782837&view=diff
==============================================================================
--- felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ConfigurationPrinterImpl.java (original)
+++ felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ConfigurationPrinterImpl.java Mon Feb 13 19:22:25 2017
@@ -16,6 +16,7 @@
  */
 package org.apache.felix.webconsole.plugins.upnp.internal;
 
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Arrays;
 import java.util.Iterator;
@@ -23,10 +24,8 @@ import java.util.TreeMap;
 
 import org.apache.felix.inventory.Format;
 import org.apache.felix.inventory.InventoryPrinter;
+import org.apache.felix.utils.json.JSONWriter;
 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;
@@ -76,7 +75,7 @@ class ConfigurationPrinterImpl implement
             {
                 printJSON(componentMap, pw);
             }
-            catch (JSONException e)
+            catch (IOException e)
             {
                 printText(componentMap, pw);
             }
@@ -87,11 +86,13 @@ class ConfigurationPrinterImpl implement
         }
     }
 
-    private void printJSON(TreeMap componentMap, PrintWriter pw) throws JSONException
+    private void printJSON(TreeMap componentMap, PrintWriter pw) throws IOException
     {
-        final JSONObject ret = new JSONObject();
-        final JSONArray jDevices = new JSONArray();
-        ret.put("devices", jDevices); //$NON-NLS-1$
+        final JSONWriter writer = new JSONWriter(pw);
+        writer.object();
+
+        writer.key("devices");
+        writer.array();
 
         // render components
         for (Iterator ci = componentMap.values().iterator(); ci.hasNext();)
@@ -100,13 +101,15 @@ class ConfigurationPrinterImpl implement
             final UPnPDevice device = (UPnPDevice) tracker.getService(ref);
             if (device != null)
             {
-                jDevices.put(Serializer.deviceToJSON(ref, device));
+                writer.value(Serializer.deviceToJSON(ref, device));
             }
         }
+        writer.endArray();
 
-        ret.write(pw);
+        writer.endObject();
+        writer.flush();
     }
-    
+
     private void printText(TreeMap componentMap, PrintWriter pw)
     {
 
@@ -208,7 +211,7 @@ class ConfigurationPrinterImpl implement
             {
                 print(pw, vars[i]);
             }
-        } 
+        }
     }
 
     private void print(PrintWriter pw, UPnPStateVariable var)
@@ -240,6 +243,6 @@ class ConfigurationPrinterImpl implement
         pw.println();
     }
 
-    
+
 
 }

Modified: felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ControlServlet.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ControlServlet.java?rev=1782837&r1=1782836&r2=1782837&view=diff
==============================================================================
--- felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ControlServlet.java (original)
+++ felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/ControlServlet.java Mon Feb 13 19:22:25 2017
@@ -21,6 +21,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.lang.reflect.Constructor;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.Dictionary;
@@ -28,6 +29,7 @@ import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.servlet.ServletException;
@@ -35,8 +37,7 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.json.JSONException;
-import org.json.JSONObject;
+import org.apache.felix.utils.json.JSONWriter;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.log.LogService;
@@ -52,7 +53,7 @@ import org.osgi.util.tracker.ServiceTrac
  * the WebConsolePlugin just to improve readability. This servlet actually
  * is not registered in HTTP service.
  */
-public final class ControlServlet extends HttpServlet 
+public final class ControlServlet extends HttpServlet
 {
 
     private static final long serialVersionUID = -5789642544511401813L;
@@ -65,7 +66,7 @@ public final class ControlServlet extend
     // holds lock for the devices & icons cache above
     private final Object cacheLock = new Object();
 
-    private final Map/*<String,SessionObject>*/ sessions = Collections.synchronizedMap(new HashMap(10)); 
+    private final Map/*<String,SessionObject>*/ sessions = Collections.synchronizedMap(new HashMap(10));
 
     private final ServiceTracker tracker;
     private final BundleContext bc;
@@ -76,6 +77,7 @@ public final class ControlServlet extend
      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
      *      javax.servlet.http.HttpServletResponse)
      */
+    @Override
     protected final void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
     {
@@ -130,12 +132,13 @@ public final class ControlServlet extend
      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
      *      javax.servlet.http.HttpServletResponse)
      */
+    @Override
     protected final void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
     {
         try
         {
-            JSONObject json = new JSONObject();
+            Map<String, Object> json = new HashMap<String, Object>();
 
             String method = request.getParameter("action"); //$NON-NLS-1$
 
@@ -150,10 +153,17 @@ public final class ControlServlet extend
                     if (refs[i] != null
                         && refs[i].getProperty(UPnPDevice.PARENT_UDN) == null)
                     {
-                        JSONObject deviceJSON = deviceTreeToJSON(refs[i]);
+                        Map<String, Object> deviceJSON = deviceTreeToJSON(refs[i]);
                         if (null != deviceJSON)
                         {
-                            json.append("devices", deviceJSON); //$NON-NLS-1$
+                            @SuppressWarnings("unchecked")
+                            List<Object> list = (List<Object>) json.get("devices"); //$NON-NLS-1$
+                            if ( list == null )
+                            {
+                                list = new ArrayList<Object>();
+                                json.put("devices", list); //$NON-NLS-1$
+                            }
+                            list.add(deviceJSON);
                         }
                     }
                 }
@@ -191,7 +201,9 @@ public final class ControlServlet extend
 
             response.setContentType("application/json"); //$NON-NLS-1$
             response.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
-            response.getWriter().print(json.toString(2));
+            final JSONWriter writer = new JSONWriter(response.getWriter());
+            writer.value(json);
+            writer.flush();
         }
         catch (ServletException e)
         {
@@ -240,7 +252,7 @@ public final class ControlServlet extend
         return getService(device, serviceUrn);
     }
 
-    private final JSONObject deviceTreeToJSON(ServiceReference ref) throws JSONException
+    private final Map<String, Object> deviceTreeToJSON(ServiceReference ref)
     {
         final UPnPDevice device = (UPnPDevice) tracker.getService(ref);
         if (null == device)
@@ -256,40 +268,45 @@ public final class ControlServlet extend
             return null;
         }
 
-        final JSONObject json = Serializer.deviceToJSON(ref, device);
+        final Map<String, Object> json = Serializer.deviceToJSON(ref, device);
 
         // add child devices
         final Object[] refs = tracker.getServiceReferences();
-        for (int i = 0; refs != null && i < refs.length; i++)
+        if ( refs != null )
         {
-            ref = (ServiceReference) refs[i];
-
-            final Object parent = ref.getProperty(UPnPDevice.PARENT_UDN);
-            final Object currentUDN = ref.getProperty(UPnPDevice.UDN);
-            if (parent == null)
-            { // no parent
-                continue;
-            }
-            else if (currentUDN != null && currentUDN.equals(parent))
-            { // self ?
-                continue;
-            }
-            else if (parentUdn.equals(parent))
+            List<Object> children = new ArrayList<Object>();
+            json.put("children", children); //$NON-NLS-1$
+            for (int i = 0; i < refs.length; i++)
             {
-                JSONObject deviceJSON = deviceTreeToJSON(ref);
-                if (null != deviceJSON)
+                ref = (ServiceReference) refs[i];
+
+                final Object parent = ref.getProperty(UPnPDevice.PARENT_UDN);
+                final Object currentUDN = ref.getProperty(UPnPDevice.UDN);
+                if (parent == null)
+                { // no parent
+                    continue;
+                }
+                else if (currentUDN != null && currentUDN.equals(parent))
+                { // self ?
+                    continue;
+                }
+                else if (parentUdn.equals(parent))
                 {
-                    json.append("children", deviceJSON); //$NON-NLS-1$
+                    Map<String, Object> deviceJSON = deviceTreeToJSON(ref);
+                    if (null != deviceJSON)
+                    {
+                        children.add(deviceJSON);
+                    }
                 }
             }
         }
         return json;
     }
 
-    private static final JSONObject invoke(UPnPAction action, String[] names,
+    private static final Map<String, Object> invoke(UPnPAction action, String[] names,
         String[] vals) throws Exception
     {
-        final JSONObject json = new JSONObject();
+        final Map<String, Object> json = new HashMap<String, Object>();
 
         // check input arguments
         Hashtable inputArgs = null;
@@ -335,6 +352,8 @@ public final class ControlServlet extend
         // prepare output arguments
         if (out != null && out.size() > 0)
         {
+            final Object[] outputs = new Object[out.size()];
+            int index = 0;
             for (Enumeration e = out.keys(); e.hasMoreElements();)
             {
                 final String key = (String) e.nextElement();
@@ -353,11 +372,15 @@ public final class ControlServlet extend
                     value = Hex.encode((byte[]) value);
                 }
 
-                json.append("output", new JSONObject() // //$NON-NLS-1$
-                .put("name", key)// //$NON-NLS-1$
-                .put("type", var.getUPnPDataType()) // //$NON-NLS-1$
-                .put("value", value)); //$NON-NLS-1$
+                final Map<String, Object> output = new HashMap<String, Object>();
+                output.put("name", key); // //$NON-NLS-1$
+                output.put("type", var.getUPnPDataType()); // //$NON-NLS-1$
+                output.put("value", value); //$NON-NLS-1$
+
+                outputs[index] = output;
+                index++;
             }
+            json.put("output", outputs); // //$NON-NLS-1$
         }
         return json;
     }
@@ -431,7 +454,7 @@ public final class ControlServlet extend
 
     /**
      * Creates new XML-RPC handler.
-     * 
+     *
      * @param bc the bundle context
      * @param iconServlet the icon servlet.
      */
@@ -451,7 +474,7 @@ public final class ControlServlet extend
         {
           icons.clear();
         }
-        synchronized (sessions) 
+        synchronized (sessions)
         {
           for (Iterator i = sessions.values().iterator(); i.hasNext();)
           {

Modified: felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/Serializer.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/Serializer.java?rev=1782837&r1=1782836&r2=1782837&view=diff
==============================================================================
--- felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/Serializer.java (original)
+++ felix/trunk/webconsole-plugins/upnp/src/main/java/org/apache/felix/webconsole/plugins/upnp/internal/Serializer.java Mon Feb 13 19:22:25 2017
@@ -16,8 +16,9 @@
  */
 package org.apache.felix.webconsole.plugins.upnp.internal;
 
-import org.json.JSONException;
-import org.json.JSONObject;
+import java.util.HashMap;
+import java.util.Map;
+
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.upnp.UPnPAction;
 import org.osgi.service.upnp.UPnPDevice;
@@ -33,15 +34,14 @@ class Serializer
         // prevent instantiation
     }
 
-    static final JSONObject deviceToJSON(ServiceReference ref, UPnPDevice device)
-        throws JSONException
+    static final Map<String, Object> deviceToJSON(ServiceReference ref, UPnPDevice device)
     {
-        final JSONObject json = new JSONObject();
+        final Map<String, Object> json = new HashMap<String, Object>();
         json.put("icon", device.getIcons(null) != null); //$NON-NLS-1$
 
         // add properties
         final String[] props = ref.getPropertyKeys();
-        final JSONObject _props = new JSONObject();
+        final Map<String, Object> _props = new HashMap<String, Object>();
         for (int i = 0; props != null && i < props.length; i++)
         {
             _props.put(props[i], ref.getProperty(props[i]));
@@ -49,18 +49,23 @@ class Serializer
         json.put("props", _props); //$NON-NLS-1$
 
         final UPnPService[] services = device.getServices();
-        for (int i = 0; services != null && i < services.length; i++)
+        if ( services != null )
         {
-            json.append("services", services[i].getType()); //$NON-NLS-1$
+            final String[] serviceTypes = new String[services.length];
+            for (int i = 0; i < services.length; i++)
+            {
+                serviceTypes[i] = services[i].getType();
+            }
+            json.put("services", serviceTypes); //$NON-NLS-1$
         }
 
         return json;
     }
 
-    static final JSONObject serviceToJSON(UPnPService service,
-        SessionObject session) throws JSONException
+    static final Map<String, Object> serviceToJSON(UPnPService service,
+        SessionObject session)
     {
-        final JSONObject json = new JSONObject();
+        final Map<String, Object> json = new HashMap<String, Object>();
 
         // add service properties
         json.put("type", service.getType()); //$NON-NLS-1$
@@ -68,56 +73,73 @@ class Serializer
 
         // add state variables
         final UPnPStateVariable[] vars = service.getStateVariables();
-        for (int i = 0; vars != null && i < vars.length; i++)
+        if ( vars != null )
         {
-            Object value = null;
-            if (vars[i] instanceof UPnPLocalStateVariable)
+            @SuppressWarnings("unchecked")
+            Map<String, Object>[] arr = new Map[vars.length];
+            for (int i = 0; vars != null && i < vars.length; i++)
             {
-                value = ((UPnPLocalStateVariable) vars[i]).getCurrentValue();
-            }
-
-            if (value == null)
-                value = session.getValue(vars[i].getName());
-            if (value == null)
-                value = "---"; //$NON-NLS-1$
+                Object value = null;
+                if (vars[i] instanceof UPnPLocalStateVariable)
+                {
+                    value = ((UPnPLocalStateVariable) vars[i]).getCurrentValue();
+                }
+
+                if (value == null)
+                    value = session.getValue(vars[i].getName());
+                if (value == null)
+                    value = "---"; //$NON-NLS-1$
 
-            json.append("variables", variableToJSON(vars[i], vars[i].getName()) //$NON-NLS-1$
-            .put("value", value));// //$NON-NLS-1$
+                arr[i] = variableToJSON(vars[i], vars[i].getName());
+                arr[i].put("value", value);// //$NON-NLS-1$
+            }
+            json.put("variables", arr); //$NON-NLS-1$
         }
 
         // add actions
         final UPnPAction[] actions = service.getActions();
-        for (int i = 0; actions != null && i < actions.length; i++)
+        if ( actions != null )
         {
-            json.append("actions", actionToJSON(actions[i])); //$NON-NLS-1$
+            Object[] arr = new Object[actions.length];
+            for (int i = 0; i < actions.length; i++)
+            {
+                arr[i] = actionToJSON(actions[i]);
+            }
+            json.put("actions", arr); //$NON-NLS-1$
         }
 
         return json;
     }
 
-    static final JSONObject variableToJSON(final UPnPStateVariable var,
-        final String name) throws JSONException
+    static final Map<String, Object> variableToJSON(final UPnPStateVariable var,
+        final String name)
     {
-        return new JSONObject()//
-        .put("name", name) // //$NON-NLS-1$
-        .put("default", var.getDefaultValue()) // //$NON-NLS-1$
-        .put("min", var.getMinimum()) //$NON-NLS-1$
-        .put("max", var.getMaximum()) //$NON-NLS-1$
-        .put("step", var.getStep()) //$NON-NLS-1$
-        .put("allowed", var.getAllowedValues()) //$NON-NLS-1$
-        .put("sendsEvents", var.sendsEvents()) //$NON-NLS-1$
-        .put("type", var.getUPnPDataType()); //$NON-NLS-1$
+        final Map<String, Object> json = new HashMap<String, Object>();
+        json.put("name", name); // //$NON-NLS-1$
+        json.put("default", var.getDefaultValue()); // //$NON-NLS-1$
+        json.put("min", var.getMinimum()); //$NON-NLS-1$
+        json.put("max", var.getMaximum()); //$NON-NLS-1$
+        json.put("step", var.getStep()); //$NON-NLS-1$
+        json.put("allowed", var.getAllowedValues()); //$NON-NLS-1$
+        json.put("sendsEvents", var.sendsEvents()); //$NON-NLS-1$
+        json.put("type", var.getUPnPDataType()); //$NON-NLS-1$
+        return json;
     }
 
-    static final JSONObject actionToJSON(UPnPAction action) throws JSONException
+    static final Map<String, Object> actionToJSON(UPnPAction action)
     {
-        final JSONObject json = new JSONObject();
+        final Map<String, Object> json = new HashMap<String, Object>();
         json.put("name", action.getName()); //$NON-NLS-1$
         final String[] names = action.getInputArgumentNames();
-        for (int i = 0; names != null && i < names.length; i++)
+        if ( names != null )
         {
-            final UPnPStateVariable variable = action.getStateVariable(names[i]);
-            json.append("inVars", variableToJSON(variable, names[i])); //$NON-NLS-1$
+            Object[] vars = new Object[names.length];
+            for (int i = 0; i < names.length; i++)
+            {
+                final UPnPStateVariable variable = action.getStateVariable(names[i]);
+                vars[i] = variableToJSON(variable, names[i]); //$NON-NLS-1$
+            }
+            json.put("inVars", vars); //$NON-NLS-1$
         }
 
         return json;