Code style cleanup for prov authz and beans
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / NodeClass.java
old mode 100644 (file)
new mode 100755 (executable)
index e3dd852..ef491ca
@@ -3,13 +3,13 @@
  * * org.onap.dmaap\r
  * * ===========================================================================\r
  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
 * * ===========================================================================\r
  * * Licensed under the Apache License, Version 2.0 (the "License");\r
  * * you may not use this file except in compliance with the License.\r
  * * You may obtain a copy of the License at\r
- * * \r
+ * *\r
  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
+ * *\r
  *  * Unless required by applicable law or agreed to in writing, software\r
  * * distributed under the License is distributed on an "AS IS" BASIS,\r
  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
@@ -23,6 +23,8 @@
 \r
 package org.onap.dmaap.datarouter.provisioning.beans;\r
 \r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
 import java.sql.Connection;\r
 import java.sql.PreparedStatement;\r
 import java.sql.ResultSet;\r
@@ -32,8 +34,6 @@ import java.util.HashMap;
 import java.util.Map;\r
 import java.util.Set;\r
 import java.util.TreeSet;\r
-\r
-import org.apache.log4j.Logger;\r
 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
 \r
 /**\r
@@ -42,137 +42,150 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
  * @author Robert P. Eby\r
  * @version $Id: NodeClass.java,v 1.2 2014/01/15 16:08:43 eby Exp $\r
  */\r
