Portlet Services
Portlet services are used to provide common functionality/services to portlets. WebSphere Portal provides many out of the box portlet services for different functionality like PUMA service, Authentication service and lot more.
Portlets use JNDI look up to obtain an instance of the required portlet service. A portlet service can only be invoked from inside a portlet. New services can be added to the portal but it requires restart of the portal.
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. Export the Service as Java archive (JAR) file.
4. Register the portlet service with Portal.
5. Restart the server for changes to take effect.
Step1: Defining the interface
Portlet services are used to provide common functionality/services to portlets. WebSphere Portal provides many out of the box portlet services for different functionality like PUMA service, Authentication service and lot more.
Portlets use JNDI look up to obtain an instance of the required portlet service. A portlet service can only be invoked from inside a portlet. New services can be added to the portal but it requires restart of the portal.
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. Export the Service as Java archive (JAR) file.
4. Register the portlet service with Portal.
5. Restart the server for changes to take effect.
Step1: Defining the interface
package com.sample.portlet.service;
import java.io.IOException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import com.ibm.portal.portlet.service.PortletService;
public interface MyService extends PortletService {
public void myFirstPortletService(PortletRequest request, PortletResponse response) throws IOException;
}
Step2: Writing the Service Implementation
package com.sample.portlet.service;
import java.io.IOException;
import java.util.prefs.Preferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import com.ibm.portal.portlet.service.spi.PortletServiceProvider;
public class MyServiceImpl implements MyService,PortletServiceProvider {
private String message;
public void init(Preferences servicePreferences) {
// read the message from the portlet preferences , default is "My First Service"
message = servicePreferences.get("message", "My First Service");
}
public void myFirstPortletService(PortletRequest request, PortletResponse response) throws IOException {
System.out.println("Calling myFirstPortletService Method : " + message);
}
}
Step3: Registering the service
- Put all service interface and implementation classes into a JAR file.
- Place the JAR file in the wp_profile/PortalServer_root/config directory.
- Register the new portlet service with the WP PortletServiceRegistryService resource environment provider in the WebSphere Application Server administration console.
- Create an entry to register the implementation in the JNDI directory. The name for this entry is jndi.service_interface and the value is service implementation. The fully qualified service interface name can then be used to look-up the service.
- Restart WebSphere Portal to activate the new settings.
name :jndi.com.sample.portlet.service.MyServicevalue: com.sample.portlet.service.MyServiceImpl
Step4: Invoking the Service From the Portlet
If the service is invoked properly then the default "My First Service" can be viewed in the SystemOut.log.
import javax.portlet.*;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.PortletServiceUnavailableException;
import com.sample.portlet.service.MyService;
public class CustomPortletServicePortlet extends GenericPortlet {
private PortletServiceHome accountInfoServiceHome;
private MyService myservice;
public void init() throws PortletException {
/* Lookup the portlet service */
try {
javax.naming.Context ctx = new javax.naming.InitialContext();
Object home = ctx.lookup("portletservice/com.sample.portlet.service.MyService");
if (home != null)
accountInfoServiceHome = (PortletServiceHome) home;
} catch(NameNotFoundException ex) {
// Exception handling code goes here
} catch (NamingException ex) {
// Exception handling code goes here
}
}
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// Set the MIME type for the render response
response.setContentType(request.getResponseContentType());
if(accountInfoServiceHome != null){
try {
myservice = (MyService)accountInfoServiceHome.getPortletService(MyService.class);
myservice.myFirstPortletService(request, response);
} catch (PortletServiceUnavailableException e) {
// Exception handling code goes here
}
}
}
If the service is invoked properly then the default "My First Service" can be viewed in the SystemOut.log.
No comments:
Post a Comment