Skip to content

Commit

Permalink
Fixed according to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffery-Wasty committed Jan 30, 2025
1 parent 742108c commit f902c81
Showing 1 changed file with 18 additions and 7 deletions.
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 Down Expand Up @@ -285,15 +288,23 @@ private static String getCurrentClassPath() throws SQLServerException {
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();
// When the driver is used as a jar / as a dependency, the above will be the correct "main" directory where
// the props file should be placed (as per documentation). When testing, or building the driver manually,
// the above will return with a suffix "target/classes/" which must be removed, as, with the first case, the
// properties file should be in the main directory.

if (location.endsWith(locationSuffix)) {
location = location.substring(0, location.length() - locationSuffix.length());
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);
Expand Down

0 comments on commit f902c81

Please sign in to comment.