Skip to content

Commit

Permalink
Synchronisation of Downloader class
Browse files Browse the repository at this point in the history
Downloader.download() method has been synchronised in order to ensure
that multiple instances try to download the driver just once.
  • Loading branch information
igracia committed Jul 7, 2015
1 parent be6a819 commit 8ba5157
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/github/bonigarcia/wdm/BrowserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void manage(Architecture arch, String version) {
for (URL url : urls) {
String export = urlFilter.contains(url) ? getExportParameter()
: null;
new Downloader().download(url, versionToDownload, export);
Downloader.download(url, versionToDownload, export);
}
} catch (RuntimeException re) {
throw re;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/io/github/bonigarcia/wdm/ChromeDriverManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@
* @since 1.0.0
*/
public class ChromeDriverManager extends BrowserManager {

private static ChromeDriverManager instance = null;

public static ChromeDriverManager instance;
protected ChromeDriverManager() {
}

public static ChromeDriverManager getInstance() {
if (instance == null) {
instance = new ChromeDriverManager();
synchronized(ChromeDriverManager.class) {
if (instance == null) {
instance = new ChromeDriverManager();
}
}
}
return instance;
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/github/bonigarcia/wdm/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public class Downloader {

private static final String HOME = "~";

public void download(URL url, String version, String export)
public static final synchronized void download(URL url, String version, String export)
throws IOException {
File targetFile = new File(getTarget(version, url));
File binary;

if (!targetFile.getParentFile().exists()
|| WdmConfig.getBoolean("wdm.override")) {
log.info("Downloading " + url + " to " + targetFile);
log.info("Downloading {} to {}", url, targetFile);
FileUtils.copyURLToFile(url, targetFile);

binary = unZip(targetFile);
Expand All @@ -63,11 +63,11 @@ public void download(URL url, String version, String export)

}

public File unZip(String fileInput, String outputFolder) throws IOException {
public static final File unZip(String fileInput, String outputFolder) throws IOException {
return null;
}

public File unZip(File folder) throws IOException {
public static final File unZip(File folder) throws IOException {
ZipFile zipFolder = new ZipFile(folder);
Enumeration<?> enu = zipFolder.entries();
File file = null;
Expand Down Expand Up @@ -113,7 +113,7 @@ public File unZip(File folder) throws IOException {
return file.getAbsoluteFile();
}

private String getTarget(String version, URL url) throws IOException {
private static final String getTarget(String version, URL url) throws IOException {
String zip = url.getFile().substring(url.getFile().lastIndexOf("/"));

int iFirst = zip.indexOf("_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,23 @@
*/
public class InternetExplorerDriverManager extends BrowserManager {

private static InternetExplorerDriverManager instance = null;
private static InternetExplorerDriverManager instance;

protected InternetExplorerDriverManager() {
}

public static InternetExplorerDriverManager getInstance() {
if (instance == null) {
instance = new InternetExplorerDriverManager();
synchronized(InternetExplorerDriverManager.class) {
if (instance == null) {
instance = new InternetExplorerDriverManager();
}
}
}
return instance;
}


@Override
protected List<URL> getDrivers(Architecture arch, String version) throws Exception {
String driverVersion = (version == null) ? WdmConfig.getString("wdm.internetExplorerVersion") : version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@
*/
public class OperaDriverManager extends BrowserManager {

private static OperaDriverManager instance = null;
private static OperaDriverManager instance;

protected OperaDriverManager() {
}

public static OperaDriverManager getInstance() {
if (instance == null) {
instance = new OperaDriverManager();
synchronized(OperaDriverManager.class) {
if (instance == null) {
instance = new OperaDriverManager();
}
}
}
return instance;
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/io/github/bonigarcia/wdm/WdmConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
public class WdmConfig {

private static WdmConfig instance = null;
private static WdmConfig instance;
private Config conf;

protected WdmConfig() {
Expand All @@ -37,7 +37,12 @@ protected WdmConfig() {

public static WdmConfig getInstance() {
if (instance == null) {
instance = new WdmConfig();
synchronized(WdmConfig.class) {
if (instance == null) {
instance = new WdmConfig();
}
}

}
return instance;
}
Expand Down

0 comments on commit 8ba5157

Please sign in to comment.