Skip to content

Commit

Permalink
Include debug log with wdm version
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Aug 25, 2023
1 parent 2a88a4a commit f21e550
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static io.github.bonigarcia.wdm.config.OperatingSystem.WIN;
import static io.github.bonigarcia.wdm.online.Downloader.deleteFile;
import static io.github.bonigarcia.wdm.versions.Shell.runAndWait;
import static io.github.bonigarcia.wdm.versions.VersionDetector.getWdmVersion;
import static java.lang.Integer.parseInt;
import static java.lang.String.valueOf;
import static java.lang.System.getenv;
Expand Down Expand Up @@ -219,6 +220,10 @@ protected abstract List<URL> getDriverUrls(String driverVersion)
protected String downloadedDriverPath;

protected WebDriverManager() {
Optional<String> wdVersion = getWdmVersion(getClass());
if (wdVersion.equals(wdVersion)) {
log.debug("Using WebDriverManager {}", wdVersion.get());
}
config = new Config();
webDriverList = new CopyOnWriteArrayList<>();
}
Expand Down Expand Up @@ -1426,7 +1431,6 @@ protected UrlHandler createUrlHandler(String driverVersion)
String shortDriverName = getShortDriverName();
UrlHandler urlHandler = new UrlHandler(config(), candidateUrls,
driverVersion, shortDriverName, this::buildUrl);
log.trace("All driver URLs: {}", candidateUrls);

boolean getLatest = isUnknown(driverVersion);
boolean continueSearchingVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@
import java.util.Properties;
import java.util.stream.Collectors;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.w3c.dom.Document;

import io.github.bonigarcia.wdm.config.Config;
import io.github.bonigarcia.wdm.config.OperatingSystem;
Expand Down Expand Up @@ -438,4 +443,65 @@ public boolean isSnap() {
return isSnap;
}

public static final Optional<String> getWdmVersion(Class<?> clazz) {
try {
String className = clazz.getName();
String classfileName = "/" + className.replace('.', '/') + ".class";
URL classfileResource = clazz.getResource(classfileName);
if (classfileResource != null) {
Path absolutePackagePath = Paths.get(classfileResource.toURI())
.getParent();
int packagePathSegments = className.length()
- className.replace(".", "").length();
Path path = absolutePackagePath;
for (int i = 0, segmentsToRemove = packagePathSegments
+ 2; i < segmentsToRemove; i++) {
path = path.getParent();
}
Path pom = path.resolve("pom.xml");
try (InputStream is = Files.newInputStream(pom)) {
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(is);
doc.getDocumentElement().normalize();
String version = (String) XPathFactory.newInstance()
.newXPath().compile("/project/version")
.evaluate(doc, XPathConstants.STRING);
if (version != null) {
version = version.trim();
if (!version.isEmpty()) {
return Optional.of(version);
}
}
}
}
} catch (Exception e) {
// Ignore
}

try (InputStream is = clazz.getResourceAsStream(
"/META-INF/maven/io.github.bonigarcia/webdrivermanger/pom.properties")) {
if (is != null) {
Properties p = new Properties();
p.load(is);
String version = p.getProperty("version", "").trim();
if (!version.isEmpty()) {
return Optional.of(version);
}
}
} catch (Exception e) {
// Ignore
}

String version = null;
Package pkg = clazz.getPackage();
if (pkg != null) {
version = pkg.getImplementationVersion();
if (version == null) {
version = pkg.getSpecificationVersion();
}
}
version = version == null ? "" : version.trim();
return version.isEmpty() ? Optional.empty() : Optional.of(version);
}

}

0 comments on commit f21e550

Please sign in to comment.