Step to follow to create a portlet service:
- Create a utility project.
- Create an interface class. Extend the PortletService interface com.ibm.portal.portlet.service.PortletService.
- Implement the com.ibm.portal.portlet.service.spi.PortletServiceProvider interface to create an implementation class. This is your interface.
- Export the service Java archive (JAR) file.
- Register the service.
- Restart the server
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{
/** print a nice greeting */
public void sayHello(PortletRequest request, PortletResponse response) throws IOException;
}
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;
// called by the portal when the service is initialized
public void init(Preferences servicePreferences) {
// read the message from the configuration, default is "Hello"
message = servicePreferences.get("message", "Hello");
}
public void sayHello(PortletRequest request, PortletResponse response)
throws IOException {
String user = request.getRemoteUser();
System.out.println("Calling SayHello"+message);
}
}
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 lookup the service.
· Restart WebSphere Portal to activate the new settings.
name :jndi.com.sample.portlet.service.MyService
Value: com.sample.portlet.service.MyServiceImpl
Portlet Code
import javax.portlet.*;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.PortletServiceUnavailableException;
import com.sample.portlet.service.MyService;
/**
* A sample portlet based on GenericPortlet
*/
public class CustomPortletServicePortlet extends GenericPortlet {
public static final String JSP_FOLDER = "/_CustomPortletServicePortlet/jsp/"; // JSP folder name
public static final String VIEW_JSP = "CustomPortletServicePortletView"; // JSP file name to be rendered on the view mode
private PortletServiceHome accountInfoServiceHome;
private MyService myservice;
/**
* @see javax.portlet.Portlet#init()
*/
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) {
System.out.println("MyService is not found");
ex.printStackTrace();
} catch (NamingException ex) {
System.out.println("MyService is not available");
ex.printStackTrace();
}
}
/**
* 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());
if(accountInfoServiceHome != null){
try {
myservice = (MyService)accountInfoServiceHome.getPortletService(MyService.class);
myservice.sayHello(request, response);
} catch (PortletServiceUnavailableException e) {
System.out.println("MyService is not available");
e.printStackTrace();
}
}
// Invoke the JSP to render
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
rd.include(request,response);
}
No comments:
Post a Comment