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.tools; 024 025import java.io.File; 026import java.io.FileInputStream; 027import java.io.IOException; 028 029import org.archive.io.arc.ARCWriter; 030 031import dk.netarkivet.common.exceptions.IOFailure; 032import dk.netarkivet.common.utils.FileUtils; 033import dk.netarkivet.common.utils.SystemUtils; 034import dk.netarkivet.common.utils.arc.ARCUtils; 035 036/** 037 * Command line tool for creating an ARC file from given data. Uses ToolRunnerBase and SimpleCmdlineTool to coordinate 038 * task. Usage: java dk.netarkivet.common.tools.ArcWrap input_file uri mime-type > myarchive.arc Note: Does not depend 039 * on logging - communicates failure on stderr 040 */ 041public class ArcWrap extends ToolRunnerBase { 042 /** 043 * Main method. Reads given content and outputs an ARC file on stdout. The output ARC file has two records: the ARC 044 * file header and one record containing the given content. The uri and mimetype of the latter record are specified 045 * as command line parameters. Setup, teardown and run is delegated to the ArcWrapTool class. Management of this, 046 * exception handling etc. is delegated to ToolRunnerBase class. 047 * 048 * @param args Takes three command line parameters: - input file (the content to archive) - uri (the name to record 049 * the content by) - mime-type (the type to record for the content) 050 */ 051 public static void main(String[] args) { 052 ArcWrap instance = new ArcWrap(); 053 instance.runTheTool(args); 054 } 055 056 protected SimpleCmdlineTool makeMyTool() { 057 return new ArcWrapTool(); 058 } 059 060 private static class ArcWrapTool implements SimpleCmdlineTool { 061 062 /** 063 * This instance is declared outside of run method to ensure reliable teardown in case of exceptions during 064 * execution. 065 */ 066 private ARCWriter aw; 067 068 /** 069 * Accept only exactly 3 parameters. 070 * 071 * @param args the arguments 072 * @return true, if length of argument list is 3; returns false otherwise 073 */ 074 public boolean checkArgs(String... args) { 075 return args.length == 3; 076 } 077 078 /** 079 * Create the ARCWriter instance here for reliable execution of close method in teardown. 080 * 081 * @param args the arguments (presently not used) 082 */ 083 public void setUp(String... args) { 084 try { 085 aw = ARCUtils.getToolsARCWriter(System.out, new File("dummy.arc")); 086 } catch (IOException e) { 087 throw new IOFailure(e.getMessage()); 088 } 089 } 090 091 /** 092 * Ensure reliable execution of the ARCWriter.close() method. Remember to check if aw was actually created. 093 */ 094 public void tearDown() { 095 try { 096 if (aw != null) { 097 aw.close(); 098 } 099 } catch (IOException e) { 100 throw new IOFailure(e.getMessage()); 101 } 102 } 103 104 /** 105 * Perform the actual work. Procure the necessary information to run the ARCWriter from command line parameters 106 * and system settings, and perform the write. Creating and closing the ARCWriter is done in setup and teardown 107 * methods. 108 * 109 * @param args the arguments 110 */ 111 public void run(String... args) { 112 try { 113 // Prepare metadata... 114 File content = FileUtils.makeValidFileFromExisting(args[0]); 115 // ...and write the given file's content into the writer passing 116 // as parameters URI, MIME type, IP, Timestamp, Length and IS 117 String uri = args[1]; 118 String mimetype = args[2]; 119 aw.write(uri, mimetype, SystemUtils.getLocalIP(), content.lastModified(), content.length(), 120 new FileInputStream(content)); 121 } catch (IOException e) { 122 throw new IOFailure(e.getMessage()); 123 } 124 } 125 126 /** 127 * Return the list of parameters accepted by the ArcWrapTool class. 128 * 129 * @return the list of parameters accepted 130 */ 131 public String listParameters() { 132 return "file uri mimetype"; 133 } 134 135 } 136}