Web services in Java - Axis  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Java
Written by Adi Bach   
Wednesday, 30 May 2007

A variety of frameworks are available for using web services in Java. The one we’ll use is the open-source Axis (Apache Extensible Interaction System) framework from Apache.


 

You can download it at http://ws.apache.org/axis. It is the successor to Apache’s SOAP version 2.

The developers of that package realized that it had some shortcomings that were irreparable; the ground-up rewrite is Axis.

Axis includes:

 A simple stand-alone server (primarily for testing)
 A server that plugs into servlet engines such as Tomcat
 Extensive support for WSDL
 An emitter tool that generates Java classes from WSDL
 Some sample programs
 A tool for monitoring TCP/IP packets

Architecture of Axis

The requestor is any client that makes a request over any of the protocols supported by Axis. Generally,
HTTP is used. The requestor may be a desktop application, another web application, or another web service. The Axis engine acts as a facilitator between the client and the web service method, managing the translation to and from web services standards.

Axis allows the developer to define a series of handlers, tied to either the request or the response. These handlers are similar to filter servlets; each handler performs a specific task and forwards to the next handler in line.

Handlers fit together into a chain, which is a specific set of handlers that a web service request or a response traverses.

Examples of handlers are security components, logging systems, and transformations.

One special handler, known as the pivot point handler, exists for every web service. This handler performs the actual invocation of the web service method. It is the point at which the request becomes the response. In other words, this is where the content defined by the method called as a web service is sent back to the requesting client.

Handler chains are defined in a configuration document used by the Axis engine named (by default) server.config. This file is an XML document that defines configuration parameters for how Axis behaves.

Axis tools

Axis comes with tools that make it easy to develop web services in Java. The first is a facility for creating simple web services; Axis creates the entire infrastructure for you. The other tools are transformation tools that take WSDL definitions and convert them to Java classes, and vice versa.
Simple web services

To create a simple web service, you can develop a web application that includes Axis and a publicly accessible file (available from the web application’s root directory) with a .jws extension. Note that this isn’t a standard extension—both Axis and WebLogic use this extension to define simple web services, but they aren’t compatible.The next listing shows a simple Java source file that Axis will convert into a web service.

public class Simple {
public String sayHello(String name) {
return "Hello, " + name;
}
}

When someone invokes the simple service via HTTP, Axis automatically builds the necessary infrastructure to make it accessible as a web service. Like a JSP, Axis generates a Java source file for the web service, compiles it, and redirects the request to it automatically.

Another facility offered by Axis is the automatic generation of a WSDL file for any web service (not just the ones created with a JWS file).

To access the WSDL for the simple web service defined above, simply invoke it with ?WSDL tagged onto the end of the URI.

WSDL2Java

To call a web service that already exists, you must generate Java interfaces and helper classes. Axis includes a tool called WSDL2Java that takes a WSDL file and generates all the necessary Java classes to call the web service.

If you are familiar with RMI or CORBA, this process is exactly like running the rmic or idl2java compilersused for those distributed platforms. This similarity is not just skin deep.

Every distributed architecture must supply a tool that allows you to translate back and forth between the native language representation of classes and objects and the distributed format for the same constructs.

Let’s look at an example of what WSDL2Java produces. Consider the WSDL file generated from the simple web service defined. Running WSDL2Java on that document produces four Java source files, summarized in the next table:

Java Source File Description

Simple.java The remotable interface that describes the method available through
this web service.

SimpleService.java The Java interface that defines methods that return an instance of a
class implementing the Simple interface defined in Simple.java.

SimpleServiceLocator.java A utility class for locating, binding, and returning a class that implements
SimpleService.java. This is the class you call to bind to the
web service.

SimpleSoapBindingStub.java A class that implements all the details of actually calling the web service
from the client.

As you can see, WSDL2Java performs a lot of work on your behalf. Fortunately, you don’t have to understand anything about the internal structure of those generated classes. The only ones you care about are the interface and the ServiceLocator.

The interface (Simple.java) appears in the next listing.

/** * Simple.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/
package localhost;
public interface Simple extends java.rmi.Remote {
public java.lang.String sayHello(java.lang.String name)
throws java.rmi.RemoteException;
}

The ServiceLocator class implements the methods you call to retrieve a class that implements the interface.

The SimpleServiceLocator classappears in next listing:

package localhost;
public class SimpleServiceLocator
extends org.apache.axis.client.Service
implements localhost.SimpleService {
// Use to get a proxy class for Simple
private final java.lang.String Simple_address =
"http://localhost:8080/axis/Simple.jws";

public String getSimpleAddress() {
return Simple_address;
}

public localhost.Simple getSimple()
throws javax.xml.rpc.ServiceException {
java.net.URL endpoint;
try {
endpoint = new java.net.URL(Simple_address);
} catch (java.net.MalformedURLException e) {
return null;
}
return getSimple(endpoint);
}

public localhost.Simple getSimple(java.net.URL portAddress)
throws javax.xml.rpc.ServiceException {
try {
return new localhost.SimpleSoapBindingStub(
portAddress, this);
} catch (org.apache.axis.AxisFault e) {
return null; // ???
}
}


public java.rmi.Remote getPort(Class serviceEndpointInterface)
throws javax.xml.rpc.ServiceException {
try {
if (localhost.Simple.class.isAssignableFrom(
serviceEndpointInterface))
return new localhost.SimpleSoapBindingStub(
new java.net.URL(Simple_address), this);
} catch (Throwable t) {
throw new javax.xml.rpc.ServiceException(t);
}
throw new javax.xml.rpc.ServiceException(
"There is no stub implementation: " +
((serviceEndpointInterface == null)
? "null"
: serviceEndpointInterface.getName()));
}
}


Java2WSDL
Axis also includes the Java2WSDL utility, which takes a Java interface and generates the necessary WSDL to implement it as a web service.

Using this utility is certainly more complex than letting the Axis framework automatically generate the WSDL for you.

Use it in cases where you need to customize the WSDL or the deployment options for your web service.


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Sunday, 07 September 2008 )
 
< Prev   Next >