Tuesday, January 12, 2010

Custom Portlet Service

A WebSphere Portlet service is used to provide common functionality/services to portlets. WebSphere Portal provides many out of the box portlet services for different functionality like PUMA, authentication, etc.

JSR 168 portlets use JNDI lookup to obtain an instance of the required portlet service. A portlet service can only be invoked from inside a portlet.

Following are the steps to create a custom Portlet Service:
1. Create an interface which extends com.ibm.portal.portlet.service.PortletService interface and defines public methods which you want to expose through the service.
2. Implement the above interface along with com.ibm.portal.portlet.service.spi.PortletServiceProvider interface in your custom class.
3. Register the portlet service with Portal.

CODE for the above is as follows:

package com.pankaj.portlet.service;

import java.util.Map;

import com.ibm.portal.portlet.service.PortletService;

/**
* @author Pankaj Gupta
*
*/

public interface PortalUserCreationService extends PortletService {

public static final String JNDI_NAME = "portletservice/com.pankaj.portlet.service.PortalUserCreationService";

public static final Class serviceClass = com.pankaj.portlet.service.PortalUserCreationService.class;

public boolean createUser(String shortName, String parentDN, Map map);
}

Code Listing 1: Interface PortalUserCreationService


package com.pankaj.portlet.service.impl;

import java.util.Map;
import java.util.prefs.Preferences;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.PortletServiceUnavailableException;
import com.ibm.portal.portlet.service.spi.PortletServiceProvider;
import com.ibm.portal.um.PumaController;
import com.ibm.portal.um.PumaHome;
import com.ibm.portal.um.User;
import com.ibm.portal.um.exceptions.PumaAttributeException;
import com.ibm.portal.um.exceptions.PumaMissingAccessRightsException;
import com.ibm.portal.um.exceptions.PumaModelException;
import com.ibm.portal.um.exceptions.PumaSystemException;
import com.ibm.websphere.cache.DistributedMap;

/**
* @author Pankaj Gupta
*
*/

public class PortalUserCreationServiceImpl implements PortalUserCreationService, PortletServiceProvider {

private Context ctx = null;

private PumaController pumaController = null;
private PumaHome pumaHome = null;

public boolean createUser(String shortName, String parentDN, Map map) {
boolean flag = false;
try {
User user = pumaController.createUser(shortName, parentDN, map);
flag = true;
}
catch (PumaAttributeException e) {
e.printStackTrace();
}
catch (PumaSystemException e) {
e.printStackTrace();
}
catch (PumaModelException e) {
e.printStackTrace();
}
catch (PumaMissingAccessRightsException e) {
e.printStackTrace();
}
return flag;
}

public void init(Preferences arg0) throws PortletServiceUnavailableException {

try {
ctx = new InitialContext();
pumaHome = (PumaHome)ctx.lookup(PumaHome.JNDI_NAME);
pumaController = pumaHome.getController();
}
catch (NamingException e) {
e.printStackTrace();
}

}

}

Code Listing 2: Implementation of the Listing 1 Interface

package com.pankaj.portlet.service.common;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.ibm.portal.portlet.service.PortletService;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.PortletServiceUnavailableException;

/**
* @author Pankaj Gupta
*
*/
public class PortletServiceLocator {
//
public static PortletService getPortletService(String jndiName, Class serviceClass){
PortletServiceHome portletServiceHome = null;
PortletService portletService = null;

try {
Context ctx = new InitialContext();
portletServiceHome = (PortletServiceHome)ctx.lookup(jndiName);
portletService = (PortletService) portletServiceHome.getPortletService(serviceClass);
}
catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (PortletServiceUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return portletService;
}
}

Code Listing 3: Service Locator class which abstracts the service lookup.

No comments:

Post a Comment

Sponsor Advertisement