Skip to content

Commit 83072af

Browse files
Fix for mssql-jdbc.properties location logic (#2579)
* 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 <[email protected]>
1 parent b1d9b3e commit 83072af

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.io.IOException;
1313
import java.net.URI;
1414
import java.net.URISyntaxException;
15+
import java.nio.file.Files;
16+
import java.nio.file.Paths;
1517
import java.text.MessageFormat;
1618
import java.util.Collections;
1719
import java.util.Date;
@@ -36,7 +38,6 @@ public class ConfigurableRetryLogic {
3638
.getLogger("com.microsoft.sqlserver.jdbc.ConfigurableRetryLogic");
3739
private static final String SEMI_COLON = ";";
3840
private static final String COMMA = ",";
39-
private static final String FORWARD_SLASH = "/";
4041
private static final String EQUALS_SIGN = "=";
4142
private static final String RETRY_EXEC = "retryExec";
4243
private static final String RETRY_CONN = "retryConn";
@@ -287,12 +288,19 @@ private static String getCurrentClassPath() throws SQLServerException {
287288
try {
288289
className = new Object() {}.getClass().getEnclosingClass().getName();
289290
location = Class.forName(className).getProtectionDomain().getCodeSource().getLocation().getPath();
290-
location = location.substring(0, location.length() - 16);
291-
URI uri = new URI(location + FORWARD_SLASH);
292-
return uri.getPath() + DEFAULT_PROPS_FILE; // For now, we only allow "mssql-jdbc.properties" as file name.
291+
292+
if (Files.isDirectory(Paths
293+
.get(ConfigurableRetryLogic.class.getProtectionDomain().getCodeSource().getLocation().toURI()))) {
294+
// We check if the Path we get from the CodeSource location is a directory. If so, we are running
295+
// from class files and should remove a suffix (i.e. the props file is in a different location from the
296+
// location returned)
297+
location = location.substring(0, location.length() - ("target/classes/").length());
298+
}
299+
300+
return new URI(location).getPath() + DEFAULT_PROPS_FILE; // TODO: Allow custom paths
293301
} catch (URISyntaxException e) {
294302
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_URLInvalid"));
295-
Object[] msgArgs = {location + FORWARD_SLASH};
303+
Object[] msgArgs = {location};
296304
throw new SQLServerException(form.format(msgArgs), null, 0, e);
297305
} catch (ClassNotFoundException e) {
298306
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_UnableToFindClass"));

0 commit comments

Comments
 (0)