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 unresting of invoke the methods


Thursday, December 18, 2014

Liferay JSP Tag Libraries Introduction

Liferay have given many tag libraries to make development easy and rapid. Liferay have developed their own tag libraries which we can use extensively in liferay development.

What is Tag Library?

Tag library is set of JSP tags and each tag have its own syntax and required attributes.

What is JSP Tag?

JSP tag is way to avoid the writing repeated code snippets in the JSP pages. Each tag have its own piece of presentation markup(html/java script/css/java code) code which can be encapsulates as a separate part and it will be identify by the specific notation called Tag.

When we use the tag in the JSP page it will be replaced by respective code snippet/presentation markup at the time of page rendering.

When we work with JSP pages we aware of JavaServer Pages Standard Tag Library (JSTL)

Whats is JSTL?

The Java Server Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications.

We have many tag libraries in JSTL and the following are JSTL tag Libraries
  • Core Tags
  • Formatting tags
  • SQL tags
  • XML tags
  • JSTL Functions
JSP Tag Libraries Artifacts

When we work with tag libraries we have three artifacts we need to consider
  1. Tag Library Description(TLD)
  2. Tag Library URI
  3. Tag Library Implementation JAR file
Tag Library Description(TLD)

Each tag library have its own Tag Library Description(TLD) file and its a xml file consist of tag information like tag name, tag attributes, what attributes are required and what are optional.

Tag Library URI

Tag Library URI is unique URL to identify the tag library,this will be used in JSP page to import tag libraries and we will use Tag Library URI in application deployment descriptor(web.xml) to integrate tag library with application. Tag Library URI is represented in TLD file.

Example:

<uri>http://alloy.liferay.com/tld/aui</uri>

Tag Library Implementation JAR file

Tag Library Implementation JAR file is a jsp tags implementation java classes make it as JAR file and we need to add this jar file in application class path that is application lib directory or other server lib directory.

Usage of JSP Tag Libraries

We already know JSP tag libraries are used only in JSP pages. If we want use JSP tag Libraries in Web application JSP pages then we have to follow set of following steps.
  1. Register Tag Library in Application Deployment Descriptor(web.xml)
  2. Add Tag Library TLD file in WEB-INF directory
  3. Add Tag Library Implementation JAR file in Application Class Path
  4. Refer Tag Library URI in JSP page
Register Tag Library in Application Deployment Descriptor(web.xml)

First we need to register tag libraries in application web.xml file so that application can use tag in the JSP pages. When we register Tag libraries in web.xml we need to know tag library URI and Location of TLD file.

The following is registering tab libraries in web.xml file

<jsp-config>
<taglib>
<taglib-uri>http://liferay.com/tld/aui</taglib-uri>
<taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>
</taglib>
</jsp-config>

Note:

Configuration should be enclosed by <jsp-config/> tag. In the above configuration we have registered two tag libraries.

Add Tag Library TLD file in WEB-INF directory

We need to add Tag Library TLD file in the application WEB-INF directory. We can directly place in WEB-INF or we can create separate directory and we can place there. Make sure the path of TLD file that we have given in web.xml file to register tag library.

Example:

<taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>

Add Tag Library Implementation JAR file in Application Class Path

Every tag library have its implementation java JAR file and this should be available in application class path that is WEB-INF/lib directory.

Example Implementation JAR files:

For JSTL (jstl-api.jar,jstl-impl.jar)
For Liferay(util-taglib.jar)

Refer Tag Library URI in JSP page

Its final step where we need to refer tag library in JSP page so that we can use respective tags in the JSP pages.

The following is the way to refer Tag Library in JSP page

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

We need to give the tag library URI path that is available in TLD file and we will use prefix name to identify the tag library in the JSP page.

Example Usage of Tab Library Tags in JSP Page

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:actionURL var="sendUserNotification" windowState="normal" name="sendUserNotification">
</portlet:actionURL>

Note:

We will use prefix name to each tag notation and each tag may have attributes and we need to pass it in the tag.

Liferay JSP Tag Libraries

