XML Schema Data Typing (cont.) - Page 10
April 10, 2002
Automatically invoking array services
Once you move beyond basic data types, the simple WSDL
invocation methods described previously in this chapter no longer work quite
as easily. For example, you cannot simply open the GLUE console, pass an array
of strings, and hope to receive back an array of doubles. Additional work is
necessary, and some manual code is required. Nonetheless, the additional work
is minimal, and the discussion that follows focuses on the GLUE platform. We
have chosen to focus on the GLUE platform because it represents the most
elegant platform for working with complex data types; other tools, such as the
IBM Web Services Toolkit, do, however, provide similar functionality.
To get started, you should become familiar with the GLUE wsdl2java command-line tool. The tool takes in a WSDL
file and generates a suite of Java class files to automatically interface with
the specified service. You can then write your own Java class to invoke the
specified service. Best of all, the code you write is minimally simple, and
all SOAP-specific details are completely hidden from your view. (See Figure
6-12.)
Figure 6-12. The GLUE wsdl2java tool and the GLUE architecture
|
|
Here is the wsdl2java command-line
usage: usage: wsdl2java <arguments>
where valid arguments are:
http://host:port/filename URL of WSDL
-c checked exceptions
-d directory output directory for files
-l user password realm login credentials
-m map-file read mapping instructions
-p package set default package
-v verbose
-x command-file command file to execute
Complete information on each argument is available online within
the GLUE User Guide at http://themindelectric.com/docs/glue/guide/index.html.
For now, we will focus on the most basic arguments. For example, to generate
Java class files for the PriceListService.wsdl file,
first make sure that the WSDL file is available publicly on a web site or
locally via a web server such as Tomcat. Then, issue the following command:
wsdl2java.bat http://localhost:8080/wsdl/PriceListService.wsdl -p com.
ecerami.wsdl.glue
The first argument specifies the location of the WSDL file; the
second argument specifies that the generated files should be placed in the
package com.ecerami.wsdl.glue.
GLUE will automatically download the specified WSDL file and
generate two Java class files: write file IPriceList_Service.java
write file PriceList_ServiceHelper.java
The first file, IPriceList_Service.java,
is shown in Example
6-7. This file represents a Java interface that mirrors the public methods
exposed by the WSDL file. Specifically, the interface shows a getPriceList( ) method that receives an array of String values, and returns an array of double values.
Example 6-7: IPriceList_Service.java
// generated by GLUE
package com.ecerami.wsdl.glue;
public interface IPriceList_Service
{
double[] getPriceList( String[] sku_list );
}
The second file, PriceList_ServiceHelper.java, is shown in Example
6-8. This is known as a GLUE helper file, and it can dynamically bind to
the service specified by the WSDL file. To access the service, simply call the
static bind( ) method.
Example 6-8: PriceList_ServiceHelper.java
// generated by GLUE
package com.ecerami.wsdl.glue;
import electric.registry.Registry;
import electric.registry.RegistryException;
public class PriceList_ServiceHelper
{
public static IPriceList_Service bind( ) throws RegistryException
{
return bind( "http://localhost:8080/wsdl/PriceListService.wsdl" );
}
public static IPriceList_Service bind( String url )
throws RegistryException
{
return (IPriceList_Service)
Registry.bind( url, IPriceList_Service.class );
}
}
Once GLUE has generated the interface and helper files, you just
need to write your own class that actually invokes the service. Example
6-9 shows a sample application that invokes the Price List Service. The
code first calls PriceList_ServiceHelper.bind( ),
which then returns an IPriceList_Service object.
All subsequent code behaves as if the Price List Service is a local object,
and all SOAP-specific details are completely hidden from the developer.
Here is a sample output of the Invoke_PriceList application:
Product Catalog
SKU: A358185 --> Price: 54.99
SKU: A358565 --> Price: 19.99
Example 6-9: Invoke_PriceList.java
package com.ecerami.wsdl;
import com.ecerami.wsdl.glue.*;
/**
* SOAP Invoker. Uses the PriceListServiceHelper to invoke
* SOAP service. PriceListServiceHelper and IPriceListService
* are automatically generated by GLUE.
*/
public class Invoke_PriceList {
/**
* Get Product List via SOAP
*/
public double[] getPrices (String skus[]) throws Exception {
IPriceList_Service priceListService =
PriceList_ServiceHelper.bind( );
double[] prices = priceListService.getPriceList(skus);
return prices;
}
/**
* Main Method
*/
public static void main (String[] args) throws Exception {
Invoke_PriceList invoker = new Invoke_PriceList( );
System.out.println ("Product Catalog");
String skus[] = {"A358185", "A358565" };
double[] prices = invoker.getPrices (skus);
for (int i=0; i<prices.length; i++) {
System.out.print ("SKU: "+skus[i]);
System.out.println (" --> Price: "+prices[i]);
}
}
}
Note:Color coded lines have been split for display purposes
XML Schema Data Typing (cont.) - Page 9
Web Services Essentials
XML Schema Data Typing (cont.) - Page 11
|