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.javais class which contains set of methods from this we can get logged in users/portal live users information.

Problem:

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

Solution:

We will use portal class loader to load LiveUsersclass 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

packagecom.meera.portallive.users;
importjava.io.IOException;
importjava.lang.reflect.Method;
importjava.util.Map;
importjava.util.concurrent.ConcurrentHashMap;
importjavax.portlet.ActionRequest;
importjavax.portlet.ActionResponse;
importjavax.portlet.PortletException;
importcom.liferay.portal.kernel.util.PortalClassLoaderUtil;
importcom.liferay.portal.kernel.util.WebKeys;
importcom.liferay.portal.model.UserTracker;
importcom.liferay.portal.theme.ThemeDisplay;
importcom.liferay.util.bridges.mvc.MVCPortlet;
publicclassPortalLiveUsersPortletAction extendsMVCPortlet {
@SuppressWarnings("unchecked")
publicvoidgetLiveUsers(ActionRequest actionRequest,
ActionResponse actionResponse) throwsIOException, 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

<%@pageimport="java.util.concurrent.ConcurrentHashMap"%>
<%@pageimport="com.liferay.portal.model.UserTracker"%>
<%@pageimport="java.util.Map"%>
<%@ tagliburi="http://java.sun.com/portlet_2_0"prefix="portlet"%>
<%@taglib uri="http://liferay.com/tld/aui"prefix="aui"%>
<%@tagliburi="http://liferay.com/tld/portlet"prefix="liferay-portlet"%>
<%@tagliburi="http://liferay.com/tld/security"prefix="liferay-security"%>
<%@tagliburi="http://liferay.com/tld/theme"prefix="liferay-theme"%>
<%@tagliburi="http://liferay.com/tld/ui"prefix="liferay-ui"%>
<%@
tagliburi="http://liferay.com/tld/util"prefix="liferay-util"%>
<portlet:defineObjects/>
<liferay-theme:defineObjects/>
<portlet:actionURLvar="getLiveUsers"windowState="normal"name="getLiveUsers">
</portlet:actionURL>
<h2>Portal Live Users/Portal Logged in Users</h2>
<formaction="<%=getLiveUsers%>"name="getLiveUsersForm" method="POST">
<inputtype="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");
%>
<tableborder="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


Share on Google Plus

About Meera Prince

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment