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 */
023package dk.netarkivet.common.distribute;
024
025import java.io.File;
026import java.io.FileInputStream;
027import java.io.IOException;
028import java.io.InputStream;
029import java.io.OutputStream;
030
031import org.apache.commons.io.IOUtils;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035import dk.netarkivet.common.exceptions.ArgumentNotValid;
036import dk.netarkivet.common.exceptions.IOFailure;
037import dk.netarkivet.common.utils.ChecksumCalculator;
038import dk.netarkivet.common.utils.FileUtils;
039import dk.netarkivet.common.utils.StreamUtils;
040
041/**
042 * A file represented as a RemoteFile. To avoid transferring data to and from a remote machine, when you now, that
043 * recipient is a local process. The file is deleted during cleanup.
044 */
045@SuppressWarnings({"serial"})
046public class FileRemoteFile implements RemoteFile {
047
048    /** The logger for this class. */
049    private static final Logger log = LoggerFactory.getLogger(FileRemoteFile.class);
050
051    /** The local File where the data is stored. */
052    private File dataFile;
053
054    public FileRemoteFile(File dataFile) {
055        ArgumentNotValid.checkNotNull(dataFile, "File dataFile");
056        ArgumentNotValid.checkTrue(dataFile.isFile(), "The dataFile with value '" + dataFile.getAbsolutePath()
057                + "' does not exist.");
058        this.dataFile = dataFile;
059    }
060
061    @Override
062    public void copyTo(File destFile) {
063        FileUtils.copyFile(dataFile, destFile);
064    }
065
066    @Override
067    public void appendTo(OutputStream out) {
068        InputStream in = null;
069        try {
070            in = new FileInputStream(dataFile);
071            StreamUtils.copyInputStreamToOutputStream(in, out);
072        } catch (IOException e) {
073            throw new IOFailure("Unable to append data: ", e);
074        } finally {
075            IOUtils.closeQuietly(in);
076        }
077    }
078
079    @Override
080    public InputStream getInputStream() {
081        try {
082            return new FileInputStream(dataFile);
083        } catch (IOException e) {
084            throw new IOFailure("Unable to return inputStream: ", e);
085        }
086    }
087
088    @Override
089    public String getName() {
090        return dataFile.getName();
091    }
092
093    @Override
094    public String getChecksum() {
095        return ChecksumCalculator.calculateMd5(dataFile);
096    }
097
098    @Override
099    public void cleanup() {
100        boolean deleted = dataFile.delete();
101        if (!deleted) {
102            log.warn("Unable to delete file '{}'", dataFile.getAbsolutePath());
103        }
104    }
105
106    @Override
107    public long getSize() {
108        return dataFile.length();
109    }
110
111}