ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Working with Complex Data Types, Part 4
Pages: 1, 2, 3

The following code is the new IBasicTradingService interface generated by wsdl2java. The getHighLow( ) method returns an instance of HighLow_ServerSide; remember that this class is the new one generated by GLUE as part of the javasoap.book.ch5.client package, not the one being used by our server class, BasicTradingApp.



// generated by GLUE
package javasoap.book.ch5.client;
public interface IBasicTradingService
  {
  HighLow_ServerSide getHighLow( String arg0 );
  String executeStockTrade( StockTradeServer arg0 );
  String[] getMostActive(  );
  int getTotalVolume( String[] arg0 );
  String executeTrade( Object[] arg0 );
  }

The next class, HighLow_ServerSide, is simply a Java data structure reflecting the data fields that will be mapped to the HighLow custom type. I added the toString( ) method by hand to make it simpler to display the results.

package javasoap.book.ch5.client;
public class HighLow_ServerSide {
  public float _high;
  public float _low;
  public String toString(  ) {
     return "High: " + _high +
        " Low: " + _low;
  }  
}

Now let's create a client application using GLUE that invokes the getHighLow service method and displays the contents of the resulting return value. There's not much to this, really; we simply read the map file, perform the bind, and then call the getHighLow method on the bound interface. The resulting instance of javasoap.book.ch5.client.HighLow_ServerSide is passed to System.out.println( ) for display. Just as in the Apache SOAP example, the toString( ) method of the class handles the creation of the display string.

package javasoap.book.ch5.client;
import electric.registry.RegistryException;
import electric.registry.Registry;
import electric.xml.io.Mappings;
public class HighLowClient2 {
   public static void main(String[] args) throws Exception 
   {
      try {
        Mappings.readMappings("BasicTradingService.map");
        IBasicTradingService srv = (IBasicTradingService)Registry.bind(
          "http://georgetown:8004/glue/urn:BasicTradingService.wsdl",
          IBasicTradingService.class);
        HighLow_ServerSide hilo = srv.getHighLow("ANY");
        System.out.println(hilo);
    }
    catch (RegistryException e)
    {
       System.out.println(e);
    }
  }
}

Here's the SOAP envelope returned from the server. You should be able to follow this by now. GLUE uses a reference to a separately serialized instance of the custom type, just as it did when returning an array.

<soap:Envelope 
 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
 xmlns:xsd='http://www.w3.org/2001/XMLSchema'   
 xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' 
 xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
 soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
    <soap:Body>
      <n:getHighLowResponse xmlns:n='urn:BasicTradingService'>
        <Result href='#id0'/>
      </n:getHighLowResponse>
      <id0 id='id0' soapenc:root='0' 
        xmlns:ns2='http://www.themindelectric.com/package/javasoap.book.ch5/'
        xsi:type='ns2:HighLow_ServerSide'>
        <_high xsi:type='xsd:float'>110.375</_high>
        <_low xsi:type='xsd:float'>109.5</_low>
      </id0>
    </soap:Body>
</soap:Envelope>

In this chapter, we've taken an in-depth look at the use of arrays and custom data types, and discussed how these structures are supported by Apache SOAP and GLUE. There are other useful types that you may find support for in these, or other, SOAP implementations. These types might include Java Vector and Hashtable classes, Java collections, and a variety of other commonly used Java classes.

We've seen that Apache SOAP and GLUE approach complex types in different ways, and both do a good job of supporting them. However, there may be times when you need to work with a custom type that either can't, or shouldn't, be serialized in the way provided by your SOAP implementation. This situation may arise because there simply is no support for a particular type of data, as is the case with multidimensional or sparse arrays, or perhaps for some other reason related to your application. We'll tackle this issue in the next chapter.

Robert Englander is Principal Engineer and President of MindStream Software, Inc. (www.mindstrm.com). He provides consulting services in software architecture, design, and development, as well as developing frameworks for use on client projects.


Return to ONJava.com.