1
22
23 package com.liferay.portlet.mail.util.multiaccount;
24
25 import com.liferay.portal.util.WebKeys;
26 import com.liferay.util.CollectionFactory;
27
28 import java.util.Collection;
29 import java.util.Iterator;
30 import java.util.Map;
31
32 import javax.mail.MessagingException;
33 import javax.mail.Store;
34
35 import javax.servlet.http.HttpSession;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40
46 public class MailCache {
47
48 public static Store getStore(HttpSession ses, String accountPrefix) {
49 return (Store)ses.getAttribute(WebKeys.MAIL_STORE + accountPrefix);
50 }
51
52 public static void putStore(
53 HttpSession ses, String accountPrefix, Store store) {
54
55 ses.setAttribute(WebKeys.MAIL_STORE + accountPrefix, store);
56 }
57
58 public static void removeStore(HttpSession ses, String accountPrefix) {
59 ses.removeAttribute(WebKeys.MAIL_STORE + accountPrefix);
60 }
61
62 public static Collection getUserAccounts(long userId) {
63 return (Collection)_accountsPool.get(new Long(userId));
64 }
65
66 public static void putUserAccounts(long userId, Collection accounts) {
67 _accountsPool.put(new Long(userId), accounts);
68 }
69
70 public static void removeUserAccounts(long userId) {
71 _accountsPool.remove(new Long(userId));
72 }
73
74 public static void clearCache(HttpSession ses) throws MessagingException {
75 Long userIdObj = (Long)ses.getAttribute(WebKeys.USER_ID);
76
77 if (_log.isDebugEnabled()) {
78 _log.debug("Clearing the mail cache for user " + userIdObj);
79 }
80
81 Collection accounts = getUserAccounts(userIdObj.longValue());
82
83 if (accounts != null) {
84 if (_log.isDebugEnabled()) {
85 _log.debug(
86 "User has " + accounts.size() + " accounts that might " +
87 "need to be closed");
88 }
89
90 Iterator itr = accounts.iterator();
91
92 while (itr.hasNext()) {
93 MailAccount account = (MailAccount)itr.next();
94
95 String accountPrefix = account.getName();
96
97 Store store = getStore(ses, accountPrefix);
98
99 if (store != null) {
100 store.close();
101 }
102
103 removeStore(ses, accountPrefix);
104 }
105
106 removeUserAccounts(userIdObj.longValue());
107 }
108 }
109
110 private static Log _log = LogFactory.getLog(MailCache.class);
111
112 private static Map _accountsPool = CollectionFactory.getSyncHashMap();
113
114 }