001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.util;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.servlet.HttpHeaders;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.model.Group;
027: import com.liferay.portal.model.UserTracker;
028: import com.liferay.portal.security.auth.CompanyThreadLocal;
029: import com.liferay.portal.service.GroupLocalServiceUtil;
030: import com.liferay.portal.service.UserTrackerLocalServiceUtil;
031: import com.liferay.portal.service.persistence.UserTrackerUtil;
032: import com.liferay.util.CollectionFactory;
033: import com.liferay.util.dao.hibernate.QueryUtil;
034:
035: import java.util.ArrayList;
036: import java.util.Date;
037: import java.util.Iterator;
038: import java.util.LinkedHashMap;
039: import java.util.List;
040: import java.util.Map;
041: import java.util.Set;
042:
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpSession;
045:
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048:
049: /**
050: * <a href="LiveUsers.java.html"><b><i>View Source</i></b></a>
051: *
052: * @author Charles May
053: * @author Brian Wing Shun Chan
054: *
055: */
056: public class LiveUsers {
057:
058: public static void deleteGroup(long groupId) {
059: _instance._deleteGroup(groupId);
060: }
061:
062: public static Set getGroupUsers(long groupId) {
063: return _instance._getGroupUsers(_instance._getLiveUsers(),
064: groupId);
065: }
066:
067: public static int getGroupUsersCount(long groupId) {
068: return getGroupUsers(groupId).size();
069: }
070:
071: public static Map getSessionUsers() {
072: return _instance._getSessionUsers(_instance._getLiveUsers());
073: }
074:
075: public static int getSessionUsersCount() {
076: return getSessionUsers().size();
077: }
078:
079: public static UserTracker getUserTracker(String sesId) {
080: return _instance._getUserTracker(sesId);
081: }
082:
083: public static void joinGroup(long userId, long groupId) {
084: _instance._joinGroup(userId, groupId);
085: }
086:
087: public static void joinGroup(long[] userIds, long groupId) {
088: _instance._joinGroup(userIds, groupId);
089: }
090:
091: public static void leaveGroup(long userId, long groupId) {
092: _instance._leaveGroup(userId, groupId);
093: }
094:
095: public static void leaveGroup(long[] userIds, long groupId) {
096: _instance._leaveGroup(userIds, groupId);
097: }
098:
099: public static void signIn(HttpServletRequest req)
100: throws SystemException {
101: _instance._signIn(req);
102: }
103:
104: public static void signOut(String sesId, long userId)
105: throws SystemException {
106:
107: _instance._signOut(sesId, userId);
108: }
109:
110: private LiveUsers() {
111: }
112:
113: private void _addUserTracker(long userId, UserTracker userTracker) {
114: List userTrackers = _getUserTrackers(userId);
115:
116: if (userTrackers != null) {
117: userTrackers.add(userTracker);
118: } else {
119: userTrackers = new ArrayList();
120:
121: userTrackers.add(userTracker);
122:
123: Map userTrackersMap = _getUserTrackersMap();
124:
125: userTrackersMap.put(new Long(userId), userTrackers);
126: }
127: }
128:
129: private void _deleteGroup(long groupId) {
130: Map liveUsers = _getLiveUsers();
131:
132: liveUsers.remove(new Long(groupId));
133: }
134:
135: private Set _getGroupUsers(Map liveUsers, long groupId) {
136: Long groupIdObj = new Long(groupId);
137:
138: Set groupUsers = (Set) liveUsers.get(groupIdObj);
139:
140: if (groupUsers == null) {
141: groupUsers = CollectionFactory.getSyncHashSet();
142:
143: liveUsers.put(groupIdObj, groupUsers);
144: }
145:
146: return groupUsers;
147: }
148:
149: private Map _getLiveUsers() {
150: String companyIdString = String.valueOf(CompanyThreadLocal
151: .getCompanyId());
152:
153: Map liveUsers = (Map) WebAppPool.get(companyIdString,
154: WebKeys.LIVE_USERS);
155:
156: if (liveUsers == null) {
157: liveUsers = CollectionFactory.getSyncHashMap();
158:
159: WebAppPool.put(companyIdString, WebKeys.LIVE_USERS,
160: liveUsers);
161: }
162:
163: return liveUsers;
164: }
165:
166: private Map _getSessionUsers(Map liveUsers) {
167: Long groupIdObj = new Long(0);
168:
169: Map sessionUsers = (Map) liveUsers.get(groupIdObj);
170:
171: if (sessionUsers == null) {
172: sessionUsers = CollectionFactory.getSyncHashMap();
173:
174: liveUsers.put(groupIdObj, sessionUsers);
175: }
176:
177: return sessionUsers;
178: }
179:
180: private UserTracker _getUserTracker(String sesId) {
181: Map liveUsers = _getLiveUsers();
182:
183: Map sessionUsers = _getSessionUsers(liveUsers);
184:
185: return (UserTracker) sessionUsers.get(sesId);
186: }
187:
188: private List _getUserTrackers(long userId) {
189: Map userTrackersMap = _getUserTrackersMap();
190:
191: return (List) userTrackersMap.get(new Long(userId));
192: }
193:
194: private Map _getUserTrackersMap() {
195: Map liveUsers = _getLiveUsers();
196:
197: Long groupIdObj = new Long(-1);
198:
199: Map userTrackersMap = (Map) liveUsers.get(groupIdObj);
200:
201: if (userTrackersMap == null) {
202: userTrackersMap = CollectionFactory.getSyncHashMap();
203:
204: liveUsers.put(groupIdObj, userTrackersMap);
205: }
206:
207: return userTrackersMap;
208: }
209:
210: private void _joinGroup(long userId, long groupId) {
211: Map liveUsers = _getLiveUsers();
212:
213: Set groupUsers = _getGroupUsers(liveUsers, groupId);
214:
215: if (_getUserTrackers(userId) != null) {
216: groupUsers.add(new Long(userId));
217: }
218: }
219:
220: private void _joinGroup(long[] userIds, long groupId) {
221: Map liveUsers = _getLiveUsers();
222:
223: Set groupUsers = _getGroupUsers(liveUsers, groupId);
224:
225: for (int i = 0; i < userIds.length; i++) {
226: long userId = userIds[i];
227:
228: if (_getUserTrackers(userId) != null) {
229: groupUsers.add(new Long(userId));
230: }
231: }
232: }
233:
234: private void _leaveGroup(long userId, long groupId) {
235: Map liveUsers = _getLiveUsers();
236:
237: Set groupUsers = _getGroupUsers(liveUsers, groupId);
238:
239: groupUsers.remove(new Long(userId));
240: }
241:
242: private void _leaveGroup(long[] userIds, long groupId) {
243: Map liveUsers = _getLiveUsers();
244:
245: Set groupUsers = _getGroupUsers(liveUsers, groupId);
246:
247: for (int i = 0; i < userIds.length; i++) {
248: long userId = userIds[i];
249:
250: groupUsers.remove(new Long(userId));
251: }
252: }
253:
254: private void _removeUserTracker(long userId, UserTracker userTracker) {
255: List userTrackers = _getUserTrackers(userId);
256:
257: if (userTrackers != null) {
258: Iterator itr = userTrackers.iterator();
259:
260: while (itr.hasNext()) {
261: UserTracker curUserTracker = (UserTracker) itr.next();
262:
263: if (userTracker.equals(curUserTracker)) {
264: itr.remove();
265: }
266: }
267:
268: if (userTrackers.size() == 0) {
269: Map userTrackersMap = _getUserTrackersMap();
270:
271: userTrackersMap.remove(new Long(userId));
272: }
273: }
274: }
275:
276: private void _signIn(HttpServletRequest req) throws SystemException {
277: HttpSession ses = req.getSession();
278:
279: long companyId = CompanyThreadLocal.getCompanyId();
280: long userId = GetterUtil.getLong(req.getRemoteUser());
281:
282: Map liveUsers = _updateGroupStatus(userId, true);
283:
284: Map sessionUsers = _getSessionUsers(liveUsers);
285:
286: List userTrackers = _getUserTrackers(userId);
287:
288: if (!PropsValues.AUTH_SIMULTANEOUS_LOGINS) {
289: if (userTrackers != null) {
290: for (int i = 0; i < userTrackers.size(); i++) {
291: UserTracker userTracker = (UserTracker) userTrackers
292: .get(i);
293:
294: // Disable old login
295:
296: userTracker.getHttpSession().setAttribute(
297: WebKeys.STALE_SESSION, Boolean.TRUE);
298: }
299: }
300: }
301:
302: UserTracker userTracker = (UserTracker) sessionUsers.get(ses
303: .getId());
304:
305: if ((userTracker == null)
306: && (PropsValues.SESSION_TRACKER_MEMORY_ENABLED)) {
307:
308: userTracker = UserTrackerUtil.create(0);
309:
310: userTracker.setCompanyId(companyId);
311: userTracker.setUserId(userId);
312: userTracker.setModifiedDate(new Date());
313: userTracker.setHttpSession(ses);
314: userTracker.setRemoteAddr(req.getRemoteAddr());
315: userTracker.setRemoteHost(req.getRemoteHost());
316: userTracker.setUserAgent(req
317: .getHeader(HttpHeaders.USER_AGENT));
318:
319: sessionUsers.put(ses.getId(), userTracker);
320:
321: _addUserTracker(userId, userTracker);
322: }
323: }
324:
325: private void _signOut(String sesId, long userId)
326: throws SystemException {
327: List userTrackers = _getUserTrackers(userId);
328:
329: Map liveUsers = null;
330:
331: if ((userTrackers == null) || (userTrackers.size() <= 1)) {
332: liveUsers = _updateGroupStatus(userId, false);
333: }
334:
335: if (liveUsers == null) {
336: liveUsers = _getLiveUsers();
337: }
338:
339: Map sessionUsers = _getSessionUsers(liveUsers);
340:
341: UserTracker userTracker = (UserTracker) sessionUsers
342: .remove(sesId);
343:
344: if (userTracker != null) {
345: try {
346: UserTrackerLocalServiceUtil
347: .addUserTracker(userTracker.getCompanyId(),
348: userTracker.getUserId(), userTracker
349: .getModifiedDate(), sesId,
350: userTracker.getRemoteAddr(),
351: userTracker.getRemoteHost(),
352: userTracker.getUserAgent(), userTracker
353: .getPaths());
354: } catch (Exception e) {
355: if (_log.isWarnEnabled()) {
356: _log.warn(e.getMessage());
357: }
358: }
359:
360: _removeUserTracker(userId, userTracker);
361: }
362: }
363:
364: private Map _updateGroupStatus(long userId, boolean signedIn)
365: throws SystemException {
366:
367: Long userIdObj = new Long(userId);
368:
369: long companyId = CompanyThreadLocal.getCompanyId();
370:
371: Map liveUsers = _getLiveUsers();
372:
373: LinkedHashMap groupParams = new LinkedHashMap();
374:
375: groupParams.put("usersGroups", userIdObj);
376:
377: List communities = GroupLocalServiceUtil.search(companyId,
378: null, null, groupParams, QueryUtil.ALL_POS,
379: QueryUtil.ALL_POS);
380:
381: for (int i = 0; i < communities.size(); i++) {
382: Group community = (Group) communities.get(i);
383:
384: Set groupUsers = _getGroupUsers(liveUsers, community
385: .getGroupId());
386:
387: if (signedIn) {
388: groupUsers.add(userIdObj);
389: } else {
390: groupUsers.remove(userIdObj);
391: }
392: }
393:
394: return liveUsers;
395: }
396:
397: private static Log _log = LogFactory.getLog(LiveUsers.class);
398:
399: private static LiveUsers _instance = new LiveUsers();
400:
401: }
|