X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Futils%2FDB.java;h=7700a583fd656ec824132f2c59577121ee529647;hb=29c4f8051e4fc856cd84b97e31f445f65f7e497d;hp=bbcacb5303e6a7e9f143ea52fe7ed2e619fe768a;hpb=aa3403845a7a554ab02152732bb4013de35d8554;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java index bbcacb53..7700a583 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java @@ -24,11 +24,26 @@ package org.onap.dmaap.datarouter.provisioning.utils; -import org.apache.log4j.Logger; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.LineNumberReader; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.NoSuchElementException; +import java.util.Properties; +import java.util.Queue; +import java.util.Set; -import java.io.*; -import java.sql.*; -import java.util.*; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; /** * Load the DB JDBC driver, and manage a simple pool of connections to the DB. @@ -38,7 +53,7 @@ import java.util.*; */ public class DB { - private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); + private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog"); private static String DB_URL; private static String DB_LOGIN; @@ -46,12 +61,12 @@ public class DB { private static Properties props; private static final Queue queue = new LinkedList<>(); - public static String HTTPS_PORT; - public static String HTTP_PORT; + private static String HTTPS_PORT; + private static String HTTP_PORT; /** * Construct a DB object. If this is the very first creation of this object, it will load a copy of the properties - * for the server, and attempt to load the JDBC driver for the database. If a fatal error occurs (e.g. either the + * for the server, and attempt to load the JDBC driver for the database. If a fatal error occurs (e.g. either the * properties file or the DB driver is missing), the JVM will exit. */ public DB() { @@ -69,12 +84,10 @@ public class DB { HTTP_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port"); Class.forName(DB_DRIVER); } catch (IOException e) { - intlogger.fatal("PROV9003 Opening properties: " + e.getMessage()); - e.printStackTrace(); + intlogger.error("PROV9003 Opening properties: " + e.getMessage(), e); System.exit(1); } catch (ClassNotFoundException e) { - intlogger.fatal("PROV9004 cannot find the DB driver: " + e); - e.printStackTrace(); + intlogger.error("PROV9004 cannot find the DB driver: " + e); System.exit(1); } } @@ -102,6 +115,7 @@ public class DB { try { connection = queue.remove(); } catch (NoSuchElementException nseEx) { + intlogger.error("PROV9006 No connection on queue: " + nseEx.getMessage(), nseEx); int n = 0; do { // Try up to 3 times to get a connection @@ -148,6 +162,15 @@ public class DB { return retroFit1(); } + + public static String getHttpsPort() { + return HTTPS_PORT; + } + + public static String getHttpPort() { + return HTTP_PORT; + } + /** * Retrofit 1 - Make sure the expected tables are in DB and are initialized. Uses sql_init_01.sql to setup the DB. * @@ -164,15 +187,15 @@ public class DB { connection = getConnection(); Set actualTables = getTableSet(connection); boolean initialize = false; - for (String table : expectedTables) { - initialize |= !actualTables.contains(table.toLowerCase()); + for (String tableName : expectedTables) { + initialize |= !actualTables.contains(tableName); } if (initialize) { intlogger.info("PROV9001: First time startup; The database is being initialized."); runInitScript(connection, 1); } } catch (SQLException e) { - intlogger.fatal("PROV9000: The database credentials are not working: " + e.getMessage()); + intlogger.error("PROV9000: The database credentials are not working: " + e.getMessage(), e); return false; } finally { if (connection != null) { @@ -189,25 +212,26 @@ public class DB { * @return the set of table names */ private Set getTableSet(Connection connection) { - Set tables = new HashSet(); + Set tables = new HashSet<>(); try { DatabaseMetaData md = connection.getMetaData(); ResultSet rs = md.getTables(null, null, "%", null); if (rs != null) { while (rs.next()) { - tables.add(rs.getString("TABLE_NAME")); + tables.add(rs.getString("TABLE_NAME").toUpperCase()); } rs.close(); } } catch (SQLException e) { - intlogger.fatal("PROV9010: Failed to get TABLE data from DB: " + e.getMessage()); + intlogger.error("PROV9010: Failed to get TABLE data from DB: " + e.getMessage(), e); } return tables; } /** * Initialize the tables by running the initialization scripts located in the directory specified by the property - * org.onap.dmaap.datarouter.provserver.dbscripts. Scripts have names of the form sql_init_NN.sql + * org.onap.dmaap.datarouter.provserver.dbscripts. Scripts have names of the form + * sql_init_NN.sql * * @param connection a DB connection * @param scriptId the number of the sql_init_NN.sql script to run @@ -218,7 +242,7 @@ public class DB { try { String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId); if (!(new File(scriptFile)).exists()) { - intlogger.fatal("PROV9005 Failed to load sql script from : " + scriptFile); + intlogger.error("PROV9005 Failed to load sql script from : " + scriptFile); System.exit(1); } LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile)); @@ -240,7 +264,7 @@ public class DB { lineReader.close(); strBuilder.setLength(0); } catch (Exception e) { - intlogger.fatal("PROV9002 Error when initializing table: " + e.getMessage()); + intlogger.error("PROV9002 Error when initializing table: " + e.getMessage(), e); System.exit(1); } }