dk.netarkivet.archive.checksum
Class FileChecksumArchive

java.lang.Object
  extended by dk.netarkivet.archive.checksum.ChecksumArchive
      extended by dk.netarkivet.archive.checksum.FileChecksumArchive

public final class FileChecksumArchive
extends ChecksumArchive

A checksum archive in the form of a file (as alternative to a database).
Each entry in the file is on its own line, thus the number of lines is the number of entries.
The entries on a line is in the format of a ChecksumJob:
'filename' + ## + 'checksum'
The lines are not sorted.


Method Summary
protected  java.lang.String calculateChecksum(java.io.File f)
          Method for calculating the checksum of a file.
protected  java.lang.String calculateChecksum(java.io.InputStream is)
          Method for calculating the checksum of a inputstream.
 void cleanup()
          The method for cleaning up when done.
 java.io.File getAllFilenames()
          Method for retrieving the names of all the files within the archive as a temporary file.
 java.io.File getArchiveAsFile()
          Method for retrieving the archive as a temporary file containing the checksum entries.
 java.lang.String getChecksum(java.lang.String filename)
          Method for retrieving the checksum of a record, based on the filename.
 java.lang.String getFilename()
          Method for retrieving the name of the checksum file.
static FileChecksumArchive getInstance()
          Method for obtaining the current singleton instance of this class.
 boolean hasEnoughSpace()
          Method for testing where there is enough space left on local drive.
 boolean removeRecord(java.lang.String filename, java.lang.String checksum)
          Method for removing a record.
 void upload(RemoteFile arcfile, java.lang.String filename)
          The method for uploading an arcFile to the archive.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static FileChecksumArchive getInstance()
Method for obtaining the current singleton instance of this class. If the instance of this class has not yet been constructed, then it will be initialised.

Returns:
The current instance of this class.

getFilename

public java.lang.String getFilename()
Method for retrieving the name of the checksum file.

Returns:
The checksum file name.

hasEnoughSpace

public boolean hasEnoughSpace()
Method for testing where there is enough space left on local drive.

Specified by:
hasEnoughSpace in class ChecksumArchive
Returns:
Whether there is enough space left.

upload

public void upload(RemoteFile arcfile,
                   java.lang.String filename)
            throws IOFailure,
                   ArgumentNotValid
The method for uploading an arcFile to the archive.

Specified by:
upload in class ChecksumArchive
Parameters:
arcfile - The remote file containing the arcFile to upload.
filename - The name of the arcFile.
Throws:
IOFailure - If the entry cannot be added to the archive.
ArgumentNotValid - If the RemoteFile is null or if the filename is not valid.

getChecksum

public java.lang.String getChecksum(java.lang.String filename)
                             throws IOFailure,
                                    ArgumentNotValid
Method for retrieving the checksum of a record, based on the filename.

Specified by:
getChecksum in class ChecksumArchive
Parameters:
filename - The name of the file to have recorded in the archive.
Returns:
The checksum of a record.
Throws:
IOFailure - If the file is not within the archive.
ArgumentNotValid - If the filename is not valid (null or empty).

calculateChecksum

protected java.lang.String calculateChecksum(java.io.File f)
                                      throws IOFailure
Method for calculating the checksum of a file.

Specified by:
calculateChecksum in class ChecksumArchive
Parameters:
f - The file to calculate the checksum of.
Returns:
The checksum of the file.
Throws:
IOFailure - If a IOException is caught during the calculation of the MD5-checksum.

calculateChecksum

protected java.lang.String calculateChecksum(java.io.InputStream is)
                                      throws IOFailure
Method for calculating the checksum of a inputstream.

Specified by:
calculateChecksum in class ChecksumArchive
Parameters:
is - The inputstream to calculate the checksum of.
Returns:
The checksum of the inputstream.
Throws:
IOFailure - If a error occurs during the generation of the MD5 checksum.

removeRecord

public boolean removeRecord(java.lang.String filename,
                            java.lang.String checksum)
                     throws IOFailure,
                            ArgumentNotValid
Method for removing a record. This is used to correct the record. This finds the record and removes it if the checksum is not correct.

Specified by:
removeRecord in class ChecksumArchive
Parameters:
filename - The name of the file whose entry in the archive should be removed.
checksum - The valid checksum for the entry in the archive.
Returns:
Whether the record was removed. It returns false when either the entry cannot be found in the archive, or that it already has the correct checksum.
Throws:
IOFailure - If the archive file cannot be recreated without the given record.
ArgumentNotValid - If the filename is either null or empty, or if the checksum is null.

getArchiveAsFile

public java.io.File getArchiveAsFile()
                              throws IOFailure
Method for retrieving the archive as a temporary file containing the checksum entries. Each line should contain one checksum entry in the format produced by the ChecksumJob.

Specified by:
getArchiveAsFile in class ChecksumArchive
Returns:
A temporary checksum file, which is a copy of the archive file.
Throws:
IOFailure - If problems occurs during the creation of the file.

getAllFilenames

public java.io.File getAllFilenames()
                             throws IOFailure
Method for retrieving the names of all the files within the archive as a temporary file.

Specified by:
getAllFilenames in class ChecksumArchive
Returns:
A temporary file containing the list of all the filenames. This file has one filename per line.
Throws:
IOFailure - If problems occurs during the creation of the file.

cleanup

public void cleanup()
The method for cleaning up when done. It sets the checksum file and the instance to null.

Specified by:
cleanup in class ChecksumArchive