1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
40   * <a href="MailCache.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Jorge Ferrer
43   *
44   */
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 }