Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions src/org/openbravo/ddlutils/util/DBSMOBUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -57,10 +56,12 @@
import org.apache.log4j.Logger;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.openbravo.base.exception.OBException;
import org.openbravo.ddlutils.task.DatabaseUtils;

public class DBSMOBUtil {

public static final String SSLMODE = "sslmode";
private Vector<ModuleRow> allModules = new Vector<ModuleRow>();
private Vector<ModuleRow> activeModules = new Vector<ModuleRow>();
private HashMap<String, Vector<String>> dependencies = new HashMap<String, Vector<String>>();
Expand Down Expand Up @@ -614,21 +615,72 @@
return statement;
}

private Connection getUnpooledConnection() {

Check failure on line 618 in src/org/openbravo/ddlutils/util/DBSMOBUtil.java

View check run for this annotation

SonarScanEtendo / SonarQube Code Analysis

src/org/openbravo/ddlutils/util/DBSMOBUtil.java#L618

Refactor this method to reduce its Cognitive Complexity from 32 to the 15 allowed.
Connection connection = null;
Properties connProps = new Properties();
try {
Properties obProps = getOpenbravoProperties();
String strURL = obProps.getProperty("bbdd.url");
if (obProps.getProperty("bbdd.rdbms").equalsIgnoreCase("POSTGRE")) {
strURL += "/" + obProps.getProperty("bbdd.sid");
}
connection = DriverManager.getConnection(strURL, obProps.getProperty("bbdd.user"),
obProps.getProperty("bbdd.password"));
} catch (SQLException e) {
getLog().error("Error while retrieving an unpooled connection: ", e);

// 1. Prepare base connection properties
connProps.setProperty("user", obProps.getProperty("bbdd.user"));
connProps.setProperty("password", obProps.getProperty("bbdd.password"));
Comment thread
RomanMagnoli marked this conversation as resolved.

// 2. Check if SSL is enabled
if ("true".equalsIgnoreCase(obProps.getProperty("bbdd.ssl"))) {
connProps.setProperty("ssl", "true");
connProps.setProperty(SSLMODE, obProps.getProperty("bbdd.sslmode", "verify-full"));

// Check if an sslfactory was specified.
String sslFactoryClass = obProps.getProperty("bbdd.sslfactory");

if (StringUtils.isNotBlank(sslFactoryClass)) {
// --- If 'bbdd.sslfactory' EXISTS, use it ---
Comment thread
RomanMagnoli marked this conversation as resolved.
// 'bbdd.sslrootcert' is ignored in this case.
connProps.setProperty("sslfactory", sslFactoryClass);
} else {
// --- If 'bbdd.sslfactory' DOES NOT EXIST, use 'bbdd.sslrootcert' ---
String sslRootCert = obProps.getProperty("bbdd.sslrootcert");
if (StringUtils.isNotBlank(sslRootCert)) {
File sslRootCertFile = new File(sslRootCert);
if (sslRootCertFile.exists() && sslRootCertFile.isFile()) {
Comment thread
RomanMagnoli marked this conversation as resolved.
if (!sslRootCertFile.canRead()) {
throw new OBException("SSL root certificate file is not readable: " + sslRootCertFile.getAbsolutePath());
}
connProps.setProperty("sslrootcert", sslRootCertFile.getAbsolutePath());
} else {
throw new OBException("SSL root certificate file not found: " + sslRootCert);
}
Comment thread
RomanMagnoli marked this conversation as resolved.
} else {
// If the mode requires verification, the certificate is mandatory.
if (StringUtils.equals("verify-full", (connProps.getProperty(SSLMODE))) ||
StringUtils.equals("verify-ca", (connProps.getProperty(SSLMODE)))) {
throw new OBException("bbdd.sslrootcert property is required when bbdd.sslfactory is not set and sslmode is " + connProps.getProperty(
SSLMODE));
}
}
}
Comment thread
RomanMagnoli marked this conversation as resolved.
Comment thread
RomanMagnoli marked this conversation as resolved.
}
return connection;
}

connection = DriverManager.getConnection(strURL, connProps);

} catch (SQLException e) {
String sslMode = connProps.getProperty(SSLMODE);
String sslFactory = connProps.getProperty("sslfactory");
String sslRootCert = connProps.getProperty("sslrootcert");
StringBuilder errorMsg = new StringBuilder("Failed to get unpooled connection");
errorMsg.append(". SSL configuration: ");
errorMsg.append("sslmode=").append(sslMode != null ? sslMode : "not set");

Check failure on line 676 in src/org/openbravo/ddlutils/util/DBSMOBUtil.java

View check run for this annotation

SonarScanEtendo / SonarQube Code Analysis

src/org/openbravo/ddlutils/util/DBSMOBUtil.java#L676

Define a constant instead of duplicating this literal "not set" 3 times.
errorMsg.append(", sslfactory=").append(sslFactory != null ? sslFactory : "not set");
errorMsg.append(", sslrootcert=").append(sslRootCert != null ? sslRootCert : "not set");
getLog().error("Error while retrieving an unpooled connection: " + errorMsg.toString(), e);
throw new OBException(errorMsg.toString(), e);
}
Comment thread
RomanMagnoli marked this conversation as resolved.
return connection;
}

public void deleteInstallTables(Platform platform, Database database) {
String sql = "DELETE FROM AD_MODULE_INSTALL";
Expand Down
Loading