001/* 002 * #%L 003 * Netarchivesuite - harvester 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.harvester.tools; 024 025import java.io.File; 026import java.io.FileOutputStream; 027import java.io.IOException; 028import java.io.OutputStream; 029import java.util.ArrayList; 030import java.util.Iterator; 031 032import dk.netarkivet.common.exceptions.IOFailure; 033import dk.netarkivet.harvester.datamodel.HeritrixTemplate; 034import dk.netarkivet.harvester.datamodel.TemplateDAO; 035 036/** 037 * Utility for maintaining harvest-templates from the commandline. With this utility, you can - create new templates - 038 * update existing templates - download one or more templates - show all existing templates 039 */ 040public class HarvestTemplateApplication { 041 042 /** 043 * The main method of the HarvestTemplateApplication. 044 * 045 * @param args array of commandline arguments 046 */ 047 public static void main(final String[] args) { 048 if (args.length < 1) { 049 printUsage(); 050 } else { 051 String command = args[0]; 052 ArrayList<String> arguments = new ArrayList<String>(); 053 for (int i = 1; i < args.length; i++) { 054 arguments.add(args[i]); 055 } 056 String[] parameters = new String[arguments.size()]; 057 parameters = arguments.toArray(parameters); 058 if (command.equals("create")) { 059 createTemplate(parameters); 060 } else if (command.equals("update")) { 061 updateTemplate(parameters); 062 } else if (command.equals("download")) { 063 downloadTemplates(parameters); 064 } else if (command.equals("showall")) { 065 showallTemplates(); 066 } else { 067 System.err.println("The command '" + command + "' is not one of the legal commands."); 068 printUsage(); 069 } 070 } 071 } 072 073 /** 074 * Show all available templates. 075 */ 076 private static void showallTemplates() { 077 TemplateDAO dao = TemplateDAO.getInstance(); 078 Iterator<String> templateIterator = dao.getAll(); 079 if (!templateIterator.hasNext()) { 080 System.err.println("No templates found in database!"); 081 } else { 082 while (templateIterator.hasNext()) { 083 System.out.println(templateIterator.next()); 084 } 085 } 086 } 087 088 /** 089 * Download one or more templates to current working directory. if length of args is 0, all templates are 090 * downloaded. if length of args > 0, the strings in args are considered to be names of templates to be downloaded. 091 * 092 * @param args String-array containing template-names 093 */ 094 private static void downloadTemplates(final String[] args) { 095 TemplateDAO dao = TemplateDAO.getInstance(); 096 String templateName = ""; 097 if (args.length < 1) { // download all templates to Current Working dir 098 Iterator<String> templateIterator = dao.getAll(); 099 while (templateIterator.hasNext()) { 100 templateName = templateIterator.next(); 101 download(templateName); 102 } 103 } else { // Download the templates mentioned as arguments 104 for (String arg : args) { 105 templateName = arg; 106 if (!dao.exists(templateName)) { 107 System.err.println("Unable to download template '" + templateName + "'. It does not exist."); 108 } else { 109 download(templateName); 110 } 111 } 112 } 113 } 114 115 /** 116 * Download the template with a given name. The template is assumed to exist. 117 * 118 * @param templateName The name of a given template 119 */ 120 private static void download(final String templateName) { 121 System.out.println("Downloading template '" + templateName + "'."); 122 try { 123 TemplateDAO dao = TemplateDAO.getInstance(); 124 HeritrixTemplate doc = dao.read(templateName); 125 OutputStream os = new FileOutputStream(templateName + ".xml"); 126 doc.writeTemplate(os); 127 } catch (IOException e) { 128 System.err.println("Error downloading template '" + templateName + "': " + e); 129 e.printStackTrace(System.err); 130 } 131 } 132 133 /** 134 * Update a given template. 135 * 136 * @param args array of commandline-arguments args[0]: templateName args[1]: File that should replace an existing 137 * template 138 */ 139 private static void updateTemplate(final String[] args) { 140 TemplateDAO dao = TemplateDAO.getInstance(); 141 if (!(args.length == 2)) { 142 System.err.println("Unable to update template: Wrong number(" + (args.length) + ") of arguments."); 143 printUsage(); 144 } else { 145 String templateName = args[0]; 146 File templateFile = new File(args[1]); 147 if (!dao.exists(templateName)) { 148 System.err 149 .println("There is no template named '" + templateName + "'. Use the create-command instead."); 150 } else { 151 try { 152 // Try to convert orderxml-file to HeritrixTemplate object 153 HeritrixTemplate ht = HeritrixTemplate.read(templateFile); 154 dao.update(templateName, ht); 155 System.out.println("The template '" + templateName + "' has now been updated."); 156 } catch (IOFailure e) { 157 System.err.println("The file '" + args[1] + "' could not be read or is not valid xml."); 158 e.printStackTrace(System.err); 159 } 160 } 161 } 162 } 163 164 /** 165 * Create a new template. 166 * 167 * @param args array of commandline-arguments args[0]: templateName args[1]: file containing the new template. 168 */ 169 private static void createTemplate(final String[] args) { 170 TemplateDAO dao = TemplateDAO.getInstance(); 171 if (!(args.length == 2)) { 172 System.err.println("Unable to create template: Wrong number(" + (args.length) + ") of arguments."); 173 printUsage(); 174 } else { 175 String templateName = args[0]; 176 File templateFile = new File(args[1]); 177 if (dao.exists(templateName)) { 178 System.err.println("There is already a template with name '" + templateName + "'."); 179 } else { 180 try { 181 // Try to convert orderxml-file to HeritrixTemplate object 182 HeritrixTemplate ht = HeritrixTemplate.read(templateFile); 183 dao.create(templateName, ht); 184 System.out.println("The template '" + templateName + "' has now been created."); 185 } catch (IOFailure e) { 186 System.err.println("The File '" + args[1] + "' is not readable or is not valid xml."); 187 e.printStackTrace(System.err); 188 } 189 } 190 } 191 } 192 193 /** 194 * Print usage information. 195 */ 196 private static void printUsage() { 197 System.err.print("java " + HarvestTemplateApplication.class.getName()); 198 System.err.println(" <command> <args>"); 199 200 System.err.println("create <template-name> " + "<xml-file for this template>"); 201 System.err.println("download [<template-name>] "); 202 System.err.println("update <template-name> " + "<xml-file to replace this template>"); 203 System.err.println("showall"); 204 } 205 206}