001/*
002 * #%L
003 * Netarchivesuite - archive
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 */
023package dk.netarkivet.archive.checksum.distribute;
024
025import java.io.File;
026
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import dk.netarkivet.archive.distribute.ArchiveMessage;
031import dk.netarkivet.archive.distribute.ArchiveMessageVisitor;
032import dk.netarkivet.common.distribute.ChannelID;
033import dk.netarkivet.common.distribute.RemoteFile;
034import dk.netarkivet.common.distribute.RemoteFileFactory;
035import dk.netarkivet.common.exceptions.ArgumentNotValid;
036import dk.netarkivet.common.exceptions.IOFailure;
037
038/**
039 * The GetAllFilenamesMessage is sent to retrieve all the filenames in a specific replica. The result is a file in the
040 * same format as a FilelistJob.
041 */
042@SuppressWarnings({"serial"})
043public class GetAllFilenamesMessage extends ArchiveMessage {
044
045    private static final Logger log = LoggerFactory.getLogger(GetAllFilenamesMessage.class);
046
047    /** The file with the current content, which will be retrieved from the sender of this message. */
048    private RemoteFile remoteFile;
049    /** The id for the replica where this message should be sent. */
050    private String replicaId;
051
052    /**
053     * Constructor.
054     *
055     * @param to The channel the message is sent to.
056     * @param replyTo The channel the reply is sent to.
057     * @param repId The id of the replica.
058     */
059    public GetAllFilenamesMessage(ChannelID to, ChannelID replyTo, String repId) {
060        super(to, replyTo);
061
062        this.replicaId = repId;
063    }
064
065    /**
066     * Method for setting the resulting file. This file will be retrieved from the caller of this message.
067     *
068     * @param file The file with the checksum message.
069     */
070    public void setFile(File file) {
071        ArgumentNotValid.checkNotNull(file, "File file");
072
073        remoteFile = RemoteFileFactory.getMovefileInstance(file);
074    }
075
076    /**
077     * Method for retrieving the resulting file.
078     *
079     * @param toFile The file for the remotely retrieved content.
080     */
081    public void getData(File toFile) {
082        ArgumentNotValid.checkNotNull(toFile, "File toFile");
083        if (remoteFile == null) {
084            throw new IOFailure("No remote file has been retrieved. "
085                    + "This message is either NotOK or has never been sent.");
086        }
087        remoteFile.copyTo(toFile);
088        try {
089            remoteFile.cleanup();
090        } catch (IOFailure e) {
091            // Just log errors on deleting. They are fairly harmless.
092            // Can't make Logger a field, as this class is Serializable
093            log.warn("Could not delete remote file {}", remoteFile.getName());
094        }
095        remoteFile = null;
096    }
097
098    /**
099     * Method for retrieving the id for the replica where this message should be sent.
100     *
101     * @return The id for the replica.
102     */
103    public String getReplicaId() {
104        return replicaId;
105    }
106
107    /**
108     * Retrieval of a string representation of this instance.
109     *
110     * @return A string representation of this instance.
111     */
112    public String toString() {
113        return super.toString() + ", replicaId: " + replicaId;
114    }
115
116    /**
117     * Accept this message.
118     *
119     * @param v The message visitor accepting this message.
120     */
121    public void accept(ArchiveMessageVisitor v) {
122        v.visit(this);
123    }
124
125}