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.portal.servlet;
24  
25  import com.liferay.portal.events.EventsProcessor;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.security.auth.CompanyThreadLocal;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  import com.liferay.portal.struts.ActionException;
30  import com.liferay.portal.util.LiveUsers;
31  import com.liferay.portal.util.PortalInstances;
32  import com.liferay.portal.util.PropsUtil;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.mail.util.MailSessionLock;
35  import com.liferay.portlet.messaging.util.MessagingUtil;
36  
37  import javax.servlet.http.HttpSession;
38  import javax.servlet.http.HttpSessionEvent;
39  import javax.servlet.http.HttpSessionListener;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="PortalSessionListener.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class PortalSessionListener implements HttpSessionListener {
51  
52      public void sessionCreated(HttpSessionEvent event) {
53          HttpSession ses = event.getSession();
54  
55          PortalSessionContext.put(ses.getId(), ses);
56  
57          // Process session created events
58  
59          try {
60              EventsProcessor.process(PropsUtil.getArray(
61                  PropsUtil.SERVLET_SESSION_CREATE_EVENTS), ses);
62          }
63          catch (ActionException ae) {
64              _log.error(ae, ae);
65          }
66      }
67  
68      public void sessionDestroyed(HttpSessionEvent event) {
69          HttpSession ses = event.getSession();
70  
71          try {
72              Long userIdObj = (Long)ses.getAttribute(WebKeys.USER_ID);
73  
74              if (userIdObj == null) {
75                  _log.warn("User id is not in the session");
76              }
77  
78              if (userIdObj == null) {
79                  return;
80              }
81  
82              long userId = userIdObj.longValue();
83  
84              if (CompanyThreadLocal.getCompanyId() == 0) {
85                  setCompanyId(userId);
86              }
87  
88              MailSessionLock.cleanUp(ses);
89  
90              LiveUsers.signOut(ses.getId(), userId);
91          }
92          catch (IllegalStateException ise) {
93              _log.warn("Please upgrade to a servlet 2.4 compliant container");
94          }
95          catch (Exception e) {
96              _log.error(e, e);
97          }
98  
99          ses.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
100         ses.removeAttribute(WebKeys.REVERSE_AJAX);
101 
102         MessagingUtil.closeXMPPConnection(ses);
103 
104         PortalSessionContext.remove(ses.getId());
105 
106         // Process session destroyed events
107 
108         try {
109             EventsProcessor.process(PropsUtil.getArray(
110                 PropsUtil.SERVLET_SESSION_DESTROY_EVENTS), ses);
111         }
112         catch (ActionException ae) {
113             _log.error(ae, ae);
114         }
115     }
116 
117     protected void setCompanyId(long userId) throws Exception {
118         long[] companyIds = PortalInstances.getCompanyIds();
119 
120         long companyId = 0;
121 
122         if (companyIds.length == 1) {
123             companyId = companyIds[0];
124         }
125         else if (companyIds.length > 1) {
126             try {
127                 User user = UserLocalServiceUtil.getUserById(userId);
128 
129                 companyId = user.getCompanyId();
130             }
131             catch (Exception e) {
132                 if (_log.isWarnEnabled()) {
133                     _log.warn(
134                         "Unable to set the company id for user " + userId, e);
135                 }
136             }
137         }
138 
139         if (companyId > 0) {
140             CompanyThreadLocal.setCompanyId(companyId);
141         }
142     }
143 
144     private static Log _log = LogFactory.getLog(PortalSessionListener.class);
145 
146 }