mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-27 11:31:24 +01:00
Replaced boiler-plate file read with simpler IOUtils call.
This commit is contained in:
@@ -30,6 +30,7 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.owasp.dependencycheck.utils.DBUtils;
|
import org.owasp.dependencycheck.utils.DBUtils;
|
||||||
import org.owasp.dependencycheck.utils.DependencyVersion;
|
import org.owasp.dependencycheck.utils.DependencyVersion;
|
||||||
import org.owasp.dependencycheck.utils.DependencyVersionUtil;
|
import org.owasp.dependencycheck.utils.DependencyVersionUtil;
|
||||||
@@ -250,22 +251,15 @@ public final class ConnectionFactory {
|
|||||||
*/
|
*/
|
||||||
private static void createTables(Connection conn) throws DatabaseException {
|
private static void createTables(Connection conn) throws DatabaseException {
|
||||||
LOGGER.debug("Creating database structure");
|
LOGGER.debug("Creating database structure");
|
||||||
InputStream is;
|
InputStream is = null;
|
||||||
InputStreamReader reader;
|
|
||||||
BufferedReader in = null;
|
|
||||||
try {
|
try {
|
||||||
is = ConnectionFactory.class.getClassLoader().getResourceAsStream(DB_STRUCTURE_RESOURCE);
|
is = ConnectionFactory.class.getClassLoader().getResourceAsStream(DB_STRUCTURE_RESOURCE);
|
||||||
reader = new InputStreamReader(is, "UTF-8");
|
final String dbStructure = IOUtils.toString(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;
|
Statement statement = null;
|
||||||
try {
|
try {
|
||||||
statement = conn.createStatement();
|
statement = conn.createStatement();
|
||||||
statement.execute(sb.toString());
|
statement.execute(dbStructure);
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
LOGGER.debug("", ex);
|
LOGGER.debug("", ex);
|
||||||
throw new DatabaseException("Unable to create database statement", ex);
|
throw new DatabaseException("Unable to create database statement", ex);
|
||||||
@@ -275,13 +269,7 @@ public final class ConnectionFactory {
|
|||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new DatabaseException("Unable to create database schema", ex);
|
throw new DatabaseException("Unable to create database schema", ex);
|
||||||
} finally {
|
} finally {
|
||||||
if (in != null) {
|
IOUtils.closeQuietly(is);
|
||||||
try {
|
|
||||||
in.close();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
LOGGER.trace("", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,9 +291,7 @@ public final class ConnectionFactory {
|
|||||||
}
|
}
|
||||||
if ("h2".equalsIgnoreCase(databaseProductName)) {
|
if ("h2".equalsIgnoreCase(databaseProductName)) {
|
||||||
LOGGER.debug("Updating database structure");
|
LOGGER.debug("Updating database structure");
|
||||||
InputStream is;
|
InputStream is = null;
|
||||||
InputStreamReader reader;
|
|
||||||
BufferedReader in = null;
|
|
||||||
String updateFile = null;
|
String updateFile = null;
|
||||||
try {
|
try {
|
||||||
updateFile = String.format(DB_STRUCTURE_UPDATE_RESOURCE, schema);
|
updateFile = String.format(DB_STRUCTURE_UPDATE_RESOURCE, schema);
|
||||||
@@ -313,17 +299,12 @@ public final class ConnectionFactory {
|
|||||||
if (is == null) {
|
if (is == null) {
|
||||||
throw new DatabaseException(String.format("Unable to load update file '%s'", updateFile));
|
throw new DatabaseException(String.format("Unable to load update file '%s'", updateFile));
|
||||||
}
|
}
|
||||||
reader = new InputStreamReader(is, "UTF-8");
|
final String dbStructureUpdate = IOUtils.toString(is, "UTF-8");
|
||||||
in = new BufferedReader(reader);
|
|
||||||
final StringBuilder sb = new StringBuilder(is.available());
|
|
||||||
String tmp;
|
|
||||||
while ((tmp = in.readLine()) != null) {
|
|
||||||
sb.append(tmp);
|
|
||||||
}
|
|
||||||
Statement statement = null;
|
Statement statement = null;
|
||||||
try {
|
try {
|
||||||
statement = conn.createStatement();
|
statement = conn.createStatement();
|
||||||
boolean success = statement.execute(sb.toString());
|
boolean success = statement.execute(dbStructureUpdate);
|
||||||
if (!success && statement.getUpdateCount() <= 0) {
|
if (!success && statement.getUpdateCount() <= 0) {
|
||||||
throw new DatabaseException(String.format("Unable to upgrade the database schema to %s", schema));
|
throw new DatabaseException(String.format("Unable to upgrade the database schema to %s", schema));
|
||||||
}
|
}
|
||||||
@@ -337,13 +318,7 @@ public final class ConnectionFactory {
|
|||||||
final String msg = String.format("Upgrade SQL file does not exist: %s", updateFile);
|
final String msg = String.format("Upgrade SQL file does not exist: %s", updateFile);
|
||||||
throw new DatabaseException(msg, ex);
|
throw new DatabaseException(msg, ex);
|
||||||
} finally {
|
} finally {
|
||||||
if (in != null) {
|
IOUtils.closeQuietly(is);
|
||||||
try {
|
|
||||||
in.close();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
LOGGER.trace("", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LOGGER.error("The database schema must be upgraded to use this version of dependency-check. Please see {} for more information.", UPGRADE_HELP_URL);
|
LOGGER.error("The database schema must be upgraded to use this version of dependency-check. Please see {} for more information.", UPGRADE_HELP_URL);
|
||||||
|
|||||||
Reference in New Issue
Block a user