Sun Java Solaris Communities My SDN Account Join SDN
 
Article

Portlet Example on GlassFish

 
By Satish Viswanatham, Dean Polla, and Rick Palkovic, April 21, 2006  

Many users of the open source GlassFish Java EE 5 application server want to use the server when developing and testing portlets. Portlets are designed to run in the context of a portal, which GlassFish does not provide directly. Although it is possible to execute and test a portlet without a portal, it is cumbersome to do so.

This article describes how you can deploy a portlet in GlassFish by using Pluto, the reference implementation of the Java Portlet Specification (JSR 168). Pluto provides a portlet container that offers you a working example platform from which you can test portlets that have been implemented according to the JSR 168 portlet API.

In the Pluto portlet container's runtime environment, portlets can be instantiated, used, and destroyed. Although Pluto's simple portal component is useful for testing portlets, it is not designed for high-performance production use.

In this article, you install Pluto 1.0.1 and use it to test an example portlet. For simplicity, all the required files from the Pluto installation are provided in a file named pluto-on-glassfish.jar. The section titled Building the pluto-on-glassfish.jar File describes how to build the .jar file.

Note that pluto-on-glassfish.jar is provided merely for convenience; it is not a supported .jar file. Its contents are derived entirely from work of the Apache Software Foundation. The file has been created and is to be used under the terms of the Apache License.

Contents
 
Setting Up Pluto
Testing the Hello Portlet
Testing Your Own Portlet
Building the pluto-on-glassfish.jar File
Creating and Populating a Directory
Creating a Sample Portlet
Modifying the Pluto Configuration
Conclusion
References
 
Setting Up Pluto

This article assumes that you have successfully installed GlassFish. If you do not have GlassFish, download it now.