+\r
 public abstract class NodeClass extends Syncable {\r
-       private static Map<String, Integer> map;\r
-\r
-       public NodeClass() {\r
-               // init on first use\r
-               if (map == null) {\r
-                       reload();\r
-               }\r
-       }\r
-\r
-       /**\r
-        * Add nodes to the NODES table, when the NODES parameter value is changed.\r
-        * Nodes are only added to the table, they are never deleted.  The node name is normalized\r
-        * to contain the domain (if missing).\r
-        * @param nodes a pipe separated list of the current nodes\r
-        */\r
-       public static void setNodes(String[] nodes) {\r
-               if (map == null)\r
-                       reload();\r
-               int nextid = 0;\r
-               for (Integer n : map.values()) {\r
-                       if (n >= nextid)\r
-                               nextid = n+1;\r
-               }\r
-               // take | separated list, add domain if needed.\r
-               Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
-               for (String node : nodes) {\r
-                       node = normalizeNodename(node);\r
-                       if (!map.containsKey(node)) {\r
-                               intlogger.info("..adding "+node+" to NODES with index "+nextid);\r
-                               map.put(node, nextid);\r
-                               PreparedStatement ps = null;\r
-                               try {\r
-                                       DB db = new DB();\r
-                                       @SuppressWarnings("resource")\r
-                                       Connection conn = db.getConnection();\r
-                                       ps = conn.prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)");\r
-                                       ps.setInt(1, nextid);\r
-                                       ps.setString(2, node);\r
-                                       ps.execute();\r
-                                       ps.close();\r
-                                       db.release(conn);\r
-                               } catch (SQLException e) {\r
-                                       intlogger.warn("PROV0005 doInsert: "+e.getMessage());\r
-                                       e.printStackTrace();\r
-                               } finally {\r
-                                       try {\r
-                                               ps.close();\r
-                                       } catch (SQLException e) {\r
-                                               e.printStackTrace();\r
-                                       }\r
-                               }\r
-                               nextid++;\r
-                       }\r
-               }\r
-       }\r
-\r
-       public static void reload() {\r
-               Map<String, Integer> m = new HashMap<String, Integer>();\r
-               PreparedStatement ps = null;\r
-               try {\r
-                       DB db = new DB();\r
-                       @SuppressWarnings("resource")\r
-                       Connection conn = db.getConnection();\r
-                       String sql = "select NODEID, NAME from NODES";\r
-                       ps = conn.prepareStatement(sql);\r
-                       ResultSet rs = ps.executeQuery();\r
-                       while (rs.next()) {\r
-                               int id = rs.getInt("NODEID");\r
-                               String name = rs.getString("NAME");\r
-                               m.put(name, id);\r
-                       }\r
-                       rs.close();\r
-                       ps.close();\r
-                       db.release(conn);\r
-               } catch (SQLException e) {\r
-                       e.printStackTrace();\r
-               } finally {\r
-                       try {\r
-                               ps.close();\r
-                       } catch (SQLException e) {\r
-                               e.printStackTrace();\r
-                       }\r
-               }\r
-               map = m;\r
-       }\r
-\r
-       public static Integer lookupNodeName(final String name) throws IllegalArgumentException {\r
-               Integer n = map.get(name);\r
-               if (n == null)\r
-                       throw new IllegalArgumentException("Invalid node name: "+name);\r
-               return n;\r
-       }\r
-\r
-       public static Collection<String> lookupNodeNames(String patt) throws IllegalArgumentException {\r
-               Collection<String> coll = new TreeSet<String>();\r
-               final Set<String> keyset = map.keySet();\r
-               for (String s : patt.toLowerCase().split(",")) {\r
-                       if (s.endsWith("*")) {\r
-                               s = s.substring(0, s.length()-1);\r
-                               for (String s2 : keyset) {\r
-                                       if (s2.startsWith(s))\r
-                                               coll.add(s2);\r
-                               }\r
-                       } else if (keyset.contains(s)) {\r
-                               coll.add(s);\r
-                       } else if (keyset.contains(normalizeNodename(s))) {\r
-                               coll.add(normalizeNodename(s));\r
-                       } else {\r
-                               throw new IllegalArgumentException("Invalid node name: "+s);\r
-                       }\r
-               }\r
-               return coll;\r
-       }\r
-\r
-       protected String lookupNodeID(int n) {\r
-               for (String s : map.keySet()) {\r
-                       if (map.get(s) == n)\r
-                               return s;\r
-               }\r
-               return null;\r
-       }\r
-\r
-       public static String normalizeNodename(String s) {\r
-               if (s != null && s.indexOf('.') <= 0) {\r
-                       Parameters p = Parameters.getParameter(Parameters.PROV_DOMAIN);\r
-                       if (p != null) {\r
-                               String domain = p.getValue();\r
-                               s += "." + domain;\r
-                       }\r
-               }\r
-               return s.toLowerCase();\r
-       }\r
+\r
+    private static final String PROV_0005_DO_INSERT = "PROV0005 doInsert: ";\r
+    private static Map<String, Integer> map;\r
+    private static EELFLogger intLogger = EELFManager.getInstance().getLogger("InternalLog");\r
+\r
+    NodeClass() {\r
+        // init on first use\r
+        if (map == null) {\r
+            reload();\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Add nodes to the NODES table, when the NODES parameter value is changed. Nodes are only added to the table, they\r
+     * are never deleted.  The node name is normalized to contain the domain (if missing).\r
+     *\r
+     * @param nodes a pipe separated list of the current nodes\r
+     */\r
+    public static void setNodes(String[] nodes) {\r
+        if (map == null) {\r
+            reload();\r
+        }\r
+        int nextid = 0;\r
+        for (Integer n : map.values()) {\r
+            if (n >= nextid) {\r
+                nextid = n + 1;\r
+            }\r
+        }\r
+        // take | separated list, add domain if needed.\r
+\r
+        for (String node : nodes) {\r
+            node = normalizeNodename(node);\r
+            if (!map.containsKey(node)) {\r
+                intLogger.info("..adding " + node + " to NODES with index " + nextid);\r
+                map.put(node, nextid);\r
+                insertNodesToTable(nextid, node);\r
+                nextid++;\r
+            }\r
+        }\r
+    }\r
+\r
+    private static void insertNodesToTable(int nextid, String node) {\r
+        DB db = new DB();\r
+        try (Connection conn = db.getConnection()) {\r
+            try (PreparedStatement ps = conn\r
+                    .prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)")) {\r
+                ps.setInt(1, nextid);\r
+                ps.setString(2, node);\r
+                ps.execute();\r
+            } finally {\r
+                db.release(conn);\r
+            }\r
+        } catch (SQLException e) {\r
+            intLogger.error(PROV_0005_DO_INSERT + e.getMessage(), e);\r
+        }\r
+    }\r
+\r
+    private static void reload() {\r
+        Map<String, Integer> hmap = new HashMap<>();\r
+        String sql = "select NODEID, NAME from NODES";\r
+        DB db = new DB();\r
+        try (Connection conn = db.getConnection();\r
+                PreparedStatement ps = conn.prepareStatement(sql)) {\r
+            try (ResultSet rs = ps.executeQuery()) {\r
+                while (rs.next()) {\r
+                    int id = rs.getInt("NODEID");\r
+                    String name = rs.getString("NAME");\r
+                    hmap.put(name, id);\r
+                }\r
+            } finally {\r
+                db.release(conn);\r
+            }\r
+        } catch (SQLException e) {\r
+            intLogger.error(PROV_0005_DO_INSERT + e.getMessage(),e);\r
+        }\r
+        map = hmap;\r
+    }\r
+\r
+    static Integer lookupNodeName(final String name) {\r
+        Integer nodeName = map.get(name);\r
+        if (nodeName == null) {\r
+            throw new IllegalArgumentException("Invalid node name: " + name);\r
+        }\r
+        return nodeName;\r
+    }\r
+\r
+    /**\r
+     * Get node names.\r
+     * @param patt pattern to search\r
+     * @return collection of node names\r
+     */\r
+    public static Collection<String> lookupNodeNames(String patt) {\r
+        Collection<String> coll = new TreeSet<>();\r
+        final Set<String> keyset = map.keySet();\r
+        for (String s : patt.toLowerCase().split(",")) {\r
+            if (s.endsWith("*")) {\r
+                addNodeToCollection(coll, keyset, s);\r
+            } else if (keyset.contains(s)) {\r
+                coll.add(s);\r
+            } else if (keyset.contains(normalizeNodename(s))) {\r
+                coll.add(normalizeNodename(s));\r
+            } else {\r
+                throw new IllegalArgumentException("Invalid node name: " + s);\r
+            }\r
+        }\r
+        return coll;\r
+    }\r
+\r
+    private static void addNodeToCollection(Collection<String> coll, Set<String> keyset, String str) {\r
+        str = str.substring(0, str.length() - 1);\r
+        for (String s2 : keyset) {\r
+            if (s2.startsWith(str)) {\r
+                coll.add(s2);\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Method to add domain name.\r
+     * @param str nde name string\r
+     * @return normalized node name\r
+     */\r
+    public static String normalizeNodename(String str) {\r
+        if (str != null && str.indexOf('.') <= 0) {\r
+            Parameters param = Parameters.getParameter(Parameters.PROV_DOMAIN);\r
+            if (param != null) {\r
+                String domain = param.getValue();\r
+                str += "." + domain;\r
+            }\r
+            return str.toLowerCase();\r
+        } else {\r
+            return str;\r
+        }\r
+\r
+    }\r
+\r
+    String lookupNodeID(int node) {\r
+        for (Map.Entry<String, Integer> entry : map.entrySet()) {\r
+            if (entry.getValue() == node) {\r
+                return entry.getKey();\r
+            }\r
+        }\r
+        return null;\r
+    }\r
 }\r