X-Git-Url: https://gerrit.onap.org/r/gitweb?p=dmaap%2Fdatarouter.git;a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Futils%2FDB.java;h=a83f81a51a4ede324d2c35f600a8f03e34954cf3;hp=a907a03bc5c03c2838f0cd34b7c158f79d3d24bf;hb=f3b3e701a48d529ee6dc88e3a867448498e23d36;hpb=1a00b59927f6b0c17ccb7c40221ec9bce231c964 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 a907a03b..a83f81a5 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 @@ -21,14 +21,30 @@ * * ******************************************************************************/ - package org.onap.dmaap.datarouter.provisioning.utils; -import org.apache.log4j.Logger; +import static java.lang.System.exit; +import static java.lang.System.getProperty; -import java.io.*; -import java.sql.*; -import java.util.*; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +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; /** * Load the DB JDBC driver, and manage a simple pool of connections to the DB. @@ -38,46 +54,42 @@ import java.util.*; */ public class DB { - /** - * The name of the properties file (in CLASSPATH) - */ - private static String DB_URL; - private static String DB_LOGIN; - private static String DB_PASSWORD; + private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog"); + + private static String dbUrl; + private static String dbLogin; + private static String dbPassword; private static Properties props; - private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); private static final Queue queue = new LinkedList<>(); - public static String HTTPS_PORT; - public static String HTTP_PORT; + private static String httpsPort; + private static String httpPort; /** * 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() { if (props == null) { props = new Properties(); try { - props.load(new FileInputStream(System.getProperty( + props.load(new FileInputStream(getProperty( "org.onap.dmaap.datarouter.provserver.properties", "/opt/app/datartr/etc/provserver.properties"))); - String DB_DRIVER = (String) props.get("org.onap.dmaap.datarouter.db.driver"); - DB_URL = (String) props.get("org.onap.dmaap.datarouter.db.url"); - DB_LOGIN = (String) props.get("org.onap.dmaap.datarouter.db.login"); - DB_PASSWORD = (String) props.get("org.onap.dmaap.datarouter.db.password"); - HTTPS_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.https.port"); - HTTP_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port"); - Class.forName(DB_DRIVER); + dbUrl = (String) props.get("org.onap.dmaap.datarouter.db.url"); + dbLogin = (String) props.get("org.onap.dmaap.datarouter.db.login"); + dbPassword = (String) props.get("org.onap.dmaap.datarouter.db.password"); + httpsPort = (String) props.get("org.onap.dmaap.datarouter.provserver.https.port"); + httpPort = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port"); + String dbDriver = (String) props.get("org.onap.dmaap.datarouter.db.driver"); + Class.forName(dbDriver); } catch (IOException e) { - intlogger.fatal("PROV9003 Opening properties: " + e.getMessage()); - e.printStackTrace(); - System.exit(1); + intlogger.error("PROV9003 Opening properties: " + e.getMessage(), e); + exit(1); } catch (ClassNotFoundException e) { - intlogger.fatal("PROV9004 cannot find the DB driver: " + e); - e.printStackTrace(); - System.exit(1); + intlogger.error("PROV9004 cannot find the DB driver: " + e); + exit(1); } } } @@ -96,25 +108,25 @@ public class DB { * * @return the Connection */ - @SuppressWarnings("resource") public Connection getConnection() throws SQLException { Connection connection = null; while (connection == null) { synchronized (queue) { try { connection = queue.remove(); - } catch (NoSuchElementException nseEx) { - int n = 0; + } catch (NoSuchElementException ignore) { + int num = 0; do { // Try up to 3 times to get a connection try { - connection = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD); + connection = DriverManager.getConnection(dbUrl, dbLogin, dbPassword); } catch (SQLException sqlEx) { - if (++n >= 3) { + if (++num >= 3) { throw sqlEx; } } - } while (connection == null); + } + while (connection == null); } } if (connection != null && !connection.isValid(1)) { @@ -150,6 +162,15 @@ public class DB { return retroFit1(); } + + public static String getHttpsPort() { + return httpsPort; + } + + public static String getHttpPort() { + return httpPort; + } + /** * Retrofit 1 - Make sure the expected tables are in DB and are initialized. Uses sql_init_01.sql to setup the DB. * @@ -166,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) { @@ -191,59 +212,61 @@ 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 */ private void runInitScript(Connection connection, int scriptId) { String scriptDir = (String) props.get("org.onap.dmaap.datarouter.provserver.dbscripts"); - StringBuilder strBuilder = new StringBuilder(); - 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); - System.exit(1); - } - LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile)); + String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId); + if (!(new File(scriptFile)).exists()) { + intlogger.error("PROV9005 Failed to load sql script from : " + scriptFile); + exit(1); + } + try (LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile)); + Statement statement = connection.createStatement()) { + StringBuilder strBuilder = new StringBuilder(); String line; while ((line = lineReader.readLine()) != null) { if (!line.startsWith("--")) { line = line.trim(); strBuilder.append(line); - if (line.endsWith(";")) { - // Execute one DDL statement - String sql = strBuilder.toString(); - strBuilder.setLength(0); - Statement statement = connection.createStatement(); - statement.execute(sql); - statement.close(); - } + executeDdlStatement(statement, strBuilder, line); } } - lineReader.close(); strBuilder.setLength(0); } catch (Exception e) { - intlogger.fatal("PROV9002 Error when initializing table: " + e.getMessage()); - System.exit(1); + intlogger.error("PROV9002 Error when initializing table: " + e.getMessage(), e); + exit(1); + } + } + + private void executeDdlStatement(Statement statement, StringBuilder strBuilder, String line) throws SQLException { + if (line.endsWith(";")) { + // Execute one DDL statement + String sql = strBuilder.toString(); + strBuilder.setLength(0); + statement.execute(sql); } } }