Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

2.2 J2EE: The Integrated Platform for Web Services

Starting with the J2EE 1.4 platform, with its main focus on Web services, the existing Java-XML technologies are integrated into a consolidated platform in a standard way, thereby allowing applications to be exposed as Web services through a SOAP/HTTP interface. The next sections briefly describe the Web service-specific additions made in the J2EE 1.4 platform. (Chapter 1 includes an overview of the J2EE 1.4 platform. See the J2EE 1.4 specification listed in "References and Resources" on page xx for complete information on the platform.)

This section is intended to give you an overview of the various Web service-specific additions in the J2EE platform. The next three chapters cover how to use these technologies in detail.

2.2.1 Java APIs for XML Processing

JavaTM APIs for XML Processing (JAXP) is a vendor-neutral set of lightweight APIs for parsing or processing XML documents. Because XML is the common language enabling Web services, an XML parser is a necessity to process the messages—the XML documents—exchanged among Web services. Figure 2.4 depicts how the JAXP API abstracts the parser implementations from the user application.

02fig04.gifFigure 2.4 Using JAXP to Abstract Parser Implementations from User Application88380 Figure caption Figure 1.5 JAXP

Keep in mind that the JAXP API is not new to the J2EE 1.4 platform. It has been part of the earlier versions of both the J2EE and JavaTM 2 Standard Edition (J2SETM) platforms. In the J2EE 1.4 platform implementation, JAXP has added support for XML schemas.

Although it has its own reference implementation, JAXP allows JAXP specification-conforming parsers from other vendors to be plugged in. JAXP falls back to parsing an XML document using its own implementation if no other implementation is provided. JAXP processes XML documents using the SAX or DOM models, and it permits use of XSLT engines during the document processing. (XSLT, which stands for eXtensible Stylesheet Language Transformation, is used for transforming XML documents from one format to another.)

The main JAXP APIs are available through the javax.xml.parsers package, which provides two vendor-agnostic factory interfaces—one interface for SAX processing and another for DOM processing. These factory interfaces allow the use of other JAXP implementations.

Figure 2.5 shows how the SAX and DOM parsers function. SAX processes documents serially, converting the elements of an XML document into a series of events. Each particular element generates one event, with unique events representing various parts of the document. User-supplied event handlers handle the events and take appropriate actions. SAX processing is fast because of its serial access and small memory storage requirements. Code Example 2.7 shows how to use the JAXP APIs and SAX to process an XML document.

Example 2.7. Using SAX to Process an XML Document

public class AnAppThatUsesSAXForXMLProcessing
       extends DefaultHandler {

   public void someMethodWhichReadsXMLDocument() {

      // Get a SAX PArser Factory and set validation to true
      SAXParserFactory spf = SAXParserFactory.newInstance();
      spf.setValidating(true);

      // Create a JAXP SAXParser
      SAXParser saxParser = spf.newSAXParser();

      // Get the encapsulated SAX XMLReader
      xmlReader = saxParser.getXMLReader();

      // Set the ContentHandler of the XMLReader
      xmlReader.setContentHandler(this);

      // Tell the XMLReader to parse the XML document
      xmlReader.parse(XMLDocumentName);
   }
}

02fig05.gifFigure 2.5 SAX- and DOM-Based XML Parser APIs43520 Figure caption Figure 1.1 SAX- and DOM-Based XML Parser APIs

DOM processing creates a tree from the elements in the XML document. Although this requires more memory (to store the tree), this feature allows random access to document content and enables splitting of documents into fragments, which makes it easier to code DOM processing. DOM facilitates creations, changes, or additions to incoming XML documents. Code Example 2.8 shows how to use the JAXP APIs and DOM to process an XML document.

Example 2.8. Using DOM to Process an XML Document

public class AnAppThatUsesDOMForXMLProcessing {

