From a85fb3a87185a45aeca196c2e1f73e79e6ee88d7 Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Sat, 1 Mar 2014 15:26:17 -0500 Subject: [PATCH] corrected db initialization sequence Former-commit-id: bfea90ba44673f49b76d509688a6e4a4d6a912a0 --- .../data/nvdcve/ConnectionFactory.java | 109 +++++++++--------- 1 file changed, 55 insertions(+), 54 deletions(-) diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/nvdcve/ConnectionFactory.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/nvdcve/ConnectionFactory.java index b94d9cb2a..43c8c97d5 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/nvdcve/ConnectionFactory.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/nvdcve/ConnectionFactory.java @@ -85,66 +85,67 @@ public final class ConnectionFactory { if (connectionString != null) { return; } - - //load the driver if necessary - final String driverName = Settings.getString(Settings.KEYS.DB_DRIVER_NAME, ""); - if (!driverName.isEmpty()) { //likely need to load the correct driver - Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Loading driver: {0}", driverName); - final String driverPath = Settings.getString(Settings.KEYS.DB_DRIVER_PATH, ""); - try { - if (!driverPath.isEmpty()) { - Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Loading driver from: {0}", driverPath); - driver = DriverLoader.load(driverName, driverPath); - } else { - driver = DriverLoader.load(driverName); - } - } catch (DriverLoadException ex) { - Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, "Unable to load database driver", ex); - throw new DatabaseException("Unable to load database driver"); - } - } - userName = Settings.getString(Settings.KEYS.DB_USER, "dcuser"); - //yes, yes - hard-coded password - only if there isn't one in the properties file. - password = Settings.getString(Settings.KEYS.DB_PASSWORD, "DC-Pass1337!"); - try { - connectionString = getConnectionString(); - } catch (IOException ex) { - Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, - "Unable to retrieve the database connection string", ex); - throw new DatabaseException("Unable to retrieve the database connection string"); - } - boolean shouldCreateSchema = false; - try { - if (connectionString.startsWith("jdbc:h2:file:")) { //H2 - shouldCreateSchema = !dbSchemaExists(); - Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Need to create DB Structure: {0}", shouldCreateSchema); - } - } catch (IOException ioex) { - Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, "Unable to verify database exists", ioex); - throw new DatabaseException("Unable to verify database exists"); - } - Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Loading database connection"); - Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Connection String: {0}", connectionString); - Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Database User: {0}", userName); - Connection conn = null; try { - conn = DriverManager.getConnection(connectionString, userName, password); - } catch (SQLException ex) { - if (ex.getMessage().contains("java.net.UnknownHostException") && connectionString.contains("AUTO_SERVER=TRUE;")) { - connectionString = connectionString.replace("AUTO_SERVER=TRUE;", ""); + //load the driver if necessary + final String driverName = Settings.getString(Settings.KEYS.DB_DRIVER_NAME, ""); + if (!driverName.isEmpty()) { //likely need to load the correct driver + Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Loading driver: {0}", driverName); + final String driverPath = Settings.getString(Settings.KEYS.DB_DRIVER_PATH, ""); try { - conn = DriverManager.getConnection(connectionString, userName, password); - Settings.setString(Settings.KEYS.DB_CONNECTION_STRING, connectionString); - Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, - "Unable to start the database in server mode; reverting to single user mode"); - } catch (SQLException sqlex) { + if (!driverPath.isEmpty()) { + Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Loading driver from: {0}", driverPath); + driver = DriverLoader.load(driverName, driverPath); + } else { + driver = DriverLoader.load(driverName); + } + } catch (DriverLoadException ex) { + Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, "Unable to load database driver", ex); + throw new DatabaseException("Unable to load database driver"); + } + } + userName = Settings.getString(Settings.KEYS.DB_USER, "dcuser"); + //yes, yes - hard-coded password - only if there isn't one in the properties file. + password = Settings.getString(Settings.KEYS.DB_PASSWORD, "DC-Pass1337!"); + try { + connectionString = getConnectionString(); + } catch (IOException ex) { + Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, + "Unable to retrieve the database connection string", ex); + throw new DatabaseException("Unable to retrieve the database connection string"); + } + boolean shouldCreateSchema = false; + try { + if (connectionString.startsWith("jdbc:h2:file:")) { //H2 + shouldCreateSchema = !dbSchemaExists(); + Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Need to create DB Structure: {0}", shouldCreateSchema); + } + } catch (IOException ioex) { + Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, "Unable to verify database exists", ioex); + throw new DatabaseException("Unable to verify database exists"); + } + Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Loading database connection"); + Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Connection String: {0}", connectionString); + Logger.getLogger(CveDB.class.getName()).log(Level.FINE, "Database User: {0}", userName); + + try { + conn = DriverManager.getConnection(connectionString, userName, password); + } catch (SQLException ex) { + if (ex.getMessage().contains("java.net.UnknownHostException") && connectionString.contains("AUTO_SERVER=TRUE;")) { + connectionString = connectionString.replace("AUTO_SERVER=TRUE;", ""); + try { + conn = DriverManager.getConnection(connectionString, userName, password); + Settings.setString(Settings.KEYS.DB_CONNECTION_STRING, connectionString); + Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, + "Unable to start the database in server mode; reverting to single user mode"); + } catch (SQLException sqlex) { + Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, "Unable to connect to the database", ex); + throw new DatabaseException("Unable to connect to the database"); + } + } else { Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, "Unable to connect to the database", ex); throw new DatabaseException("Unable to connect to the database"); } - } else { - Logger.getLogger(ConnectionFactory.class.getName()).log(Level.FINE, "Unable to connect to the database", ex); - throw new DatabaseException("Unable to connect to the database"); } if (shouldCreateSchema) {