com.github.webdriverextensions.Repository Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webdriverextensions-maven-plugin Show documentation
Show all versions of webdriverextensions-maven-plugin Show documentation
Use this plugin to manage, download and install WebDriver drivers directly from your pom.
package com.github.webdriverextensions;
import ch.lambdaj.function.compare.ArgumentComparator;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.settings.Proxy;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import static ch.lambdaj.Lambda.*;
import static com.github.webdriverextensions.Utils.*;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.lang3.CharEncoding.UTF_8;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.is;
public class Repository {
private List drivers;
public static Repository load(URL repositoryUrl, Proxy proxySettings) throws MojoExecutionException {
String repositoryAsString;
try {
repositoryAsString = downloadAsString(repositoryUrl, proxySettings);
} catch (IOException e) {
throw new InstallDriversMojoExecutionException("Failed to download repository from url " + quote(repositoryUrl), e);
}
Repository repository;
try {
repository = new Gson().fromJson(repositoryAsString, Repository.class);
} catch (JsonSyntaxException e) {
throw new InstallDriversMojoExecutionException("Failed to parse repository json " + repositoryAsString, e);
}
repository.drivers = sortDrivers(repository.drivers);
return repository;
}
private static List sortDrivers(List drivers) {
Comparator byId = new ArgumentComparator(on(Driver.class).getId());
Comparator byVersion = new ArgumentComparator(on(Driver.class).getVersion());
Comparator orderByIdAndVersion = ComparatorUtils.chainedComparator(byId, byVersion);
return sort(drivers, on(Driver.class), orderByIdAndVersion);
}
private static String downloadAsString(URL url, Proxy proxySettings) throws IOException {
URLConnection connection;
if (proxySettings != null) {
java.net.Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP,
new InetSocketAddress(proxySettings.getHost(), proxySettings.getPort()));
if (proxySettings.getUsername() != null) {
ProxyUtils.setProxyAuthenticator(proxySettings);
}
connection = url.openConnection(proxy);
} else {
connection = url.openConnection();
}
try (InputStream inputStream = connection.getInputStream()) {
return IOUtils.toString(inputStream, UTF_8);
}
}
public List getDrivers() {
return drivers;
}
public List getDrivers(String name, String platform, String bit, String version) {
List drivers = this.drivers;
if (name != null) {
drivers = select(drivers, having(on(Driver.class).getName(), is(equalToIgnoringCase(name))));
}
if (platform != null) {
drivers = select(drivers, having(on(Driver.class).getPlatform(), is(equalToIgnoringCase(platform))));
}
if (bit != null) {
drivers = select(drivers, having(on(Driver.class).getBit(), is(equalToIgnoringCase(bit))));
}
if (version != null) {
drivers = select(drivers, having(on(Driver.class).getComparableVersion(), is(new ComparableVersion(version))));
}
return drivers;
}
public Driver enrichDriver(Driver driver) throws MojoExecutionException {
if (isBlank(driver.getName())) {
throw new InstallDriversMojoExecutionException("Driver name must be set in configuration, driver: " + driver);
}
if (isNotBlank(driver.getUrl())) {
return driver;
}
if (isNotBlank(driver.getPlatform()) || isNotBlank(driver.getBit()) || isNotBlank(driver.getVersion())) {
// Explicit driver config make sure it exists in repo
if (getDrivers(driver.getName(), driver.getPlatform(), driver.getBit(), driver.getVersion()).size() == 0) {
throw new MojoExecutionException("Could not find driver: " + driver + System.lineSeparator()
+ System.lineSeparator()
+ "in repository: " + this);
}
}
if (isBlank(driver.getPlatform())) {
String platform;
if (isMac()) {
platform = "mac";
} else if (isLinux()) {
platform = "linux";
} else {
platform = "windows";
}
driver.setPlatform(platform);
}
if (isBlank(driver.getBit())) {
String bit;
if (isLinux() && is64Bit()) {
bit = "64";
} else {
bit = "32";
}
driver.setBit(bit);
}
if (isBlank(driver.getVersion())) {
driver.setVersion(getLatestDriverVersion(driver.getId()));
}
try {
return getDrivers(driver.getName(), driver.getPlatform(), driver.getBit(), driver.getVersion()).get(0);
} catch (IndexOutOfBoundsException e) {
// Could not find any driver for the current platform/bit/version in repo
return null;
}
}
public List getLatestDrivers() {
List latestDrivers = new ArrayList();
Collection driverNames = selectDistinct(collect(drivers, on(Driver.class).getName()));
String platform;
if (isMac()) {
platform = "mac";
} else if (isLinux()) {
platform = "linux";
} else {
platform = "windows";
}
String bit;
if (isLinux() && is64Bit()) {
bit = "64";
} else {
bit = "32";
}
for (String driverName : driverNames) {
List driversWithDriverName = select(drivers, having(on(Driver.class).getName(), is(driverName)));
List driversWithDriverNameAndPlatform = select(driversWithDriverName, having(on(Driver.class).getPlatform(), is(platform)));
List driversWithDriverNameAndPlatformAndBit = select(driversWithDriverNameAndPlatform, having(on(Driver.class).getBit(), is(bit)));
Driver latestDriver = selectMax(driversWithDriverNameAndPlatformAndBit, on(Driver.class).getComparableVersion());
if (latestDriver != null) {
latestDrivers.add(latestDriver);
}
}
return sortDrivers(latestDrivers);
}
public String getLatestDriverVersion(String driverId) {
List allDriverVersions = select(drivers, having(on(Driver.class).getId(), is(driverId)));
Driver latestDriver = selectMax(allDriverVersions, on(Driver.class).getComparableVersion());
if (latestDriver == null) {
return null;
}
return latestDriver.getVersion();
}
@Override
public String toString() {
return new GsonBuilder().setPrettyPrinting().create().toJson(this);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy