Posts Tagged ‘Liferay’
Liferay Cache
Leveraging Liferay Cache to Improve performance :
Liferay provides out of box Cache MultiVMPool, which can be used by portlets to cache results of heavy network calls i.e. web services.
MultiVMPool Cache is very easy to use and yet powerful enough to work in clustered environment. Unfortunately there is no official documentation for MultiVMPool, which describes how it works and how it can be leverage by portlets.
What is MultiVMPool Cache ?
-
- Out of Box Cache from Liferay : No additional configuration infrastructure needed.
- Wrapper around EHCache : As powerful as EHCache
- Clustered Cache
- JVM Level Cache: Object cached by one WAR will be available to other WARs on same JVM.
How to Use :-
-
- MutliVMPoolUtil : Utility class provides methods to access MultiVMPool
http://docs.liferay.com/portal/5.1/javadocs/portal-kernel/com/liferay/portal/kernel/cache/MultiVMPoolUtil.html - Common Usage :
Using MultiVMPoolUtil : new cache will be dynamically created if not exits.
Following methods shows basic usage :
name= Cache Name
key=Key of Cache Entry
obj=Object to be Cached.
Put Obeject in Cache
put(java.lang.String name, java.lang.String key, java.io.Serializable obj)
Get Object from Cache
get(java.lang.String name, java.lang.String key)
Clear Cache
clear(java.lang.String name)Remove Object from Cache
remove(java.lang.String name, java.lang.String key) Please note here that Liferay MutliVMPoolUtil provides overloaded method to put Normal object (i.e non serialized) also. But in Clustered environment make sure to use Serialized object which can be copied to other caches over wire.
Clustered Cache needs to enable from portal.properties
ehcache.multi.vm.config.location=/ehcache/liferay-multi-vm-clustered.xml
- MutliVMPoolUtil : Utility class provides methods to access MultiVMPool
Pros:
-
- Easy to Use
- No configuration required from portlet
- Clustered Cache
Cons:
-
- Use Default settings for Cache created. No API to set different timeToLive,timeToIdle
settings for the cache. - To change default Cache settings only done using EXT environment.
- Use Default settings for Cache created. No API to set different timeToLive,timeToIdle
Workaround : Making setPortletMode working in Liferay
In Liferay set portlet mode from processAction() is not setting Portlet Mode properly.
There is a bug filed on Liferay JIRA ( http://issues.liferay.com/browse/LPS-114 ).
Its use case is as below described in Liferay JIRA:
1. process a portlet action and set the portlets window state and portlet mode in the portlets processAction method
2. When the portlet is rendered for the first time directly after the state/mode change, then the portlet seems to be in the correct state and portlet mode. (getPortletMode / getWindowState are equal to the new settings )
3. Afterwards – when the page is rendered again – then the portlet falls back to its previous window state and portlet mode (f.e. when you make a round trip to another tab)
A patch is provided for this bug ,but still not fixed in Liferay 5.2.3. Also the patch will be useful for team working with EXT environment where they can directly apply patch to change Liferay source code.
I found one workaround which will work without changing source code . I have tested it on Tomcat 5.5, Tomcat 6.1, Websphere 6.1 with Liferay 5.2.3 and should work for earlier versions also.
Solution Summary :
It seems Liferay is tracking mode using http parameters. We need to append those parameters in page. Need to add few Query String parameters to Portal Page which contains the portlet.
p_p_id=<PortletId>&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view
<PortletId> : It is the portlet Id for target Portlet.
Step by Step Solution:
- Go to Portlet Preferences.
- You can see default link “ Return to Full Page “ . Right click on it. It will popup browser menu copy the link address.
- Copy it in to text editor, for my portlet it is :
http://localhost:8080/web/test/mychamp?p_p_id=MyChamp_WAR_MyChamp&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1 - We need only following part for my portlet: p_p_id=MyChamp_WAR_MyChamp&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view
copy it,according to your portlet/war name it will be different. - Go To Manage pages menu.
- Select the page which contains the target portlet. In the page’s Query String paste the filtered query String.
- Save changes.
- Come back to portlet and retest after saving preferences , even if you come from different page it will work as per expected.
Liferay Session Sharing : Made Easy
- What I mean by ACTION-to-VIEW is on “action phase” of one portlet information is shared and made available to specific/all portlet’s “view phase”.
- There are use cases where we need a IPC mechanism to share information between portlets of different WARs in VIEW-to-VIEW .It means One portlet will share information from its VIEW phase and will be available to other Portlet in VIEW phase.
1.liferay-portlet.xml
<portlet>... <private-session-attributes>false</private-session-attributes> </portlet>
2. Use Namespace prefix to Share/Get shared session attribute:
portletSession.setAttribute( "LIFERAY_SHARED_mySpecialVar",value,PortletSession.APPLICATION_SCOPE);
portletSession.getAttribute( "LIFERAY_SHARED_mySpecialVar",PortletSession.APPLICATION_SCOPE);
How to Share with Servlet of Other WAR :-
portletSession.setAttribute( "LIFERAY_SHARED_mySpecialVar",value, PortletSession.APPLICATION_SCOPE);
Object value= portletSession.getAttribute ( "LIFERAY_SHARED_mySpecialVar",PortletSession.APPLICATION_SCOPE);//getting shared value portletSession.setAttribute ( "mySpecialVar",value,PortletSession.APPLICATION_SCOPE);//setting to 'local' session
request.getSession().getAttribute("mySpecialVar");
/***** <pre>* RUTVIJ SHAH MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. RUTVIJ SHAH SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * THIS SOFTWARE IS IN AS-IS FORM, YOU ARE FREE TO RE-DISTRIBUTE/CHANGE WITHOUT ANY NOTIFICATION TO AUTHOR. ******/ package myapp.liferayImpl.session.util; import com.liferay.portal.kernel.util.PropsUtil; import java.util.logging.Level; import java.util.logging.Logger; import javax.portlet.PortletRequest; import javax.portlet.PortletSession; /** * @author Rutvij Shah ( rutvij.shah@yahoo.com ) * LiferaySessionUtil provides helper methods to share your Session attributes * across WARs. * */ public class LiferaySessionUtil { /*** * This prefix is used by Liferay Portal to detect Session Attributes * for sharing betweeen WARs. */ private static final String LIFERAY_SHARED_SESSION_PREFIX=getSharedSessionPrefix(); private static final String LIFERAY_SHARED_SESSION_PREFIX_DEFAULT="LIFERAY_SHARED_"; /**** * It stores attribute as 'Shared' Attribute and will be available to other * portlets in different WARs. * * Attributes shared by this method will be available to only Portlets, * not other Web components i.e Servlet. * * @param key session Key to store value * @param value * @param request PortletRequest */ public static final void setGlobalSessionAttribute(String key,Object value,PortletRequest request){ if(key!=null){ String globalKey=getGlobalKey(key); PortletSession portletSession=request.getPortletSession(); portletSession.setAttribute(globalKey,value,PortletSession.APPLICATION_SCOPE); } } /**** * * It provides access to get shared session attributes from other portles * from diffrent WARs. * * @param key * @param request * @return */ public static final Object getGlobalSessionAttribute(String key,PortletRequest request){ Object value=null; if(key!=null){ String globalKey=getGlobalKey(key); PortletSession portletSession=request.getPortletSession(); value=portletSession.getAttribute(globalKey,PortletSession.APPLICATION_SCOPE); } return value; } /****** * * It provides a way to further share 'Shared'Session Attributes from * Portlet to other Web Components i.e Servlets . * * @param key * @param request */ public static final void shareGlobalSessionAttribute(String key,PortletRequest request){ if(key!=null){ Object value=getGlobalSessionAttribute(key, request); PortletSession portletSession=request.getPortletSession(); portletSession.setAttribute(key,value,PortletSession.APPLICATION_SCOPE); } } /*** * Helper method to generate Global key using Liferay shared prefix * @param key * @return */ private static final String getGlobalKey(String key){ return LIFERAY_SHARED_SESSION_PREFIX+key; } /******** * Helper method to get Liferay's Session Sharing prefix * Useful when Liferay is customized to use different prefix other than Default * * @return */ private static final String getSharedSessionPrefix(){ String value=null; try { /** * Getting value from portal.properties */ value = PropsUtil.get("session.shared.attributes"); } catch (Exception ex) { Logger.getLogger(LiferaySessionUtil.class.getName()).log(Level.SEVERE, null, ex); } if(value !=null){ if(value.contains(LIFERAY_SHARED_SESSION_PREFIX_DEFAULT)){ //if default prefix is configured use it value=LIFERAY_SHARED_SESSION_PREFIX_DEFAULT; }else{ //use first one from the list of prefix configured value=value.split(",")[0]; } }else{ /** * If none of the value configured use default one * Note: Session Sharing may not work as none of the value configured. */ value=LIFERAY_SHARED_SESSION_PREFIX_DEFAULT; } return value; } }
LiferaySessionUtil.setGlobalSessionAttribute ("mySpecialVar",value, portletRequest); //adding 'proper' prefix will be taken care by Utility
Object value= LiferaySessionUtil.getGlobalSessionAttribute ("mySpecialVar", portletRequest); //to get value for portlet LiferaySessionUtil.shareGlobalSessionAttribute ("mySpecialVar", portletRequest);//to share value to servlets
As you can see if Portlet Two only need to share it with servlet no need of first line.
request.getSession().getAttribute("mySpecialVar");
Feel free to share your comments on this. You can download LiferaySessionUtil.java from here : LiferaySessionUtil
User specific Preferences in Liferay
As per JSR 168 Specificaiton Portlet Preferences are user specific and may be shared across portlets/pages ( NOT Mandatory: config/edit_shared modes) which depends on portlet container.
If you have Websphere Portlet development experience you will wonder why preferences are not ‘user specific’ in liferay.
In this post I will explain how to solve this mystery and make your user specific preferences work in Liferay too.
Preferences in this example :
Portlet shows two preference values in VIEW mode JSP: A. Sets dynamically by API B. From portlet.xml.
Edit Mode shows and allow to update preferences.
processAction stores updated preferences.
Really simple use isn’t it ?
Expected Behavior:
When user Bob logs in to portal and set his preferences, he can see them in VIEW mode. So preferences are personalized only for Bob. When Alice/another user logs in to Portal she can set her preferences which is only for her, no other user can see.
In Websphere portal: As per Expected.
In Liferay portal: When Bob logs in and set his preferences and it will be set for Alice and David too .i.e when Alice logs in to portal she can see preferences values set by Bob. If she too updates, it overwrites value set by Bob.
I was wondering why its happening like this…because as per JSR if user sets Preferences in EDIT/VIEW/HELP mode it is user specific only.
Solution : Liferay supports great level of customization for preferences (I will explain it in details in my future posts).
But by default Liferay sets some customized levels for preferences ‘ON’.
So in order to make preferences user specific in liferay, add following entries to liferay-portlet.xml :
<portlet> .... <preferences-unique-per-layout>false</preferences-unique-per-layout> <preferences-owned-by-group>false</preferences-owned-by-group> .... </portlet>
Do these changes and redeploy portlet, now Bob’s preferences are for Bob only !!!