Remove critical code smells for utils classes
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / utils / DB.java
index 1952f94..7700a58 100644 (file)
@@ -41,7 +41,9 @@ import java.util.NoSuchElementException;
 import java.util.Properties;\r
 import java.util.Queue;\r
 import java.util.Set;\r
-import org.apache.log4j.Logger;\r
+\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
 \r
 /**\r
  * Load the DB JDBC driver, and manage a simple pool of connections to the DB.\r
@@ -51,8 +53,7 @@ import org.apache.log4j.Logger;
  */\r
 public class DB {\r
 \r
-    private static Logger intlogger = Logger\r
-        .getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
+    private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
 \r
     private static String DB_URL;\r
     private static String DB_LOGIN;\r
@@ -83,10 +84,10 @@ public class DB {
                 HTTP_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port");\r
                 Class.forName(DB_DRIVER);\r
             } catch (IOException e) {\r
-                intlogger.fatal("PROV9003 Opening properties: " + e.getMessage());\r
+                intlogger.error("PROV9003 Opening properties: " + e.getMessage(), e);\r
                 System.exit(1);\r
             } catch (ClassNotFoundException e) {\r
-                intlogger.fatal("PROV9004 cannot find the DB driver: " + e);\r
+                intlogger.error("PROV9004 cannot find the DB driver: " + e);\r
                 System.exit(1);\r
             }\r
         }\r
@@ -114,6 +115,7 @@ public class DB {
                 try {\r
                     connection = queue.remove();\r
                 } catch (NoSuchElementException nseEx) {\r
+                    intlogger.error("PROV9006 No connection on queue: " + nseEx.getMessage(), nseEx);\r
                     int n = 0;\r
                     do {\r
                         // Try up to 3 times to get a connection\r
@@ -123,16 +125,14 @@ public class DB {
                             if (++n >= 3) {\r
                                 throw sqlEx;\r
                             }\r
-                        } finally {\r
-                            if (connection != null && !connection.isValid(1)) {\r
-                                connection.close();\r
-                                connection = null;\r
-                            }\r
                         }\r
                     } while (connection == null);\r
                 }\r
             }\r
-\r
+            if (connection != null && !connection.isValid(1)) {\r
+                connection.close();\r
+                connection = null;\r
+            }\r
         }\r
         return connection;\r
     }\r
@@ -187,16 +187,15 @@ public class DB {
             connection = getConnection();\r
             Set<String> actualTables = getTableSet(connection);\r
             boolean initialize = false;\r
-            for (String table : expectedTables) {\r
-                initialize |= !actualTables.contains(table.toLowerCase());\r
+            for (String tableName : expectedTables) {\r
+                initialize |= !actualTables.contains(tableName);\r
             }\r
             if (initialize) {\r
                 intlogger.info("PROV9001: First time startup; The database is being initialized.");\r
                 runInitScript(connection, 1);\r
             }\r
         } catch (SQLException e) {\r
-            intlogger\r
-                .fatal("PROV9000: The database credentials are not working: " + e.getMessage());\r
+            intlogger.error("PROV9000: The database credentials are not working: " + e.getMessage(), e);\r
             return false;\r
         } finally {\r
             if (connection != null) {\r
@@ -213,18 +212,18 @@ public class DB {
      * @return the set of table names\r
      */\r
     private Set<String> getTableSet(Connection connection) {\r
-        Set<String> tables = new HashSet<String>();\r
+        Set<String> tables = new HashSet<>();\r
         try {\r
             DatabaseMetaData md = connection.getMetaData();\r
             ResultSet rs = md.getTables(null, null, "%", null);\r
             if (rs != null) {\r
                 while (rs.next()) {\r
-                    tables.add(rs.getString("TABLE_NAME"));\r
+                    tables.add(rs.getString("TABLE_NAME").toUpperCase());\r
                 }\r
                 rs.close();\r
             }\r
         } catch (SQLException e) {\r
-            intlogger.fatal("PROV9010: Failed to get TABLE data from DB: " + e.getMessage());\r
+            intlogger.error("PROV9010: Failed to get TABLE data from DB: " + e.getMessage(), e);\r
         }\r
         return tables;\r
     }\r
@@ -243,7 +242,7 @@ public class DB {
         try {\r
             String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId);\r
             if (!(new File(scriptFile)).exists()) {\r
-                intlogger.fatal("PROV9005 Failed to load sql script from : " + scriptFile);\r
+                intlogger.error("PROV9005 Failed to load sql script from : " + scriptFile);\r
                 System.exit(1);\r
             }\r
             LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile));\r
@@ -265,7 +264,7 @@ public class DB {
             lineReader.close();\r
             strBuilder.setLength(0);\r
         } catch (Exception e) {\r
-            intlogger.fatal("PROV9002 Error when initializing table: " + e.getMessage());\r
+            intlogger.error("PROV9002 Error when initializing table: " + e.getMessage(), e);\r
             System.exit(1);\r
         }\r
     }\r