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