Sun Java System Portal Server 7.1 Developer's Guide

Inter Portlet Communication Sample Portlets

This section provides the location details, Installation information, and code examples of IPC sample portlets.

Location of IPC Sample Portlets

The IPC sample portlets are located at /opt/SUNWportal/samples/ipc. The portlet samples in ipc directory are divided into ipc1 and ipc2 sub-directories. This is to demonstrate eventing between portlets belonging to different web application and those belonging to the same web application. The interaction between the IPC Portlet samples is as follows:

Source code for the following samples is located at the /opt/SUNWportal/samples/ipc/ipc1/src directory.

Source code for the following samples is located at the /opt/SUNWportal/samples/ipc/ipc2/src directory.

JSP files for the portlets are located at:

Web container deployment description files are located at:

Installing IPC Sample Portlets

The information on overview of portlets and how to install them are provided in Chapter 11, Overview of the Portlets.

Code Examples

This section provides code examples for:

Code Example for Generator Portlet (ipc2)

package com.sun.portal.portlet.ipc2;

	import com.sun.portal.portletappengine.ipc.EventRequest;
	import com.sun.portal.portletappengine.ipc.EventResponse;
	import com.sun.portal.portletappengine.ipc.PortletEvent;
	import com.sun.portal.portletappengine.ipc.PortletEventBroker;
	import com.sun.portal.portletappengine.ipc.PortletEventListener;
	...

	/**
	* The PricePortlet participates in the InterPortletCommunication.
	* This generates the event that is consumed by two portlets
	* (a)ConsiderationPortlet which is in a different web application
	* (b)DecisionPortlet which is in the same web application
	* This also consumes the event generated by the DecisionPortlet.
	*/
		public class PricePortlet extends GenericPortlet implements
		PortletEventListener {
			...
			public void processAction(ActionRequest request,
			ActionResponse actionResponse)
				throws PortletException, java.io.IOException {
				PortletEventBroker peb = new PortletEventBroker(request);
				try {
					PortletEvent pe = peb.createEvent("priceChanged");
					String price = null;
					Object obj = request.getParameter("price");
					if (obj != null) {
						price = (String) obj;
					} else {
						price = "9999";
					}
					pe.setEventData(price);
					pe.fire();

					PortletEvent pe2 = peb.createEvent("priceChanged2");
					pe2.setEventData("700");
					pe2.fire();

					PortletEvent pe3 = peb.createEvent("priceChanged3");
					pe3.setEventData("800");
					pe3.fire();

				} catch (Exception e) {
						// Do nothing!
					}
       				actionResponse.setRenderParameters(request.getParameterMap());
				}
				...
			}

Code example for Listener Portlet (ipc1)

package com.sun.portal.portlet.ipc1;

import com.sun.portal.portletappengine.ipc.EventRequest;
import com.sun.portal.portletappengine.ipc.EventResponse;
import com.sun.portal.portletappengine.ipc.PortletEventListener;
...

/**
* The ConsiderationPortlet participates in the InterPortletCommunication.
* This consumes the event generated by the PricePortlet which is in a
* different web application.
*/
public class ConsiderationPortlet extends GenericPortlet{
   implements PortletEventListener ...
   public void handleEvent(EventRequest ereq, EventResponse eres) {
      String data = (String) ereq.getEventData();
      eres.setRenderParameter("price", data);
   }
   ...
	  }

Code Example for Listener Portlet (ipc2)

package com.sun.portal.portlet.ipc2;

import com.sun.portal.portletappengine.ipc.EventNotRegisteredException;
import com.sun.portal.portletappengine.ipc.EventRequest;
import com.sun.portal.portletappengine.ipc.EventResponse;
import com.sun.portal.portletappengine.ipc.PortletEvent;
import com.sun.portal.portletappengine.ipc.PortletEventBroker;
import com.sun.portal.portletappengine.ipc.PortletEventListener;
...

/**
* The DecisionPortlet participates in the InterPortletCommunication.
* This consumes the event generated by the PricePortlet which is in the
* same web application.
* This also generates the event that is consumed by the PricePortlet.
*/
public class DecisionPortlet extends GenericPortlet{
   implements PortletEventListener ...
   public void handleEvent(EventRequest ereq, EventResponse eres) {
       String name = ereq.getEventName();
       if (name.equalsIgnoreCase("priceChanged")) {
           String data = (String) ereq.getEventData();
           int price_data = Integer.parseInt(data);
           PortletEventBroker peb = new PortletEventBroker(ereq);
           if (price_data > 500) {
               try {
                   PortletEvent pe = peb.createEvent("highPriceEvent");
                   pe.setEventData(data);
                   pe.fire();
               } catch (EventNotRegisteredException e) {
                   e.printStackTrace();
               }
           } else {
               try {
                   PortletEvent pe = peb.createEvent("lowPriceEvent");
                   pe.setEventData(data);
                   pe.fire();
               } catch (EventNotRegisteredException e) {
                   e.printStackTrace();
               }
           }
           eres.setRenderParameter("price", data);
       } else if (name.equalsIgnoreCase("priceChanged2")) {
           eres.setRenderParameter("price2", name);
       } else if (name.equalsIgnoreCase("priceChanged3")) {
           eres.setRenderParameter("price3", name);
       }
   }
   ...
}

Code Example for sun-portlet.xml of ipc2.war

<portlet>
	<portlet-name>decisionportlet</portlet-name>
	<events>
		<generates-event>decisionMade</generates-event>
		<generates-event>highPriceEvent</generates-event>
		<generates-event>lowPriceEvent</generates-event>
		<consumes-event>priceChanged</consumes-event>
		<consumes-event>priceChanged</consumes-event>
		<consumes-event>priceChanged</consumes-event>
	</events>
</portlet>

<portlet>
	<portlet-name>priceportlet</portlet-name>
	<events>
		<generates-event>priceChanged</generates-event>
		<generates-event>priceChanged</generates-event>
		<generates-event>priceChanged</generates-event>
		<consumes-event>highPriceEvent</consumes-event>
		<consumes-event>lowPriceEvent</consumes-event>
	</events>
</portlet>

Code Example for sun-portlet.xml of ipc1.war

<portlet>
	<portlet-name>considerationportlet</portlet-name>
	<events>
		<consumes-event>priceChanged</consumes-event>
	</events>
</portlet>