Tuesday, May 24, 2016

Gadget/Widget in Liferay

    We hear about widget/gadget in daily life like we can add gadgets on Windows-7's desktop. Widget is an application or icon which performs a specific functions on desktop or website. For example, if you want to add counter or social site icons, you can add a script in your page and that widget available on your page. So a widget is a piece of reusable code that you can plug into virtually any website. A gadget is same like widget but it only works on a certain website or a specific set of websites. Gadgets are simple and easy to deploy.
OpenSocial Gadget
    OpenSocial is a public specification for creating web applications using standard technologies like HTML, CSS, and JavaScript. OpenSocial applications called gadgets which are similar to portlets because gadgets can be added to portal’s pages like portlets.

Figure 1 - OpenSocial Gadget
Share your portlet as widget
    Liferay has feature which convert your portlet as standalone widget. You can share your portlet as a widget to any web site or Facebook or OpenSocial gadget.
To share your portlet, go to portlet configuration and click on Sharing tab there. Sharing tab has following options:

Figure 2 - Portlet Share Configuration
  1. Any Website: It has JavaScript code. You can copy and paste on your website where you want to show portlet. You can also allow your users to add this widget on their websites.
  2. Facebook: It provide ability to add your portlet as an application on Facebook page.
  3. OpenSocial Gadget: Liferay portlet can be used as Open Social Gadgets on any OpenSocial-compatible pages like iGoogle. You can also allow your users to use this OpenSocial Gadget.
  4. Netvibes: You can also share your portlet on Netvibes portal. You can also allow your users to use this Netvibes widget.
  5. Friends: It allows you to give your friends permission to add the application as a widget to another web site.

Figure 3 - User's Share Options
Using OpenSocial Gadget in Liferay
    You can add any Gadget on Liferay page. Liferay Marketplace has OpenSocial Gadget plugin. This plugin can be used in two ways to add gadgets in Liferay pages.
Adhoc Gadget: It is quick way to add Gadget on page. You need to add OpenSocial Widget portlet from Gadget category. Now open portlet configuration and set gadget URL which you want to add on page.
Figure 4 - OpenSocial Gadget Plugin
Portal-wide Gadgets: In this way, you can configure gadget at one place and use that as a portlet on liferay pages. To add gadget, you need to go Control Panel -> Apps -> OpenSocial Admin. There you need to provide Gadget URL and category in which your Gadget will available as portlet. Now a portal admin can add that Gadget on page like we add portlets on page.
Figure 5 - OpenSocial Admin
Liferay also provide possibility to configure oAuth credentials. When we add Twitter Widget, it also ask Consumer Key and Consumer Secret which you can get from twitter.com
OpenSocial Gadget Editor 
OpenSocial Gadget plugin also provide a gadget editor where you can create your own gadget and publish it. It generate a unique URL which can be used on any OpenSocial-compatible pages to show that gadget. To create new gadget, you need to go Site Administrator  -> Content -> OpenSocial Gadget Editor. It has editor to write OpenSocial Gadget XML. When you publish, it generate public gadget URL.
Figure 6 - OpenSocial Gadget Editor

Friday, February 26, 2016

Liferay Caching - Service Level Caching using WebCachePool

                Caching is important part of any web application. We implement caching for our application on different levels. Generally we cache static resource on web server level and DB level caching on persistence level. Most of web-framework/portal provide some level of caching. Liferay strongly integrated caching in its core part and it also provide mechanism that user can optimize caching according to their requirement.
    Liferay provide many level caching like CacheFilter, MultiVMPool/SingleVMPool, Hibernate caching.
  • Frontend Caching: Liferay cache HTML page in CacheFilter. It check if request is cachable or not. If yes, it cache the HTML data. Liferay can also cache portlet fragment.
  • Service Level Caching: Liferay provide ability to cache data on service level. SingleVMPool cache use for cache data on a single server node. MultiVMPool is used for cache data across multiple nodes.
  • Persistence Caching: Liferay cache data in persistence level like it Hibernate caching, SB finder cache etc.
    You can find more detail on Jorge Ferrer's blog.
                In Service level caching, Liferay provides WebCachePool which internally uses SingleVMPool. It hides the implementation details of getting the data and caching it at same time. WebCachePoolImpl provides get(String key, WebCacheItem wci) method and it check if provided key has already cached data or not. So developer only need to focus on their logic; getting data from cache, adding caching and refresh cache data handle by WebCachePool.
                To use get(String key, WebCacheItem wci) method, developer need to pass two parameter. First param is key. It is unique key for cache. For e.g. Liferay cache RSS feeds in RSS-Portlet. In RSS Portlet, feed URL is key. Second Parameter is object of implementation class of WebCacheItem interface. It fetch actual data from DB or webservice when it required. Like in RSS portlet, convert(key) method of RSSWebCacheItem call when portlet need first time feeds for a key(URL). It is also called when cache is expired.

                WebCachePool is easy to use. Only one class need to write. Developer don't need to know how to cache data against unique key. WebCachePool manage it. We can define refresh time of each WebCacheItem. So if there are two WebCacheItem in your portlet, they may have different refresh time. Liferay use WebCachePool in many portlet like Translator Portlet, Amazon Ranking Portlet, RSS portlet, Currency Converter Portlet etc. In Translator Portlet, Liferay use Microsoft Translator services and it cache translated output against the input key for 90 days (translation doesn't change usually).

Using WebCachePool in Custom Portlet:
                First you need to create a class with implement WebCacheItem interface. You need to override convert(String key) and getRefreshTime() method.

Now use this in you controller or service like this:

If you like to remove cached data of any key, you can do like:
If you want to clear all cache in WebCachePool, you can do like :