Monday, February 7, 2011

InterPortlet Communication Liferay 6.0

InterPortlet Communication:
  1. create ipc-baseball "IPC Baseball"
  2. in the portlet.xml file.
First, change the portlet name, display name, and portlet class:
<portlet-name>pitcher-portlet</portlet-name>
<display-name>Pitcher Portlet</display-name>
<portlet-class>com.plugin.portlet.PitcherPortlet</portlet-class>
  1. Next add the init parameter in portlet.xml
<init-param>
<name>view-jsp</name>
<value>/pitcher/view.jsp</value> A
</init-param>
  1. Modify the portlet-info tag so it reads:
<portlet-info>
<title>Pitcher Portlet</title>
<short-title>Pitcher Portlet</short-title>
<keywords>IPC Baseball Pitcher</keywords>
</portlet-info>
  1. Next add Catcher declaration for the portlet in portlet.xml creating a new
<portlet>
<portlet-name>catcher-portlet</portlet-name>
<display-name>Catcher Portlet</display-name>
<portlet-class>
com.liferayinaction.portlets.CatcherPortlet
</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/catcher/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<portlet-info>
<title>Catcher Portlet</title>
<short-title>Catcher Portlet</short-title>
<keywords>IPC Baseball Catcher</keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>
  1. Below the last </portlet> add the event
<event-definition>
<qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
<value-type>java.lang.String</value-type>
</event-definition>
  1. Place the before Pitcher Portlet’s </portet> and below last <security-role-ref> of Pitcher Portlet to tell the event handle who is sending the event.
<supported-publishing-event>
<qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
</supported-publishing-event>
  1. Place the before Catcher Portlet’s </portet>to tell the event handler who is recieveing the event
<supported-processing-event>
<qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
</supported-processing-event>
  1. Create docroot/catcher/view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%
String pitch = (String)renderRequest.getParameter("pitch");
System.out.println(pitch);
%>
<p>And the pitch is....</p>
<p>
<% if (pitch!=null) { %>
<%=pitch %>pitch
<% } else { %>
... waiting for pitch.
<% } %>

  1. Create docroot/pitcher/view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<p>Click the link below to pitch the ball. </p>
<a href="<portlet:actionURL name="pitchBall"></portlet:actionURL>">Pitch!</a>
  1. Under docroot/src create a class CatchPortlet.java
package com.plugin.portlet;

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.portlet.EventRequest;
import javax.portlet.Event;
import javax.portlet.EventResponse;
import java.math.*;
import java.lang.annotation.*;
import javax.portlet.*;
import javax.xml.namespace.QName;

public class CatcherPortlet extends GenericPortlet {
public void init() throws PortletException {
viewJSP = getInitParameter("view-jsp");
}

public void doView(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
include(viewJSP, req, res);
}

protected void include(String path, RenderRequest req, RenderResponse res)
throws IOException, PortletException {
PortletRequestDispatcher prd = getPortletContext()
.getRequestDispatcher(path);
if (prd == null) {
_log.error(path + " is not a valid include");
} else {
prd.include(req, res);
}
}

@ProcessEvent(qname = "{http://liferay.com/events}ipc.pitch")
public void catchBall(EventRequest request, EventResponse response) {
Event event = request.getEvent();
String pitch = (String) event.getValue();
System.out.println(pitch+"In class");
response.setRenderParameter("pitch", pitch);
}
protected String viewJSP;
private static Log _log = LogFactory.getLog(PitcherPortlet.class);

}

  1. Under docroot/src create a class PitchPortlet.java
package com.plugin.portlet;

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.portlet.EventRequest;
import javax.portlet.Event;
import javax.portlet.EventResponse;
import javax.portlet.*;
import java.util.Random;
import javax.xml.namespace.QName;


public class PitcherPortlet extends GenericPortlet {

public void init() throws PortletException {
viewJSP = getInitParameter("view-jsp");
}

public void doView(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
include(viewJSP, req, res);
}

protected void include(String path, RenderRequest req, RenderResponse res)
throws IOException, PortletException {
PortletRequestDispatcher prd = getPortletContext()
.getRequestDispatcher(path);
if (prd == null) {
_log.error(path + " is not a valid include");
} else {
prd.include(req, res);
}
}

@ProcessAction(name = "pitchBall")
public void pitchBall(ActionRequest request, ActionResponse response) {
String pitchType = null;
// Send an Event that the ball has been pitched.
Random random = new Random(System.currentTimeMillis());
int pitch = random.nextInt(3) + 1;
switch (pitch) {
case 1:
pitchType = "Fast Ball";
break;
case 2:
pitchType = "Curve Ball";
break;
case 3:
pitchType = "Slider";
break;
// should never print
default:
pitchType = "Screw Ball";
}
QName qName = new QName("http://liferay.com/events", "ipc.pitch");
System.out.println(pitchType+"");
response.setEvent(qName, pitchType);
}

protected String viewJSP;
private static Log _log = LogFactory.getLog(PitcherPortlet.class);

}

  1. Compile and deploy the portlet.

No comments:

Post a Comment