Skip to content

Commit 0e4f80b

Browse files
committed
refs PR 112 - fixed threading issue when creating list of exceptions
1 parent 39654ff commit 0e4f80b

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import java.io.IOException;
44
import java.nio.file.Path;
55
import java.util.ArrayList;
6-
import java.util.List;
7-
import java.util.ResourceBundle;
6+
import java.util.Collection;
7+
import java.util.Collections;
88
import java.util.Map.Entry;
9+
import java.util.ResourceBundle;
910
import java.util.concurrent.CountDownLatch;
1011
import java.util.concurrent.ExecutorService;
1112
import java.util.concurrent.Executors;
@@ -151,10 +152,8 @@ public void isValid(final Bag bag, final boolean ignoreHiddenFiles) throws IOExc
151152
void checkHashes(final Manifest manifest) throws CorruptChecksumException, InterruptedException, VerificationException{
152153
final CountDownLatch latch = new CountDownLatch( manifest.getFileToChecksumMap().size());
153154

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

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

165164
if(!exceptions.isEmpty()){
166-
final Exception e = exceptions.get(0);
165+
final Exception e = exceptions.iterator().next();
167166
if(e instanceof CorruptChecksumException){
168167
logger.debug(messages.getString("checksums_not_matching_error"), exceptions.size());
169168
throw (CorruptChecksumException)e;

src/main/java/gov/loc/repository/bagit/verify/CheckManifestHashesTask.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import java.nio.file.Path;
66
import java.security.MessageDigest;
77
import java.security.NoSuchAlgorithmException;
8-
import java.util.List;
9-
import java.util.ResourceBundle;
8+
import java.util.Collection;
109
import java.util.Map.Entry;
10+
import java.util.ResourceBundle;
1111
import java.util.concurrent.CountDownLatch;
1212

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

2828
private final Entry<Path, String> entry;
2929
private final CountDownLatch latch;
30-
private final List<Exception> exceptions;
30+
private final Collection<Exception> exceptions;
3131
private final String algorithm;
3232

33-
public CheckManifestHashesTask(final Entry<Path, String> entry, final String algorithm, final CountDownLatch latch, final List<Exception> exceptions) {
33+
public CheckManifestHashesTask(final Entry<Path, String> entry, final String algorithm, final CountDownLatch latch, final Collection<Exception> exceptions) {
3434
this.entry = entry;
3535
this.algorithm = algorithm;
3636
this.latch = latch;
@@ -68,7 +68,7 @@ public CountDownLatch getLatch() {
6868
return latch;
6969
}
7070

71-
public List<Exception> getExceptions() {
71+
public Collection<Exception> getExceptions() {
7272
return exceptions;
7373
}
7474

0 commit comments

Comments
 (0)