   public void someMethodWhichReadsXMLDocument() {

      // Step 1: create a DocumentBuilderFactory
      DocumentBuilderFactory dbf =
                DocumentBuilderFactory.newInstance();
      dbf.setValidating(true);
      // Step 2: create a DocumentBuilder that satisfies
      // the constraints specified by the DocumentBuilderFactory
      db = dbf.newDocumentBuilder();

      // Step 3: parse the input file
      Document doc = db.parse(XMLDocumentFile);

      // Parse the tree created - node by node
   }
}

2.2.2 JavaTM API for XML-Based RPC

JavaTM API for XML-based RPC (JAX-RPC) supports XML-based RPC for Java and J2EE platforms. It enables a traditional client-server remote procedure call (RPC) mechanism using an XML-based protocol. JAX-RPC enables Java technology developers to develop SOAP-based interoperable and portable Web services. Developers use the JAX-RPC programming model to develop SOAP-based Web service endpoints, along with their corresponding WSDL descriptions, and clients. A JAX-RPC-based Web service implementation can interact with clients that are not based on Java. Similarly, a JAX-RPC-based client can interact with a non-Java-based Web service implementation.

For typical Web service scenarios, using JAX-RPC reduces complexity for developers by:

  • Standardizing the creation of SOAP requests and responses

  • Standardizing marshalling and unmarshalling of parameters and other runtime and deployment-specific details

  • Removing these SOAP creation and marshalling/unmarshalling tasks from a developer's responsibilities by providing these functions in a library or a tool

  • Providing standardized support for different mapping scenarios, including XML to Java, Java to XML, WSDL-to-Java, and Java-to-WSDL mappings

JAX-RPC also defines standard mappings between WSDL/XML and Java, which enables it to support a rich type set. However, developers may use types that do not have standard type mappings. JAX-RPC defines a set of APIs for an extensible type mapping framework that developers can use for types with no standard type mappings. With these APIs, it is possible to develop and implement pluggable serializers and de-serializers for an extensible mapping. Figure 2.6 shows the high-level architecture of the JAX-RPC implementation.

02fig06.gifFigure 2.6 JAX-RPC Architecture99136 Figure caption Figure 1.6 JAX-RPC Architecture

A client application can make a request to a Web service in one of three ways. Chapter 5 contains detailed descriptions of these client access approaches.

  1. Invoking methods on generated stubs— Based on the contents of a WSDL description of a service, tools can be used to generate stubs. These generated stubs are configured with all necessary information about the Web service and its endpoint. The client application uses the stubs to invoke remote methods available in the Web service endpoint.

  2. Using a dynamic proxy— A dynamic proxy supports a Web service endpoint. When this mode is used, there is no need to create endpoint-specific stubs for the client.

  3. Using a dynamic invocation interface (DII)— In this mode, operations on target service endpoints are accessed dynamically based on an in-memory model of the WSDL description of the service.

No matter which mode is used, the client application's request passes through the client-side JAX-RPC runtime. The runtime maps the request's Java types to XML and forms a corresponding SOAP message for the request. It then sends the SOAP message across the network to the server.

On the server side, the JAX-RPC runtime receives the SOAP message for the request. The server-side runtime applies the XML to Java mappings, then maps the request to the corresponding Java method call, along with its parameters.

Note that a client of a JAX-RPC service may be a non-Java client. Also, JAX-RPC can interoperate with any Web service, whether that service is based on JAX-RPC or not. Also note that developers need only deal with JAX-RPC APIs; all the details for handling SOAP happen under the hood.

JAX-RPC supports three modes of operation:

  1. Synchronous request–response mode— After a remote method is invoked, the service client's thread blocks until a return value or exception is returned.

  2. One-way RPC mode— After a remote method is invoked, the client's thread is not blocked and continues processing. No return value or exception is expected on this call.

  3. Non-blocking RPC invocation mode— A client invokes a remote procedure and continues in its thread without blocking. Later, the client processes the remote method return by performing a blocked receive call or by polling for the return value.

