Skip to content

Commit

Permalink
a little bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
johnscancella committed Nov 22, 2016
1 parent 4f8fae7 commit 7d8ecb0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/main/java/gov/loc/repository/bagit/reader/BagReader.java
Expand Up @@ -34,7 +34,6 @@
/**
* Responsible for reading a bag from the filesystem.
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public class BagReader {
private static final Logger logger = LoggerFactory.getLogger(PayloadFileExistsInManifestVistor.class);

Expand Down Expand Up @@ -265,6 +264,7 @@ public Bag readBagMetadata(final Path rootDir, final Bag bag) throws IOException
*
* @throws IOException if there is a problem reading a file
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public Bag readFetch(final Path fetchFile, final Bag bag) throws IOException{
logger.info("Attempting to read [{}]", fetchFile);
final Bag newBag = new Bag(bag);
Expand All @@ -289,6 +289,7 @@ public Bag readFetch(final Path fetchFile, final Bag bag) throws IOException{
return newBag;
}

@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
protected List<Pair<String, String>> readKeyValuesFromFile(final Path file, final String splitRegex) throws IOException, InvalidBagMetadataException{
final List<Pair<String, String>> keyValues = new ArrayList<>();
final BufferedReader br = Files.newBufferedReader(file);
Expand Down
Expand Up @@ -2,29 +2,29 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
* A simple task to check if a file exists on the filesystem. This is thread safe, so many can be called at once.
*/
@SuppressWarnings(value = {"PMD.DoNotUseThreads", "PMD.AvoidStringBufferField"})
@SuppressWarnings(value = {"PMD.DoNotUseThreads"})
public class CheckIfFileExistsTask implements Runnable {
private transient final Path file;
private transient final StringBuilder messageBuilder;
private transient final List<Path> missingFiles;
private transient final CountDownLatch latch;

public CheckIfFileExistsTask(final Path file, final StringBuilder messageBuilder, final CountDownLatch latch) {
public CheckIfFileExistsTask(final Path file, final List<Path> missingFiles, final CountDownLatch latch) {
this.file = file;
this.messageBuilder = messageBuilder;
this.latch = latch;
this.missingFiles = missingFiles;
}

@Override
public void run() {
if(!Files.exists(file)){
messageBuilder.append("Manifest lists file [").append(file).append("] but it does not exist").append(System.lineSeparator());
missingFiles.add(file);
}
latch.countDown();
}

}
12 changes: 6 additions & 6 deletions src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java
Expand Up @@ -44,7 +44,6 @@
/**
* Responsible for verifying if a bag is valid, complete
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public class BagVerifier {
private static final Logger logger = LoggerFactory.getLogger(BagVerifier.class);

Expand Down Expand Up @@ -165,6 +164,7 @@ public void isValid(final Bag bag, final boolean ignoreHiddenFiles) throws IOExc
* @throws InterruptedException if the thread is interrupted
* @throws VerificationException if there is some other exception while checking the checksum(s)
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
protected void checkHashes(final Manifest manifest) throws CorruptChecksumException, InterruptedException, VerificationException{
final ExecutorService executor = Executors.newCachedThreadPool();
final CountDownLatch latch = new CountDownLatch( manifest.getFileToChecksumMap().size());
Expand Down Expand Up @@ -318,22 +318,22 @@ protected Set<Path> getAllFilesListedInManifests(final Bag bag) throws IOExcepti
return filesListedInManifests;
}

@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
protected void checkAllFilesListedInManifestExist(final Set<Path> files) throws FileNotInPayloadDirectoryException, InterruptedException{
final ExecutorService executor = Executors.newCachedThreadPool();
final CountDownLatch latch = new CountDownLatch(files.size());
final StringBuilder messageBuilder = new StringBuilder();
final List<Path> missingFiles = new ArrayList<>();

logger.debug("Checking if all files listed in the manifest(s) exist");
for(final Path file : files){
executor.execute(new CheckIfFileExistsTask(file, messageBuilder, latch));
executor.execute(new CheckIfFileExistsTask(file, missingFiles, latch));
}

latch.await();
executor.shutdown();

final String missingFilesMessage = messageBuilder.toString();
if(!missingFilesMessage.isEmpty()){
throw new FileNotInPayloadDirectoryException(missingFilesMessage);
if(!missingFiles.isEmpty()){
throw new FileNotInPayloadDirectoryException("Manifest(s) contains file(s) " + missingFiles + " but they don't exist!");
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/gov/loc/repository/bagit/writer/BagWriter.java
Expand Up @@ -28,7 +28,7 @@
/**
* responsible for writing out a bag.
*/
@SuppressWarnings(value = {"PMD.TooManyMethods", "PMD.AvoidInstantiatingObjectsInLoops"}) //TODO refactor to remove methods?
@SuppressWarnings("PMD.TooManyMethods")
public final class BagWriter {
private static final Logger logger = LoggerFactory.getLogger(BagWriter.class);
private static final Version VERSION_0_98 = new Version(0, 98);
Expand Down Expand Up @@ -152,6 +152,7 @@ public static void writePayloadManifests(final Set<Manifest> manifests, final Pa
writeManifests(manifests, outputDir, bagitRootDir, "manifest-", charsetName);
}

@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static Set<Manifest> updateTagManifests(final Bag bag, final Path newBagRootDir) throws NoSuchAlgorithmException, IOException{
final Set<Manifest> newManifests = new HashSet<>();

Expand Down

0 comments on commit 7d8ecb0

Please sign in to comment.