Skip to content

Commit

Permalink
Fix for mssql-jdbc.properties location logic (#2579)
Browse files Browse the repository at this point in the history
* Remove maximum testing as this is very dependent on pipeline behavior.

* Added an additional check for mssql-jdbc.properties to prevent IndexOutOfBoundsExcelption

* Fixed formatting

* Fixed formatting

* Fixed formatting

* Fixed formatting

* Fixed according to code review

* Don't need to handle null codesource case, will never happen

* Cleanup

* Formatting

* Remove a variable that's only used in one place

---------

Co-authored-by: machavan <machavan@microsoft.com>
  • Loading branch information
Jeffery-Wasty and machavan authored Feb 11, 2025
1 parent b1d9b3e commit 83072af
Showing 1 changed file with 13 additions and 5 deletions.
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 @@ public class ConfigurableRetryLogic {
.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 @@ -287,12 +288,19 @@ private static String getCurrentClassPath() throws SQLServerException {
try {
className = new Object() {}.getClass().getEnclosingClass().getName();
location = Class.forName(className).getProtectionDomain().getCodeSource().getLocation().getPath();
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() - ("target/classes/").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};
throw new SQLServerException(form.format(msgArgs), null, 0, e);
} catch (ClassNotFoundException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_UnableToFindClass"));
Expand Down

0 comments on commit 83072af

Please sign in to comment.