Sun Java logo     Previous      Contents      Index      Next     

Sun logo
Sun Java System Portal Server 6 2004Q2 Developer's Guide 

Chapter 13
Converting Providers to Portlets

This chapter includes instructions for converting the providers to JSR168 compatible portlets using a sample set of instructions for converting the HelloWorldProviderPA in Chapter 7, "Extending the Base Providers" into a portlet called HelloWorldPortlet.

This chapter contains the following sections:


Developing the Class File

Code Example 13-1 contains the HelloWorldPortlet class file. The HelloWorldPortlet, similar to the HelloWorldProviderPA, prints “Hello World!” on the channel.

Code Example 13-1  HelloWorldPortlet.java File  

package examples;

import java.io.IOException;

import java.io.PrintWriter;

import javax.portlet.GenericPortlet;

import javax.portlet.PortletException;

import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;

public class HelloWorldPortlet extends GenericPortlet {

    public void doView(RenderRequest request,RenderResponse response)

        throws PortletException, IOException {

        response.setContentType(request.getResponseContentType());

        PrintWriter writer = response.getWriter();

        writer.write("Hello World");

    }

}

Mapping ProviderAdapter to GenericPortlet

When developing the class file, use the following table comparing GenericPortlet to ProviderAdapter:

Table 13-1  Mapping ProviderAdapter to GenericPortlet  

ProviderAdapter

GenericPortlet

public void init(String n, HttpServletRequest req)

public void init (PortletConfig config)

public URL processEdit (HttpServletRequest request,HttpServletResponse response)

public void processAction (ActionRequest request,ActionResponse response)

 

public void render (RenderRequest request, RenderResponse resposne)

public StringBuffer getContent (HttpServletRequest request, HttpServletResponse response)

protected void doView (RenderRequest request,RenderResponse response)

public StringBuffer getEdit (HttpServletRequest request, HttpServletResponse response)

protected void doEdit (RenderRequest request,RenderResponse response)

public URL getHelp(HttpServletRequest req, String key)

protected void doHelp (RenderRequest request, RenderResponse response)

public String getName()

public String getPortletName ()

public String getTitle()

protected String getTitle(RenderRequest request)

public ResourceBundle getResourceBundle(String base)

public ResourceBundle getResourceBundle()

public java.util.ResourceBundle getResourceBundle (java.util.Locale locale)

 

public PortletConfig getPortletConfig ()

 

public PortletContext getPortletContext ()

 

public String getInitParameter(java.lang.String name)

 

public java.util.Enumeration getInitParameterNames()

 

public void destroy ()

public int getEditType()

 

public int getWidth()

 

public boolean isEditable()

 

public boolean isPresentable()

 

public ProviderContext getProviderContext()

 

Sample Code Fragments for Provider to Portlet Conversion

This section includes some sample code fragments for Provider to Portlet conversion.

Example 1

The

ProviderContext.getStringAttribute("firstname");

maps to:

Map ui = (Map)renderRequest.getAttribute("javax.portlet.userinfo");

String firstname = (String)ui.get("firstname");

Example 2

The

ProviderContext.getSessionProperty()/setSessionProperty()

maps to (or can use)

PortletSession.getAttribute(), PortletSession.setAttribute()

Example 3

The

ProviderContext.getDesktopURL()

maps to (or can use)

PortletURL.RenderResponse.createRenderURL(), RenderResponse.createActionURL()

Dispatching to a JSP

PortletRequestDispatcher dispatcher = pContext.getRequestDispatcher("test.jsp");

dispatcher.include(request, response);

Help

getHelp()

In Provider, this method returns a helpURL which can be pointing to a static help file.

doHelp()

For portlets, this method has to be implemented for writing out the help content dynamically.

Title

getTitle()

For ProviderAdapter, this method returns the title from the Display Profile title property.

For portlets, the title is returned by the portlet implementation.

editType

For a provider, the editType display profile property can be specified to determine whether the provider implementation will draw the complete edit page or a subset of it.

For portlets, the editType is always EDIT_COMPLETE and the complete page including the form, the Finish, and the Cancel buttons have to be generated by the portlet. Only the banner and footer are drawn by the PortletEdit.jsp in /etc/opt/SUNWps/desktop/default directory.

PortletPreferences

The

ProfileProviderAdapter.getStringProperty(key)

maps to

PortletPreferences.getValue(key, default)

The

ProfileProviderAdapter.getListProperty(key)

maps to

PortletPreferences.getValues(key, default[])


Compiling the Portlet

When compiling a portlet, include the classpath. Ensure that portlet.jar file is in your classpath when compiling the portlet. For example, to compile, type:

javac -classpath portal-server-install-root/SUNWps/lib/portlet.jar HelloWorldPortlet.java


Creating a Portlet Web Application

Assemble the portlet class file and the XML fragments into a portlet web application. A portlet web application must be packaged as a WAR file. The portlet.xml file must be placed into the same location as the web.xml file.

To create a portlet web application:

  1. Create a portlet.xml file that includes the declaration for all the portlets.
  2. Code Example 13-2 contains the portlet.xml file that includes the declaration for the sample HelloWorldPortlet.

    Code Example 13-2  portlet.xml File for the HelloWorldPortlet  

    <?xml version="1.0" encoding="UTF-8"?>

    <portlet-app xmlns="http://java.sun.com/xml/ns/portlet"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:portlet="http://java.sun.com/xml/ns/portlet"

    xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/portlet" version="1.0">

        <portlet>

            <portlet-name>HelloWorldPortlet</portlet-name>

            <portlet-class>custom.HelloWorldPortlet</portlet-class>

            <expiration-cache>0</expiration-cache>

            <supports>

                <mime-type>text/html</mime-type>

                <portlet-mode>VIEW</portlet-mode>

            </supports>

            <portlet-info>

                <title>HelloWorldPortlet</title>

            </portlet-info>

        </portlet>

    </portlet-app>

  3. Create the web.xml file. A web application also requires a web.xml file.
  4. If you have servlets for your portlet web application, then servlet definition can be incorporated in the web.xml file. Code Example 13-3 contains the web.xml file for the sample HelloWorldPortlet.

    Code Example 13-3  web.xml File for HelloWorldPortlet  

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

    <web-app>

        <display-name>Portlet Examples</display-name>

    </web-app>

  5. Package everything into a WAR file.
  6. For the sample portlets, create an examples directory where all the files of the WAR will be placed. Then within the examples directory, create the WEB-INF directory. All the portlet files will be placed into the WEB-INF directory. For example, your directory hierarchy may look like this:

    examples/WEB-INF/

                    /web.xml

                    /portlet.xml

                    /classes/examples/HelloWorldPortlet.class

  7. Create the WAR file. That is, change directories to examples and type the following command:
  8. jar -cvf examples.war *


Deploying the Application

The Portal Server software includes a deployment tool which will take your WAR file and deploy it into the portal server. Before deploying the WAR file, determine where to place the portlets inside the organization.

With the following command, the sample portlets can be deployed into the organization.

pdeploy deploy -u uid -w adminpassword -g|-d distinguishedname -p ContainerAdminPassword examples.war

For more information on the pdeploy command, see Portal Server Administration Guide.


Creating Channels from the Deployed Portlets

Once the portlet is deployed, the portal server is aware of the portlet defined in the application. You can start to create channels based on the portlet. To create channels, see Portal Server Administration Guide for more information.



Previous      Contents      Index      Next     


Copyright 2004 Sun Microsystems, Inc. All rights reserved.