From 66842fca8e76169111f97bd06e990673ef02a1f7 Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Fri, 3 Jan 2014 13:40:25 -0500 Subject: [PATCH] Moved most functions to CveDB.java; slowly converting the class to a pure DB utility rather then a base class Former-commit-id: 7b1a18596000b3506798d521801a6bb1eace32da --- .../owasp/dependencycheck/data/BaseDB.java | 145 ------------------ 1 file changed, 145 deletions(-) diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/BaseDB.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/BaseDB.java index 4579e74e1..d78446247 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/BaseDB.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/BaseDB.java @@ -42,151 +42,6 @@ import org.owasp.dependencycheck.utils.Settings; */ public class BaseDB { - /** - * Resource location for SQL file used to create the database schema. - */ - public static final String DB_STRUCTURE_RESOURCE = "data/initialize.sql"; - /** - * The version of the current DB Schema. - */ - public static final String DB_SCHEMA_VERSION = "2.8"; - /** - * Database connection - */ - private Connection conn; - - /** - * Returns the database connection. - * - * @return the database connection - */ - protected Connection getConnection() { - return conn; - } - - /** - * Opens the database connection. If the database does not exist, it will - * create a new one. - * - * @throws IOException thrown if there is an IO Exception - * @throws SQLException thrown if there is a SQL Exception - * @throws DatabaseException thrown if there is an error initializing a new - * database - * @throws ClassNotFoundException thrown if the h2 database driver cannot be - * loaded - */ - @edu.umd.cs.findbugs.annotations.SuppressWarnings( - value = "DMI_EMPTY_DB_PASSWORD", - justification = "Yes, I know... Blank password.") - public void open() throws IOException, SQLException, DatabaseException, ClassNotFoundException { - final String fileName = CveDB.getDataDirectory().getCanonicalPath(); - final File f = new File(fileName, "cve." + DB_SCHEMA_VERSION); - final File check = new File(f.getAbsolutePath() + ".h2.db"); - final boolean createTables = !check.exists(); - final String connStr = String.format("jdbc:h2:file:%s;AUTO_SERVER=TRUE", f.getAbsolutePath()); - Class.forName("org.h2.Driver"); - conn = DriverManager.getConnection(connStr, "sa", ""); - if (createTables) { - createTables(); - } - } - - /** - * Closes the DB4O database. Close should be called on this object when it - * is done being used. - */ - public void close() { - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - final String msg = "There was an error attempting to close the CveDB, see the log for more details."; - Logger.getLogger(BaseDB.class.getName()).log(Level.SEVERE, msg, ex); - Logger.getLogger(BaseDB.class.getName()).log(Level.FINE, null, ex); - } - conn = null; - } - } - - /** - * Commits all completed transactions. - * - * @throws SQLException thrown if a SQL Exception occurs - */ - public void commit() throws SQLException { - if (conn != null) { - conn.commit(); - } - } - - /** - * Cleans up the object and ensures that "close" has been called. - * - * @throws Throwable thrown if there is a problem - */ - @Override - protected void finalize() throws Throwable { - close(); - super.finalize(); //not necessary if extending Object. - } - - /** - * Creates the database structure (tables and indexes) to store the CVE data - * - * @throws SQLException thrown if there is a sql exception - * @throws DatabaseException thrown if there is a database exception - */ - private void createTables() throws SQLException, DatabaseException { - InputStream is; - InputStreamReader reader; - BufferedReader in = null; - try { - is = this.getClass().getClassLoader().getResourceAsStream(DB_STRUCTURE_RESOURCE); - reader = new InputStreamReader(is, "UTF-8"); - in = new BufferedReader(reader); - final StringBuilder sb = new StringBuilder(2110); - String tmp; - while ((tmp = in.readLine()) != null) { - sb.append(tmp); - } - Statement statement = null; - try { - statement = conn.createStatement(); - statement.execute(sb.toString()); - } finally { - closeStatement(statement); - } - } catch (IOException ex) { - throw new DatabaseException("Unable to create database schema", ex); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException ex) { - Logger.getLogger(CveDB.class - .getName()).log(Level.FINEST, null, ex); - } - } - } - } - - /** - * Retrieves the directory that the JAR file exists in so that we can ensure - * we always use a common data directory. - * - * @return the data directory for this index. - * @throws IOException is thrown if an IOException occurs of course... - */ - public static File getDataDirectory() throws IOException { - final File path = Settings.getDataFile(Settings.KEYS.CVE_DATA_DIRECTORY); - if (!path.exists()) { - if (!path.mkdirs()) { - throw new IOException("Unable to create NVD CVE Data directory"); - } - } - return path; - } - /** * Returns the generated integer primary key for a newly inserted row. *