Apart from JSTL tag libraries Liferay also implemented their own tag libraries which we will use in liferay plugins development.

Liferay have many tag libraries for different contextual use and each tag libraries have collection of tags we can use in JSP pages to make it good presentation and rapid UI development.

The following are the Liferay Tag Libraries
  1. Portlet Tag Library
  2. Liferay Portlet
  3. Liferay Security
  4. Liferay AUI
  5. Liferay UI
  6. Liferay Util
Note:

Each tag library have many tags and its based on usage its made it as different libraries.

Usage Of Liferay Tag Libraries in Portlet Development

When we work with Plugin portlet development we extensively use the liferay tag libraries and its tags in the JSP pages.

Here interesting thing is Liferay Tag Libraries registering with portlet will be take care by the liferay auto deployment process so we need not to do manually registering the tag libraries.

Only we need to refer tag library URI information in the JSP page so that we can use tags in the JSP page.

The following is referring all Liferay Tag Libraries in the JSP page

<%@ 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" %>

Example Usage of Liferay Tag from Portlet Tag Library in the JSP page

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>



<portlet:actionURL var="sendUserNotification" windowState="normal" name="sendUserNotification">

</portlet:actionURL>

The following is reference links to know each and every liferay JSP tag information

Note:

Each Library have many tags and that will be used in our portlet development.

How does Liferay Auto Deployment Process Registering the Liferay Tag Libraris in the Portlet plugin

When we create portlet from Liferay IDE there is no information in portlet web.xml file related to liferay tag libraries and no JAR file in portetl Lib directory and No TLD file in Portlet WEB-INF directory.

Once we deploy the portlet then portlet will be deployed in the application server deployment directory

If we use Tomcat then we can see portlet deployed in webapps directory(tomcat-7.0.42/webapps)

Observations:

Find your Poertlet in webapps directory and open web.xml file then you can see following configuration. Which shows that its registered all Liferay Tag Libraries

Example Path:

/tomcat-7.0.42/webapps/DockBarCustomNotification-portlet/WEB-INF/web.xml

<jsp-config>
<taglib>
<taglib-uri>http://liferay.com/tld/aui</taglib-uri>
<taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/portlet</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-portlet-ext.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/security</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-security.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/theme</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-theme.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/ui</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-ui.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://liferay.com/tld/util</taglib-uri>
<taglib-location>/WEB-INF/tld/liferay-util.tld</taglib-location>
</taglib>
</jsp-config>

Now Open Portlet WEB-INF directory then you can see tld directory once you look into tld directory then you can see all liferay tag libraries tld files.

Example Path:

/tomcat-7.0.42/webapps/DockBarCustomNotification-portlet/WEB-INF/tld

Liferay Tag Library TLD files

aui.tld
liferay-portlet.tld
liferay-portlet-ext.tld
liferay-security.tld
liferay-theme.tld
liferay-ui.tld
liferay-util.tld

As we know each tag library have its own implementation jar file. Liferay also implemented java classes for all libraries and made it as one jar file called util-taglib.jar and it will be available in portlet lib directory(/tomcat-7.0.42/webapps/DockBarCustomNotification-portlet/WEB-INF/lib)

From above all observations we can understand that in the liferay portlet development we no need to do any manual configuration to register the liferay tag libraries and Liferay auto deployment process will take all necessary configuration to make use of liferay tag libraries in portlet development that is in jsp pages.

Note:

Liferay Auto deployment process only resisted the Liferay tag libraries in the portlet not JSTL tag libraries.

Using JSTL Tag Libraries in Liferay Portlet Plugin

As we know liferay auto deployment is only configure the liferay tag libraries but we also have many useful tag libraries that is JSTL tag libraries. When we want use JSTL tag libraries such as Core Tags ,Formatting tags ,SQL tags ,XML tags ,JSTL Functions then we are the responsible to register the JSTL tag libraries in the portlet development.

Assume we want to use JSTL core tag library

Use JSTL tag Libraries in the Portlet Development Using Liferay IDE

