- /** The name of the properties file (in CLASSPATH) */\r
- public static final String CONFIG_FILE = "provserver.properties";\r
-\r
- private static String DB_DRIVER = "com.mysql.jdbc.Driver";\r
- private static String DB_URL = "jdbc:mysql://127.0.0.1:3306/datarouter";\r
- private static String DB_LOGIN = "datarouter";\r
- private static String DB_PASSWORD = "datarouter";\r
- private static Properties props;\r
- private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
- private static Queue<Connection> queue = new LinkedList<Connection>();\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
- InputStream inStream = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE);\r
- try {\r
- props.load(inStream);\r
- DB_DRIVER = (String) props.get("com.att.research.datarouter.db.driver");\r
- DB_URL = (String) props.get("com.att.research.datarouter.db.url");\r
- DB_LOGIN = (String) props.get("com.att.research.datarouter.db.login");\r
- DB_PASSWORD = (String) props.get("com.att.research.datarouter.db.password");\r
- HTTPS_PORT = (String) props.get("com.att.research.datarouter.provserver.https.port");\r
- HTTP_PORT = (String) props.get("com.att.research.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
- } finally {\r
- try {\r
- inStream.close();\r
- } catch (IOException e) {\r
- }\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 c = null;\r
- while (c == null) {\r
- synchronized (queue) {\r
- try {\r
- c = queue.remove();\r
- } catch (NoSuchElementException e) {\r
- int n = 0;\r
- do {\r
- // Try up to 3 times to get a connection\r
- try {\r
- c = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD);\r
- } catch (SQLException e1) {\r
- if (++n >= 3)\r
- throw e1;\r
- }\r
- } while (c == null);\r
- }\r
- }\r
- if (c != null && !c.isValid(1)) {\r
- c.close();\r
- c = null;\r
- }\r
- }\r
- return c;\r
- }\r
- /**\r
- * Returns a JDBC connection to the pool.\r
- * @param c the Connection to return\r
- * @throws SQLException\r
- */\r
- public void release(Connection c) {\r
- if (c != null) {\r
- synchronized (queue) {\r
- if (!queue.contains(c))\r
- queue.add(c);\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
- && retroFit2()\r
- && retroFit3()\r
- && retroFit4()\r
- && retroFit5()\r
- && retroFit6()\r
- && retroFit7()\r
- && retroFit8()\r
- && retroFit9() //New retroFit call to add CREATED_DATE column Rally:US674199 - 1610\r
- && retroFit10() //New retroFit call to add BUSINESS_DESCRIPTION column Rally:US708102 - 1610\r
- && retroFit11() //New retroFit call for groups feature Rally:US708115 - 1610 \r
- ;\r
- }\r
- /**\r
- * Retrofit 1 - Make sure the expected tables are in MySQL and are initialized.\r
- * Uses mysql_init_0000 and mysql_init_0001 to setup the DB.\r
- * @return true if the retrofit worked, false otherwise\r
- */\r
- private boolean retroFit1() {\r
- final String[] expected_tables = {\r
- "FEEDS", "FEED_ENDPOINT_ADDRS", "FEED_ENDPOINT_IDS", "PARAMETERS", "SUBSCRIPTIONS"\r
- };\r
- Connection c = null;\r
- try {\r
- c = getConnection();\r
- Set<String> tables = getTableSet(c);\r
- boolean initialize = false;\r
- for (String s : expected_tables) {\r
- initialize |= !tables.contains(s);\r
- }\r
- if (initialize) {\r
- intlogger.info("PROV9001: First time startup; The database is being initialized.");\r
- runInitScript(c, 0); // script 0 creates the provisioning tables\r
- runInitScript(c, 1); // script 1 initializes PARAMETERS\r
- }\r
- } catch (SQLException e) {\r
- intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage());\r
- return false;\r
- } finally {\r
- if (c != null)\r
- release(c);\r
- }\r
- return true;\r
- }\r
- /**\r
- * Retrofit 2 - if the LOG_RECORDS table is missing, add it.\r
- * Uses mysql_init_0002 to create this table.\r
- * @return true if the retrofit worked, false otherwise\r
- */\r
- private boolean retroFit2() {\r
- Connection c = null;\r
- try {\r
- // If LOG_RECORDS table is missing, add it\r
- c = getConnection();\r
- Set<String> tables = getTableSet(c);\r
- if (!tables.contains("LOG_RECORDS")) {\r
- intlogger.info("PROV9002: Creating LOG_RECORDS table.");\r
- runInitScript(c, 2); // script 2 creates the LOG_RECORDS table\r
- }\r
- } catch (SQLException e) {\r
- intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage());\r
- return false;\r
- } finally {\r
- if (c != null)\r
- release(c);\r
- }\r
- return true;\r
- }\r
- /**\r
- * Retrofit 3 - if the FEEDS_UNIQUEID table (from release 1.0.*) exists, drop it.\r
- * If SUBSCRIPTIONS.SUBID still has the auto_increment attribute, remove it.\r
- * @return true if the retrofit worked, false otherwise\r
- */\r
- @SuppressWarnings("resource")\r
- private boolean retroFit3() {\r
- Connection c = null;\r
- try {\r
- // if SUBSCRIPTIONS.SUBID still has auto_increment, remove it\r
- boolean doremove = false;\r
- c = getConnection();\r
- DatabaseMetaData md = c.getMetaData();\r
- ResultSet rs = md.getColumns("datarouter", "", "SUBSCRIPTIONS", "SUBID");\r
- if (rs != null) {\r
- while (rs.next()) {\r
- doremove = rs.getString("IS_AUTOINCREMENT").equals("YES");\r
- }\r
- rs.close();\r
- rs = null;\r
- }\r
- if (doremove) {\r
- intlogger.info("PROV9002: Modifying SUBSCRIPTIONS SUBID column to remove auto increment.");\r
- Statement s = c.createStatement();\r
- s.execute("ALTER TABLE SUBSCRIPTIONS MODIFY COLUMN SUBID INT UNSIGNED NOT NULL");\r
- s.close();\r
- }\r