Wednesday, January 21, 2015

Access Liferay Portal Live Users in Plugin Portlet


Some time we may get requirement to access liferay portal live users/logged in users information in Plugin portlets.

Generally Liferay have implemented class called “LiveUsers.java” class in the portal level which is not available in Plugin environment.

LiveUsers.java is class which contains set of methods from this we can get logged in users/portal live users information.

Problem:

LiveUsers.java implemented in portal level so which is not directly available in Plugin portlet environment.

Solution:

We will use portal class loader to load LiveUsers class and we will use Java Reflection Mechanism to call methods from LiveUsers.java

What is portal Live Users?

Portal Live users are the logged in users they are currently live in the portal.

Access Portal Live Users in Plugin Portlet Environment

Add “live.users.enabled=true” property in portal-ext.properties file

Use Portal Class Loader and Java Reflection API to access methods in LiveUsers.java

Add “live.users.enabled=true” property in portal-ext.properties file

Generally live.users.enabled property set as “false” in portal.properties file . If we want to know portal live users information we need to make that value as “true” this property value overridden through the portal-ext.properties file which in Liferay Home directory.

Add following property in “portal-ext.properties”

live.users.enabled=true

Use Portal Class Loader and Java Reflection API to access methods in “LiveUsers.java”

As we know that “com.liferay.portal.liveusers.LiveUsers.java” class implemented in portal level so it count not be accessible in Plugin portlet environment.

So we will use portal class loaded to load class and crate instance from that we will use Java Reflection API to invoke methods which are implemented in “LiveUsers.java”

We have different methods in LiveUsers.java we will use “getSessionUsers” method to get Portal Live Users information and it will return map object which contains all portal live users information.

The following is sample Code to get Portal Live Users

ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
Class<?> liveUsers = PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.liveusers.LiveUsers");
System.out.println(liveUsers);
Method getSessionUsers = liveUsers.getDeclaredMethod("getSessionUsers",long.class);
Object map = getSessionUsers.invoke(null,themeDisplay.getCompanyId());
Map<String, UserTracker> sessionUsers = null;
sessionUsers=(ConcurrentHashMap<String, UserTracker>)map;
System.out.println(sessionUsers);

Output Some thing like below and its map object

{92C0414115E50E0D5A3F9EDA23CBC6FE={userTrackerId=0, companyId=10157, userId=10201, modifiedDate=Wed Jan 21 17:09:42 GMT 2015, sessionId=92C0414115E50E0D5A3F9EDA23CBC6FE, remoteAddr=127.0.0.1, remoteHost=127.0.0.1, userAgent=Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:32.0) Gecko/20100101 Firefox/32.0}, 5508BE3C06C9AECABF99088009E866F5={userTrackerId=0, companyId=10157, userId=10201, modifiedDate=Wed Jan 21 17:10:45 GMT 2015, sessionId=5508BE3C06C9AECABF99088009E866F5, remoteAddr=127.0.0.1, remoteHost=127.0.0.1, userAgent=Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:32.0) Gecko/20100101 Firefox/32.0}}

Scenario:

Assume we want display all portal live users information in Plugin portlet. When we click on some button it will display all portal live users in the portlet view page.

The following is full code of implementation

Portlet Action class

package com.meera.portallive.users;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.UserTracker;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class PortalLiveUsersPortletAction extends MVCPortlet {
@SuppressWarnings("unchecked")
public void getLiveUsers(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
try{
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
Class<?> liveUsers = PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.liveusers.LiveUsers");
System.out.println(liveUsers);
Method getSessionUsers = liveUsers.getDeclaredMethod("getSessionUsers",long.class);
Object map = getSessionUsers.invoke(null,themeDisplay.getCompanyId());
Map<String, UserTracker> sessionUsers = null;
sessionUsers=(ConcurrentHashMap<String, UserTracker>)map;
System.out.println(sessionUsers);
actionRequest.setAttribute("portalLiveUsers", sessionUsers);
}catch(Exception e){
e.printStackTrace();
}
} 
}

Portlet view.jsp page

<%@page import="java.util.concurrent.ConcurrentHashMap"%>
<%@page import="com.liferay.portal.model.UserTracker"%>
<%@page import="java.util.Map"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>
<%@taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@
taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<portlet:actionURL var="getLiveUsers" windowState="normal" name="getLiveUsers">
</portlet:actionURL>
<h2>Portal Live Users/Portal Logged in Users</h2>
<form action="<%=getLiveUsers%>" name="getLiveUsersForm" method="POST">
<input type="submit" name="GetPortalLiveUsers" id="GetPortalLiveUsers" value="Get Portal Live Users"/>
</form>

<%
if(renderRequest.getAttribute("portalLiveUsers")!=null){
Map<String, UserTracker> sessionUsers = null;
sessionUsers=(ConcurrentHashMap<String, UserTracker>)renderRequest.getAttribute("portalLiveUsers");
%>
<table border="1">
<tr>
<th>Company Id</th>
<th>Email Address</th>
<th>FullName</th>
<th>User Agent</th>
<th>Remote Host</th>
<th>Remote Address</th>
<th>Session Id</th>
</tr>
<%

for (Map.Entry<String, UserTracker> entry : sessionUsers.entrySet())
{
UserTracker liveUserTracker=entry.getValue();%>
<tr>
<td><%=liveUserTracker.getCompanyId()%></td>
<td><%=liveUserTracker.getEmailAddress()%></td>
<td><%=liveUserTracker.getFullName()%></td>
<td><%=liveUserTracker.getUserAgent()%></td>
<td><%=liveUserTracker.getRemoteHost()%></td>
<td><%=liveUserTracker.getRemoteAddr()%></td>
<td><%=liveUserTracker.getSessionId()%></td>
</tr>

<%}%>
</table>
<%}%>

Portlet view in the portal page


Download Porta Live users Portlet


Important Point

Please have look into LiveUsers.java class you can find more useful methods


Please look into Java Reflection API for better understanding of invoke the methods


0 comments :

Post a Comment

Recent Posts

Recent Posts Widget

Popular Posts