* * Licensed under the Apache License, Version 2.0 (the "License");\r
* * you may not use this file except in compliance with the License.\r
* * You may obtain a copy of the License at\r
- * * \r
+ * *\r
* * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
+ * *\r
* * Unless required by applicable law or agreed to in writing, software\r
* * distributed under the License is distributed on an "AS IS" BASIS,\r
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
* @version $Id$\r
*/\r
public class DB {\r
- /** The name of the properties file (in CLASSPATH) */\r
- private static final String CONFIG_FILE = "provserver.properties";\r
-\r
- private static String DB_URL;\r
- private static String DB_LOGIN;\r
- private static String DB_PASSWORD;\r
- private static Properties props;\r
- private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
- private static final Queue<Connection> queue = new LinkedList<>();\r
-\r
- public static String HTTPS_PORT;\r
- public static String HTTP_PORT;\r
-\r
- /**\r
- * Construct a DB object. If this is the very first creation of this object, it will load a copy\r
- * of the properties for the server, and attempt to load the JDBC driver for the database. If a fatal\r
- * error occurs (e.g. either the properties file or the DB driver is missing), the JVM will exit.\r
- */\r
- public DB() {\r
- if (props == null) {\r
- props = new Properties();\r
- try (InputStream inStream = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)) {\r
- props.load(inStream);\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
- } catch (IOException e) {\r
- intlogger.fatal("PROV9003 Opening properties: " + e.getMessage());\r
- e.printStackTrace();\r
- System.exit(1);\r
- } catch (ClassNotFoundException e) {\r
- intlogger.fatal("PROV9004 cannot find the DB driver: " + e);\r
- e.printStackTrace();\r
- System.exit(1);\r
- }\r
- }\r
- }\r
- /**\r
- * Get the provisioning server properties (loaded from provserver.properties).\r
- * @return the Properties object\r
- */\r
- public Properties getProperties() {\r
- return props;\r
- }\r
- /**\r
- * Get a JDBC connection to the DB from the pool. Creates a new one if none are available.\r
- * @return the Connection\r
- * @throws SQLException\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
- do {\r
- // Try up to 3 times to get a connection\r
- try {\r
- connection = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD);\r
- } catch (SQLException sqlEx) {\r
- if (++n >= 3)\r
- throw sqlEx;\r
- }\r
- } while (connection == null);\r
- }\r
- }\r
- if (connection != null && !connection.isValid(1)) {\r
- connection.close();\r
- connection = null;\r
- }\r
- }\r
- return connection;\r
- }\r
- /**\r
- * Returns a JDBC connection to the pool.\r
- * @param connection the Connection to return\r
- */\r
- public void release(Connection connection) {\r
- if (connection != null) {\r
- synchronized (queue) {\r
- if (!queue.contains(connection))\r
- queue.add(connection);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Run all necessary retrofits required to bring the database up to the level required for this version\r
- * of the provisioning server. This should be run before the server itself is started.\r
- * @return true if all retrofits worked, false otherwise\r
- */\r
- public boolean runRetroFits() {\r
- return retroFit1();\r
- }\r
-\r
- /**\r
- * Retrofit 1 - Make sure the expected tables are in DB and are initialized.\r
- * Uses sql_init_01.sql to setup the DB.\r
- * @return true if the retrofit worked, false otherwise\r
- */\r
- private boolean retroFit1() {\r
- final String[] expectedTables = {\r
- "FEEDS", "FEED_ENDPOINT_ADDRS", "FEED_ENDPOINT_IDS", "PARAMETERS",\r
- "SUBSCRIPTIONS", "LOG_RECORDS", "INGRESS_ROUTES", "EGRESS_ROUTES",\r
- "NETWORK_ROUTES", "NODESETS", "NODES", "GROUPS"\r
- };\r
- Connection connection = null;\r
- try {\r
- connection = getConnection();\r
- Set<String> actualTables = getTableSet(connection);\r
- boolean initialize = false;\r
- for (String table : expectedTables) {\r
- initialize |= !actualTables.contains(table);\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.fatal("PROV9000: The database credentials are not working: "+e.getMessage());\r
- return false;\r
- } finally {\r
- if (connection != null)\r
- release(connection);\r
- }\r
- return true;\r
- }\r
-\r
- /**\r
- * Get a set of all table names in the DB.\r
- * @param connection a DB connection\r
- * @return the set of table names\r
- */\r
- private Set<String> getTableSet(Connection connection) {\r
- Set<String> tables = new HashSet<String>();\r
- try {\r
- DatabaseMetaData md = connection.getMetaData();\r
- ResultSet rs = md.getTables("datarouter", "", "", null);\r
- if (rs != null) {\r
- while (rs.next()) {\r
- tables.add(rs.getString("TABLE_NAME"));\r
- }\r
- rs.close();\r
- }\r
- } catch (SQLException e) {\r
- }\r
- return tables;\r
- }\r
- /**\r
- * Initialize the tables by running the initialization scripts located in the directory specified\r
- * by the property <i>org.onap.dmaap.datarouter.provserver.dbscripts</i>. Scripts have names of\r
- * the form sql_init_NN.sql\r
- * @param connection a DB connection\r
- * @param scriptId the number of the sql_init_NN.sql script to run\r
- */\r
- private void runInitScript(Connection connection, int scriptId) {\r
- String scriptDir = (String) props.get("org.onap.dmaap.datarouter.provserver.dbscripts");\r
- StringBuilder sb = new StringBuilder();\r
- try {\r
- String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId);\r
- if (!(new File(scriptFile)).exists())\r
- return;\r
-\r
- LineNumberReader in = new LineNumberReader(new FileReader(scriptFile));\r
- String line;\r
- while ((line = in.readLine()) != null) {\r
- if (!line.startsWith("--")) {\r
- line = line.trim();\r
- sb.append(line);\r
- if (line.endsWith(";")) {\r
- // Execute one DDL statement\r
- String sql = sb.toString();\r
- sb.setLength(0);\r
- Statement s = connection.createStatement();\r
- s.execute(sql);\r
- s.close();\r
- }\r
- }\r
- }\r
- in.close();\r
- sb.setLength(0);\r
- } catch (Exception e) {\r
- intlogger.fatal("PROV9002 Error when initializing table: "+e.getMessage());\r
- System.exit(1);\r
- }\r
- }\r
+\r
+ private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
+\r
+ private static String DB_URL;\r
+ private static String DB_LOGIN;\r
+ private static String DB_PASSWORD;\r
+ private static Properties props;\r
+ private static final Queue<Connection> queue = new LinkedList<>();\r
+\r
+ public static String HTTPS_PORT;\r
+ public static String HTTP_PORT;\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
+ * for the server, and attempt to load the JDBC driver for the database. If a fatal error occurs (e.g. either the\r
+ * properties file or the DB driver is missing), the JVM will exit.\r
+ */\r
+ public DB() {\r
+ if (props == null) {\r
+ props = new Properties();\r
+ try {\r
+ props.load(new FileInputStream(System.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
+ } catch (IOException e) {\r
+ intlogger.fatal("PROV9003 Opening properties: " + e.getMessage());\r
+ e.printStackTrace();\r
+ System.exit(1);\r
+ } catch (ClassNotFoundException e) {\r
+ intlogger.fatal("PROV9004 cannot find the DB driver: " + e);\r
+ e.printStackTrace();\r
+ System.exit(1);\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Get the provisioning server properties (loaded from provserver.properties).\r
+ *\r
+ * @return the Properties object\r
+ */\r
+ public Properties getProperties() {\r
+ return props;\r
+ }\r
+\r
+ /**\r
+ * Get a JDBC connection to the DB from the pool. Creates a new one if none are available.\r
+ *\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
+ do {\r
+ // Try up to 3 times to get a connection\r
+ try {\r
+ connection = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD);\r
+ } catch (SQLException sqlEx) {\r
+ if (++n >= 3) {\r
+ throw sqlEx;\r
+ }\r
+ }\r
+ } while (connection == null);\r
+ }\r
+ }\r
+ if (connection != null && !connection.isValid(1)) {\r
+ connection.close();\r
+ connection = null;\r
+ }\r
+ }\r
+ return connection;\r
+ }\r
+\r
+ /**\r
+ * Returns a JDBC connection to the pool.\r
+ *\r
+ * @param connection the Connection to return\r
+ */\r
+ public void release(Connection connection) {\r
+ if (connection != null) {\r
+ synchronized (queue) {\r
+ if (!queue.contains(connection)) {\r
+ queue.add(connection);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Run all necessary retrofits required to bring the database up to the level required for this version of the\r
+ * provisioning server. This should be run before the server itself is started.\r
+ *\r
+ * @return true if all retrofits worked, false otherwise\r
+ */\r
+ public boolean runRetroFits() {\r
+ return retroFit1();\r
+ }\r
+\r
+ /**\r
+ * Retrofit 1 - Make sure the expected tables are in DB and are initialized. Uses sql_init_01.sql to setup the DB.\r
+ *\r
+ * @return true if the retrofit worked, false otherwise\r
+ */\r
+ private boolean retroFit1() {\r
+ final String[] expectedTables = {\r
+ "FEEDS", "FEED_ENDPOINT_ADDRS", "FEED_ENDPOINT_IDS", "PARAMETERS",\r
+ "SUBSCRIPTIONS", "LOG_RECORDS", "INGRESS_ROUTES", "EGRESS_ROUTES",\r
+ "NETWORK_ROUTES", "NODESETS", "NODES", "GROUPS"\r
+ };\r
+ Connection connection = null;\r
+ try {\r
+ connection = getConnection();\r
+ Set<String> actualTables = getTableSet(connection);\r
+ boolean initialize = false;\r
+ for (String table : expectedTables) {\r
+ initialize |= !actualTables.contains(table.toLowerCase());\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.fatal("PROV9000: The database credentials are not working: " + e.getMessage());\r
+ return false;\r
+ } finally {\r
+ if (connection != null) {\r
+ release(connection);\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * Get a set of all table names in the DB.\r
+ *\r
+ * @param connection a DB connection\r
+ * @return the set of table names\r
+ */\r
+ private Set<String> getTableSet(Connection connection) {\r
+ Set<String> tables = new HashSet<String>();\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
+ }\r
+ rs.close();\r
+ }\r
+ } catch (SQLException e) {\r
+ intlogger.fatal("PROV9010: Failed to get TABLE data from DB: " + e.getMessage());\r
+ }\r
+ return tables;\r
+ }\r
+\r
+ /**\r
+ * Initialize the tables by running the initialization scripts located in the directory specified by the property\r
+ * <i>org.onap.dmaap.datarouter.provserver.dbscripts</i>. Scripts have names of the form sql_init_NN.sql\r
+ *\r
+ * @param connection a DB connection\r
+ * @param scriptId the number of the sql_init_NN.sql script to run\r
+ */\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.fatal("PROV9005 Failed to load sql script from : " + scriptFile);\r
+ System.exit(1);\r
+ }\r
+ LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile));\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
+ }\r
+ }\r
+ lineReader.close();\r
+ strBuilder.setLength(0);\r
+ } catch (Exception e) {\r
+ intlogger.fatal("PROV9002 Error when initializing table: " + e.getMessage());\r
+ System.exit(1);\r
+ }\r
+ }\r
}\r