* * 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
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
* @version $Id: NodeClass.java,v 1.2 2014/01/15 16:08:43 eby Exp $\r
*/\r
public abstract class NodeClass extends Syncable {\r
- private static Map<String, Integer> map;\r
\r
+ private static Map<String, Integer> map;\r
+ private static Logger intLogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
public NodeClass() {\r
// init on first use\r
if (map == null) {\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
+ * 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
+ if (map == null) {\r
reload();\r
+ }\r
int nextid = 0;\r
for (Integer n : map.values()) {\r
- if (n >= nextid)\r
+ if (n >= nextid) {\r
nextid = n + 1;\r
+ }\r
}\r
// take | separated list, add domain if needed.\r
- Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\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
+ intLogger.info("..adding " + node + " to NODES with index " + nextid);\r
map.put(node, nextid);\r
PreparedStatement ps = null;\r
try {\r
ps.close();\r
db.release(conn);\r
} catch (SQLException e) {\r
- intlogger.warn("PROV0005 doInsert: " + e.getMessage());\r
- e.printStackTrace();\r
+ intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
} finally {\r
try {\r
- ps.close();\r
+ if(ps!=null){\r
+ ps.close();\r
+ }\r
} catch (SQLException e) {\r
- e.printStackTrace();\r
+ intLogger.error("Error in closing PreparedStatement: " + e.getMessage(),e);\r
}\r
}\r
nextid++;\r
public static void reload() {\r
Map<String, Integer> m = new HashMap<String, Integer>();\r
PreparedStatement ps = null;\r
+\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
+ try(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
}\r
- rs.close();\r
ps.close();\r
db.release(conn);\r
} catch (SQLException e) {\r
- e.printStackTrace();\r
+ intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
} finally {\r
try {\r
- ps.close();\r
+ if(ps!=null){\r
+ ps.close();\r
+ }\r
} catch (SQLException e) {\r
- e.printStackTrace();\r
+ intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
}\r
}\r
map = m;\r
}\r
\r
- public static Integer lookupNodeName(final String name) throws IllegalArgumentException {\r
+ public static Integer lookupNodeName(final String name) {\r
Integer n = map.get(name);\r
- if (n == null)\r
+ if (n == null) {\r
throw new IllegalArgumentException("Invalid node name: " + name);\r
+ }\r
return n;\r
}\r
\r
- public static Collection<String> lookupNodeNames(String patt) throws IllegalArgumentException {\r
+ public static Collection<String> lookupNodeNames(String patt) {\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
+ if (s2.startsWith(s)) {\r
coll.add(s2);\r
+ }\r
}\r
} else if (keyset.contains(s)) {\r
coll.add(s);\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
String domain = p.getValue();\r
s += "." + domain;\r
}\r
+ return s.toLowerCase();\r
+ }\r
+ else{\r
+ return s;\r
}\r
- return s.toLowerCase();\r
+\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
+ }\r
+ return null;\r
}\r
}\r