Skip to content

Commit

Permalink
refs PR 112 - fixed threading issue when creating list of exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnscancella committed Feb 26, 2018
1 parent 39654ff commit 0e4f80b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
13 changes: 6 additions & 7 deletions src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Collection;
import java.util.Collections;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -151,10 +152,8 @@ public void isValid(final Bag bag, final boolean ignoreHiddenFiles) throws IOExc
void checkHashes(final Manifest manifest) throws CorruptChecksumException, InterruptedException, VerificationException{
final CountDownLatch latch = new CountDownLatch( manifest.getFileToChecksumMap().size());

//TODO maybe return all of these at some point...
//if that is ever the case make sure to use Collections.synchronizedCollection(new ArrayList<>())
//we aren't doing it now because it is a huge performance hit for little value
final List<Exception> exceptions = new ArrayList<>();
//TODO maybe return all of these at some point...
final Collection<Exception> exceptions = Collections.synchronizedCollection(new ArrayList<>());

for(final Entry<Path, String> entry : manifest.getFileToChecksumMap().entrySet()){
executor.execute(new CheckManifestHashesTask(entry, manifest.getAlgorithm().getMessageDigestName(), latch, exceptions));
Expand All @@ -163,7 +162,7 @@ void checkHashes(final Manifest manifest) throws CorruptChecksumException, Inter
latch.await();

if(!exceptions.isEmpty()){
final Exception e = exceptions.get(0);
final Exception e = exceptions.iterator().next();
if(e instanceof CorruptChecksumException){
logger.debug(messages.getString("checksums_not_matching_error"), exceptions.size());
throw (CorruptChecksumException)e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
Expand All @@ -27,10 +27,10 @@ public class CheckManifestHashesTask implements Runnable {

private final Entry<Path, String> entry;
private final CountDownLatch latch;
private final List<Exception> exceptions;
private final Collection<Exception> exceptions;
private final String algorithm;

public CheckManifestHashesTask(final Entry<Path, String> entry, final String algorithm, final CountDownLatch latch, final List<Exception> exceptions) {
public CheckManifestHashesTask(final Entry<Path, String> entry, final String algorithm, final CountDownLatch latch, final Collection<Exception> exceptions) {
this.entry = entry;
this.algorithm = algorithm;
this.latch = latch;
Expand Down Expand Up @@ -68,7 +68,7 @@ public CountDownLatch getLatch() {
return latch;
}

public List<Exception> getExceptions() {
public Collection<Exception> getExceptions() {
return exceptions;
}

Expand Down

0 comments on commit 0e4f80b

Please sign in to comment.