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