Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Deploy Web services in Apache Geronimo

Walk through the process with Amazon Web services

Kunal Mittal (kunal@kunalmittal.com), Portal/J2EE architect, consultant, freelance writer
Kunal Mittal is a consultant specializing in Java technology, J2EE, and Web services technologies. He is the co-author of and has contributed to several books on these topics. Kunal is currently working on a portal project for Sony Pictures Entertainment. For more information, visit the author's Web site.

Summary:  Deciding on an application server to support your Web services development efforts? Meet the Apache Geronimo application server, one of the latest projects from the Apache Software Foundation. Java™ specialist Kunal Mittal introduces you to Geronimo's Web services capabilities by showing you how to write and develop standard J2EE Web services code in Geronimo. You'll learn how to consume Amazon Web services using Apache Axis as the underlying Simple Object Access Protocol (SOAP) implementation, and you'll see how to use a simple JavaServer Pages (JSP)-based client to access the Web service.

Date:  28 Jun 2005
Level:  Intermediate
Also available in:   Russian  Japanese

Activity:  9709 views
Comments:  

Geronimo supports Web services standards

The new Apache Geronimo project is a powerful open source J2EE application server built on top of J2EE 1.4 standards. It uses all open source implementations and will soon be J2EE compliant. Interestingly, Geronimo is built upon a collection of code gathered from many other open source projects. Geronimo uses Apache Axis and Apache Scout (see Resources) to support the following Web services standards:

  • Java Specification request (JSR) 109 (implementing Enterprise Web Services 1.1)
  • Java API for XML-based Remote Procedure Call (JAX-RPC)
  • SOAP with Attachments API for Java (SAAJ) 1.2
  • Java API for XML Registries (JAXR) 1.0

Support for these standards makes Geronimo a viable option when deciding on an application server to support your Web services development efforts. If you've already used the open source projects that make up Geronimo, the transition to Geronimo as your deployment container should come naturally. To make development and deployment of J2EE applications on Geronimo even easier, several Eclipse plug-ins are also available. These plug-ins are part of the Eclipse Web Tools Platform (WTP) (scheduled to have its 1.0 launch in July 2005). See Resources for links to the WTP and related information.

Using Amazon Web Services (AWS) as an example, you'll learn how to consume Web services with Geronimo. You'll be guided through taking the Web Services Description Language (WSDL) for AWS and using Apache Axis to build the consumer code. You'll see a simple Java class that consumes the Web service and learn how this class is invoked from a JSP file to display the results of the Web service. Finally, you'll bundle the code as a J2EE WAR file and deploy it on Geronimo.


Consuming Amazon Web Services

To consume AWS, you need to build the service consumer using Apache Axis as the SOAP implementation. (Axis is the underlying SOAP implementation that Geronimo supports.) If you have done this in the past and have the code, feel free to jump ahead.

At the time of this writing, Geronimo is not a production-ready application server, but it is going through the J2EE certification process. When released, it will be J2EE 1.4 certified.

Requirements for generating AWS code

Begin by setting up your environment. To generate the AWS consumer code, you need four things:

  • Java software development kit (JDK) 1.4.2 or later. (See Resources to link to the Java Web site.)
  • Apache Axis 1.1 or later. (See Resources for the Apache Axis Web link.)
  • The Web Services Description Language (WSDL) for the Web service. (See Resources for a link to download the WSDL.)
  • An AWS subscription ID from Amazon so you can use their Web services. (See Resources for a link to register for the free ID.)

Set up your environment

The next series of steps guides you through setting up your environment by downloading the elements listed above as follows:

  • Install the JDK into C:\jdk_142_05. Set the JAVA_HOME to this directory.
  • Extract Apache Axis into C:\axis1-2, and define AXIS_HOME as this directory.
  • Copy the WSDL file into the AXIS_HOME directory.
  • Register for the AWS subscription ID.

Now that you have the basic environment ready to go, you can start generating the code to consume the Web service.

Generate the Java code from the WSDL

First, generate the Java code that will consume the Web service from the WSDL file. Apache Axis comes with a utility called WSDL2Java that performs this task for you. To run this tool, make sure the following Java Archive (JAR) files are in your classpath. The sample setenv.bat script shown in Listing 1 does this for you.


