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=7f0d56b7834d112bd7f1ddc5e4019c2a94f4f4b2;hb=26fb914c67acb060987671e0ce12d584d9786e54;hp=c65ee267e92e19524fe2b511a6fdfdfd764949c1;hpb=8cbe8a88bc6dfe8673a33a017fe6a5a3e7ce86c3;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 c65ee267..7f0d56b7 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 @@ -37,31 +37,30 @@ import java.util.*; * @version $Id$ */ public class DB { - /** - * The name of the properties file (in CLASSPATH) - */ - private static final String CONFIG_FILE = "provserver.properties"; + + private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); private static String DB_URL; private static String DB_LOGIN; private static String DB_PASSWORD; 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; /** - * 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 properties file or the DB driver is missing), the JVM will exit. + * 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 + * properties file or the DB driver is missing), the JVM will exit. */ public DB() { if (props == null) { props = new Properties(); - try (InputStream inStream = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)) { - props.load(inStream); + try { + props.load(new FileInputStream(System.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"); @@ -94,7 +93,6 @@ public class DB { * Get a JDBC connection to the DB from the pool. Creates a new one if none are available. * * @return the Connection - * @throws SQLException */ @SuppressWarnings("resource") public Connection getConnection() throws SQLException { @@ -110,16 +108,20 @@ public class DB { try { connection = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD); } catch (SQLException sqlEx) { - if (++n >= 3) + if (++n >= 3) { throw sqlEx; + } + } + finally { + if (connection != null && !connection.isValid(1)) { + connection.close(); + connection = null; + } } } while (connection == null); } } - if (connection != null && !connection.isValid(1)) { - connection.close(); - connection = null; - } + } return connection; } @@ -132,15 +134,16 @@ public class DB { public void release(Connection connection) { if (connection != null) { synchronized (queue) { - if (!queue.contains(connection)) + if (!queue.contains(connection)) { queue.add(connection); + } } } } /** - * Run all necessary retrofits required to bring the database up to the level required for this version - * of the provisioning server. This should be run before the server itself is started. + * Run all necessary retrofits required to bring the database up to the level required for this version of the + * provisioning server. This should be run before the server itself is started. * * @return true if all retrofits worked, false otherwise */ @@ -149,16 +152,15 @@ public class DB { } /** - * Retrofit 1 - Make sure the expected tables are in DB and are initialized. - * Uses sql_init_01.sql to setup the DB. + * Retrofit 1 - Make sure the expected tables are in DB and are initialized. Uses sql_init_01.sql to setup the DB. * * @return true if the retrofit worked, false otherwise */ private boolean retroFit1() { final String[] expectedTables = { - "FEEDS", "FEED_ENDPOINT_ADDRS", "FEED_ENDPOINT_IDS", "PARAMETERS", - "SUBSCRIPTIONS", "LOG_RECORDS", "INGRESS_ROUTES", "EGRESS_ROUTES", - "NETWORK_ROUTES", "NODESETS", "NODES", "GROUPS" + "FEEDS", "FEED_ENDPOINT_ADDRS", "FEED_ENDPOINT_IDS", "PARAMETERS", + "SUBSCRIPTIONS", "LOG_RECORDS", "INGRESS_ROUTES", "EGRESS_ROUTES", + "NETWORK_ROUTES", "NODESETS", "NODES", "GROUPS" }; Connection connection = null; try { @@ -166,7 +168,7 @@ public class DB { Set actualTables = getTableSet(connection); boolean initialize = false; for (String table : expectedTables) { - initialize |= !actualTables.contains(table); + initialize |= !actualTables.contains(table.toLowerCase()); } if (initialize) { intlogger.info("PROV9001: First time startup; The database is being initialized."); @@ -176,8 +178,9 @@ public class DB { intlogger.fatal("PROV9000: The database credentials are not working: " + e.getMessage()); return false; } finally { - if (connection != null) + if (connection != null) { release(connection); + } } return true; } @@ -192,7 +195,7 @@ public class DB { Set tables = new HashSet(); try { DatabaseMetaData md = connection.getMetaData(); - ResultSet rs = md.getTables("datarouter", "", "", null); + ResultSet rs = md.getTables(null, null, "%", null); if (rs != null) { while (rs.next()) { tables.add(rs.getString("TABLE_NAME")); @@ -200,44 +203,45 @@ public class DB { rs.close(); } } catch (SQLException e) { + intlogger.fatal("PROV9010: Failed to get TABLE data from DB: " + e.getMessage()); } 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 + * 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 * * @param connection a DB connection - * @param scriptId the number of the sql_init_NN.sql script to run + * @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 sb = new StringBuilder(); + StringBuilder strBuilder = new StringBuilder(); try { String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId); - if (!(new File(scriptFile)).exists()) - return; - - LineNumberReader in = new LineNumberReader(new FileReader(scriptFile)); + 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 line; - while ((line = in.readLine()) != null) { + while ((line = lineReader.readLine()) != null) { if (!line.startsWith("--")) { line = line.trim(); - sb.append(line); + strBuilder.append(line); if (line.endsWith(";")) { // Execute one DDL statement - String sql = sb.toString(); - sb.setLength(0); - Statement s = connection.createStatement(); - s.execute(sql); - s.close(); + String sql = strBuilder.toString(); + strBuilder.setLength(0); + Statement statement = connection.createStatement(); + statement.execute(sql); + statement.close(); } } } - in.close(); - sb.setLength(0); + lineReader.close(); + strBuilder.setLength(0); } catch (Exception e) { intlogger.fatal("PROV9002 Error when initializing table: " + e.getMessage()); System.exit(1);