KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > checker > DailyReportEmailer


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.checker;
35
36 import java.io.File JavaDoc;
37 import java.io.FileWriter JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.util.Date JavaDoc;
40 import java.util.GregorianCalendar JavaDoc;
41 import java.util.Properties JavaDoc;
42
43 import javax.activation.DataHandler JavaDoc;
44 import javax.activation.DataSource JavaDoc;
45 import javax.activation.FileDataSource JavaDoc;
46 import javax.mail.BodyPart JavaDoc;
47 import javax.mail.Message JavaDoc;
48 import javax.mail.Multipart JavaDoc;
49 import javax.mail.Session JavaDoc;
50 import javax.mail.Transport JavaDoc;
51 import javax.mail.internet.InternetAddress JavaDoc;
52 import javax.mail.internet.MimeBodyPart JavaDoc;
53 import javax.mail.internet.MimeMessage JavaDoc;
54 import javax.mail.internet.MimeMultipart JavaDoc;
55
56 import org.apache.commons.cli.CommandLine;
57 import org.apache.commons.cli.CommandLineParser;
58 import org.apache.commons.cli.HelpFormatter;
59 import org.apache.commons.cli.Options;
60 import org.apache.commons.cli.ParseException;
61 import org.apache.commons.cli.PosixParser;
62 import org.apache.log4j.Logger;
63 import org.dspace.core.ConfigurationManager;
64
65 /**
66  * <p>
67  * The email reporter creates and sends emails to an administrator. This only
68  * reports information for todays date. It is expected this will be used just
69  * after the checksum checker has been run.
70  * </p>
71  *
72  * @author Jim Downing
73  * @author Grace Carpenter
74  * @author Nathan Sarr
75  *
76  *
77  */

