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 mssql-jdbc.properties location logic #2579

Merged
merged 17 commits into from
Feb 11, 2025
Merged
Changes from 16 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.Date;
Expand All @@ -36,7 +38,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 +284,24 @@
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 {
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);
return uri.getPath() + DEFAULT_PROPS_FILE; // For now, we only allow "mssql-jdbc.properties" as file name.

if (Files.isDirectory(Paths
.get(ConfigurableRetryLogic.class.getProtectionDomain().getCodeSource().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());
}

return new URI(location).getPath() + DEFAULT_PROPS_FILE; // TODO: Allow custom paths
} catch (URISyntaxException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_URLInvalid"));
Object[] msgArgs = {location + FORWARD_SLASH};
Object[] msgArgs = {location};

Check warning on line 304 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#L304

Added line #L304 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