Liferay IDE provide very easy way to configure the JSTL tag libraries in Plugin Portlet development.

Go to your Plugin portlet in the eclipse project explorer

Double Click on “liferay-plugin-package.properties file its in portlet WEB-INF directory.

One we open this file it will be open in “properties” view mode as follows


You can see two inputs fields “Portal Dependency Jars” and “Portal Dependency Tlds” and we can also have Add buttons.

As we know when we work with tag libraries we need tag library implementation JAR file and Tag library TLD file.

jstl-api.jar, jstl-impl.jar” are the JAR files that was implemented the JSTL tag library and its have different TLD files like c.tld for JSTL core tags.

Now “Portal Dependency Jar” section click on Add button then it will open dialog there you can find available JAR files list and search for “jstl-api.jar,jstl-impl.jar” files and select both files and then click on OK .

The following screen shows you adding JSTL jar files


Now “Portal Dependency Tlds” section click on Add button then it will open dialog there you can have available JAR files list and search for “c.tld” file and select it then click on OK .

The following screen shows you adding JSTL TLD files


We have added JAR files and TLD file finally you can save the file(Ctrl+S).

The following screen shows added dependency jar files and dependency TLD files in the “liferay-plugin-package.properties

The following is source view of “liferay-plugin-package.properties

Liferay Plugin Portlet development when we need portal dependency jar files or other files like tld we will use “liferay-plugin-package.propertiesfile and the following is configuration.

portal-dependency-jars=\
jstl-api.jar,\
jstl-impl.jar
portal-dependency-tlds=c.tld

When we configure portal dependency jar files or tld files in “liferay-plugin-package.propertiesfile then liferay auto deployment process will copy specified JAR files and TLD files from Liferay Portal and Place into the Plugin Portlet appropriate locations like jar files will be placed in the portlet “WEB-INF/lib” directory and TLD files will be placed in the portlet “WEB-INF/tld” directory.

We have configured the JSTL tag libraries in our portlet, now we can use JSTL core tags in JSP pages .

The following is example code in JSP page to use JSTL Core tags

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:if test="<%= MicroblogsPermission.contains(permissionChecker, scopeGroupId, ActionKeys.ADD_ENTRY) && !userPublicPage %>">
<a>Add<p>
</c:if>

How Liferay Auto Deployment Process Configure JSTL Tag Libraries.

Once we add dependency jar files and dependency tld files in “liferay-plugin-package.propertiesfile then following things takes places after successful deployment of portler and these we can observed in deployed portlet in server deployment directory .
Go to tomcat webapps directory and find you pottlet then you can see following things

Observations:

Find your Poertlet in webapps directory and open web.xml file then you can see following configuration. Which shows that its registered all Liferay Tag Libraries

Example Path:

/tomcat-7.0.42/webapps/PortletConfigaration-portlet/WEB-INF/web.xml

<jsp-config>

<taglib>

<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>

<taglib-location>/WEB-INF/tld/c.tld</taglib-location>

</taglib>

</jsp-config>

Now Open Portlet WEB-INF directory then you can see tld directory once you look into tld directory then you can see c.tld file

Example Path:

/tomcat-7.0.42/webapps/PortletConfigaration-portlet/WEB-INF/tld

As we know each tag library have its implementation jar file. JSTL implementation jar files called “jstl-api.jar, jstl-impl.jar” and it will be available in portlet lib directory(/tomcat-7.0.42/webapps/PortletConfigaration-portlet/WEB-INF/lib)

From above all observations we can understand that liferay portlet development when want add JSTL tag libraries we just need to specify the dependency jar files and dependency tld files in the “liferay-plugin-package.propertiesfile then Liferay auto deployment process will take all necessary configuration to make use of JSTL tag libraries in portlet development that is in jsp pages.

Manual Configuration to Use JSTL Tag Library in Plugin Portlet.

  1. Configure JSTL tag information in web.xml
  2. Add JSTL Implementation JAR file in Portlet lib Directory
  3. Add JST td file in portlet WEB-INF directory
  4. Refer Tag Library in JSP page 
 
Author