In addition, JAX-RPC, by specifying a standard way to plug in SOAP message handlers, allows both pre- and post-processing of SOAP requests and responses. These message handlers can intercept incoming SOAP requests and outgoing SOAP responses, allowing the service to do additional processing. See the JAX-RPC specification (listed in "References and Resources" on page xx) for more information on JAX-RPC.

Code Example 2.9 is an example of a JAX-RPC service interface for a simple service that provides weather information for a city.

Example 2.9. JAX-RPC Service Endpoint Interface Example

public interface WeatherService extends Remote {
   public String getWeather(String city) throws RemoteException;
}

Code Example 2.10 shows the implementation of the weather service interface using a Web component.

Example 2.10. JAX-RPC Service Implementation

public class WeatherServiceImpl implements
                 WeatherService, ServiceLifecycle {
   public void init(Object context) throws JAXRPCException {}

   public String getWeather(String city) {
      return ("Early morning fog clearing midday; " +
             "over all great day expected in " + city);
   }

   public void destroy() {}
}

Code Example 2.11 shows how a client, using JAX-RPC to access this weather service.

Example 2.11. A Java/J2EE Client Accessing the Weather Service

.....
Context ic = new InitialContext();
Service svc = (Service)
       ic.lookup("java:comp/env/service/WeatherService");
WeatherSvcIntf port = (WeatherSvcIntf)
       svc.getPort(WeatherSvcIntf.class);
String info = port.getWeather("New York");
.....

These examples illustrate that a developer has to code very little configuration and deployment information. The JAX-RPC implementation handles the details of creating a SOAP request, handling the SOAP response, and so forth, thereby relieving the developer of these complexities.

2.2.3 JavaTM API for XML Registries

JavaTM API for XML Registries (JAXR), a Java API for accessing business registries, has a flexible architecture that supports UDDI, and other registry specifications (such as ebXML). Figure 2.7 illustrates the JAXR architecture.

02fig07.gifFigure 2.7 JAXR Architecture72953 Figure caption Figure 1.7 JAXR Specification

A JAXR client, which can be a stand-alone Java application or a J2EE component, uses an implementation of the JAXR API provided by a JAXR provider to access business registries. A JAXR provider consists of two parts: a registry-specific JAXR provider, which provides a registry-specific implementation of the API, and a JAXR pluggable provider, which implements those features of the API that are independent of the type of registry. The pluggable provider hides the details of registry-specific providers from clients.

The registry-specific provider plugs into the pluggable provider, and acts on requests and responses between the client and the target registry. The registry-specific provider converts client requests into a form understood by the target registry and sends the requests to the registry provider using registry-specific protocols. It converts responses from the registry provider from a registry-specific format to a JAXR response, then passes the response to the client.

Refer to the JAXR specification for more information.

2.2.4 SOAP with Attachments API for JavaTM

SOAP with Attachments API for JavaTM (SAAJ), which enables developers to produce and consume messages conforming to the SOAP 1.1 specification and SOAP with Attachments note, provides an abstraction for handling SOAP messages with attachments. Advanced developers can use SAAJ to have their applications operate directly with SOAP messages. Attachments may be complete XML documents, XML fragments, or MIME-type attachments. In addition, SAAJ allows developers to enable support for other MIME types. JAX technologies, such as JAX-RPC, internally use SAAJ to hide SOAP complexities from developers.

SAAJ allows the following modes of message exchanges:

  • Synchronous request-response messaging— the client sends a message and then waits for the response

  • One-way asynchronous messaging (also called fire and forget)— the client sends a message and continues with its processing without waiting for a response

    Refer to the SAAJ specification for more information.

2.2.5 Web Service Technologies Integrated in J2EE Platform

