Skip to content

Commit

Permalink
First step of repository parser code rewrite in preparation for #21 and
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardesco committed Mar 29, 2015
1 parent e64d479 commit e7f1b6f
Show file tree
Hide file tree
Showing 20 changed files with 726 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.lazerycode.selenium.download;

import com.lazerycode.selenium.extract.BinaryFileNames;
import com.lazerycode.selenium.repository.BinaryType;
import com.lazerycode.selenium.extract.ExtractFilesFromArchive;
import com.lazerycode.selenium.repository.FileDetails;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -67,7 +67,7 @@ public void ensureStandaloneExecutableFilesExist() throws MojoFailureException,
String extractedFileLocation = this.rootStandaloneServerDirectory.getAbsolutePath() + File.separator + fileToDownload.getKey();
String binaryForOperatingSystem = fileToDownload.getKey().replace("\\", "/").split("/")[1].toUpperCase(); //TODO should really store the OSType we have extracted somewhere rather than doing this hack!
LOG.debug("Detected a binary for OSType: " + binaryForOperatingSystem);
if (ExtractFilesFromArchive.extractFileFromArchive(fileToUnzip, extractedFileLocation, this.overwriteFilesThatExist, BinaryFileNames.valueOf(binaryForOperatingSystem))) {
if (ExtractFilesFromArchive.extractFileFromArchive(fileToUnzip, extractedFileLocation, this.overwriteFilesThatExist, BinaryType.valueOf(binaryForOperatingSystem))) {
LOG.info("File(s) copied to " + extractedFileLocation);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lazerycode.selenium.extract;

import com.lazerycode.selenium.repository.BinaryType;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
Expand Down Expand Up @@ -34,7 +35,7 @@ public class ExtractFilesFromArchive {
* @return boolean
* @throws IOException
*/
public static boolean extractFileFromArchive(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryFileNames possibleFilenames) throws IOException, IllegalArgumentException, MojoFailureException {
public static boolean extractFileFromArchive(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryType possibleFilenames) throws IOException, IllegalArgumentException, MojoFailureException {
String fileType = FilenameUtils.getExtension(downloadedCompressedFile.getAbsolutePath());
LOG.debug("Determined archive type: " + fileType);
if (fileType.equals("zip")) {
Expand All @@ -56,7 +57,7 @@ public static boolean extractFileFromArchive(File downloadedCompressedFile, Stri
* @throws IOException
*/

static boolean unzipFile(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryFileNames possibleFilenames) throws IOException {
static boolean unzipFile(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryType possibleFilenames) throws IOException {
Boolean fileExtracted = false;
LOG.debug("Extracting binary from .zip file");
ZipFile zip = new ZipFile(downloadedCompressedFile);
Expand Down Expand Up @@ -104,7 +105,7 @@ static boolean unzipFile(File downloadedCompressedFile, String extractedToFilePa
* @throws IOException
*/

static boolean untarFile(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryFileNames possibleFilenames) throws IOException, MojoFailureException {
static boolean untarFile(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryType possibleFilenames) throws IOException, MojoFailureException {
Boolean fileExtracted = false;
ArrayList<String> filenamesWeAreSearchingFor = possibleFilenames.getBinaryFilenames();
ArchiveInputStream fileInArchive;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/lazerycode/selenium/hash/HashTypeAdaptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.lazerycode.selenium.hash;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class HashTypeAdaptor extends XmlAdapter<String, HashType> {

@Override
public HashType unmarshal(String str) throws Exception {
return HashType.valueOf(str.toUpperCase());
}

@Override
public String marshal(HashType hashType) throws Exception {
return hashType.toString().toLowerCase();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.lazerycode.selenium.extract;
package com.lazerycode.selenium.repository;

import java.util.ArrayList;

public enum BinaryFileNames {
public enum BinaryType {
INTERNETEXPLORER(new ArrayList<String>() {{
add("IEDriverServer.exe");
}}),
Expand All @@ -21,7 +21,7 @@ public enum BinaryFileNames {

private final ArrayList<String> binaryFilenames;

BinaryFileNames(ArrayList<String> binaryFilenames) {
BinaryType(ArrayList<String> binaryFilenames) {
this.binaryFilenames = binaryFilenames;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.lazerycode.selenium.repository;

public class DriverContext {
private final BinaryType driverType;
private final SystemArchitecture systemArchitecture;
private final OperatingSystem operatingSystem;

private DriverContext(BinaryType driverType, OperatingSystem operatingSystem, SystemArchitecture systemArchitecture) {
this.operatingSystem = operatingSystem;
this.driverType = driverType;
this.systemArchitecture = systemArchitecture;
}

private DriverContext(String driverType, String operatingSystem, SystemArchitecture systemArchitecture) {
this.operatingSystem = OperatingSystem.valueOf(operatingSystem.toUpperCase());
this.driverType = BinaryType.valueOf(driverType.toUpperCase());
this.systemArchitecture = systemArchitecture;
}

public static DriverContext binaryDataFor(BinaryType browserType, OperatingSystem osName, SystemArchitecture architecture) {
return new DriverContext(browserType, osName, architecture);
}

public static DriverContext binaryDataFor(String browserType, String osName, SystemArchitecture architecture) {
return new DriverContext(browserType, osName, architecture);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

DriverContext that = (DriverContext) o;

if (driverType != that.driverType) return false;
if (operatingSystem != that.operatingSystem) return false;
if (systemArchitecture != that.systemArchitecture) return false;

return true;
}

@Override
public int hashCode() {
int result = driverType.hashCode();
result = 31 * result + systemArchitecture.hashCode();
result = 31 * result + operatingSystem.hashCode();
return result;
}

@Override
public String toString() {
return this.operatingSystem.getOperatingSystemType() + " - " + this.driverType.toString().toLowerCase() + " - " + this.systemArchitecture.getSystemArchitectureType();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.lazerycode.selenium.repository;

import com.lazerycode.selenium.hash.HashType;
import com.lazerycode.selenium.hash.HashTypeAdaptor;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.net.URL;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class DriverDetails {

@XmlElement(name = "filelocation")
URL fileLocation;

@XmlElement()
String hash;

@XmlElement(name = "hashtype")
@XmlJavaTypeAdapter(HashTypeAdaptor.class)
HashType hashType;

@Override
public int hashCode() {
int result = fileLocation != null ? fileLocation.hashCode() : 0;
result = 31 * result + (hash != null ? hash.hashCode() : 0);
result = 31 * result + (hashType != null ? hashType.hashCode() : 0);
return result;
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/lazerycode/selenium/repository/DriverMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.lazerycode.selenium.repository;

import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.TreeMap;

public class DriverMap {

protected HashMap<DriverContext, TreeMap<String, DriverDetails>> repository = new HashMap<DriverContext, TreeMap<String, DriverDetails>>();

public TreeMap<String, DriverDetails> getMapForDriverContext(DriverContext driverContext) {
if (!repository.containsKey(driverContext)) {
repository.put(driverContext, new TreeMap<String, DriverDetails>());
}

return repository.get(driverContext);
}

public DriverDetails getDetailsForVersionOfDriverContext(DriverContext driverContext, String version) throws IllegalArgumentException {
if (!repository.containsKey(driverContext)) {
throw new IllegalArgumentException("Driver context not found in driver repository");
}

TreeMap<String, DriverDetails> driverVersions = repository.get(driverContext);
DriverDetails detailsToReturn = driverVersions.get(driverVersions.lastKey());
if (detailsToReturn.hashCode() == 0) {
throw new NoSuchElementException("No driver version " + version + " exists for the context " + driverContext.toString());
}

return detailsToReturn;
}

public DriverDetails getDetailsForLatestVersionOfDriverContext(DriverContext driverContext) {
if (!repository.containsKey(driverContext)) {
throw new IllegalArgumentException("Driver context not found in driver repository");
}

TreeMap<String, DriverDetails> driverVersions = repository.get(driverContext);

return driverVersions.get(driverVersions.lastKey());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.net.MalformedURLException;
import java.net.URL;

@Deprecated
public class FileDetails {

private URL fileLocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.lazerycode.selenium.repository;

import org.apache.maven.plugin.MojoFailureException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.xpath.XPathExpressionException;
import java.net.MalformedURLException;

import static com.lazerycode.selenium.repository.SystemArchitecture.ARCHITECTURE_32_BIT;
import static com.lazerycode.selenium.repository.SystemArchitecture.ARCHITECTURE_64_BIT;

public class FileRepository {

public static DriverMap buildDownloadableFileRepository(NodeList nodesFound) throws MalformedURLException, MojoFailureException, JAXBException, XPathExpressionException {
DriverMap driverMap = new DriverMap();
Unmarshaller unmarshaller = JAXBContext.newInstance(DriverDetails.class).createUnmarshaller();
unmarshaller.setEventHandler(new unmarshallingEventHandler());
for (int nodeNumber = 0; nodeNumber < nodesFound.getLength(); nodeNumber++) {
Node node = nodesFound.item(nodeNumber);
String operatingSystem = node.getParentNode().getParentNode().getParentNode().getNodeName();
String driver = node.getParentNode().getParentNode().getAttributes().getNamedItem("id").getNodeValue();
String version = node.getParentNode().getAttributes().getNamedItem("id").getNodeValue();
boolean thisIs64Bit = false;
boolean thisIs32Bit = false;
if (node.getAttributes().getNamedItem("thirtytwobit") != null) {
if (Boolean.valueOf(node.getAttributes().getNamedItem("thirtytwobit").getNodeValue())) {
thisIs32Bit = true;
}
}
if (node.getAttributes().getNamedItem("sixtyfourbit") != null) {
if (Boolean.valueOf(node.getAttributes().getNamedItem("sixtyfourbit").getNodeValue())) {
thisIs64Bit = true;
}
}

DriverDetails driverDetails = unmarshaller.unmarshal(node, DriverDetails.class).getValue();
if (thisIs32Bit) {
driverMap.getMapForDriverContext(DriverContext.binaryDataFor(driver, operatingSystem, ARCHITECTURE_32_BIT)).put(version, driverDetails);
}
if (thisIs64Bit) {
driverMap.getMapForDriverContext(DriverContext.binaryDataFor(driver, operatingSystem, ARCHITECTURE_64_BIT)).put(version, driverDetails);
}
}

return driverMap;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lazerycode.selenium.repository;

public enum OperatingSystem {

WINDOWS("windows"),
OSX("mac"),
LINUX("linux");

private String operatingSystemName;

OperatingSystem(String operatingSystemName) {
this.operatingSystemName = operatingSystemName;
}

String getOperatingSystemType() {
return operatingSystemName;
}

static OperatingSystem getOperatingSystem() {

String name = System.getProperties().getProperty("os.name");

for (OperatingSystem operatingSystemName : values()) {
if (name.toLowerCase().contains(operatingSystemName.getOperatingSystemType())) {
return operatingSystemName;
}
}

throw new IllegalArgumentException("Unrecognised operating system name '" + name + "'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.HashMap;
import java.util.Map;

@Deprecated
public class RepositoryParser {

private static final Logger LOG = Logger.getLogger(RepositoryParser.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.lazerycode.selenium.repository;

import java.util.Arrays;
import java.util.List;

public enum SystemArchitecture {

ARCHITECTURE_64_BIT("64 bit"),
ARCHITECTURE_32_BIT("32 bit");

private String systemArchitectureName;

SystemArchitecture(String systemArchitectureName) {
this.systemArchitectureName = systemArchitectureName;
}

String getSystemArchitectureType() {
return systemArchitectureName;
}

public static final SystemArchitecture defaultSystemArchitecture = ARCHITECTURE_32_BIT;
private static List<String> architecture64bitNames = Arrays.asList("amd64", "x86_64");

static SystemArchitecture getSystemArchitecture() {

final String currentArchitecture = System.getProperties().getProperty("os.arch");

SystemArchitecture result = defaultSystemArchitecture;

if (architecture64bitNames.contains(currentArchitecture)) {
result = ARCHITECTURE_64_BIT;
}

return result;
}
}
Loading

0 comments on commit e7f1b6f

Please sign in to comment.