From 833c0ee571a6b115c02256888714fef582c3fc7c Mon Sep 17 00:00:00 2001 From: Manuel Blechschmidt Date: Mon, 12 Aug 2019 16:18:54 +0200 Subject: [PATCH] #90 added synchronized keywords to parts that should be synchronized --- .../selenium/SeleniumServerMojo.java | 2 +- .../selenium/download/DownloadHandler.java | 32 ++++++++++--------- .../selenium/extract/FileExtractor.java | 6 ++-- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/lazerycode/selenium/SeleniumServerMojo.java b/src/main/java/com/lazerycode/selenium/SeleniumServerMojo.java index a823127..4d64d69 100644 --- a/src/main/java/com/lazerycode/selenium/SeleniumServerMojo.java +++ b/src/main/java/com/lazerycode/selenium/SeleniumServerMojo.java @@ -195,7 +195,7 @@ public class SeleniumServerMojo extends AbstractMojo { private static final Logger LOG = Logger.getLogger(SeleniumServerMojo.class); @Override - public synchronized void execute() throws MojoExecutionException, MojoFailureException { + public void execute() throws MojoExecutionException, MojoFailureException { BasicConfigurator.configure(new MavenLoggerLog4jBridge(getLog())); LOG.info(" "); LOG.info("--------------------------------------------------------"); diff --git a/src/main/java/com/lazerycode/selenium/download/DownloadHandler.java b/src/main/java/com/lazerycode/selenium/download/DownloadHandler.java index 65e3a03..d151b47 100644 --- a/src/main/java/com/lazerycode/selenium/download/DownloadHandler.java +++ b/src/main/java/com/lazerycode/selenium/download/DownloadHandler.java @@ -41,7 +41,8 @@ public DownloadHandler(File rootStandaloneServerDirectory, File downloadedZipFil fileDownloader.setConnectTimeout(fileDownloadConnectTimeout); } - private boolean checkFileHash(File fileToCheck, String hash, HashType hashType) throws IOException, MojoExecutionException { + private synchronized boolean checkFileHash(File fileToCheck, String hash, HashType hashType) throws IOException, MojoExecutionException { + FileHashChecker fileHashChecker = new FileHashChecker(fileToCheck); fileHashChecker.setExpectedHash(hash, hashType); @@ -59,30 +60,31 @@ private boolean checkFileHash(File fileToCheck, String hash, HashType hashType) * @throws URISyntaxException Invalid URI */ protected File downloadFile(DriverDetails driverDetails, boolean shouldWeCheckFileHash) throws MojoExecutionException, IOException, URISyntaxException { - URL remoteFileLocation = driverDetails.fileLocation; final String filename = FilenameUtils.getName(remoteFileLocation.getFile()); - for (int retryAttempts = 1; retryAttempts <= this.fileDownloadRetryAttempts; retryAttempts++) { - File downloadedFile = fileDownloader.attemptToDownload(remoteFileLocation); - if (null != downloadedFile) { - if (!shouldWeCheckFileHash || checkFileHash(downloadedFile, driverDetails.hash, driverDetails.hashType)) { - LOG.info("Archive file '" + downloadedFile.getName() + "' is valid : true"); - return downloadedFile; - } else { - LOG.info("Archive file '" + downloadedFile.getName() + "' is valid : false"); + synchronized (filename.intern()) { + for (int retryAttempts = 1; retryAttempts <= this.fileDownloadRetryAttempts; retryAttempts++) { + File downloadedFile = fileDownloader.attemptToDownload(remoteFileLocation); + if (null != downloadedFile) { + if (!shouldWeCheckFileHash || checkFileHash(downloadedFile, driverDetails.hash, driverDetails.hashType)) { + LOG.info("Archive file '" + downloadedFile.getName() + "' is valid : true"); + return downloadedFile; + } else { + LOG.info("Archive file '" + downloadedFile.getName() + "' is valid : false"); + } + } + LOG.info("Problem downloading '" + filename + "'... "); + if (retryAttempts < this.fileDownloadRetryAttempts) { + LOG.info("Retry attempt " + (retryAttempts) + " for '" + filename + "'"); } - } - LOG.info("Problem downloading '" + filename + "'... "); - if (retryAttempts < this.fileDownloadRetryAttempts) { - LOG.info("Retry attempt " + (retryAttempts) + " for '" + filename + "'"); } } throw new MojoExecutionException("Unable to successfully download '" + filename + "'!"); } - public DriverMap ensureStandaloneExecutableFilesExist() throws MojoFailureException, MojoExecutionException, IOException, URISyntaxException { + public synchronized DriverMap ensureStandaloneExecutableFilesExist() throws MojoFailureException, MojoExecutionException, IOException, URISyntaxException { LOG.info("Archives will be downloaded to '" + this.downloadedZipFileDirectory.getAbsolutePath() + "'"); LOG.info("Standalone executable files will be extracted to '" + this.rootStandaloneServerDirectory + "'"); LOG.info(" "); diff --git a/src/main/java/com/lazerycode/selenium/extract/FileExtractor.java b/src/main/java/com/lazerycode/selenium/extract/FileExtractor.java index 7be78e8..78aa434 100644 --- a/src/main/java/com/lazerycode/selenium/extract/FileExtractor.java +++ b/src/main/java/com/lazerycode/selenium/extract/FileExtractor.java @@ -46,7 +46,7 @@ public FileExtractor(boolean overwriteFilesThatExist) { * @throws IllegalArgumentException Unsupported archive * @throws MojoFailureException Error running plugin */ - public String extractFileFromArchive(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, IllegalArgumentException, MojoFailureException { + public synchronized String extractFileFromArchive(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, IllegalArgumentException, MojoFailureException { DownloadableFileType fileType = DownloadableFileType.valueOf(FilenameUtils.getExtension(downloadedCompressedFile.getName()).toUpperCase()); LOG.debug("Determined archive type: " + fileType); LOG.debug("Overwrite files that exist: " + overwriteFilesThatExist); @@ -80,8 +80,7 @@ public String extractFileFromArchive(File downloadedCompressedFile, String extra * @return boolean * @throws IOException IOException */ - - String unzipFile(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, ExpectedFileNotFoundException { + synchronized String unzipFile(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, ExpectedFileNotFoundException { LOG.debug("Attempting to extract binary from .zip file..."); ArrayList filenamesWeAreSearchingFor = new ArrayList(possibleFilenames.getBinaryFilenames()); ZipFile zip = new ZipFile(downloadedCompressedFile); @@ -118,7 +117,6 @@ String unzipFile(File downloadedCompressedFile, String extractedToFilePath, Bina * @return boolean * @throws IOException MojoFailureException */ - private String untarFile(InputStream compressedFileInputStream, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, ExpectedFileNotFoundException { LOG.debug("Attempting to extract binary from a .tar file..."); ArchiveEntry currentFile;