001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.mail.util.multiaccount;
022:
023: import com.liferay.portal.util.WebKeys;
024: import com.liferay.util.CollectionFactory;
025:
026: import java.util.Collection;
027: import java.util.Iterator;
028: import java.util.Map;
029:
030: import javax.mail.MessagingException;
031: import javax.mail.Store;
032:
033: import javax.servlet.http.HttpSession;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: /**
039: * <a href="MailCache.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Jorge Ferrer
042: *
043: */
044: public class MailCache {
045:
046: public static Store getStore(HttpSession ses, String accountPrefix) {
047: return (Store) ses.getAttribute(WebKeys.MAIL_STORE
048: + accountPrefix);
049: }
050:
051: public static void putStore(HttpSession ses, String accountPrefix,
052: Store store) {
053:
054: ses.setAttribute(WebKeys.MAIL_STORE + accountPrefix, store);
055: }
056:
057: public static void removeStore(HttpSession ses, String accountPrefix) {
058: ses.removeAttribute(WebKeys.MAIL_STORE + accountPrefix);
059: }
060:
061: public static Collection getUserAccounts(long userId) {
062: return (Collection) _accountsPool.get(new Long(userId));
063: }
064:
065: public static void putUserAccounts(long userId, Collection accounts) {
066: _accountsPool.put(new Long(userId), accounts);
067: }
068:
069: public static void removeUserAccounts(long userId) {
070: _accountsPool.remove(new Long(userId));
071: }
072:
073: public static void clearCache(HttpSession ses)
074: throws MessagingException {
075: Long userIdObj = (Long) ses.getAttribute(WebKeys.USER_ID);
076:
077: if (_log.isDebugEnabled()) {
078: _log.debug("Clearing the mail cache for user " + userIdObj);
079: }
080:
081: Collection accounts = getUserAccounts(userIdObj.longValue());
082:
083: if (accounts != null) {
084: if (_log.isDebugEnabled()) {
085: _log
086: .debug("User has " + accounts.size()
087: + " accounts that might "
088: + "need to be closed");
089: }
090:
091: Iterator itr = accounts.iterator();
092:
093: while (itr.hasNext()) {
094: MailAccount account = (MailAccount) itr.next();
095:
096: String accountPrefix = account.getName();
097:
098: Store store = getStore(ses, accountPrefix);
099:
100: if (store != null) {
101: store.close();
102: }
103:
104: removeStore(ses, accountPrefix);
105: }
106:
107: removeUserAccounts(userIdObj.longValue());
108: }
109: }
110:
111: private static Log _log = LogFactory.getLog(MailCache.class);
112:
113: private static Map _accountsPool = CollectionFactory
114: .getSyncHashMap();
115:
116: }
|