Oracle9iAS Portal Developer Kit
How to Build a Java Portlet
Once you have successfully installed and deployed the PDK-Java samples, you will probably want to begin building Java portlets of your own. The PDK-Java Framework makes this easy, as it provides a generic mechanism for building portlets and you can easily 'plug in' your own extensions.
When using the PDK-Java Framework to create a Java portlet, you'll follow these two basic steps:
First, select or implement the portlet controller classes that provide your portlet's behavior. The default runtime uses controller classes which implement the following interfaces:
PortletRenderer: Generates a visual representation of the portlet (usually in HTML) in all the various 'render modes' that it supports. Since a portlet must always support at least one render mode (the basic 'show' mode), every portlet requires such an object.
PortletPersonalizationManager: If portlet behavior can be customized through an HTML form, one of these controller classes are required in order to store and retrieve customized parameters.
PortletSecurityManager: More advanced portlets may want to restrict users to certain portlet instances. An object of this type is required to enforce such security policies.
Then, create an XML provider definition that configures your portlet and its controller classes within a provider.
This article describes these steps in more detail, showing you how to create a simple Java portlet and display it on a portal page. In this example we use a single PortletRenderer controller called RenderManager to give the portlet its basic rendering capabilities. In further articles, we will show you how to extend your portlet's functionality using other types of controller class.
ASSUMPTIONS
You have already installed the samples downloaded with the PDK-Java and understand the steps required to display a Java portlet on a portal page. For more information on installing the sample, please review the article Installing the PDK-Java Framework and Samples.
You are using the Oracle HTTP Server to execute and display servlets used by the PDK-Java. This listener need not be the same listener that serves your portal pages. If you are using a third party listener, this article will still be useful, but you may have to take alternative steps when you get to the section "Configuring your Webserver".
CREATING A RENDERER
Your first task is to set up a PortletRenderer controller to render portlet output. For this, PDK-Java Framework provides a very flexible RenderManager implementation. RenderManager takes care of the basics required of any PortletRenderer, for example it renders standard headers and footers for portlet 'containers'. All you need to do is to provide a simple 'content generator', or ManagedRenderer that generates the main body of your portlet for a particular render mode. Note that a content generator is required for each of the render modes the portlet is to support.
The PDK-Java Framework provides a range of ManagedRenderers that allow you to utilize other Web technologies to generate portlet content, such as static HTML files, JSPs and Servlets. In later articles, we will illustrate how to use of some of these classes, but in this article you will create your own, just to show you how easy it is.
Using your favorite text editor, create a Java class called MyCustomRenderer, that extends BaseManagedRenderer (the base of all ManagedRenderers) and provide an implementation of the renderBody method to generate the content of your portlet.
You may write your own Java code, or use the sample code provided below which outputs a personalized greeting to the user currently logged in.
import java.io.*; import oracle.portal.provider.v1.*; import oracle.portal.provider.v1.http.BaseManagedRenderer; public class MyCustomRenderer extends BaseManagedRenderer { public void renderBody(PortletRenderRequest pr) throws PortletException { try { // Get a writer for this renderer's output PrintWriter out = pr.getWriter(); // Generate output and send to the writer out.println("<b>Hi " + pr.getUser().getName() + "!</b><br>"); out.println("This output was generated by my very own renderer!"); } catch (IOException ioe) { throw new PortletException(ioe); } } }
Remember the name of the directory where the file is saved as you will need this later. For example: C:\MyProvider\MyClasses
For example, at the system prompt type:
On Windows NT:
set CLASSPATH=C:\jpdk\lib\provider.jar;%CLASSPATH%
On UNIX (csh):
setenv CLASSPATH ~/jpdk/lib/provider.jar:$CLASSPATH
Verify that the Java executable is available in your system path.
To do this, type "java -version" at your system prompt. If this command is unsuccessful, locate the directory of the Java executable, and add it to your PATH environment variable, in a similar manner to the step above.
For example, if you have an Oracle9i Application Server installed in C:\iAS on Windows NT you would issue the following command:
set PATH=C:\iAS\Apache\jdk\bin;%PATH%
Make the directory you in which you saved MyCustomRenderer.java the current directory and then compile this file using the following command:
javac MyCustomRenderer.java
This creates a class file called MyCustomRenderer.class in the same directory.
CREATING AN XML PROVIDER DEFINITION
This section explains how to create an XML provider definition for the provider object that will host your new portlet and allow it to be added to an Oracle Portal page. Within a XML provider definition you configure provider parameters for your portlets and any 'controller' objects that they use.
Using your favorite text editor, create an XML file that configures a provider for your portlet.
For this example, specify that you are using a RenderManager object as a controller for rendering and an object of your own custom class (MyCustomRenderer) as the ManagedRenderer for 'show' mode.
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<?providerDefinition version="2.0"?>
<provider class="oracle.portal.provider.v1.http.DefaultProvider">
<session>true</session>
<portlet class="oracle.portal.provider.v1.http.DefaultPortlet">
<id>1</id>
<name>MyFirstPortlet</name>
<title>My first portlet</title>
<description>My first ever portlet, using my own custom renderer</description>
<timeout>10</timeout>
<timeoutMessage>Timed out waiting for MyFirstPortlet</timeoutMessage>
<renderer class="oracle.portal.provider.v1.RenderManager">
<showPage class="MyCustomRenderer"/>
</renderer>
</portlet>
</provider>
|
Save the file as "provider.xml", making sure not to overwrite one of the PDK-Java sample files of the same name.
Remember the name of the directory where the file is saved as you will need this later. For example: C:\MyProvider\provider.xml
CONFIGURING YOUR WEBSERVER
These steps describe how to configure the Oracle HTTP Server only. If you are using a third party listener, please take the appropriate steps.
Stop the Oracle HTTP Server.
Open the configuration file zone.properties
Under the Servlet Aliases section, register an alias myfirst for a new instance of the Provider Adapter servlet, to use the XML provider definition you have just created.
For example, add the line:
servlet.MyProvider.code=oracle.portal.provider.v1.http.HttpProvider
Under the Aliased Servlet Init Parameters section, add settings for the provider_root and sessiontimeout parameters, making sure that provider_root points to the directory where you saved your provider.xml file.
For example, add the line:
servlet.MyProvider.initArgs=provider_root=C:\MyProvider,sessiontimeout=1800000,debuglevel=1
Note: If the debuglevel parameter is set to 1, you will activate a special 'test page' feature which allows you to ensure that your provider is configured properly before you try and register it with a portal.
Save zone.properties
Make sure that MyCustomRenderer.class is in the Oracle HTTP Server classpath, so that Provider Adapter can locate the class file for your custom renderer:
Open the file jserv.properties
Add a wrapper.classpath entry at the end of the file which specifies the name of the directory containing MyCustomRenderer.class.
For example:
wrapper.classpath=C:\MyProvider\MyClasses
Start the Oracle HTTP Server.
Access your provider's test page by entering into a browser the URL for the aliased servlet you just set up. For example:
http://host.domain:port/servlet/MyProvider
Note: You will use this URL later when you register the provider on a portal.
REGISTERING THE PROVIDER
Once you have successfully tested your new provider you may register the provider with Oracle9iAS Portal.
Under the Administer tab (Oracle Portal Home Page), click Add a Portlet Provider.
Enter the following provider information:
Name: MyFirstPortletProvider
Display Name: My First Portlet Provider
Timeout: 100
Timeout Message: My First Portlet Timed Out
Implementation Style: Web
Provider Login Frequency: Once per User Session
ADDING THE JAVA PORTLET TO A PAGE
When you have registered your provider you can add your new Java portlet to a page. Please refer to the Oracle9iAS Portal online documentation for more information.
Now that you have successfully built your own Java portlet, please look for future articles that describe how to add extra features to your portlet, including customization, session storage, multi-language capabilities, and more.
Revision History:
June 2001