Saturday, August 29, 2009

Hello World on Pluto

I'm going to use the manual method of constructing a portlet because people don't like Maven and they may not be able to get it to work behind an NTLM proxy (but I managed that in the end).

First write yourself a basic HelloWorldPortlet. It doesn't do much, which is good. Because you want to understand it:

package examples.portlet;
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 
{
 protected void doEdit(RenderRequest request, RenderResponse response) 
          throws PortletException, IOException 
 {
         response.setContentType("text/html");  
         PrintWriter writer = new PrintWriter(response.getWriter());
         writer.println("You're now in Edit mode.");  
 }
 protected void doHelp(RenderRequest request, RenderResponse response) 
          throws PortletException, IOException 
 {
         response.setContentType("text/html");  
         PrintWriter writer = new PrintWriter(response.getWriter());
         writer.println("You're now in Help mode."); 
 }
 protected void doView(RenderRequest request, RenderResponse response) 
          throws PortletException, IOException 
 {
         response.setContentType("text/html");
         PrintWriter writer = new PrintWriter(response.getWriter());
         writer.println("Hello world! You're in View mode.");
    }
}

Save that in a file HelloWorldPortlet.java. We'll assemble it later.

Now create a web application description file. Call it web.xml. Here's the one I used:

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

<!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>HelloWorldPortlet</display-name>
 <servlet>
  <servlet-name>HelloWorldPortlet</servlet-name>
  <servlet-class>
   org.apache.pluto.container.driver.PortletServlet
  </servlet-class>
  <init-param>
   <param-name>portlet-name</param-name>
   <param-value>HelloWorldPortlet</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>HelloWorldPortlet</servlet-name>
  <url-pattern>/PlutoInvoker/HelloWorldPortlet</url-pattern>
 </servlet-mapping>
  <security-role>
    <role-name>tomcat</role-name>
  </security-role>
</web-app>

The org.apache.pluto.container.driver.PortletServlet is really important. This is the class inside Pluto that registers your portlet. Without that it won't know it's there.

Now create a portlet app definition file, call it portlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<portlet-app
    xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
    version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
                        http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
 <portlet>
    <description>Hello, world! portlet</description>
    <portlet-name>HelloWorldPortlet</portlet-name>
    <display-name>HelloWorldPortlet</display-name>
    <portlet-class>
       examples.portlet.HelloWorldPortlet
    </portlet-class>    
    <supports>
      <mime-type>text/html</mime-type>
      <portlet-mode>VIEW</portlet-mode>
      <portlet-mode>EDIT</portlet-mode>
      <portlet-mode>HELP</portlet-mode>
    </supports>
    <portlet-info>
        <title>Hello, Pluto at last!</title>
    </portlet-info>        
  </portlet>
</portlet-app>

There's only one portlet inside it, but you can have several. In fact you can define all your portlets inside Pluto but that means hacking Pluto, which I don't recommend. It's much better to provide a portlet app with separate portlets, like the testsuite.

Finally you need to create a context-descriptor. It's very simple. Call it hello.xml:

<Context path="/hello" docBase="hello.war" crossContext="true"> </Context>

Now compile the HelloWorldPortlet. You'll need to add the portlet-api_2.0_spec-1.0.jar which is in the lib folder of your Pluto (aka Tomcat 6) installation to the classpath:
javac -cp ~/pluto/lib/portlet-api_2.0_spec-1.0.jar HelloWorldPortlet.java
(assuming that your Pluto installation is in your home directory)

Now create a directory called hello, and populate it as follows:

  • META-INF
    • hello.xml
  • WEB-INF
    • portlet.xml
    • web.xml
    • classes
      • examples
        • portlet
          • HelloWorldPortlet.class

Finally, cd into the hello directory and type:
java cvf hello.war .

Now copy the hello.war file into the webapps directory inside your Pluto installation. Restart Pluto and if you go to Pluto Admin then the hello world portlet will be available for adding to pages. It even works.

No comments:

Post a Comment