1   /**
2    * Copyright (c) 2000-2007 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  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  /**
41   * <a href="MailCache.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Jorge Ferrer
44   *
45   */
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 }