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:

package com.ibm.myeventdistributionservice;
import java.io.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.portlet.*;
import javax.xml.namespace.QName;
import com.ibm.portal.eventing.service.EventDistributionService;
import com.ibm.portal.eventing.service.EventDistributionServiceException;
import com.ibm.portal.portlet.service.PortletServiceHome;
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{
        super.init();


           try {
             Context ctx = new InitialContext();
             PortletServiceHome serviceHome = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.eventing.service.EventDistributionService");
             edService = (EventDistributionService)serviceHome.getPortletService(com.ibm.portal.eventing.service.EventDistributionService.class);
             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
        response.setContentType(request.getResponseContentType());
        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
            e.printStackTrace();
        }
        // Invoke the JSP to render
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
        rd.include(request,response);
    }

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

    /**
     * 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";
        else
            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";
    }

}

Portlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" id="com.ibm.myeventdistributionservice.MyEventDistributionServicePortlet.c01f970a23">
    <portlet>
        <portlet-name>MyEventDistributionService</portlet-name>
        <display-name xml:lang="en">MyEventDistributionService</display-name>
        <display-name>MyEventDistributionService</display-name>
        <portlet-class>com.ibm.myeventdistributionservice.MyEventDistributionServicePortlet</portlet-class>
        <init-param>
            <name>wps.markup</name>
            <value>html</value>
        </init-param>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>view</portlet-mode>
        </supports>
        <supported-locale>en</supported-locale>
        <resource-bundle>com.ibm.myeventdistributionservice.nl.MyEventDistributionServicePortletResource</resource-bundle>
        <portlet-info>
            <title>MyEventDistributionService</title>
            <short-title>MyEventDistributionService</short-title>
            <keywords>MyEventDistributionService</keywords>
        </portlet-info>
    <supported-processing-event>
            <qname>hello</qname>
        </supported-processing-event>   
    <supported-publishing-event>
      <name>hello</name>
    </supported-publishing-event>
    </portlet>
    <default-namespace>http://MyEventDistributionService/</default-namespace>
     <event-definition>
    <name>hello</name>
    <value-type>java.lang.String</value-type>
  </event-definition>
   
</portlet-app>

 Wiring:


 Result ScreenShot:


Click here to download sample code







 

No comments:

Post a Comment