KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > checker > ChecksumChecker


1 /*
2  * Copyright (c) 2004-2005, Hewlett-Packard Company and Massachusetts
3  * Institute of Technology. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Hewlett-Packard Company nor the name of the
17  * Massachusetts Institute of Technology nor the names of their
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32  * DAMAGE.
33  */

34 package org.dspace.app.checker;
35
36 import java.io.FileNotFoundException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Calendar JavaDoc;
39 import java.util.Date JavaDoc;
40 import java.util.List JavaDoc;
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 /**
64  * Command line access to the checksum checker. Options are listed in the
65  * documentation for the main method.</p>
66  *
67  * @author Jim Downing
68  * @author Grace Carpenter
69  * @author Nathan Sarr
70  */

71 public class ChecksumChecker
72 {
73     private static final Logger LOG = Logger.getLogger(ChecksumChecker.class);
74
75     /**
76      * Blanked off constructor, this class should be used as a command line
77      * tool.
78      *
79      */

80     private ChecksumChecker()
81     {
82         ;
83     }
84
85     /**
86      * Command line access to the checksum package.
87      *
88      * @param args
89      * <dl>
90      * <dt>-h</dt>
91      * <dd>Print help on command line options</dd>
92      * <dt>-l</dt>
93      * <dd>loop through bitstreams once</dd>
94      * <dt>-L</dt>
95      * <dd>loop continuously through bitstreams</dd>
96      * <dt>-d</dt>
97      * <dd>specify duration of process run</dd>
98      * <dt>-b</dt>
99      * <dd>specify bitstream IDs</dd>
100      * <dt>-a [handle_id]</dt>
101      * <dd>check anything by handle</dd>
102      * <dt>-e</dt>
103      * <dd>Report only errors in the logs</dd>
104      * <dt>-p</dt>
105      * <dd>Don't prune results before running checker</dd>
106      * </dl>
107      */

108     public static void main(String JavaDoc[] args)
109     {
110         // set up command line parser
111
CommandLineParser parser = new PosixParser();
112         CommandLine line = null;
113
114         // create an options object and populate it
115
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         // user asks for help
152
if (line.hasOption('h'))
153         {
154             printHelp(options);
155         }
156
157         // Prune stage
158
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 JavaDoc 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 JavaDoc processStart = Calendar.getInstance().getTime();
178
179         BitstreamDispatcher dispatcher = null;
180         
181         // process should loop infinitely through
182
// most_recent_checksum table
183
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             // check only specified bitstream(s)
194
String JavaDoc[] ids = line.getOptionValues('b');
195             List JavaDoc idList = new ArrayList JavaDoc(ids.length);
196
197             for (int i = 0; i < ids.length; i++)
198             {
199                 try
200                 {
201                     idList.add(new Integer JavaDoc(ids[i]));
202                 }
203                 catch (NumberFormatException JavaDoc 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             // run checker process for specified duration
220
try
221             {
222                 dispatcher = new LimitedDurationDispatcher(
223                         new SimpleDispatcher(new BitstreamInfoDAO(), processStart, true), new Date JavaDoc(
224                                 System.currentTimeMillis()
225                                         + Utils.parseDuration(line
226                                                 .getOptionValue('d'))));
227             }
228             catch (Exception JavaDoc 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 JavaDoc(line.getOptionValue('c')).intValue();
238             
239             // run checker process for specified number of bitstreams
240
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         // verbose reporting
252
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     /**
264      * Print the help options for the user
265      *
266      * @param options that are available for the user
267      */

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