Merge changes Id40d25d3,I12263a65
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / utils / LOGJSONObject.java
index dec3cc1..1140a1c 100644 (file)
@@ -23,6 +23,8 @@ package org.onap.dmaap.datarouter.provisioning.utils;
  * *
  ******************************************************************************/
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
@@ -104,6 +106,8 @@ public class LOGJSONObject {
      */
     private static Map<String, Object> keyPool = new LinkedHashMap<String, Object>(keyPoolSize);
 
+    private static final EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
+
     /**
      * JSONObject.NULL is equivalent to the value that JavaScript calls null,
      * whilst Java's null is equivalent to the value that JavaScript calls
@@ -182,13 +186,11 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * The map where the JSONObject's properties are kept.
      */
     private final Map<String, Object> map;
 
-
     /**
      * It is sometimes more convenient and less ambiguous to have a
      * <code>NULL</code> object than to use Java's <code>null</code> value.
@@ -197,15 +199,13 @@ public class LOGJSONObject {
      */
     public static final Object NULL = new Null();
 
-
     /**
      * Construct an empty JSONObject.
      */
     public LOGJSONObject() {
-        this.map = new LinkedHashMap<String, Object>();
+        this.map = new LinkedHashMap<>();
     }
 
-
     /**
      * Construct a JSONObject from a subset of another JSONObject.
      * An array of strings is used to identify the keys that should be copied.
@@ -213,8 +213,6 @@ public class LOGJSONObject {
      *
      * @param jo    A JSONObject.
      * @param names An array of strings.
-     * @throws JSONException
-     * @throws JSONException If a value is a non-finite number or if a name is duplicated.
      */
     public LOGJSONObject(LOGJSONObject jo, String[] names) {
         this();
@@ -222,11 +220,11 @@ public class LOGJSONObject {
             try {
                 this.putOnce(names[i], jo.opt(names[i]));
             } catch (Exception ignore) {
+                intlogger.error("PROV0001 LOGJSONObject: " + ignore.getMessage(), ignore);
             }
         }
     }
 
-
     /**
      * Construct a JSONObject from a JSONTokener.
      *
@@ -234,7 +232,7 @@ public class LOGJSONObject {
      * @throws JSONException If there is a syntax error in the source string
      *                       or a duplicated key.
      */
-    public LOGJSONObject(JSONTokener x) throws JSONException {
+    public LOGJSONObject(JSONTokener x) {
         this();
         char c;
         String key;
@@ -284,7 +282,6 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * Construct a JSONObject from a Map.
      *
@@ -306,7 +303,6 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * Construct a JSONObject from an Object using bean getters.
      * It reflects on all of the public methods of the object.
@@ -331,7 +327,6 @@ public class LOGJSONObject {
         this.populateMap(bean);
     }
 
-
     /**
      * Construct a JSONObject from an Object, using reflection to find the
      * public members. The resulting JSONObject's keys will be the strings
@@ -356,7 +351,6 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * Construct a JSONObject from a source JSON text string.
      * This is the most commonly used JSONObject constructor.
@@ -371,7 +365,6 @@ public class LOGJSONObject {
         this(new JSONTokener(source));
     }
 
-
     /**
      * Construct a JSONObject from a ResourceBundle.
      *
@@ -412,7 +405,6 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * Accumulate values under a key. It is similar to the put method except
      * that if there is already an object stored under the key then a
@@ -448,7 +440,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Append values to the array under a key. If the key does not exist in the
      * JSONObject, then the key is put in the JSONObject with its value being a
@@ -475,7 +466,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Produce a string from a double. The string "null" will be returned if
      * the number is not finite.
@@ -503,7 +493,6 @@ public class LOGJSONObject {
         return string;
     }
 
-
     /**
      * Get the value object associated with a key.
      *
@@ -523,7 +512,6 @@ public class LOGJSONObject {
         return object;
     }
 
-
     /**
      * Get the boolean value associated with a key.
      *
@@ -546,7 +534,6 @@ public class LOGJSONObject {
                 "] is not a Boolean.");
     }
 
-
     /**
      * Get the double value associated with a key.
      *
@@ -555,19 +542,18 @@ public class LOGJSONObject {
      * @throws JSONException if the key is not found or
      *                       if the value is not a Number object and cannot be converted to a number.
      */
-    public double getDouble(String key) throws JSONException {
+    public double getDouble(String key) {
         Object object = this.get(key);
         try {
             return object instanceof Number
                     ? ((Number) object).doubleValue()
                     : Double.parseDouble((String) object);
         } catch (Exception e) {
-            throw new JSONException("JSONObject[" + quote(key) +
-                    "] is not a number.");
+            intlogger.error("JSONObject[" + quote(key) + "] is not a number.", e);
+            throw new JSONException("JSONObject[" + quote(key) + "] is not a number.");
         }
     }
 
-
     /**
      * Get the int value associated with a key.
      *
@@ -576,19 +562,18 @@ public class LOGJSONObject {
      * @throws JSONException if the key is not found or if the value cannot
      *                       be converted to an integer.
      */
-    public int getInt(String key) throws JSONException {
+    public int getInt(String key) {
         Object object = this.get(key);
         try {
             return object instanceof Number
                     ? ((Number) object).intValue()
                     : Integer.parseInt((String) object);
         } catch (Exception e) {
-            throw new JSONException("JSONObject[" + quote(key) +
-                    "] is not an int.");
+            intlogger.error("JSONObject[" + quote(key) + "] is not an int.", e);
+            throw new JSONException("JSONObject[" + quote(key) + "] is not an int.");
         }
     }
 
-
     /**
      * Get the JSONArray value associated with a key.
      *
@@ -606,7 +591,6 @@ public class LOGJSONObject {
                 "] is not a JSONArray.");
     }
 
-
     /**
      * Get the JSONObject value associated with a key.
      *
@@ -624,7 +608,6 @@ public class LOGJSONObject {
                 "] is not a JSONObject.");
     }
 
-
     /**
      * Get the long value associated with a key.
      *
@@ -640,12 +623,11 @@ public class LOGJSONObject {
                     ? ((Number) object).longValue()
                     : Long.parseLong((String) object);
         } catch (Exception e) {
-            throw new JSONException("JSONObject[" + quote(key) +
-                    "] is not a long.");
+            intlogger.error("JSONObject[" + quote(key) + "] is not a long.", e);
+            throw new JSONException("JSONObject[" + quote(key) + "] is not a long.");
         }
     }
 
-
     /**
      * Get an array of field names from a JSONObject.
      *
@@ -666,30 +648,6 @@ public class LOGJSONObject {
         return names;
     }
 
-
-    /**
-     * Get an array of field names from an Object.
-     *
-     * @return An array of field names, or null if there are no names.
-     */
-    public static String[] getNames(Object object) {
-        if (object == null) {
-            return null;
-        }
-        Class<? extends Object> klass = object.getClass();
-        Field[] fields = klass.getFields();
-        int length = fields.length;
-        if (length == 0) {
-            return null;
-        }
-        String[] names = new String[length];
-        for (int i = 0; i < length; i += 1) {
-            names[i] = fields[i].getName();
-        }
-        return names;
-    }
-
-
     /**
      * Get the string associated with a key.
      *
@@ -697,7 +655,7 @@ public class LOGJSONObject {
      * @return A string which is the value.
      * @throws JSONException if there is no string value for the key.
      */
-    public String getString(String key) throws JSONException {
+    public String getString(String key) {
         Object object = this.get(key);
         if (object instanceof String) {
             return (String) object;
@@ -706,7 +664,6 @@ public class LOGJSONObject {
                 "] not a string.");
     }
 
-
     /**
      * Determine if the JSONObject contains a specific key.
      *
@@ -717,7 +674,6 @@ public class LOGJSONObject {
         return this.map.containsKey(key);
     }
 
-
     /**
      * Increment a property of a JSONObject. If there is no such property,
      * create one with a value of 1. If there is such a property, and if
@@ -728,7 +684,7 @@ public class LOGJSONObject {
      * @throws JSONException If there is already a property with this name
      *                       that is not an Integer, Long, Double, or Float.
      */
-    public LOGJSONObject increment(String key) throws JSONException {
+    public LOGJSONObject increment(String key) {
         Object value = this.opt(key);
         if (value == null) {
             this.put(key, 1);
@@ -746,20 +702,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
-    /**
-     * Determine if the value associated with the key is null or if there is
-     * no value.
-     *
-     * @param key A key string.
-     * @return true if there is no value associated with the key or if
-     * the value is the JSONObject.NULL object.
-     */
-    public boolean isNull(String key) {
-        return LOGJSONObject.NULL.equals(this.opt(key));
-    }
-
-
     /**
      * Get an enumeration of the keys of the JSONObject.
      *
@@ -769,7 +711,6 @@ public class LOGJSONObject {
         return this.keySet().iterator();
     }
 
-
     /**
      * Get a set of keys of the JSONObject.
      *
@@ -779,7 +720,6 @@ public class LOGJSONObject {
         return this.map.keySet();
     }
 
-
     /**
      * Get the number of keys stored in the JSONObject.
      *
@@ -789,7 +729,6 @@ public class LOGJSONObject {
         return this.map.size();
     }
 
-
     /**
      * Produce a JSONArray containing the names of the elements of this
      * JSONObject.
@@ -835,7 +774,6 @@ public class LOGJSONObject {
         return string;
     }
 
-
     /**
      * Get an optional value associated with a key.
      *
@@ -846,20 +784,6 @@ public class LOGJSONObject {
         return key == null ? null : this.map.get(key);
     }
 
-
-    /**
-     * Get an optional boolean associated with a key.
-     * It returns false if there is no such key, or if the value is not
-     * Boolean.TRUE or the String "true".
-     *
-     * @param key A key string.
-     * @return The truth.
-     */
-    public boolean optBoolean(String key) {
-        return this.optBoolean(key, false);
-    }
-
-
     /**
      * Get an optional boolean associated with a key.
      * It returns the defaultValue if there is no such key, or if it is not
@@ -873,25 +797,11 @@ public class LOGJSONObject {
         try {
             return this.getBoolean(key);
         } catch (Exception e) {
+            intlogger.trace("Using defaultValue: " + defaultValue, e);
             return defaultValue;
         }
     }
 
-
-    /**
-     * Get an optional double associated with a key,
-     * or NaN if there is no such key or if its value is not a number.
-     * If the value is a string, an attempt will be made to evaluate it as
-     * a number.
-     *
-     * @param key A string which is the key.
-     * @return An object which is the value.
-     */
-    public double optDouble(String key) {
-        return this.optDouble(key, Double.NaN);
-    }
-
-
     /**
      * Get an optional double associated with a key, or the
      * defaultValue if there is no such key or if its value is not a number.
@@ -906,25 +816,11 @@ public class LOGJSONObject {
         try {
             return this.getDouble(key);
         } catch (Exception e) {
+            intlogger.trace("Using defaultValue: " + defaultValue, e);
             return defaultValue;
         }
     }
 
-
-    /**
-     * Get an optional int value associated with a key,
-     * or zero if there is no such key or if the value is not a number.
-     * If the value is a string, an attempt will be made to evaluate it as
-     * a number.
-     *
-     * @param key A key string.
-     * @return An object which is the value.
-     */
-    public int optInt(String key) {
-        return this.optInt(key, 0);
-    }
-
-
     /**
      * Get an optional int value associated with a key,
      * or the default if there is no such key or if the value is not a number.
@@ -939,25 +835,11 @@ public class LOGJSONObject {
         try {
             return this.getInt(key);
         } catch (Exception e) {
+            intlogger.trace("Using defaultValue: " + defaultValue, e);
             return defaultValue;
         }
     }
 
-
-    /**
-     * Get an optional JSONArray associated with a key.
-     * It returns null if there is no such key, or if its value is not a
-     * JSONArray.
-     *
-     * @param key A key string.
-     * @return A JSONArray which is the value.
-     */
-    public JSONArray optJSONArray(String key) {
-        Object o = this.opt(key);
-        return o instanceof JSONArray ? (JSONArray) o : null;
-    }
-
-
     /**
      * Get an optional JSONObject associated with a key.
      * It returns null if there is no such key, or if its value is not a
@@ -971,21 +853,6 @@ public class LOGJSONObject {
         return object instanceof LOGJSONObject ? (LOGJSONObject) object : null;
     }
 
-
-    /**
-     * Get an optional long value associated with a key,
-     * or zero if there is no such key or if the value is not a number.
-     * If the value is a string, an attempt will be made to evaluate it as
-     * a number.
-     *
-     * @param key A key string.
-     * @return An object which is the value.
-     */
-    public long optLong(String key) {
-        return this.optLong(key, 0);
-    }
-
-
     /**
      * Get an optional long value associated with a key,
      * or the default if there is no such key or if the value is not a number.
@@ -1004,20 +871,6 @@ public class LOGJSONObject {
         }
     }
 
-
-    /**
-     * Get an optional string associated with a key.
-     * It returns an empty string if there is no such key. If the value is not
-     * a string and is not null, then it is converted to a string.
-     *
-     * @param key A key string.
-     * @return A string which is the value.
-     */
-    public String optString(String key) {
-        return this.optString(key, "");
-    }
-
-
     /**
      * Get an optional string associated with a key.
      * It returns the defaultValue if there is no such key.
@@ -1031,7 +884,6 @@ public class LOGJSONObject {
         return NULL.equals(object) ? defaultValue : object.toString();
     }
 
-
     private void populateMap(Object bean) {
         Class<? extends Object> klass = bean.getClass();
 
@@ -1075,11 +927,11 @@ public class LOGJSONObject {
                     }
                 }
             } catch (Exception ignore) {
+                intlogger.trace("populateMap: " + ignore.getMessage(), ignore);
             }
         }
     }
 
-
     /**
      * Put a key/boolean pair in the JSONObject.
      *
@@ -1093,7 +945,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/value pair in the JSONObject, where the value will be a
      * JSONArray which is produced from a Collection.
@@ -1108,7 +959,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/double pair in the JSONObject.
      *
@@ -1122,7 +972,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/int pair in the JSONObject.
      *
@@ -1136,7 +985,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/long pair in the JSONObject.
      *
@@ -1150,7 +998,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/value pair in the JSONObject, where the value will be a
      * JSONObject which is produced from a Map.
@@ -1165,7 +1012,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/value pair in the JSONObject. If the value is null,
      * then the key will be removed from the JSONObject if it is present.
@@ -1201,7 +1047,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/value pair in the JSONObject, but only if the key and the
      * value are both non-null, and only if there is not already a member
@@ -1222,7 +1067,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/value pair in the JSONObject, but only if the
      * key and the value are both non-null.
@@ -1241,7 +1085,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Produce a string in double quotes with backslash sequences in all the
      * right places. A backslash will be inserted within </, producing <\/,
@@ -1256,8 +1099,8 @@ public class LOGJSONObject {
         synchronized (sw.getBuffer()) {
             try {
                 return quote(string, sw).toString();
-            } catch (IOException ignored) {
-                // will never happen - we are writing to a string writer
+            } catch (IOException e) {
+                intlogger.trace("Ignore Exception message: ", e);
                 return "";
             }
         }
@@ -1380,20 +1223,20 @@ public class LOGJSONObject {
                         return myLong;
                     }
                 }
-            } catch (Exception ignore) {
+            } catch (Exception e) {
+                intlogger.trace("Ignore Exception message: ", e);
             }
         }
         return string;
     }
 
-
     /**
      * Throw an exception if the object is a NaN or infinite number.
      *
      * @param o The object to test.
      * @throws JSONException If o is a non-finite number.
      */
-    public static void testValidity(Object o) throws JSONException {
+    public static void testValidity(Object o) {
         if (o != null) {
             if (o instanceof Double) {
                 if (((Double) o).isInfinite() || ((Double) o).isNaN()) {
@@ -1409,7 +1252,6 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * Produce a JSONArray containing the values of the members of this
      * JSONObject.
@@ -1446,11 +1288,11 @@ public class LOGJSONObject {
         try {
             return this.toString(0);
         } catch (Exception e) {
-            return null;
+            intlogger.trace("Exception: ", e);
+            return "";
         }
     }
 
-
     /**
      * Make a prettyprinted JSON text of this JSONObject.
      * <p>
@@ -1495,7 +1337,7 @@ public class LOGJSONObject {
      */
     @SuppressWarnings("unchecked")
     public static String valueToString(Object value) throws JSONException {
-        if (value == null || value.equals(null)) {
+        if (value == null) {
             return "null";
         }
         if (value instanceof JSONString) {
@@ -1579,29 +1421,15 @@ public class LOGJSONObject {
             }
             return new LOGJSONObject(object);
         } catch (Exception exception) {
+            intlogger.trace("Exception: ", exception);
             return null;
         }
     }
 
-
-    /**
-     * Write the contents of the JSONObject as JSON text to a writer.
-     * For compactness, no whitespace is added.
-     * <p>
-     * Warning: This method assumes that the data structure is acyclical.
-     *
-     * @return The writer.
-     * @throws JSONException
-     */
-    public Writer write(Writer writer) throws JSONException {
-        return this.write(writer, 0, 0);
-    }
-
-
     @SuppressWarnings("unchecked")
     static final Writer writeValue(Writer writer, Object value,
                                    int indentFactor, int indent) throws JSONException, IOException {
-        if (value == null || value.equals(null)) {
+        if (value == null) {
             writer.write("null");
         } else if (value instanceof LOGJSONObject) {
             ((LOGJSONObject) value).write(writer, indentFactor, indent);