Listing 1. Setenv.bat
set AXIS_HOME=c:\axis-1-2
set CLASSPATH=.
set CLASSPATH=%AXIS_HOME%\lib\axis.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\commons-discovery.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\commons-logging.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\jaxrpc.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\saaj.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\log4j-1.2.8.jar;%CLASSPATH%
set CLASSPATH=%AXIS_HOME%\lib\wsdl4j.jar;%CLASSPATH%

Now, from the AXIS_HOME directory, run the following command to generate the Java code:

java org.apache.axis.wsdl.WSDL2Java AWSECommerceService.wsdl

This process takes a few seconds to create a directory called com under AXIS_HOME. Now you can start creating a Web Archive (WAR) file that you will eventually deploy on Geronimo.

Create a directory called C:\amazon-client. You'll use this directory to store the code for the WAR file. Under this directory, create a directory called WEB-INF and a directory called src. Copy the com directory from your AXIS_HOME to C:\amazon-client\WEB-INF\src.

This code is the basic code for consuming a Web service. For now, you don't need to go into the details of this code.

Write code to consume the Web service

Next, write a client to the Web service. In this example, you'll write a simple Java class that calls the appropriate classes that were generated using WSDL2Java. (Tools such as Eclipse and IBM® Rational® Application Developer generate a stubbed version of this code for you automatically. Thus, I won't spend much time on the specifics of how to write this code.)

For simplicity, I created a class called AmazonClient in the same package structure that was generated using WSDL2Java (com.amazon.xml.AWSECommerceServer). The code for this class is shown in Listing 2. This class exposes a single method called lookupISBN. As the name suggests, this method calls AWS and returns information about the book represented by the ISBN number passed in.


Listing 2. AmazonClient.java
package com.amazon.xml.AWSECommerceServer;

import java.lang.*; 
import java.util.*;

public class  AmazonClient {

    public AmazonClient() {    }

    public Items[] lookupISBN(String isbn) throws Exception {

        try {

            System.out.println("Given ISBN is " + isbn);

            AWSECommerceServiceLocator locator = 
                    new AWSECommerceServiceLocator(); 

            AWSECommerceServicePortType type = 
                    locator.getAWSECommerceServicePort(); 

            String itemId[] = {isbn.trim()}; 
            ItemLookup lookup = new ItemLookup(); 
            lookup.setAssociateTag("MY ID");   // fill in your own 
            lookup.setSubscriptionId("MY ID"); // fill in your own 
            ItemLookupRequest lookupReq = new ItemLookupRequest(); 
            lookupReq.setMerchantId("All"); 
            lookupReq.setItemId(itemId); 
            lookupReq.setResponseGroup(new String[] 
            {"Medium", "OfferFull", "Variations", "Images"}); 
            ItemLookupRequest[] requests = lookup.getRequest(); 
            requests = new ItemLookupRequest[1]; 
            requests[0] = lookupReq; 
            lookup.setRequest(requests); 
            ItemLookupResponse response = 
                     type.itemLookup(lookup); 
            Items[] items = response.getItems(); 
            if (items != null && items.length > 0) { 
                System.out.println("Number of results "+ items.length); 
                return items; 
            } 
        } catch (javax.xml.rpc.ServiceException se) { 
            throw new Exception(se.getMessage()); 
        } 
        return new Items[0]; 
    } 
}

You must register with Amazon to get a subscription ID (see Consuming Amazon Web Services above). After you get your subscription ID, replace it in the code in Listing 2.

Now you're ready to compile this code. After you have run the setenv.bat file, you should be able to compile the code easily by running javac *.java:. You get several class files in this directory. For simplicity of packaging, I copy the entire src directory into a new directory called WEB-INF/classes. Then, from WEB-INF/classes, I delete all the Java source files; from WEB-INF/src, I delete all the class files. If you use an integrated development environment (IDE), such as Eclipse, a lot of this manual work is done for you automatically.

Now you can invoke your Web service and see the results. Use a JSP file to do this.

Write a JSP file to display results from the Web service

Under the amazonclient directory, create a JSP file called searchAmazon.jsp. The code is shown in Listing 3.

In the JSP file shown in Listing 3, you're calling the AmazonClient class that you defined and receiving an array of Item objects. You then iterate over this array to display the values.