78 public class DailyReportEmailer
79 {
80     /** log4j logger. */
81     private static Logger log = Logger.getLogger(DailyReportEmailer.class);
82
83     /**
84      * Default constructor.
85      */

86     public DailyReportEmailer()
87     {
88     }
89
90     /**
91      * Send the report through email.
92      *
93      * @param attachment
94      * the file conntaing the report
95      * @param numberOfBitstreams
96      * the number of bitstreams reported
97      *
98      * @throws IOException
99      * if IO exception occurs
100      * @throws javax.mail.MessagingException
101      * if message cannot be sent.
102      */

103     public void sendReport(File JavaDoc attachment, int numberOfBitstreams)
104             throws IOException JavaDoc, javax.mail.MessagingException JavaDoc
105     {
106         // Get the mail configuration properties
107
String JavaDoc server = ConfigurationManager.getProperty("mail.server");
108
109         // Set up properties for mail session
110
Properties JavaDoc props = System.getProperties();
111         props.put("mail.smtp.host", server);
112
113         // Get session
114
Session JavaDoc session = Session.getDefaultInstance(props, null);
115
116         MimeMessage JavaDoc msg = new MimeMessage JavaDoc(session);
117         Multipart JavaDoc multipart = new MimeMultipart JavaDoc();
118
119         // create the first part of the email
120
BodyPart JavaDoc messageBodyPart = new MimeBodyPart JavaDoc();
121         messageBodyPart
122                 .setText("This is the checksum checker report see attachement for details \n"
123                         + numberOfBitstreams
124                         + " Bitstreams found with POSSIBLE issues");
125         multipart.addBodyPart(messageBodyPart);
126
127         // add the file
128
messageBodyPart = new MimeBodyPart JavaDoc();
129
130         DataSource JavaDoc source = new FileDataSource JavaDoc(attachment);
131         messageBodyPart.setDataHandler(new DataHandler JavaDoc(source));
132         messageBodyPart.setFileName("checksum_checker_report.txt");
133         multipart.addBodyPart(messageBodyPart);
134         msg.setContent(multipart);
135         msg.setFrom(new InternetAddress JavaDoc(ConfigurationManager
136                 .getProperty("mail.from.address")));
137         msg.addRecipient(Message.RecipientType.TO, new InternetAddress JavaDoc(
138                 ConfigurationManager.getProperty("mail.admin")));
139
140         msg.setSentDate(new Date JavaDoc());
141         msg.setSubject("Checksum checker Report - " + numberOfBitstreams
142                 + " Bitstreams found with POSSIBLE issues");
143         Transport.send(msg);
144     }
145
146     /**
147      * Allows users to have email sent to them. The default is to send all
148      * reports in one email
149      *
150      * @param args
151      * <dl>
152      * <dt>-h</dt>
153      * <dd>help</dd>
154      * <dt>-d</dt>
155      * <dd>Select deleted bitstreams</dd>
156      * <dt>-m</dt>
157      * <dd>Bitstreams missing from assetstore</dd>
158      * <dt>-c</dt>
159      * <dd>Bitstreams whoses checksums were changed</dd>
160      * <dt>-n</dt>
161      * <dd>Bitstreams whoses checksums were changed</dd>
162      * <dt>-a</dt>
163      * <dd>Send all reports in one email</dd>
164      * </dl>
165      *
166      */

167     public static void main(String JavaDoc[] args)
168     {
169         // set up command line parser
170
CommandLineParser parser = new PosixParser();
171         CommandLine line = null;
172
173         // create an options object and populate it
174
Options options = new Options();
175
176         options.addOption("h", "help", false, "Help");
177         options
178                 .addOption("d", "Deleted", false,
179                         "Send E-mail report for all bitstreams set as deleted for today");
180         options
181                 .addOption("m", "Missing", false,
182                         "Send E-mail report for all bitstreams not found in assetstore for today");
183         options
184                 .addOption(
185                         "c",
186                         "Changed",
187                         false,
188                         "Send E-mail report for all bitstrems where checksum has been changed for today");
189         options.addOption("a", "All", false, "Send all E-mail reports");
190
191         options.addOption("u", "Unchecked", false,
192                 "Send the Unchecked bitstream report");
193
194         options
195                 .addOption("n", "Not Processed", false,
196                         "Send E-mail report for all bitstreams set to longer be processed for today");
197
198         try
199         {
200             line = parser.parse(options, args);
201         }
202         catch (ParseException e)
203         {
204             log.fatal(e);
205             System.exit(1);
206         }
207
208         // user asks for help
209
if (line.hasOption('h'))
210         {
211             HelpFormatter myhelp = new HelpFormatter();
212
213             myhelp.printHelp("Checksum Reporter\n", options);
214             System.out
215                     .println("\nSend Deleted bitstream email report: DailyReportEmailer -d");
216             System.out
217                     .println("\nSend Missing bitstreams email report: DailyReportEmailer -m");
218             System.out
219                     .println("\nSend Checksum Changed email report: DailyReportEmailer -c");
220
221             System.out
222                     .println("\nSend bitstream not to be processed email report: DailyReportEmailer -n");
223
224             System.out
225                     .println("\nSend Un-checked bitstream report: DailyReportEmailer -u");
226
227             System.out.println("\nSend All email reports: DailyReportEmailer");
228             System.exit(0);
229         }
230
231         // create a new simple reporter
232
SimpleReporter reporter = new SimpleReporterImpl();
233
234         DailyReportEmailer emailer = new DailyReportEmailer();
235
236         // get dates for yesterday and tomorrow
237
GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc();
238         calendar.add(GregorianCalendar.DAY_OF_YEAR, -1);
239
240         Date JavaDoc yesterday = calendar.getTime();
241         calendar.add(GregorianCalendar.DAY_OF_YEAR, 2);
242
243         Date JavaDoc tomorrow = calendar.getTime();
244
245         File JavaDoc report = null;
246         FileWriter JavaDoc writer = null;
247
248         try
249         {
250             // the number of bitstreams in report
251
int numBitstreams = 0;
252
253             // create a temporary file in the log directory
254
String JavaDoc dirLocation = ConfigurationManager.getProperty("log.dir");
255             File JavaDoc directory = new File JavaDoc(dirLocation);
256
257             if (directory.exists() && directory.isDirectory())
258             {
259                 report = File.createTempFile("checker_report", ".txt",
260                         directory);
261             }
262             else
263             {
264                 throw new IllegalStateException JavaDoc("directory :" + dirLocation
265                         + " does not exist");
266             }
267
268             writer = new FileWriter JavaDoc(report);
269
270             if ((line.hasOption("a")) || (line.getOptions().length == 0))
271             {
272                 writer
273                         .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
274                 numBitstreams += reporter.getDeletedBitstreamReport(yesterday,
275                         tomorrow, writer);
276                 writer
277                         .write("\n--------------------------------- Report Spacer ---------------------------\n\n");
278                 numBitstreams += reporter.getChangedChecksumReport(yesterday,
279                         tomorrow, writer);
280                 writer
281                         .write("\n--------------------------------- Report Spacer ---------------------------\n\n");
282                 numBitstreams += reporter.getBitstreamNotFoundReport(yesterday,
283                         tomorrow, writer);
284                 writer
285                         .write("\n--------------------------------- Report Spacer ---------------------------\n\n");
286                 numBitstreams += reporter.getNotToBeProcessedReport(yesterday,
287                         tomorrow, writer);
288                 writer
289                         .write("\n--------------------------------- Report Spacer ---------------------------\n\n");
290                 numBitstreams += reporter.getUncheckedBitstreamsReport(writer);
291                 writer
292                         .write("\n--------------------------------- End Report ---------------------------\n\n");
293                 writer.flush();
294                 writer.close();
295                 emailer.sendReport(report, numBitstreams);
296             }
297             else
298             {
299                 if (line.hasOption("d"))
300                 {
301                     writer
302                             .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
303                     numBitstreams += reporter.getDeletedBitstreamReport(
304                             yesterday, tomorrow, writer);
305                     writer.flush();
306                     writer.close();
307                     emailer.sendReport(report, numBitstreams);
308                 }
309
310                 if (line.hasOption("m"))
311                 {
312                     writer
313                             .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
314                     numBitstreams += reporter.getBitstreamNotFoundReport(
315                             yesterday, tomorrow, writer);
316                     writer.flush();
317                     writer.close();
318                     emailer.sendReport(report, numBitstreams);
319                 }
320
321                 if (line.hasOption("c"))
322                 {
323                     writer
324                             .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
325                     numBitstreams += reporter.getChangedChecksumReport(
326                             yesterday, tomorrow, writer);
327                     writer.flush();
328                     writer.close();
329                     emailer.sendReport(report, numBitstreams);
330                 }
331
332                 if (line.hasOption("n"))
333                 {
334                     writer
335                             .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
336                     numBitstreams += reporter.getNotToBeProcessedReport(
337                             yesterday, tomorrow, writer);
338                     writer.flush();
339                     writer.close();
340                     emailer.sendReport(report, numBitstreams);
341                 }
342
343                 if (line.hasOption("u"))
344                 {
345                     writer
346                             .write("\n--------------------------------- Begin Reporting ------------------------\n\n");
347                     numBitstreams += reporter
348                             .getUncheckedBitstreamsReport(writer);
349                     writer.flush();
350                     writer.close();
351                     emailer.sendReport(report, numBitstreams);
352                 }
353             }
354         }
355         catch (Exception JavaDoc e)
356         {
357             log.fatal(e);
358         }
359         finally
360         {
361             if (writer != null)
362             {
363                 try
364                 {
365                     writer.close();
366                 }
367                 catch (Exception JavaDoc e)
368                 {
369                     log.fatal("Could not close writer", e);
370                 }
371             }
372
373             if (report != null)
374             {
375                 if (report.exists())
376                 {
377                     report.delete();
378                 }
379             }
380         }
381     }
382 }
383
Popular Tags