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 */
023
024package dk.netarkivet.archive.tools;
025
026import java.io.File;
027
028import dk.netarkivet.common.CommonSettings;
029import dk.netarkivet.common.distribute.JMSConnectionFactory;
030import dk.netarkivet.common.distribute.arcrepository.ArcRepositoryClientFactory;
031import dk.netarkivet.common.distribute.arcrepository.Replica;
032import dk.netarkivet.common.distribute.arcrepository.ViewerArcRepositoryClient;
033import dk.netarkivet.common.exceptions.NetarkivetException;
034import dk.netarkivet.common.tools.SimpleCmdlineTool;
035import dk.netarkivet.common.tools.ToolRunnerBase;
036import dk.netarkivet.common.utils.Settings;
037
038/**
039 * A command-line tool to get ARC files from the bitarchive.
040 * <p>
041 * Usage: java dk.netarkivet.archive.tools.GetFile arcfilename [destination-file]
042 */
043
044public class GetFile extends ToolRunnerBase {
045
046    /**
047     * Main method. Retrieves a file from the bitarchive and copies it to current working directory. Setup, teardown and
048     * run is delegated to the GetFileTool class. Management of this, exception handling etc. is delegated to
049     * ToolRunnerBase class.
050     *
051     * @param argv Takes one or two command line parameter: the name of the file to retrieve. optionally, the name of
052     * the destination file.
053     */
054    public static void main(String[] argv) {
055        GetFile instance = new GetFile();
056        instance.runTheTool(argv);
057    }
058
059    /**
060     * Create an instance of GetFileTool.
061     *
062     * @return an instance of GetFileTool.
063     */
064    protected SimpleCmdlineTool makeMyTool() {
065        return new GetFileTool();
066    }
067
068    /** The implementation of SimpleCmdlineTool for GetFile. */
069    private static class GetFileTool implements SimpleCmdlineTool {
070        /**
071         * This instance is declared outside of run method to ensure reliable teardown in case of exceptions during
072         * execution.
073         */
074        private ViewerArcRepositoryClient arcrep;
075
076        /**
077         * the bitarchive replica requested to deliver the file.
078         */
079        private Replica myReplica;
080
081        /**
082         * Accept 1 or 2 parameters.
083         *
084         * @param args the arguments
085         * @return true, if length of args list is 1 or 2; returns false otherwise
086         */
087        public boolean checkArgs(String... args) {
088            return (args.length >= 1 && args.length <= 2);
089        }
090
091        /**
092         * Create the ArcRepositoryClient instance here for reliable execution of close method in teardown.
093         *
094         * @param args the arguments (not used)
095         */
096        public void setUp(String... args) {
097            arcrep = ArcRepositoryClientFactory.getViewerInstance();
098            myReplica = Replica.getReplicaFromId(Settings.get(CommonSettings.USE_REPLICA_ID));
099        }
100
101        /**
102         * Ensure reliable execution of the ArcRepositoryClient.close() method. Remember to check if arcrep was actually
103         * created. Also reliably cleans up the JMSConnection.
104         */
105        public void tearDown() {
106            if (arcrep != null) {
107                arcrep.close();
108            }
109            JMSConnectionFactory.getInstance().cleanup();
110        }
111
112        /**
113         * Perform the actual work. Procure the necessary information from command line parameters and system settings
114         * required to run the ViewerArcRepositoryClient.getFile(), and perform the operation. Creating and closing the
115         * ArcRepositoryClient (arcrep) is done in setup and teardown methods.
116         *
117         * @param args the arguments
118         */
119        public void run(String... args) {
120            try {
121                String filename = args[0];
122                File destfile;
123                if (args.length != 2) {
124                    destfile = new File(filename);
125                } else {
126                    destfile = new File(args[1]);
127                }
128                System.out.println("Retrieving file '" + filename + "' from replica '" + myReplica.getName()
129                        + "' as file " + destfile.getAbsolutePath());
130                arcrep.getFile(filename, myReplica, destfile);
131
132            } catch (NetarkivetException e) {
133                System.out.println("Execution of arcrep.getFile(arcfilename, " + "replica, toFile) failed).");
134                e.printStackTrace();
135                System.exit(1);
136            }
137        }
138
139        /**
140         * Return the list of parameters accepted by the GetFileTool class.
141         *
142         * @return the list of parameters accepted.
143         */
144        public String listParameters() {
145            return "filename [destination-file]";
146        }
147
148    }
149}