1
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
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
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
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 }