Use the following procedure to set up your GlassFish installation and prepare it for portlet development. The procedure uses the variable $GLASSFISH_HOME to represent the directory in which GlassFish has been installed.

  1. Download the file.

  2. Save the pluto-on-glassfish.jar file in the $GLASSFISH_HOME directory. Then, open a command shell and expand the file with the following command.

    jar xvf $GLASSFISH_HOME/pluto-on-glassfish.jar
    

    As shown below, the files hello.war and pluto.war are stored in the directory $GLASSFISH_HOME/domains/domain1/autodeploy and five Pluto libraries are placed into the directory $GLASSFISH_HOME/glassfish/lib.

    created: META-INF/
    inflated: META-INF/MANIFEST.MF
    created: domains/
    created: domains/domain1/
    created: domains/domain1/autodeploy/
    inflated: domains/domain1/autodeploy/hello.war
    inflated: domains/domain1/autodeploy/pluto.war
    created: lib/
    inflated: lib/commons-logging-1.0.4.jar
    inflated: lib/pluto-1.0.1.jar
    inflated: lib/portlet-api-1.0.jar
    inflated: lib/xercesImpl-2.6.2.jar
    inflated: lib/xmlParserAPIs-2.6.2.jar
    

  3. Remove the directory $GLASSFISH_HOME/META-INF. This step is optional, but because the directory will never be used again, it is good practice to remove it.

    Stop the GlassFish server with the following command:

    $GLASSFISH_HOME/bin/asadmin stop-domain domain1
    

    The following message is displayed: Domain domain1 stopped.

  4. Restart the server with the following command:

    $GLASSFISH_HOME/bin/asadmin start-domain domain1
    

    The following message is displayed: Starting domain domain1, please wait....

  5. As the server restarts, it autodeploys the pluto.war file from the autodeploy subdirectory. Wait until pluto.war has been deployed. The $GLASSFISH_HOME/domains/domain1/server.log file shows a final message similar to the following:

    [#|2006-03-27T16:45:43.101-0800|INFO|sun-appserver-pe9.0| 
    javax.enterprise.system.tools.deployment|_ThreadID=11; _ThreadName=Timer-4;|[AutoDeploy] 
    Successfully autodeployed : $GLASSFISH_HOME/domains/domain1/autodeploy/pluto.war.|#]
    

  6. Deploy the Hello Portlet web module with the following command:

    $GLASSFISH_HOME/bin/asadmin deploy hello.war
    

    The following message is displayed: Command deploy executed successfully.

    Alternatively, you can use the GlassFish Admin Console to deploy the .war file (direct your browser to http://localhost:4848/).

    After successful deployment, the server.log file shows a final message like the following:

    #|2006-03-27T16:45:48.606-0800|INFO|sun-appserver-pe9.0| 
    javax.enterprise.system.tools.deployment|_ThreadID=11; 
    _ThreadName=Timer-4;|[AutoDeploy] Successfully deployed :
    $GLASSFISH_HOME/domains/domain1/hello.war.|#
    

Testing the Hello Portlet

Now, test the Hello portlet to verify that it has been deployed.

  1. Open the Pluto portal by directing your browser to the following URL:
    http://localhost:8080/pluto/portal

    A window showing the Pluto Apache Portals console is displayed. At the left side of the console are links to Admin and Hello portlets.

  2. Click the Hello link.

    The output of the HelloWorld portlet, as shown in Figure 1, is displayed.

  3. FIGURE 1: Hello World Portlet in Pluto Apache Portals Console
    FIGURE 1: Hello World Portlet in Pluto Apache Portals Console
     
    Testing Your Own Portlet

    Now that you have incorporated pluto-on-glassfish.jar into your GlassFish installation, you can run any JSR 168-compliant portlet, not only the Hello portlet provided in this article.

    To develop a portlet from scratch, follow the steps in the sections Creating a Sample Portlet and Modifying the Pluto Configuration, then restart the GlassFish server. Your portlet will be deployed automatically and will be shown in the Pluto Apache Portals console shown in Figure 1.

     
    Building the pluto-on-glassfish.jar File

    This section describes how to build the pluto-on-glassfish.jar file, should you choose to build such a file yourself. This exercise is entirely optional; installing the pluto-on-glassfish.jar file and modifying the GlassFish installation as described above is sufficient for you to build and test your own portlets, as described in Creating a Sample Portlet and Modifying the Pluto Configuration.

    Building the pluto-on-glassfish.jar file involves the following steps:

    • Creating a directory in which to package the pluto-on-glassfish.jar file and populating the directory with Pluto shared .jar files
    • Creating a portlet
    • Modifying the Pluto configuration

    This section uses the variable $PLUTO_INSTALL_DIR to represent the directory where Pluto 1.0.1 has been installed.

     
    Creating and Populating a Directory

    Start by creating a directory and populating it with shared and common files from the Pluto 1.0.1 distribution.

    1. Download Pluto 1.0.1.

    2. Expand the compressed download file in an empty directory, identified in these steps by the $PLUTO_INSTALL_DIR variable.

    3. Create a directory in which to package your .jar file. Name the directory pluto-on-glassfish.

    4. Create a subdirectory in pluto-on-glassfish, and copy the Pluto shared/lib/*.jar and common/endorsed/*.jar files to the pluto-on-glassfish/lib directory. Copy the files with your computer's GUI, or use the following commands from a command shell.

      cd pluto-on-glassfish 
      mkdir lib
      cd lib
      cp $PLUTO_INSTALL_DIR/shared/lib/*.jar . 
      cp $PLUTO_INSTALL_DIR/common/endorsed/*.jar . 
      

      The pluto-on-glassfish/lib directory now contains the following five files:

      pluto-1.0.1.jar 
      portlet-api-1.0.jar  
      commons-logging-1.0.4.jar 
      xercesImpl-2.6.2.jar 
      xmlParserAPIs-2.6.2.jar
      

      Note that other than relocation, these files are not changed in any way. They are used under the terms of the Apache License.

     
    Creating a Sample Portlet

    The Sun Java Studio Creator 2 integrated development environment gives you the visual tools for creating and designing portlets that can be deployed readily on Sun Java Portal Server 6.1. Other portlet containers such as Pluto may require you to manually edit the web.xml deployment descriptor.

    The following steps describe how to create a portlet for use with Pluto on GlassFish. The Hello World portlet is used as an example.

    1. Create the HelloWorldPortlet class by saving the source code shown below as HelloWorldPortlet.java and compiling it with the following command.

      javac -classpath ${GLASSFISH_HOME}/lib/portlet-api-1.0.jar 
      HelloWorldPortlet.java 
      

      package sample;
      import java.io.IOException;
      import javax.portlet.GenericPortlet;
      import javax.portlet.PortletException;
      import javax.portlet.RenderRequest;
      import javax.portlet.RenderResponse;
      
      public class HelloWorldPortlet extends GenericPortlet {
          protected void doView(RenderRequest request, RenderResponse response) 
                                throws PortletException, IOException {
              response.setContentType("text/html");
              response.getWriter().write("Hello World!");
          }
      }
      

    2. Set up the web application directory structure as shown below. Steps 35 below provide the contents of the files HelloWorldPortlet.properties, web.xml, and portlet.xml.

      $GLASSFISH_HOME/domains/domain1/applications/j2ee-modules/
          hello/
              META-INF/
                  MANIFEST.MF
              WEB-INF/
                  portlet.xml
                  lib/
                      helloworldportlet.jar
                  classes/
                      HelloWorldPortlet.properties
                  web.xml
      

    3. Create a resource bundle for the HelloWorldPortlet class by creating the file HelloWorldPortlet.properties. Place the following lines in the file, and save the file as $GLASSFISH_HOME/domains/domain1/applications/j2ee-modules/hello/WEB-INF/classes/ HelloWorldPortlet.properties.

      javax.portlet.title = Hello World
      javax.portlet.short-title = Hello World
      javax.portlet.keywords = Hello,World
      

    4. Create a deployment descriptor by creating the file web.xml. Place the following lines in the file, and save the file as $GLASSFISH_HOME/domains/domain1/applications/j2ee-modules/hello/WEB-INF/web.xml.

      <?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>Sample Hello World Portlet</display-name>
          <servlet>
              <servlet-name>HelloWorldPortlet</servlet-name>
              <display-name>HelloWorldPortlet Wrapper</display-name>
              <servlet-class>org.apache.pluto.core.PortletServlet</servlet-class>
              <init-param>
                  <param-name>portlet-guid</param-name>
                  <param-value>hello.HelloWorldPortlet</param-value>
              </init-param>
              <init-param>
                  <param-name>portlet-class</param-name>
                  <param-value>sample.HelloWorldPortlet</param-value>
              </init-param>
          </servlet>
          <servlet-mapping>
              <servlet-name>HelloWorldPortlet</servlet-name>
              <url-pattern>/HelloWorldPortlet/*</url-pattern>
          </servlet-mapping>
      </web-app>
      

    5. Create a deployment descriptor by creating the file portlet.xml. Place the following lines in the file, and save the file as $GLASSFISH_HOME/domains/domain1/applications/j2ee-modules/hello/WEB-INF/portlet.xml.

      <?xml version="1.0" encoding="UTF-8"?>
      <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/
                                       portlet-app_1_0.xsd/etc/opt/SUNWportal/dtd/portlet.xsd"
                   version="1.0">
          <portlet>
              <portlet-name>HelloWorldPortlet</portlet-name>
              <portlet-class>sample.HelloWorldPortlet</portlet-class>
              <expiration-cache>0</expiration-cache>
              <resource-bundle>HelloWorldPortlet</resource-bundle>
              <supports>
                  <mime-type>text/html</mime-type>
                  <portlet-mode>VIEW</portlet-mode>
              </supports>
              <portlet-info>
                  <title>HelloWorldPortlet</title>
              </portlet-info>
          </portlet>
      </portlet-app>
      

     
    Modifying the Pluto Configuration

    The following steps modify the Pluto web application and remove the test suite web application from Pluto. The test suite does not work with portlets.

    1. Register HelloWorldPortlet for use with Pluto by editing the following file:

      $PLUTO_INSTALL_DIR/pluto-1.0.1/webapps/pluto/WEB-INF/data/
      portletentityregistry.xml
      

      Add the following lines to the file, just before the final tag.

      <application id="6">
          <definition-id>hello</definition-id>
          <portlet id="0">
              <definition-id>hello.HelloWorldPortlet</definition-id>
          </portlet>
      </application>
      

      Tags in the portletentityregistry.xml file have the following meanings:

      • <application> Defines one application (corresponding to one web application on the portal container), which can contain multiple tags. The id attribute in this tag identifies the application, and it does not need to be numeric.
      • <definition-id> Within the file, refers to the name of the web application that contains the portlets in this application.
      • <portlet> Each tag defines one portlet. The id attribute in this tag identifies the portlet. The application id, concatenated with the portlet id, is mapped to the value attribute in the tag in pageregistry.xml, as explained below.

    2. Define the Pluto layout by editing the following file:

      $PLUTO_INSTALL_DIR/pluto-1.0.1/webapps/pluto/WEB-INF/data/pageregistry.xml
      

      Add the following lines to the file just before the final tag.

      <fragment name="hello" type="page" >
          <navigation>
              <title>Hello</title>
              <description>sample portlet</description>
          </navigation>
          <fragment name="row" type="row">
              <fragment name="col1" type="column">
                  <fragment name="p1" type="portlet">
                      <property name="portlet" value="6.0"/>
                  </fragment>
              </fragment>
           </fragment>
      </fragment>
      

    3. Define Pluto portlet contexts by editing the following file:

      $PLUTO_INSTALL_DIR/pluto-1.0.1/webapps/pluto/WEB-INF/data/portletcontexts.txt
      

      Add the following line at the end of the file.

      /hello
      

    4. Remove the test suite from the Pluto configuration.

      1. Remove the test suite configuration from the $PLUTO_INSTALL_DIR/pluto-1.0.1/webapps/pluto/WEB-INF/data/pageregistry.xml file. Open the file in an editor and remove the following lines.

        <fragment name="test" type="page">
                <navigation>
                    <title>Test</title>
                    <description>...</description>
                </navigation>
                <fragment name="row" type="row">
                    <fragment name="col1" type="column">
                        <fragment name="p1" type="portlet">
                            <property name="portlet" value="3.1"/>
                        </fragment>
                        <fragment name="p2" type="portlet">
                            <property name="portlet" value="4.1"/>
                        </fragment>
                    </fragment>
                </fragment>
        </fragment>
        

      2. Remove the test suite configuration from the $PLUTO_INSTALL_DIR/pluto-1.0.1/webapps/pluto/WEB-INF/data/portletentityregistry.xml file. Open the file in an editor and remove the following lines.

        <application id="4">
            <definition-id>testsuite</definition-id>
            <portlet id="1">
                <definition-id>testsuite.TestPortlet2</definition-id>
                <preferences>
                    <pref-name>TestName4</pref-name>
                    <pref-value>TestValue4</pref-value>
                    <read-only>true</read-only>
                </preferences>
            </portlet>
        </application>
        

      3. Remove the test suite configuration from the $PLUTO_INSTALL_DIR/pluto-1.0.1/webapps/pluto/WEB-INF/data/portletcontexts.txt file. Open the file in an editor and remove the following line.

        /testsuite
        

      Conclusion

      In this article you learned how to use Pluto, the reference implementation of the Java Portlet Specification, along with the GlassFish Java EE 5 application server to test a sample portlet. The article then described how to configure Pluto and GlassFish to test your own portlets and how to create your own .jar file containing Pluto elements and your own portlet for testing with GlassFish.

      References

      GlassFish Application Server

      Rate and Review
      Tell us what you think of the content of this page.
      Excellent   Good   Fair   Poor  
      Comments:
      Your email address (no reply is possible without an address):
      Sun Privacy Policy

      Note: We are not able to respond to all submitted comments.
Learn more about the Sun Java System Application Server and Project GlassFish
Read more technical articles, learn more.
 
Satish Viswanatham Satish Viswanatham, a senior engineer in the Sun Java System Application Server team, has been developing products on J2EE technology since its inception and is adept in JDBC and EJB transaction and management technologies. Currently, he focuses on transforming Application Server into an SOA engine. Since joining Sun Microsystems, Satish has applied for three patents.
 
Dean PollaDean Polla, also a member of the Sun Java System Portal Server development team, is currently involved with the open-source portal projects. He contributes to the Portlet Repository and participates in the development of several other Portal Server features, including single sign-on access and sample portal desktops.
 
Rick PalkovicRick Palkovic is a staff writer for Sun Developer Network. He has written about SunOS and Java technologies for longer than he likes to admit, composing everything from man pages to technical white papers.