Sunday, 9 October 2011

Sample Code for EventDistributionService in Websphere Portal Server

EventDistributionService provides the option to have user interface elements displayed dependent on whether a given event is wired to targets or not. In this example,i have created a portlet with circular events

Sample Code:

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.portlet.*;
import javax.xml.namespace.QName;
public class MyEventDistributionServicePortlet extends GenericPortlet {
    public static final String JSP_FOLDER    = "/_MyEventDistributionService/jsp/";    // JSP folder name
    public static final String VIEW_JSP      = "MyEventDistributionServicePortletView";         // JSP file name to be rendered on the view mode
    boolean edServiceAvailable = false;
    EventDistributionService edService = null;
     * @see javax.portlet.Portlet#init()
    public void init() throws PortletException{

           try {
             Context ctx = new InitialContext();
             PortletServiceHome serviceHome = (PortletServiceHome) ctx.lookup("portletservice/");
             edService = (EventDistributionService)serviceHome.getPortletService(;
             edServiceAvailable = true;
           catch(Throwable t) {


     * Serve up the <code>view</code> mode.
     * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
    public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        // Set the MIME type for the render response
        QName qname =new QName("http://MyEventDistributionService/","hello");
        try {
            boolean result=edService.isEventWired(request,response, qname);
            request.setAttribute("result", result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
        // Invoke the JSP to render
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));

     * Process an action request.
     * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
    public void processAction(ActionRequest request, ActionResponse response) throws PortletException, {

     * Returns JSP file path.
     * @param request Render request
     * @param jspFile JSP file name
     * @return JSP file path
    private static String getJspFilePath(RenderRequest request, String jspFile) {
        String markup = request.getProperty("wps.markup");
        if( markup == null )
            markup = getMarkup(request.getResponseContentType());
        return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);

     * Convert MIME type to markup name.
     * @param contentType MIME type
     * @return Markup name
    private static String getMarkup(String contentType) {
        if( "text/vnd.wap.wml".equals(contentType) )
            return "wml";
            return "html";

     * Returns the file extension for the JSP file
     * @param markupName Markup name
     * @return JSP extension
    private static String getJspExtension(String markupName) {
        return "jsp";



<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="" version="2.0" xmlns:xsi="" xsi:schemaLocation="" id="">
        <display-name xml:lang="en">MyEventDistributionService</display-name>


 Result ScreenShot:

Click here to download sample code


No comments:

Post a Comment