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;
24  
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.mail.util.multiaccount.MailCache;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import javax.mail.Folder;
32  import javax.mail.MessagingException;
33  
34  import javax.servlet.http.HttpServletRequest;
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="MailSessionLock.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Alexander Chow
44   *
45   */
46  public class MailSessionLock {
47  
48      public static void cleanUp(HttpSession ses) {
49          _instance._cleanUp(ses);
50      }
51  
52      public static void lock(HttpServletRequest req) {
53          _instance._lock(req.getSession().getId());
54      }
55  
56      public static void unlock(HttpServletRequest req) {
57          _instance._unlock(req.getSession().getId());
58      }
59  
60      private MailSessionLock() {
61      }
62  
63      private void _cleanUp(HttpSession ses) {
64          try {
65  
66              // This method duplicates the same method in MailUtil because of
67              // LEP-4829.
68  
69              Folder folder = (Folder)ses.getAttribute(WebKeys.MAIL_FOLDER);
70  
71              if ((folder != null) && folder.isOpen()) {
72                  try {
73                      folder.close(false);
74                  }
75                  catch (MessagingException me) {
76                      if (_log.isWarnEnabled()) {
77                          _log.warn(me);
78                      }
79                  }
80  
81                  ses.removeAttribute(WebKeys.MAIL_FOLDER);
82              }
83  
84              MailCache.clearCache(ses);
85  
86              ses.removeAttribute(WebKeys.MAIL_MESSAGE_ID);
87          }
88          catch (Exception e) {
89          }
90  
91          synchronized (_sessionMap) {
92              _sessionMap.remove(ses.getId());
93          }
94      }
95  
96      private void _lock(String sessionId) {
97          ThreadLocal<Long> threadLocal = null;
98  
99          for (;;) {
100             synchronized (_sessionMap) {
101                 threadLocal = _sessionMap.get(sessionId);
102 
103                 if (threadLocal == null) {
104 
105                     // Initialize reentrant counter.
106 
107                     threadLocal = new ThreadLocal<Long>();
108 
109                     threadLocal.set(new Long(0));
110 
111                     _sessionMap.put(sessionId, threadLocal);
112 
113                     break;
114                 }
115                 else if (threadLocal.get() != null) {
116 
117                     // This thread instantiated the thread local. Increment the
118                     // reentrant counter.
119 
120                     Long count = threadLocal.get();
121 
122                     threadLocal.set(new Long(count.longValue() + 1L));
123 
124                     break;
125                 }
126             }
127 
128             // Another thread instantiated the thread local. Wait until that
129             // thread is done.
130 
131             try {
132                 wait(100);
133             }
134             catch (Exception ex) {
135             }
136         }
137     }
138 
139     private void _unlock(String sessionId) {
140         ThreadLocal<Long> threadLocal = null;
141 
142         synchronized (_sessionMap) {
143             threadLocal = _sessionMap.get(sessionId);
144 
145             // The variable can be null at this time if a method called unlock()
146             // twice or cleanUp() was called.
147 
148             if (threadLocal != null) {
149                 Long count = threadLocal.get();
150 
151                 if (count.longValue() == 0L) {
152 
153                     // All reentrant calls have completed
154 
155                     _sessionMap.remove(sessionId);
156                 }
157                 else {
158 
159                     // Finished one of the reentrant calls
160 
161                     count = new Long(count.longValue() - 1L);
162 
163                     threadLocal.set(count);
164                 }
165             }
166         }
167     }
168 
169     private static Log _log = LogFactory.getLog(MailSessionLock.class);
170 
171     private static MailSessionLock _instance = new MailSessionLock();
172 
173     private Map<String, ThreadLocal<Long>> _sessionMap =
174         new HashMap<String, ThreadLocal<Long>>();
175 
176 }