001: /*
002: * Copyright (c) 2004-2005, Hewlett-Packard Company and Massachusetts
003: * Institute of Technology. All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are
007: * met:
008: *
009: * - Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * - Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * - Neither the name of the Hewlett-Packard Company nor the name of the
017: * Massachusetts Institute of Technology nor the names of their
018: * contributors may be used to endorse or promote products derived from
019: * this software without specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
024: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
025: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
028: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
029: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
030: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
031: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
032: * DAMAGE.
033: */
034: package org.dspace.checker;
035:
036: import java.io.File;
037: import java.io.FileWriter;
038: import java.io.IOException;
039: import java.util.Date;
040: import java.util.GregorianCalendar;
041: import java.util.Properties;
042:
043: import javax.activation.DataHandler;
044: import javax.activation.DataSource;
045: import javax.activation.FileDataSource;
046: import javax.mail.BodyPart;
047: import javax.mail.Message;
048: import javax.mail.Multipart;
049: import javax.mail.Session;
050: import javax.mail.Transport;
051: import javax.mail.internet.InternetAddress;
052: import javax.mail.internet.MimeBodyPart;
053: import javax.mail.internet.MimeMessage;
054: import javax.mail.internet.MimeMultipart;
055:
056: import org.apache.commons.cli.CommandLine;
057: import org.apache.commons.cli.CommandLineParser;
058: import org.apache.commons.cli.HelpFormatter;
059: import org.apache.commons.cli.Options;
060: import org.apache.commons.cli.ParseException;
061: import org.apache.commons.cli.PosixParser;
062: import org.apache.log4j.Logger;
063: import org.dspace.core.ConfigurationManager;
064:
065: /**
066: * <p>
067: * The email reporter creates and sends emails to an administrator. This only
068: * reports information for todays date. It is expected this will be used just
069: * after the checksum checker has been run.
070: * </p>
071: *
072: * @author Jim Downing
073: * @author Grace Carpenter
074: * @author Nathan Sarr
075: *
076: *
077: */
078: public class DailyReportEmailer {
079: /** log4j logger. */
080: private static Logger log = Logger
081: .getLogger(DailyReportEmailer.class);
082:
083: /**
084: * Default constructor.
085: */
086: public DailyReportEmailer() {
087: }
088:
089: /**
090: * Send the report through email.
091: *
092: * @param attachment
093: * the file conntaing the report
094: * @param numberOfBitstreams
095: * the number of bitstreams reported
096: *
097: * @throws IOException
098: * if IO exception occurs
099: * @throws javax.mail.MessagingException
100: * if message cannot be sent.
101: */
102: public void sendReport(File attachment, int numberOfBitstreams)
103: throws IOException, javax.mail.MessagingException {
104: // Get the mail configuration properties
105: String server = ConfigurationManager.getProperty("mail.server");
106:
107: // Set up properties for mail session
108: Properties props = System.getProperties();
109: props.put("mail.smtp.host", server);
110:
111: // Get session
112: Session session = Session.getDefaultInstance(props, null);
113:
114: MimeMessage msg = new MimeMessage(session);
115: Multipart multipart = new MimeMultipart();
116:
117: // create the first part of the email
118: BodyPart messageBodyPart = new MimeBodyPart();
119: messageBodyPart
120: .setText("This is the checksum checker report see attachement for details \n"
121: + numberOfBitstreams
122: + " Bitstreams found with POSSIBLE issues");
123: multipart.addBodyPart(messageBodyPart);
124:
125: // add the file
126: messageBodyPart = new MimeBodyPart();
127:
128: DataSource source = new FileDataSource(attachment);
129: messageBodyPart.setDataHandler(new DataHandler(source));
130: messageBodyPart.setFileName("checksum_checker_report.txt");
131: multipart.addBodyPart(messageBodyPart);
132: msg.setContent(multipart);
133: msg.setFrom(new InternetAddress(ConfigurationManager
134: .getProperty("mail.from.address")));
135: msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
136: ConfigurationManager.getProperty("mail.admin")));
137:
138: msg.setSentDate(new Date());
139: msg.setSubject("Checksum checker Report - "
140: + numberOfBitstreams
141: + " Bitstreams found with POSSIBLE issues");
142: Transport.send(msg);
143: }
144:
145: /**
146: * Allows users to have email sent to them. The default is to send all
147: * reports in one email
148: *
149: * @param args
150: * <dl>
151: * <dt>-h</dt>
152: * <dd>help</dd>
153: * <dt>-d</dt>
154: * <dd>Select deleted bitstreams</dd>
155: * <dt>-m</dt>
156: * <dd>Bitstreams missing from assetstore</dd>
157: * <dt>-c</dt>
158: * <dd>Bitstreams whoses checksums were changed</dd>
159: * <dt>-n</dt>
160: * <dd>Bitstreams whoses checksums were changed</dd>
161: * <dt>-a</dt>
162: * <dd>Send all reports in one email</dd>
163: * </dl>
164: *
165: */
166: public static void main(String[] args) {
167: // set up command line parser
168: CommandLineParser parser = new PosixParser();
169: CommandLine line = null;
170:
171: // create an options object and populate it
172: Options options = new Options();
173:
174: options.addOption("h", "help", false, "Help");
175: options
176: .addOption("d", "Deleted", false,
177: "Send E-mail report for all bitstreams set as deleted for today");
178: options
179: .addOption("m", "Missing", false,
180: "Send E-mail report for all bitstreams not found in assetstore for today");
181: options
182: .addOption(
183: "c",
184: "Changed",
185: false,
186: "Send E-mail report for all bitstrems where checksum has been changed for today");
187: options.addOption("a", "All", false, "Send all E-mail reports");
188:
189: options.addOption("u", "Unchecked", false,
190: "Send the Unchecked bitstream report");
191:
192: options
193: .addOption("n", "Not Processed", false,
194: "Send E-mail report for all bitstreams set to longer be processed for today");
195:
196: try {
197: line = parser.parse(options, args);
198: } catch (ParseException e) {
199: log.fatal(e);
200: System.exit(1);
201: }
202:
203: // user asks for help
204: if (line.hasOption('h')) {
205: HelpFormatter myhelp = new HelpFormatter();
206:
207: myhelp.printHelp("Checksum Reporter\n", options);
208: System.out
209: .println("\nSend Deleted bitstream email report: DailyReportEmailer -d");
210: System.out
211: .println("\nSend Missing bitstreams email report: DailyReportEmailer -m");
212: System.out
213: .println("\nSend Checksum Changed email report: DailyReportEmailer -c");
214:
215: System.out
216: .println("\nSend bitstream not to be processed email report: DailyReportEmailer -n");
217:
218: System.out
219: .println("\nSend Un-checked bitstream report: DailyReportEmailer -u");
220:
221: System.out
222: .println("\nSend All email reports: DailyReportEmailer");
223: System.exit(0);
224: }
225:
226: // create a new simple reporter
227: SimpleReporter reporter = new SimpleReporterImpl();
228:
229: DailyReportEmailer emailer = new DailyReportEmailer();
230:
231: // get dates for yesterday and tomorrow
232: GregorianCalendar calendar = new GregorianCalendar();
233: calendar.add(GregorianCalendar.DAY_OF_YEAR, -1);
234:
235: Date yesterday = calendar.getTime();
236: calendar.add(GregorianCalendar.DAY_OF_YEAR, 2);
237:
238: Date tomorrow = calendar.getTime();
239:
240: File report = null;
241: FileWriter writer = null;
242:
243: try {
244: // the number of bitstreams in report
245: int numBitstreams = 0;
246:
247: // create a temporary file in the log directory
248: String dirLocation = ConfigurationManager
249: .getProperty("log.dir");
250: File directory = new File(dirLocation);
251:
252: if (directory.exists() && directory.isDirectory()) {
253: report = File.createTempFile("checker_report", ".txt",
254: directory);
255: } else {
256: throw new IllegalStateException("directory :"
257: + dirLocation + " does not exist");
258: }
259:
260: writer = new FileWriter(report);
261:
262: if ((line.hasOption("a"))
263: || (line.getOptions().length == 0)) {
264: writer
265: .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
266: numBitstreams += reporter.getDeletedBitstreamReport(
267: yesterday, tomorrow, writer);
268: writer
269: .write("\n--------------------------------- Report Spacer ---------------------------\n\n");
270: numBitstreams += reporter.getChangedChecksumReport(
271: yesterday, tomorrow, writer);
272: writer
273: .write("\n--------------------------------- Report Spacer ---------------------------\n\n");
274: numBitstreams += reporter.getBitstreamNotFoundReport(
275: yesterday, tomorrow, writer);
276: writer
277: .write("\n--------------------------------- Report Spacer ---------------------------\n\n");
278: numBitstreams += reporter.getNotToBeProcessedReport(
279: yesterday, tomorrow, writer);
280: writer
281: .write("\n--------------------------------- Report Spacer ---------------------------\n\n");
282: numBitstreams += reporter
283: .getUncheckedBitstreamsReport(writer);
284: writer
285: .write("\n--------------------------------- End Report ---------------------------\n\n");
286: writer.flush();
287: writer.close();
288: emailer.sendReport(report, numBitstreams);
289: } else {
290: if (line.hasOption("d")) {
291: writer
292: .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
293: numBitstreams += reporter
294: .getDeletedBitstreamReport(yesterday,
295: tomorrow, writer);
296: writer.flush();
297: writer.close();
298: emailer.sendReport(report, numBitstreams);
299: }
300:
301: if (line.hasOption("m")) {
302: writer
303: .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
304: numBitstreams += reporter
305: .getBitstreamNotFoundReport(yesterday,
306: tomorrow, writer);
307: writer.flush();
308: writer.close();
309: emailer.sendReport(report, numBitstreams);
310: }
311:
312: if (line.hasOption("c")) {
313: writer
314: .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
315: numBitstreams += reporter.getChangedChecksumReport(
316: yesterday, tomorrow, writer);
317: writer.flush();
318: writer.close();
319: emailer.sendReport(report, numBitstreams);
320: }
321:
322: if (line.hasOption("n")) {
323: writer
324: .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
325: numBitstreams += reporter
326: .getNotToBeProcessedReport(yesterday,
327: tomorrow, writer);
328: writer.flush();
329: writer.close();
330: emailer.sendReport(report, numBitstreams);
331: }
332:
333: if (line.hasOption("u")) {
334: writer
335: .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
336: numBitstreams += reporter
337: .getUncheckedBitstreamsReport(writer);
338: writer.flush();
339: writer.close();
340: emailer.sendReport(report, numBitstreams);
341: }
342: }
343: } catch (Exception e) {
344: log.fatal(e);
345: } finally {
346: if (writer != null) {
347: try {
348: writer.close();
349: } catch (Exception e) {
350: log.fatal("Could not close writer", e);
351: }
352: }
353:
354: if (report != null) {
355: if (report.exists()) {
356: report.delete();
357: }
358: }
359: }
360: }
361: }
|