001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.liveusers.messaging;
016    
017    import com.liferay.portal.kernel.json.JSONObject;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.messaging.Message;
021    import com.liferay.portal.kernel.messaging.MessageListener;
022    import com.liferay.portal.liveusers.LiveUsers;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class LiveUsersMessageListener implements MessageListener {
028    
029            public void receive(Message message) {
030                    try {
031                            doReceive(message);
032                    }
033                    catch (Exception e) {
034                            _log.error("Unable to process message " + message, e);
035                    }
036            }
037    
038            protected void doCommandSignIn(JSONObject jsonObj) throws Exception {
039                    long companyId = jsonObj.getLong("companyId");
040                    long userId = jsonObj.getLong("userId");
041                    String sessionId = jsonObj.getString("sessionId");
042                    String remoteAddr = jsonObj.getString("remoteAddr");
043                    String remoteHost = jsonObj.getString("remoteHost");
044                    String userAgent = jsonObj.getString("userAgent");
045    
046                    LiveUsers.signIn(
047                            companyId, userId, sessionId, remoteAddr, remoteHost, userAgent);
048            }
049    
050            protected void doCommandSignOut(JSONObject jsonObj) throws Exception {
051                    long companyId = jsonObj.getLong("companyId");
052                    long userId = jsonObj.getLong("userId");
053                    String sessionId = jsonObj.getString("sessionId");
054    
055                    LiveUsers.signOut(companyId, userId, sessionId);
056            }
057    
058            protected void doReceive(Message message) throws Exception {
059                    JSONObject jsonObj = (JSONObject)message.getPayload();
060    
061                    String command = jsonObj.getString("command");
062    
063                    if (command.equals("signIn")) {
064                            doCommandSignIn(jsonObj);
065                    }
066                    else if (command.equals("signOut")) {
067                            doCommandSignOut(jsonObj);
068                    }
069            }
070    
071            private static Log _log = LogFactoryUtil.getLog(
072                    LiveUsersMessageListener.class);
073    
074    }