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;
026
027import org.archive.io.ArchiveRecord;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import dk.netarkivet.common.CommonSettings;
032import dk.netarkivet.common.exceptions.ArgumentNotValid;
033import dk.netarkivet.common.utils.Settings;
034import dk.netarkivet.common.utils.SettingsFactory;
035
036/**
037 * Factory for creating remote files.
038 */
039@SuppressWarnings({"unchecked", "rawtypes"})
040public class RemoteFileFactory extends SettingsFactory<RemoteFile> {
041
042    /** A named logger for this class. */
043    private static final transient Logger log = LoggerFactory.getLogger(RemoteFileFactory.class);
044
045    /**
046     * Create a remote file that handles the transport of the remote file data. This method is used by the sender to
047     * prepare the transport over JMS.
048     *
049     * @param file The File object to make accessable on another machine
050     * @param useChecksums Whether transfers should be doublechecked with checksums. Added value is access to checksum
051     * of objects.
052     * @param fileDeletable If true, the local file will be deleted when it is no longer needed.
053     * @param multipleDownloads Whether this file should be allowed to be transferred more than once.
054     * @return A RemoteFile instance encapsulating the file argument.
055     */
056    public static RemoteFile getInstance(File file, boolean useChecksums, boolean fileDeletable,
057            boolean multipleDownloads, RemoteFileSettings connectionParams) {
058        ArgumentNotValid.checkNotNull(file, "File file");
059        return SettingsFactory.getInstance(CommonSettings.REMOTE_FILE_CLASS, file, useChecksums, fileDeletable,
060                multipleDownloads, connectionParams);
061    }
062
063    /* Same as the above method, but without the required RemoteFileSettings. */
064    public static RemoteFile getInstance(File file, boolean useChecksums, boolean fileDeletable,
065            boolean multipleDownloads) {
066        ArgumentNotValid.checkNotNull(file, "File file");
067        return SettingsFactory.getInstance(CommonSettings.REMOTE_FILE_CLASS, file, useChecksums, fileDeletable,
068                multipleDownloads);
069    }
070
071    /**
072     * Get an instance connected to an ArchiveRecord. Records are not deletable so there is no concept of a "movefile"
073     * instance.
074     *
075     * @param record
076     * @return the file to be copied.
077     */
078    public static RemoteFile getExtendedInstance(ArchiveRecord record) {
079        return SettingsFactory.getInstance(CommonSettings.REMOTE_FILE_CLASS, record);
080    }
081
082    /**
083     * Returns true iff the defined RemoteFile class has a factory method with signature public static RemoteFile
084     * getInstance(ArchiveRecord record)
085     *
086     * @return true if using an extended remote file.
087     */
088    public static boolean isExtendedRemoteFile() {
089        String remoteFileClass = Settings.get(CommonSettings.REMOTE_FILE_CLASS);
090        try {
091            Class theClass = Class.forName(remoteFileClass);
092            try {
093                theClass.getMethod("getInstance", ArchiveRecord.class);
094                return true;
095            } catch (NoSuchMethodException e) {
096                return false;
097            }
098        } catch (ClassNotFoundException e) {
099            log.error("Unknown RemoteFile class :{}", remoteFileClass);
100            throw new ArgumentNotValid("Unknown RemoteFile class :" + remoteFileClass);
101        }
102    }
103
104    /**
105     * Same as getInstance(file, false, true, false).
106     *
107     * @param file The file to move to another computer.
108     */
109    public static RemoteFile getMovefileInstance(File file) {
110        return getInstance(file, false, true, false);
111    }
112
113    /**
114     * Same as getInstance(file, false, false, false, null).
115     *
116     * @param file The file to copy to another computer.
117     */
118    public static RemoteFile getCopyfileInstance(File file) {
119        return getInstance(file, false, false, false);
120    }
121
122    /**
123     * Same as getInstance(file, false, false, false, connectionParams).
124     *
125     * @param file The file to copy to another computer.
126     */
127    public static RemoteFile getCopyfileInstance(File file, RemoteFileSettings connectionParams) {
128        if (connectionParams != null) {
129            return getInstance(file, false, false, false, connectionParams);
130        } else {
131            return getInstance(file, false, false, false);
132        }
133    }
134
135    /**
136     * Same as getInstance(file, false, false, false).
137     *
138     * @param file The file to copy to another computer.
139     */
140    public static RemoteFile getDistributefileInstance(File file) {
141        return getInstance(file, true, false, true);
142    }
143
144}