Up to now, we have examined how the Java XML technologies support various Web service standards. Now let's see how the J2EE 1.4 platform combines these technologies into a standard platform that is portable and integrated. Not only are the Java XML technologies integrated into the platform, the platform also defines Web service-related responsibilities for existing Web and EJB containers, artifacts, and port components. The J2EE 1.4 platform ensures portability by integrating the Java XML technologies as extensions to existing J2EE containers, packaging formats, deployment models, and runtime services.

A Web service on the J2EE 1.4 platform may be implemented as follows:

  • Using a JAX-RPC service endpoint— The service implementation is a Java class in the Web container. The service adheres to the Web container's servlet lifecycle and concurrency requirements.

  • Using an EJB service endpoint— The service implementation is a stateless session bean in an EJB container. The service adheres to the EJB container's lifecycle and concurrency requirements.

In either case, the service is made portable with the definition of a port component, which provides the service's outside view for Web service implementation. A port component consists of:

  • A WSDL document describing the Web service that its clients can use

  • A service endpoint interface defining the Web service's methods that are available to clients

  • A service implementation bean implementing the business logic of the methods defined in the service endpoint interface. The implementation may be either a Java class in the Web container or a stateless session bean in the EJB container.

Container-specific service interfaces, created by the J2EE container, provide static stub and dynamic proxies for all ports. A client of a J2EE platform Web service can be a Web service peer, a J2EE component, or a stand-alone application. It is not required that the client be a Web service or application implemented in Java.

How do clients use a J2EE platform Web service? Here is an example of a J2EE component that is a client of some Web service. Such a client uses JNDI to look up the service, then it accesses the Web service's port using methods defined in the javax.xml.rpc.Service interface. The client accesses the service's functionality using its service endpoint interface. A client that is a J2EE component needs only consider that the Web service implementation is stateless. Thus, the client cannot depend on the service holding state between successive service invocations. A J2EE component client does not have to know any other details of the Web service, such as how the service interface accesses the service, the service implementation, how its stubs are generated, and so forth.

Recall (from Code Example 2.9 and Code Example 2.10) what a Web service interface, such as the weather Web service, looks like when implemented as a JAX-RPC service endpoint on a J2EE platform. In contrast, Code Example 2.12 shows the equivalent EJB service endpoint implementation for the same weather service.

Example 2.12. EJB Service Endpoint Implementation for a Weather Service

public class HelloService implements SessionBean {
   private SessionContext sc;

   public WeatherService(){}
   public void ejbCreate() {}
   public String getWeather(String city) {
      return ("Early morning fog clearing midday; " +
             "over all great day expected in " + city);
   }
   public void setSessionContext(SessionContext sc) {
      this.sc = sc;
   }
   public void ejbRemove() {}
   public void ejbActivate() {}
   public void ejbPassivate() {}
}

Keep in mind that any client can use the code shown in Code Example 2.11 to access this weather service. This holds true

  • Regardless of whether the service is implemented as a JAX-RPC service endpoint or an EJB service endpoint

  • Regardless of whether the client is a servlet, an enterprise bean, or a stand-alone Java client

2.2.6 Support for WS-I Basic Profile

So far we have seen how the various Java technologies support Web service standards. We have also examined how these Java technologies have been integrated into the J2EE platform in a standard way to ensure portability of Web service implementations across J2EE platforms. Since ensuring interoperability among heterogeneous platforms is a primary force for Web services, the J2EE platform supports the WS-I Basic Profile.

As already seen in "Emerging Standards" on page 40, WS-I is an organization that spans industries and whose charter is to create and promote interoperability of Web services. WS-I has published the WS-I Basic Profile, which dictates how a set of Web service standards should be used together to ensure interoperability. The WS-I Basic Profile covers:

  • Messaging standards (such as SOAP)

  • Description and discovery standards (such as UDDI)

  • Security

By supporting the WS-I Basic Profile, the J2EE platform is assured of providing an interoperable and portable platform for the development of Web services.

  • + Share This
  • 🔖 Save To Your Account