001/*
002 * #%L
003 * Netarchivesuite - common
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.common.utils.batch;
025
026import java.io.File;
027import java.io.IOException;
028import java.io.ObjectInputStream;
029import java.io.OutputStream;
030
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import dk.netarkivet.common.exceptions.ArgumentNotValid;
035import dk.netarkivet.common.exceptions.IOFailure;
036
037/**
038 * A batch job which returns a list of all files in the bitarchive in which it runs.
039 */
040@SuppressWarnings({"serial"})
041public class FileListJob extends FileBatchJob {
042
043    private static final transient Logger log = LoggerFactory.getLogger(FileListJob.class);
044
045    /** The constructor. */
046    public FileListJob() {
047        // Keep the batchJobTimeout at default (-1) so it will be overridden
048        // by the settings for default batch timeout.
049    }
050
051    /**
052     * Initializes fields in this class.
053     *
054     * @param os the OutputStream to which data is to be written
055     */
056    public void initialize(OutputStream os) {
057    }
058
059    /**
060     * Invoke default method for deserializing object, and reinitialise the logger.
061     *
062     * @param s the ObjectInputStream from which the object is read
063     */
064    private void readObject(ObjectInputStream s) {
065        try {
066            s.defaultReadObject();
067        } catch (Exception e) {
068            throw new IOFailure("Unexpected error during deserialization", e);
069        }
070    }
071
072    /**
073     * Writes the name of the arcfile to the OutputStream.
074     *
075     * @param file an arcfile
076     * @param os the OutputStream to which data is to be written
077     * @return false If listing of this arcfile fails; otherwise true
078     */
079    public boolean processFile(File file, OutputStream os) {
080        ArgumentNotValid.checkNotNull(file, "file");
081        String result = file.getName() + "\n";
082        try {
083            os.write(result.getBytes());
084        } catch (IOException e) {
085            log.warn("Listing of file {} failed: ", file.getName(), e);
086            return false;
087        }
088        return true;
089    }
090
091    /**
092     * Does nothing.
093     *
094     * @param os the OutputStream to which data is to be written
095     */
096    public void finish(OutputStream os) {
097    }
098
099    /**
100     * Return a human-readable representation of a FileListJob.
101     *
102     * @return a human-readable representation of a FileListJob
103     */
104    public String toString() {
105        int filesFailedCount;
106        if (filesFailed == null) {
107            filesFailedCount = 0;
108        } else {
109            filesFailedCount = filesFailed.size();
110        }
111        return ("\nFileList job:\nFiles Processed = " + noOfFilesProcessed + "\nFiles  failed = " + filesFailedCount);
112    }
113
114}