Friday, June 15, 2012

Inter Portlet Coordination with JSR 286

JSR 168 (Potlet 1.0) specification doesn't clearly suggest any mechanism for the inter-portlet communication i.e. communication between two portlets. This was regarded as one of the major short-comings for JSR 168. Though most of the vendors had their own extensions to JSR 168 for managing inter-portlet communication, use of any of those methods defeat the very purpose of JSR 168. In absence of any well defined sophisticated mechanism, JSR 168 developer had to rely upon either PortalContext or Application Scope of Session for sharing information between the protlets.

JSR 286 has come up with a well defined model to achieve inter-portlet communication. There are two primary ways by which inter-portlet communication can achieved as follows -

  1. public render parameters in order to share render state between portlets.
  2. portlet events that a portlet can receive and send.


There are pros and cons of each method. Though the biggest advantage with Portlet Events method is that an object can be passed from one portlet to another as opposed to merely "String" in case of "Public Render Parameters" method.

Below are the steps to get it going:

Step 1:

Open portlet.xml and add the following entries

<portlet-app ...>
<portlet> ... </portlet>
<public-render-parameter>
<identifier>user-id</identifier> <qname xmlns:x="http://abc.com/userId">x:userId</qname>

</public-render-parameter>

</portlet-app>

Note:
  1. A developer can declare a list of public paramters for a portlet application in portlet.xml.
  2. Parameter names are namespaced to avoid naming conflict.

Step 2:

Portlet must declare which public param they want to use. e.g.

After adding declaration portlet.xml will look something like this.

<portlet-app ......>
<portlet>
<portlet-name>test1</portlet-name> <display-name>test1</display-name>
........ ........

<supported-public-render-parameter> user-id</supported-public-render-parameter>
</portlet>
<public-render-parameter>
<identifier>user-id</identifier> <qname xmlns:x="http://abc.com/userId">x:userId</qname>

</public-render-parameter>

</portlet-app>

Note: 
  1. Public params are available in all lifecycle method like processAction , processEvent, render and serveResource.

Step 3: 

We can set render parameter in the processAction() method by using the defined public render parameter identifier as the key. e.g.

public void processAction(ActionRequest request, ActionResponse response)

throws IOException, PortletException {
 ........ 
response.setRenderParameter("user-id", userId);
 }

Step 4: 

A portlet can read public render parameter using follwing method

request.getPublicParameterMap()

Note: 
  1. Public render parameters are merged with regular parameters so can also be read using

request.getParameter(name) or request.getParameterMap()

Step 5: 

A portlet can remove a public render parameter by invoking following methods.

response.removePublicRenderParameter(name)

or

portletURL.removePublicRenderParameter(name) 

References

http://blog.xebia.com/2009/04/19/inter-portlet-coordination-with-jsr-286/

No comments:

Post a Comment