The point here is not to demonstrate good J2EE coding practices, but rather to demonstrate that all the code you're writing is standard J2EE code that can easily be deployed on the IBM WebSphere®; platform, Apache Tomcat, Apache Geronimo, JBoss, or BEA WebLogic.

You now have all the code you need.

Final steps before deploying the code

The last step is to set up the deployment descriptors so you can deploy the WAR file on Geronimo. Under the WEB-INF directory, create two simple XML deployment descriptors. The first is the standard J2EE WAR deployment descriptor, called web.xml (see Listing 4).


Listing 4. web.xml
<?xml version="1.0"  encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_3.xsd"
    version="2.3">
 
  <display-name>Amazon Sample</display-name>
  <welcome-file-list>
      <welcome-file>t;searchAmazon.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Geronimo requires a geronimo-jetty.xml descriptor. The code is shown in Listing 5.


Listing 5. geronimo-jetty.xml descriptor
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app
xmlns="http://geronimo.apache.org/xml/ns/web/jetty"
xmlns:naming="http://geronimo.apache.org/xml/ns/naming"
configId="amazonclient"
parentId="amazonClient">
   <context-root>/amazonclient</context-root>
   <context-priority-classloader>true</context-priority-classloader>
</web-app>

Now you can save this file as a WAR file called amazon.war. To do so, run the following command from the amazonclient directory:

jar cf amazon.war 

Now you're ready to deploy this code on Geronimo.


Deploy the consumer code on Geronimo

Although you built a standard J2EE WAR file, you could easily deploy this WAR file on any application server (or servlet engine) that you choose. The next sections show you how to deploy this code on Geronimo.

Install Geronimo

Begin by downloading and installing Geronimo. (See Resources to link to the download site.) Download the Geronimo 1.0 M3 Installer. To run the installer, use the following command:

java -jar geronimo-1.0-M3-installer.jar

Follow the steps to install Geronimo into GERONIMO_HOME, defined as C:\geronimo.

Start Geronimo

To start the server, from the GERONIMO_HOME directory, run the following command:

java -jar bin/server.jar

Deploy the Amazon.war file

You should have all the JAR files defined in the setenv batch file in your classpath. Copy the files from AXIS_HOME\lib into GERONIMO_HOME\lib. Many of these JAR files will already exist in GERONIMO_HOME\lib. Do not overwrite the files that exist, even if the files you're copying are later versions of the same file. Click No when prompted to replace existing files.

To deploy the amazon.war file on the application server, copy the WAR file into GERONIMO_HOME. From this directory, run the following command:

java -jar bin/deployer.jar deploy amazon.war

Run searchAmazon.jsp

The last task left is to run the JSP file you wrote. Open a browser and type http://localhost:8080/amazon/searchAmazon.jsp in the address bar.

Figure 1 shows the result. You can modify the JSP file to include a form field in which you can enter an ISBN number and get a result.


Figure 1: Execution of searchAmazon.jsp
searchAmazon.jsp

You have now successfully deployed your first Web services consumer in Geronimo.


Standard J2EE Web services

The example in this article is simple, but it has shown that Geronimo supports standard J2EE Web services. As an exercise in using Web services, you can try out other options available in AWS. For example, change the ISBN search to be a title or author search. Then, from the Web service results, figure out how to display a URL that links to Amazon.com, and show an image of the book.

Another interesting exercise is to use other J2EE technologies along with Apache Axis to consume Web services, and then deploy them in Geronimo. For example, replace the AmazonClient.java Plain Old Java Object (POJO) with a stateless session Enterprise JavaBean (EJB) component, and then deploy it in Geronimo.

You can take any Web service from webMethods or the Google Web services as an example. If you apply the same steps described in this article, you can quickly deploy a consumer to these Web services in Geronimo.


Summary

Now that you've met Geronimo and seen its Web services capabilities, you should be ready to get started using the Geronimo application server in your Web services development efforts.


Resources

About the author

Kunal Mittal is a consultant specializing in Java technology, J2EE, and Web services technologies. He is the co-author of and has contributed to several books on these topics. Kunal is currently working on a portal project for Sony Pictures Entertainment. For more information, visit the author's Web site.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, Java technology, SOA and Web services, WebSphere
ArticleID=87240
ArticleTitle=Deploy Web services in Apache Geronimo
publish-date=06282005
author1-email=kunal@kunalmittal.com
author1-email-cc=ruterbo@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers