Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for broken mssql-jdbc.properties location logic #2579

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.CodeSource;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.Date;
Expand All @@ -36,7 +39,6 @@
.getLogger("com.microsoft.sqlserver.jdbc.ConfigurableRetryLogic");
private static final String SEMI_COLON = ";";
private static final String COMMA = ",";
private static final String FORWARD_SLASH = "/";
private static final String EQUALS_SIGN = "=";
private static final String RETRY_EXEC = "retryExec";
private static final String RETRY_CONN = "retryConn";
Expand Down Expand Up @@ -283,16 +285,33 @@
private static String getCurrentClassPath() throws SQLServerException {
String location = "";
String className = "";
String locationSuffix = "target/classes/";
Jeffery-Wasty marked this conversation as resolved.
Show resolved Hide resolved

try {
// Attempt to get the location and CodeSource for this class
className = new Object() {}.getClass().getEnclosingClass().getName();
location = Class.forName(className).getProtectionDomain().getCodeSource().getLocation().getPath();
Jeffery-Wasty marked this conversation as resolved.
Show resolved Hide resolved
location = location.substring(0, location.length() - 16);
URI uri = new URI(location + FORWARD_SLASH);
CodeSource codeSource = ConfigurableRetryLogic.class.getProtectionDomain().getCodeSource();

if (codeSource == null) {
// CodeSource should never be null, so throw an error
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_UnableToFindClass"));
Object[] msgArgs = {ConfigurableRetryLogic.class};
throw new SQLServerException(form.format(msgArgs), null, 0, null);

Check warning on line 300 in src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java#L298-L300

Added lines #L298 - L300 were not covered by tests
} else {
if (Files.isDirectory(Paths.get(codeSource.getLocation().toURI()))) {
// We check if the Path we get from the CodeSource location is a directory. If so, we are running
// from class files and should remove a suffix (i.e. the props file is in a different location from the
// location returned)
location = location.substring(0, location.length() - locationSuffix.length());
}
}

URI uri = new URI(location);
return uri.getPath() + DEFAULT_PROPS_FILE; // For now, we only allow "mssql-jdbc.properties" as file name.
} catch (URISyntaxException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_URLInvalid"));
Object[] msgArgs = {location + FORWARD_SLASH};
Object[] msgArgs = {location};

Check warning on line 314 in src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java#L314

Added line #L314 was not covered by tests
throw new SQLServerException(form.format(msgArgs), null, 0, e);
} catch (ClassNotFoundException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_UnableToFindClass"));
Expand Down
Loading