KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > msgsperweek


1 /*
2  * @(#)msgsperweek.java 1.5 01/09/06
3  *
4  * Copyright (c) 1998-2001 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  */

7
8 import java.util.*;
9 import java.text.*;
10 import java.io.*;
11 import javax.mail.*;
12 import javax.mail.search.*;
13 import javax.mail.internet.*;
14
15 /**
16  * Program that generates stats about new messages received per week
17  * by the javamail@Sun.COM mailing list. It tracks *new* messages only,
18  * the rule being that the message has a single addressee:
19  * "javamail@Sun.COM". Also searches each message body for the "pop3"
20  * string and counts its occurrence.
21  *
22  * Note that this program operates on the log file directly and thus all
23  * access to the log file using this program must be through the same
24  * IMAP server so that the flags are handled consistently.
25  *
26  * @author Max Spivak
27  */

28
29 public class msgsperweek {
30
31     static String JavaDoc protocol = "imap";
32     static String JavaDoc host = "anybodys.sfbay";
33     static String JavaDoc user = "javamail";
34     static String JavaDoc password = "1javamail1";
35     static String JavaDoc mbox =
36     "/net/anybodys.sfbay/export6/javamail/logs/javamail.log";
37     static String JavaDoc url = null;
38     static boolean verbose = false;
39     static boolean doPop3 = false;
40     static Calendar cal = null;
41
42     public static void main(String JavaDoc argv[]) {
43     int optind;
44
45     for (optind = 0; optind < argv.length; optind++) {
46         if (argv[optind].equals("-T")) {
47         protocol = argv[++optind];
48         } else if (argv[optind].equals("-H")) {
49         host = argv[++optind];
50         } else if (argv[optind].equals("-U")) {
51         user = argv[++optind];
52         } else if (argv[optind].equals("-P")) {
53         password = argv[++optind];
54         } else if (argv[optind].equals("-v")) {
55         verbose = true;
56         } else if (argv[optind].equals("-f")) {
57         mbox = argv[++optind];
58         } else if (argv[optind].equals("-L")) {
59         url = argv[++optind];
60         } else if (argv[optind].equals("-p")) {
61         doPop3 = true;
62         } else if (argv[optind].equals("--")) {
63         optind++;
64         break;
65         } else if (argv[optind].startsWith("-")) {
66         System.out.println(
67 "Usage: msgperweek [-L url] [-T protocol] [-H host] [-U user] [-P password]\n"+
68 "\t[-f mailbox] [-v] [-p]");
69         System.exit(1);
70         } else {
71         break;
72         }
73     }
74
75     System.out.println("msgsperweek: Generating stats, please wait...");
76     
77     cal = Calendar.getInstance();
78     
79         try {
80         // Get a Properties object
81
Properties props = System.getProperties();
82
83         // Get a Session object
84
Session session = Session.getDefaultInstance(props,
85                     new TtyAuthenticator());
86
87         //if (verbose)
88
//session.setDebug(true);
89

90         // Get a Store object
91
Store store = null;
92         if (url != null) {
93         URLName urln = new URLName(url);
94         store = session.getStore(urln);
95         store.connect();
96         } else {
97         if (protocol != null)
98             store = session.getStore(protocol);
99         else
100             store = session.getStore();
101
102         // Connect
103
if (host != null || user != null || password != null)
104             store.connect(host, user, password);
105         else
106             store.connect();
107         }
108         
109
110         // Open the Folder
111

112         Folder folder = store.getDefaultFolder();
113         if (folder == null) {
114             System.out.println("No default folder");
115             System.exit(1);
116         }
117
118         folder = folder.getFolder(mbox);
119         if (folder == null) {
120             System.out.println("Invalid folder");
121             System.exit(1);
122         }
123
124         folder.open(Folder.READ_ONLY);
125         int totalMessages = folder.getMessageCount();
126
127         if (totalMessages == 0) {
128         System.out.println("Empty folder");
129         folder.close(false);
130         store.close();
131         System.exit(1);
132         }
133
134         if (verbose) {
135         int newMessages = folder.getNewMessageCount();
136         pv("Total messages = " + totalMessages);
137         pv("New messages = " + newMessages);
138         pv("-------------------------------");
139         }
140
141         // Use a suitable FetchProfile
142
FetchProfile fp = new FetchProfile();
143         fp.add(FetchProfile.Item.ENVELOPE);
144
145         SearchTerm term = new RecipientTerm(Message.RecipientType.TO,
146                      new InternetAddress("javamail@sun.com"));
147         Message[] msgs = folder.search(term);
148         folder.fetch(msgs, fp);
149
150         Hashtable wks = new Hashtable();
151         int totalCount = 0;
152         BodyTerm pop3Search = new BodyTerm("pop3");
153         int pop3Requests = 0;
154
155         // go through each msg and count it if it's an incoming msg
156
for (int i = 0; i < msgs.length; i++) {
157         // make sure we only have a single addressee:
158
// javamail@sun.com, which means it's a new message
159
// from outside of Sun
160
Address[] recs=msgs[i].getRecipients(Message.RecipientType.TO);
161         if (recs.length > 1)
162             continue;
163
164         // get msgs date
165
Date d = msgs[i].getSentDate();
166         if (d == null)
167             d = msgs[i].getReceivedDate();
168         cal.setTime(d);
169
170         // figure out what week it is
171
int week = cal.get(Calendar.WEEK_OF_YEAR);
172         int yr = cal.get(Calendar.YEAR);
173         String JavaDoc wkInYr = week + " " + yr;
174
175         // increment that week's count
176
String JavaDoc num = (String JavaDoc)wks.get(wkInYr);
177         totalCount++;
178         if (num == null)
179             wks.put(wkInYr, "1");
180         else {
181             int count = Integer.parseInt(num);
182             count++;
183             Integer JavaDoc str = new Integer JavaDoc(count);
184             wks.put(wkInYr, str.toString());
185         }
186
187         // check for pop3 requests
188
if (doPop3 && pop3Search.match(msgs[i]))
189             pop3Requests++;
190         }
191
192         // print out the statistics
193
int startWk = 40;
194         int startYr = 1997;
195         Calendar now = Calendar.getInstance();
196         now.setTime(new Date());
197         cal.set(startYr, 1, 1);
198         cal.set(Calendar.WEEK_OF_YEAR, startWk);
199         cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
200         DateFormat df = new SimpleDateFormat("E MMM dd yyyy");
201         int totalWks = 0;
202
203         System.out.println("\nNumber of new messages a week sent to javamail@Sun.COM by external JavaMail\nusers. This does not include our answers and follow-up mail.");
204         for (;;) {
205         if (cal.after(now))
206             break;
207         else {
208             totalWks++;
209             String JavaDoc wkInYr = (cal.get(Calendar.WEEK_OF_YEAR)) +
210             " " +
211             (cal.get(Calendar.YEAR));
212             Object JavaDoc o = wks.get(wkInYr);
213             if (o != null) {
214             String JavaDoc i = (String JavaDoc)o;
215             System.out.println(" week of " +
216                        df.format(cal.getTime()) +
217                        ": " +
218                        i + " messages");
219             }
220                         
221             // increment to next week
222
cal.add(Calendar.WEEK_OF_YEAR, 1);
223         }
224         }
225         System.out.println("------------------");
226         System.out.println("Total messages received: " + totalCount);
227         System.out.println("Average messages/week: " +
228                    totalCount/totalWks);
229         if (doPop3)
230         System.out.println("Total POP3 requests: " + pop3Requests);
231         folder.close(false);
232         store.close();
233     } catch (Exception JavaDoc ex) {
234         System.out.println("Oops, got exception! " + ex.getMessage());
235         ex.printStackTrace();
236     }
237     System.exit(1);
238     }
239
240
241     /**
242      * Print verbose.
243      */

244     static void pv(String JavaDoc s) {
245     if (verbose)
246         System.out.println(s);
247     }
248 }
249
Popular Tags