Friday, June 22, 2012

Liferay: Invoke a servlet from portlet

Goal: 

To call a servlet from portlet deployed in liferay 6.

Solution:

 Step 1: Create a servlet

 public class myServlet extends HttpServlet {
  public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
     System.out.println ("inside servlet");
     response.sendRedirect(portal_url); //url of portal page
  }
}

myServlet.java is a simple servlet class in which service() method serves the purpose of redirecting the control back to portal page. portal_url is the actual absolute URL where the servlet should redirect after processing.

Step 2: Configuring web descriptor

Go to portlet web.xml and make an entry like below:
<servlet>
        <servlet-name>myAction</servlet-name>
        <servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>
        <init-param>
            <param-name>servlet-class</param-name>
            <param-value>com.rdg.portlet
.myServlet</param-value>
        </init-param>
        <init-param>
            <param-name>sub-context</param-name>
            <param-value>
myAction</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


In the above web descriptor com.liferay.portal.kernel.servlet.PortalDelegateServlet serves to redirect any request coming to "http://localhost:8080/myAction" to service() method of com.rdg.portlet.myServlet and will redirect back to the liferay portal url.

Step 3: Creating portlet action class to redirect to the servlet

public class myPortletAction extends MVCPortlet {
  public void redirectMe(ActionRequest actionRequest,
            ActionResponse actionResponse) throws IOException, PortletException {


    actionResponse.sendRedirect("http://localhost:8080/myAction");
 }
}

So when a form is submitted to the action and control is sent to redirectMe() method, it redirects the request to "http://localhost:8080/myAction" for further processing.



Monday, June 18, 2012

AUI: Refresh on popup close button

To referesh the parent window while closing a AUI pop in liferay, use the following code:
window.myDialog = new A.Dialog(
            {
     on: {
      close: function() {
      document.location.href='<%=oncloseURL%>';
     }

    },
                title: title,
                centered: true,
    resizable: false,
    draggable: false,
    modal: true,
    width: 800,
    height:550,
   
       
            }
        )

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/

Thursday, June 07, 2012

Liferay Tomcat Performance

Motive

To get performance improvement in terms of time taken by liferay tomcat to start.

Solution

Tomcat's core libraries are available in Java Byte code normally. Apache Tomcat Native Library is a Java Native Interface that provide most of the core functionality in native code. Using these libraries only mean one thing to you and i.e. SPEED.
 For Windows
  1. Download http://archive.apache.org/dist/tomcat/tomcat-connectors/native/1.1.15/binaries/win64/x64/
  2. Copy tcnative-1-ip4.dll to %TOMCAT_HOME%/bin
  3. Restart Tomcat and check the tomcat startup performance.