1 34 package org.dspace.app.checker; 35 36 import java.io.FileNotFoundException ; 37 import java.util.ArrayList ; 38 import java.util.Calendar ; 39 import java.util.Date ; 40 import java.util.List ; 41 42 import org.apache.commons.cli.CommandLine; 43 import org.apache.commons.cli.CommandLineParser; 44 import org.apache.commons.cli.HelpFormatter; 45 import org.apache.commons.cli.Option; 46 import org.apache.commons.cli.OptionBuilder; 47 import org.apache.commons.cli.Options; 48 import org.apache.commons.cli.ParseException; 49 import org.apache.commons.cli.PosixParser; 50 import org.apache.log4j.Logger; 51 import org.dspace.checker.BitstreamDispatcher; 52 import org.dspace.checker.BitstreamInfoDAO; 53 import org.dspace.checker.CheckerCommand; 54 import org.dspace.checker.HandleDispatcher; 55 import org.dspace.checker.LimitedCountDispatcher; 56 import org.dspace.checker.LimitedDurationDispatcher; 57 import org.dspace.checker.ListDispatcher; 58 import org.dspace.checker.ResultsLogger; 59 import org.dspace.checker.ResultsPruner; 60 import org.dspace.checker.SimpleDispatcher; 61 import org.dspace.core.Utils; 62 63 71 public class ChecksumChecker 72 { 73 private static final Logger LOG = Logger.getLogger(ChecksumChecker.class); 74 75 80 private ChecksumChecker() 81 { 82 ; 83 } 84 85 108 public static void main(String [] args) 109 { 110 CommandLineParser parser = new PosixParser(); 112 CommandLine line = null; 113 114 Options options = new Options(); 116 117 options.addOption("l", "looping", false, "Loop once through bitstreams"); 118 options.addOption("L", "continuous", false, 119 "Loop continuously through bitstreams"); 120 options.addOption("h", "help", false, "Help"); 121 options.addOption("d", "duration", true, "Checking duration"); 122 options.addOption("c", "count", true, "Check count"); 123 options.addOption("a", "handle", true, "Specify a handle to check"); 124 options.addOption("v", "verbose", false, "Report all processing"); 125 126 OptionBuilder.withArgName("bitstream-ids").hasArgs().withDescription( 127 "Space separated list of bitstream ids"); 128 Option useBitstreamIds = OptionBuilder.create('b'); 129 130 options.addOption(useBitstreamIds); 131 132 options.addOption("p", "prune", false, "Prune configuration file"); 133 options 134 .addOption(OptionBuilder 135 .withArgName("prune") 136 .hasOptionalArgs(1) 137 .withDescription( 138 "Prune old results (optionally using specified properties file for configuration)") 139 .create('p')); 140 141 try 142 { 143 line = parser.parse(options, args); 144 } 145 catch (ParseException e) 146 { 147 LOG.fatal(e); 148 System.exit(1); 149 } 150 151 if (line.hasOption('h')) 153 { 154 printHelp(options); 155 } 156 157 if (line.hasOption('p')) 159 { 160 ResultsPruner rp = null; 161 try 162 { 163 rp = (line.getOptionValue('p') != null) ? ResultsPruner 164 .getPruner(line.getOptionValue('p')) : ResultsPruner 165 .getDefaultPruner(); 166 } 167 catch (FileNotFoundException e) 168 { 169 e.printStackTrace(); 170 System.exit(1); 171 } 172 int count = rp.prune(); 173 System.out.println("Pruned " + count 174 + " old results from the database."); 175 } 176 177 Date processStart = Calendar.getInstance().getTime(); 178 179 BitstreamDispatcher dispatcher = null; 180 181 if (line.hasOption('l')) 184 { 185 dispatcher = new SimpleDispatcher(new BitstreamInfoDAO(), processStart, false); 186 } 187 else if (line.hasOption('L')) 188 { 189 dispatcher = new SimpleDispatcher(new BitstreamInfoDAO(), processStart, true); 190 } 191 else if (line.hasOption('b')) 192 { 193 String [] ids = line.getOptionValues('b'); 195 List idList = new ArrayList (ids.length); 196 197 for (int i = 0; i < ids.length; i++) 198 { 199 try 200 { 201 idList.add(new Integer (ids[i])); 202 } 203 catch (NumberFormatException nfe) 204 { 205 System.err.println("The following argument: " + ids[i] 206 + " is not an integer"); 207 System.exit(0); 208 } 209 } 210 dispatcher = new ListDispatcher(idList); 211 } 212 213 else if (line.hasOption('a')) 214 { 215 dispatcher = new HandleDispatcher(new BitstreamInfoDAO(), line.getOptionValue('a')); 216 } 217 else if (line.hasOption('d')) 218 { 219 try 221 { 222 dispatcher = new LimitedDurationDispatcher( 223 new SimpleDispatcher(new BitstreamInfoDAO(), processStart, true), new Date ( 224 System.currentTimeMillis() 225 + Utils.parseDuration(line 226 .getOptionValue('d')))); 227 } 228 catch (Exception e) 229 { 230 LOG.fatal("Couldn't parse " + line.getOptionValue('d') 231 + " as a duration: ", e); 232 System.exit(0); 233 } 234 } 235 else if (line.hasOption('c')) 236 { 237 int count = new Integer (line.getOptionValue('c')).intValue(); 238 239 dispatcher = new LimitedCountDispatcher(new SimpleDispatcher( 241 new BitstreamInfoDAO(), processStart, false), count); 242 } 243 else 244 { 245 dispatcher = new LimitedCountDispatcher(new SimpleDispatcher( 246 new BitstreamInfoDAO(), processStart, false), 1); 247 } 248 249 ResultsLogger logger = new ResultsLogger(processStart); 250 CheckerCommand checker = new CheckerCommand(); 251 if (line.hasOption('v')) 253 { 254 checker.setReportVerbose(true); 255 } 256 checker.configureLog(); 257 checker.setProcessStartDate(processStart); 258 checker.setDispatcher(dispatcher); 259 checker.setCollector(logger); 260 checker.process(); 261 } 262 263 268 private static void printHelp(Options options) 269 { 270 HelpFormatter myhelp = new HelpFormatter(); 271 272 myhelp.printHelp("Checksum Checker\n", options); 273 System.out 274 .println("\nSpecify a duration for checker process, using s(seconds)," 275 + "m(minutes), or h(hours): ChecksumChecker -d 30s" 276 + " OR ChecksumChecker -d 30m" 277 + " OR ChecksumChecker -d 2h"); 278 System.out 279 .println("\nSpecify bitstream IDs: ChecksumChecker -b 13 15 17 20"); 280 System.out.println("\nLoop once through all bitstreams: " 281 + "ChecksumChecker -l"); 282 System.out 283 .println("\nLoop continuously through all bitstreams: ChecksumChecker -L"); 284 System.out 285 .println("\nCheck a defined number of bitstreams: ChecksumChecker -c 10"); 286 System.out.println("\nReport all processing (verbose)(default reports only errors): ChecksumChecker -v"); 287 System.out.println("\nDefault (no arguments) is equivalent to '-c 1'"); 288 System.exit(0); 289 } 290 291 } 292 | Popular Tags |