01: package org.claros.intouch.webmail.adapters;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05: import java.util.ArrayList;
06: import java.util.HashMap;
07: import java.util.List;
08:
09: import javax.mail.Message;
10: import javax.mail.MessagingException;
11: import javax.mail.event.MessageCountAdapter;
12: import javax.mail.event.MessageCountEvent;
13:
14: import org.apache.commons.logging.Log;
15: import org.apache.commons.logging.LogFactory;
16:
17: public class IdleMessageCountAdapter extends MessageCountAdapter {
18: private static Log log = LogFactory
19: .getLog(IdleMessageCountAdapter.class);
20: private static HashMap notices = new HashMap();
21: private String username;
22:
23: public IdleMessageCountAdapter() {
24: super ();
25: }
26:
27: public IdleMessageCountAdapter(String username) {
28: this .username = username;
29: }
30:
31: public void messagesAdded(MessageCountEvent ev) {
32: Message[] msgs = ev.getMessages();
33:
34: log.debug("Got " + msgs.length + " new messages in folder");
35:
36: // Just dump out the new messages
37: for (int i = 0; i < msgs.length; i++) {
38: try {
39: log
40: .debug("Message " + msgs[i].getMessageNumber()
41: + ":");
42: ByteArrayOutputStream bs = new ByteArrayOutputStream();
43: msgs[i].writeTo(bs);
44: log.debug(new String(bs.toByteArray()));
45: bs.close();
46:
47: addNotice(msgs[i]);
48: } catch (IOException ioex) {
49: log.error(ioex);
50: ioex.printStackTrace();
51: } catch (MessagingException mex) {
52: log.error(mex);
53: mex.printStackTrace();
54: }
55: }
56: }
57:
58: /**
59: *
60: * @param username2
61: * @param message
62: */
63: private synchronized void addNotice(Message message) {
64: List userNotices = (List) notices.get(username);
65: if (userNotices == null) {
66: userNotices = new ArrayList();
67: }
68: userNotices.add(message);
69: notices.put(username, userNotices);
70: }
71:
72: /**
73: *
74: * @param username
75: * @return
76: */
77: public synchronized static List getNotices(String username) {
78: List userNotices = null;
79: if (notices != null) {
80: userNotices = (List) notices.get(username);
81: }
82: return userNotices;
83: }
84:
85: /**
86: *
87: * @param username
88: */
89: public synchronized static void resetUserNotices(String username) {
90: notices.put(username, null);
91: }
92:
93: }
|