Saturday, January 31, 2015

Liferay Pugins SDK Ivy Download Problem and Solution

Some time we may face problem of creating Liferay plugin application with liferay-plugins-sdk-6.2-ce-ga3 with Liferay IDE 2.x due to incomplete download of ivy required jar files and configuration files through eclipse.

When we trying to create Liferay plugin project for first time, the project may not be created successfully due to problem with downloading of ivy related required jar files from eclipse.

In Liferay plugins sdk 6.2 GA3, we can find .ivy directory (liferay-plugins-sdk-6.2\.ivy). In side of .ivy directory we can see cache directory and ivy-2.3.0.jar file.

Due to some proxy issues or fire wall problems or internet connectivity, the ivy related all jar files and other configuration files will not be downloaded in the cache (liferay-plugins-sdk-6.2\.ivy\cache) directory for first time when we create Liferay plugin project.

For the first time if everything is fine then it will take several minutes to download all required jar files and configuration files in plugins-sdk cache location (liferay-plugins-sdk-6.2\.ivy\cache).

In some cases the creation of new Liferay plugin project may not be completed and keep on loading like shown in below screenshot, then there might be a problem with downloading of ivy related jar file. In that case we need to download all .ivy related jar files manually.



What are all jar file should be downloaded and its repository locations was specified in the ivy-settings.xml file which is in liferay-plugins-sdk-6.2.

The following is reference of settings.xml



Solution for the above problem:

To fix the above explained problem, we should manually download ivy related jar files and configuration files and place them in appropriate location of liferay plugins sdk 6.2 GA3.

Procedure:

Download ivy zip files from following location


Copy ivy-cache.zip to liferay-plugins-sdk-6.2 (this is your Liferay plugins sdk)

Now extract the downloaded ivy-cache.zip in the same location. i.e., Liferay-plugins-sdk-6.2 (use extract here from WinRAR or zip software)

Now all required jar files and configuration files will be placed in .ivy directory.

The following is the screenshot of .ivy directory (\liferay-plugins-sdk-6.2\.ivy)


The following is the cache directory screenshot, which shows all required jar files



You can follow above procedure or you can download fully working Liferay-plugins-sdk-6.2-ce-ga3 from following location and  in this plugins SDK i already downloaded ivy related jar files and included in the Liferay-plugins-sdk-6.2-ce-ga3.


Author

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


Recent Posts

Recent Posts Widget

Popular Posts