Attachment 'ChecksumJob.java'
Download 1 /* File: $Id: ChecksumJob.java 544 2008-11-19 16:59:25Z svc $
2 * Date: $Date: 2008-11-19 17:59:25 +0100 (Wed, 19 Nov 2008) $
3 * Revision: $Revision: 544 $
4 * Author: $Author: svc $
5 *
6 * The Netarchive Suite - Software to harvest and preserve websites
7 * Copyright 2004-2007 Det Kongelige Bibliotek and Statsbiblioteket, Denmark
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 package dk.netarkivet.archive.arcrepository.bitpreservation;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.ObjectInputStream;
28 import java.io.ObjectOutputStream;
29 import java.io.OutputStream;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import dk.netarkivet.common.exceptions.ArgumentNotValid;
35 import dk.netarkivet.common.exceptions.IOFailure;
36 import dk.netarkivet.common.utils.KeyValuePair;
37 import dk.netarkivet.common.utils.MD5;
38 import dk.netarkivet.common.utils.batch.FileBatchJob;
39
40
41 /**
42 * Class responsible for checksumming files locally in a
43 * bit archive application.
44 *
45 */
46 public class ChecksumJob extends FileBatchJob {
47
48 protected transient Log log = LogFactory.getLog(getClass().getName());
49
50 /**
51 * Initialization of a ChecksumJob: a new structure for storing files
52 * failed is created.
53 *
54 * @see FileBatchJob#initialize(OutputStream)
55 */
56 public void initialize(OutputStream os) {
57 }
58
59 /**
60 * Generates MD5 checksum for file identified by 'file' and writes
61 * the checksum to the given OutputStream.
62 * Errors during checksumming are logged and files on which checksumming
63 * fails are stored in filesFailed.
64 *
65 * @param file The file to process.
66 * @param os The outputStream to write the result to
67 * @return false, if errors occurred while processing the file
68 * @see FileBatchJob#processFile(File, OutputStream)
69 */
70 public boolean processFile(File file, OutputStream os) {
71 ArgumentNotValid.checkNotNull(file, "file");
72 try {
73 os.write((file.getName()
74 + dk.netarkivet.archive.arcrepository.bitpreservation
75 .Constants.STRING_FILENAME_SEPARATOR
76 + MD5.generateMD5onFile(file) + "\n").getBytes());
77 } catch (IOException e) {
78 log.warn("Checksumming of file " + file.getName()
79 + " failed: ", e);
80 return false;
81 }
82 return true;
83 }
84
85 /**
86 * Finishing the job requires nothing particular.
87 *
88 * @see FileBatchJob#finish(OutputStream)
89 */
90 public void finish(OutputStream os) {
91 }
92
93 /** Create a line in checksum job format from a filename and a checksum.
94 *
95 * @param filename A filename (no path)
96 * @param checksum An MD5 checksum
97 * @return A string of the correct format for a checksum job output.
98 */
99 public static String makeLine(String filename, String checksum) {
100 ArgumentNotValid.checkNotNullOrEmpty(filename, "filename");
101 ArgumentNotValid.checkNotNullOrEmpty(checksum, "checksum");
102 return filename + Constants.STRING_FILENAME_SEPARATOR + checksum;
103 }
104
105 /** Parse a line of output into a key-value pair.
106 *
107 * @param line The line to parse, of the form
108 * <filename>##<checksum>
109 * @return The filename->checksum mapping.
110 * @throws ArgumentNotValid if the line is not on the correct form.
111 */
112 public static KeyValuePair<String, String> parseLine(String line) {
113 ArgumentNotValid.checkNotNull(line, "checksum line");
114 String[] parts = line.split(Constants.STRING_FILENAME_SEPARATOR);
115 if (parts.length != 2) {
116 throw new ArgumentNotValid("String '" + line + "' is not on"
117 + " checksum output form");
118 }
119 return new KeyValuePair<String, String>(parts[0], parts[1]);
120 }
121
122 /**
123 * Write a human-readily description of this ChecksumJob object.
124 * Writes out the name of the ChecksumJob, the number of files processed,
125 * and the number of files that failed during processing.
126 * @return a human-readily description of this ChecksumJob object
127 */
128 public String toString() {
129 int noOfFailedFiles;
130 if (filesFailed == null) {
131 noOfFailedFiles = 0;
132 } else {
133 noOfFailedFiles = filesFailed.size();
134 }
135 return ("Checksum job " + getClass().getName()
136 + ": [Files Processed = " + noOfFilesProcessed
137 + "; Files failed = " + noOfFailedFiles + "]");
138 }
139
140 /**
141 * Invoke default method for deserializing object, and reinitialise the
142 * logger.
143 * @param s the InputStream
144 */
145 private void readObject(ObjectInputStream s) {
146 try {
147 s.defaultReadObject();
148 } catch (Exception e) {
149 throw new IOFailure("Unexpected error during deserialization", e);
150 }
151 log = LogFactory.getLog(getClass().getName());
152 }
153
154 /**
155 * Invoke default method for serializing object.
156 * @param s the OutputStream
157 */
158 private void writeObject(ObjectOutputStream s) {
159 try {
160 s.defaultWriteObject();
161 } catch (Exception e) {
162 throw new IOFailure("Unexpected error during serialization", e);
163 }
164 }
165
166 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.