Release DR images via self release
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / utils / DB.java
index d29876f..a83f81a 100644 (file)
  * *\r
  ******************************************************************************/\r
 \r
-\r
 package org.onap.dmaap.datarouter.provisioning.utils;\r
 \r
+import static java.lang.System.exit;\r
+import static java.lang.System.getProperty;\r
+\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
 import java.io.File;\r
 import java.io.FileInputStream;\r
 import java.io.FileReader;\r
@@ -42,9 +46,6 @@ import java.util.Properties;
 import java.util.Queue;\r
 import java.util.Set;\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
  *\r
@@ -55,14 +56,14 @@ public class DB {
 \r
     private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
 \r
-    private static String DB_URL;\r
-    private static String DB_LOGIN;\r
-    private static String DB_PASSWORD;\r
+    private static String dbUrl;\r
+    private static String dbLogin;\r
+    private static String dbPassword;\r
     private static Properties props;\r
     private static final Queue<Connection> queue = new LinkedList<>();\r
 \r
-    private static String HTTPS_PORT;\r
-    private static String HTTP_PORT;\r
+    private static String httpsPort;\r
+    private static String httpPort;\r
 \r
     /**\r
      * Construct a DB object.  If this is the very first creation of this object, it will load a copy of the properties\r
@@ -73,22 +74,22 @@ public class DB {
         if (props == null) {\r
             props = new Properties();\r
             try {\r
-                props.load(new FileInputStream(System.getProperty(\r
+                props.load(new FileInputStream(getProperty(\r
                     "org.onap.dmaap.datarouter.provserver.properties",\r
                     "/opt/app/datartr/etc/provserver.properties")));\r
-                String DB_DRIVER = (String) props.get("org.onap.dmaap.datarouter.db.driver");\r
-                DB_URL = (String) props.get("org.onap.dmaap.datarouter.db.url");\r
-                DB_LOGIN = (String) props.get("org.onap.dmaap.datarouter.db.login");\r
-                DB_PASSWORD = (String) props.get("org.onap.dmaap.datarouter.db.password");\r
-                HTTPS_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.https.port");\r
-                HTTP_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port");\r
-                Class.forName(DB_DRIVER);\r
+                dbUrl = (String) props.get("org.onap.dmaap.datarouter.db.url");\r
+                dbLogin = (String) props.get("org.onap.dmaap.datarouter.db.login");\r
+                dbPassword = (String) props.get("org.onap.dmaap.datarouter.db.password");\r
+                httpsPort = (String) props.get("org.onap.dmaap.datarouter.provserver.https.port");\r
+                httpPort = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port");\r
+                String dbDriver = (String) props.get("org.onap.dmaap.datarouter.db.driver");\r
+                Class.forName(dbDriver);\r
             } catch (IOException e) {\r
-                intlogger.error("PROV9003 Opening properties: " + e.getMessage());\r
-                System.exit(1);\r
+                intlogger.error("PROV9003 Opening properties: " + e.getMessage(), e);\r
+                exit(1);\r
             } catch (ClassNotFoundException e) {\r
                 intlogger.error("PROV9004 cannot find the DB driver: " + e);\r
-                System.exit(1);\r
+                exit(1);\r
             }\r
         }\r
     }\r
@@ -107,25 +108,25 @@ public class DB {
      *\r
      * @return the Connection\r
      */\r
-    @SuppressWarnings("resource")\r
     public Connection getConnection() throws SQLException {\r
         Connection connection = null;\r
         while (connection == null) {\r
             synchronized (queue) {\r
                 try {\r
                     connection = queue.remove();\r
-                } catch (NoSuchElementException nseEx) {\r
-                    int n = 0;\r
+                } catch (NoSuchElementException ignore) {\r
+                    int num = 0;\r
                     do {\r
                         // Try up to 3 times to get a connection\r
                         try {\r
-                            connection = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD);\r
+                            connection = DriverManager.getConnection(dbUrl, dbLogin, dbPassword);\r
                         } catch (SQLException sqlEx) {\r
-                            if (++n >= 3) {\r
+                            if (++num >= 3) {\r
                                 throw sqlEx;\r
                             }\r
                         }\r
-                    } while (connection == null);\r
+                    }\r
+                    while (connection == null);\r
                 }\r
             }\r
             if (connection != null && !connection.isValid(1)) {\r
@@ -163,11 +164,11 @@ public class DB {
 \r
 \r
     public static String getHttpsPort() {\r
-        return HTTPS_PORT;\r
+        return httpsPort;\r
     }\r
 \r
     public static String getHttpPort() {\r
-        return HTTP_PORT;\r
+        return httpPort;\r
     }\r
 \r
     /**\r
@@ -194,8 +195,7 @@ public class DB {
                 runInitScript(connection, 1);\r
             }\r
         } catch (SQLException e) {\r
-            intlogger\r
-                .error("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
@@ -223,7 +223,7 @@ public class DB {
                 rs.close();\r
             }\r
         } catch (SQLException e) {\r
-            intlogger.error("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
@@ -238,34 +238,35 @@ public class DB {
      */\r
     private void runInitScript(Connection connection, int scriptId) {\r
         String scriptDir = (String) props.get("org.onap.dmaap.datarouter.provserver.dbscripts");\r
-        StringBuilder strBuilder = new StringBuilder();\r
-        try {\r
-            String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId);\r
-            if (!(new File(scriptFile)).exists()) {\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
+        String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId);\r
+        if (!(new File(scriptFile)).exists()) {\r
+            intlogger.error("PROV9005 Failed to load sql script from : " + scriptFile);\r
+            exit(1);\r
+        }\r
+        try (LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile));\r
+                Statement statement = connection.createStatement()) {\r
+            StringBuilder strBuilder = new StringBuilder();\r
             String line;\r
             while ((line = lineReader.readLine()) != null) {\r
                 if (!line.startsWith("--")) {\r
                     line = line.trim();\r
                     strBuilder.append(line);\r
-                    if (line.endsWith(";")) {\r
-                        // Execute one DDL statement\r
-                        String sql = strBuilder.toString();\r
-                        strBuilder.setLength(0);\r
-                        Statement statement = connection.createStatement();\r
-                        statement.execute(sql);\r
-                        statement.close();\r
-                    }\r
+                    executeDdlStatement(statement, strBuilder, line);\r
                 }\r
             }\r
-            lineReader.close();\r
             strBuilder.setLength(0);\r
         } catch (Exception e) {\r
-            intlogger.error("PROV9002 Error when initializing table: " + e.getMessage());\r
-            System.exit(1);\r
+            intlogger.error("PROV9002 Error when initializing table: " + e.getMessage(), e);\r
+            exit(1);\r
+        }\r
+    }\r
+\r
+    private void executeDdlStatement(Statement statement, StringBuilder strBuilder, String line) throws SQLException {\r
+        if (line.endsWith(";")) {\r
+            // Execute one DDL statement\r
+            String sql = strBuilder.toString();\r
+            strBuilder.setLength(0);\r
+            statement.execute(sql);\r
         }\r
     }\r
 }\r