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