1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.liveusers.messaging;
16  
17  import com.liferay.portal.kernel.json.JSONFactoryUtil;
18  import com.liferay.portal.kernel.json.JSONObject;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.messaging.Message;
22  import com.liferay.portal.kernel.messaging.MessageListener;
23  import com.liferay.portal.liveusers.LiveUsers;
24  
25  /**
26   * <a href="LiveUsersMessageListener.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class LiveUsersMessageListener implements MessageListener {
31  
32      public void receive(Message message) {
33          try {
34              doReceive(message);
35          }
36          catch (Exception e) {
37              _log.error("Unable to process message " + message, e);
38          }
39      }
40  
41      protected void doCommandSignIn(JSONObject jsonObject) throws Exception {
42          long companyId = jsonObject.getLong("companyId");
43          long userId = jsonObject.getLong("userId");
44          String sessionId = jsonObject.getString("sessionId");
45          String remoteAddr = jsonObject.getString("remoteAddr");
46          String remoteHost = jsonObject.getString("remoteHost");
47          String userAgent = jsonObject.getString("userAgent");
48  
49          LiveUsers.signIn(
50              companyId, userId, sessionId, remoteAddr, remoteHost, userAgent);
51      }
52  
53      protected void doCommandSignOut(JSONObject jsonObject) throws Exception {
54          long companyId = jsonObject.getLong("companyId");
55          long userId = jsonObject.getLong("userId");
56          String sessionId = jsonObject.getString("sessionId");
57  
58          LiveUsers.signOut(companyId, userId, sessionId);
59      }
60  
61      protected void doReceive(Message message) throws Exception {
62          String payload = (String)message.getPayload();
63  
64          JSONObject jsonObject = JSONFactoryUtil.createJSONObject(payload);
65  
66          String command = jsonObject.getString("command");
67  
68          if (command.equals("signIn")) {
69              doCommandSignIn(jsonObject);
70          }
71          else if (command.equals("signOut")) {
72              doCommandSignOut(jsonObject);
73          }
74      }
75  
76      private static Log _log = LogFactoryUtil.getLog(
77          LiveUsersMessageListener.class);
78  
79  }