diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java b/src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java index df9e6b956..e6e47dabb 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java @@ -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; @@ -36,7 +39,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"; @@ -283,16 +285,33 @@ private static void createConnectionRules(LinkedList listOfRules) throws private static String getCurrentClassPath() throws SQLServerException { String location = ""; String className = ""; + String locationSuffix = "target/classes/"; 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(); - 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); + } 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}; throw new SQLServerException(form.format(msgArgs), null, 0, e); } catch (ClassNotFoundException e) { MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_UnableToFindClass"));