Package dk.netarkivet.archive.checksum
Class FileChecksumArchive
- java.lang.Object
-
- dk.netarkivet.archive.checksum.FileChecksumArchive
-
- All Implemented Interfaces:
ChecksumArchive
public final class FileChecksumArchive extends Object implements 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.If no file exists when the class is instantiated then it will be created, and if an 'admin.data' file exists, then it will be loaded and put into the archive file.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
calculateChecksum(File f)
Method for calculating the checksum of a file.String
calculateChecksum(InputStream is)
Method for calculating the checksum of a inputstream.void
cleanup()
The method for cleaning up when done.File
correct(String filename, File correctFile)
Method for correcting a bad entry from the archive.File
getAllFilenames()
Method for retrieving the names of all the files within the archive as a temporary file.File
getArchiveAsFile()
Method for retrieving the archive as a temporary file containing the checksum entries.String
getChecksum(String filename)
Method for retrieving the checksum of a record, based on the filename.String
getFileName()
Method for retrieving the name of the checksum file.static FileChecksumArchive
getInstance()
Method for obtaining the current singleton instance of this class.String
getWrongEntryFilename()
Method for retrieving the name of the wrongEntryFile.boolean
hasEnoughSpace()
Method for testing where there is enough space left on local drive.boolean
hasEntry(String filename)
Method for checking whether an entry exists within the archive.void
upload(RemoteFile file, String filename)
The method for uploading a file to the archive.void
upload(String checksum, String filename)
Upload a filename with a pre-computed checksum.
-
-
-
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 String getFileName()
Method for retrieving the name of the checksum file.- Returns:
- The checksum file name.
-
getWrongEntryFilename
public String getWrongEntryFilename()
Method for retrieving the name of the wrongEntryFile.- Returns:
- The wrong entry file name.
-
hasEnoughSpace
public boolean hasEnoughSpace()
Method for testing where there is enough space left on local drive.- Specified by:
hasEnoughSpace
in interfaceChecksumArchive
- Returns:
- Whether there is enough space left.
-
upload
public void upload(RemoteFile file, String filename) throws ArgumentNotValid, IllegalState
The method for uploading a file to the archive.- Specified by:
upload
in interfaceChecksumArchive
- Parameters:
file
- The remote file containing the file to be uploaded.filename
- The name of the arcFile.- Throws:
ArgumentNotValid
- If the RemoteFile is null or if the filename is not valid.IllegalState
- If the file already within the archive but with a different checksum.
-
upload
public void upload(String checksum, String filename) throws ArgumentNotValid, IllegalState
Description copied from interface:ChecksumArchive
Upload a filename with a pre-computed checksum.- Specified by:
upload
in interfaceChecksumArchive
- Throws:
ArgumentNotValid
IllegalState
-
getChecksum
public String getChecksum(String filename) throws ArgumentNotValid
Method for retrieving the checksum of a record, based on the filename.- Specified by:
getChecksum
in interfaceChecksumArchive
- Parameters:
filename
- The name of the file to have recorded in the archive.- Returns:
- The checksum of a record, or null if it was not found.
- Throws:
ArgumentNotValid
- If the filename is not valid (null or empty).
-
hasEntry
public boolean hasEntry(String filename)
Method for checking whether an entry exists within the archive.- Specified by:
hasEntry
in interfaceChecksumArchive
- Parameters:
filename
- The name of the file whose entry in the archive should be determined.- Returns:
- Whether an entry with the filename was found.
-
calculateChecksum
public String calculateChecksum(File f) throws IOFailure
Method for calculating the checksum of a file.- Specified by:
calculateChecksum
in interfaceChecksumArchive
- 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
public String calculateChecksum(InputStream is) throws IOFailure
Method for calculating the checksum of a inputstream.- Specified by:
calculateChecksum
in interfaceChecksumArchive
- 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.
-
correct
public File correct(String filename, File correctFile) throws IOFailure, ArgumentNotValid, IllegalState
Method for correcting a bad entry from the archive. The current incorrect entry is put into the wrongEntryFile. Then it calculates the checksum and corrects the entry for the file, and then the checksum file is recreated from the archive in the memory.- Specified by:
correct
in interfaceChecksumArchive
- Parameters:
filename
- The name of the file whose record should be removed.correctFile
- The file that should replace the current entry- Returns:
- A file containing the removed entry.
- Throws:
ArgumentNotValid
- If one of the arguments are not valid.IOFailure
- If the entry cannot be corrected. Either the bad entry cannot be stored, or the new checksum file cannot be created. Or if a file for the removed entry cannot be created.IllegalState
- If no such entry exists to be corrected, or if the entry has a different checksum than the incorrectChecksum.
-
getArchiveAsFile
public 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 interfaceChecksumArchive
- 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 File getAllFilenames() throws IOFailure
Method for retrieving the names of all the files within the archive as a temporary file.- Specified by:
getAllFilenames
in interfaceChecksumArchive
- 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 interfaceChecksumArchive
-
-