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

advertisement

AddThis Social Bookmark Button

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

To deploy this version of the BasicTradingService class in GLUE, we can use our old BasicTradingApp class. We can modify the Java interface, IBasicTradingService, to include the new method:



package javasoap.book.ch5;
public interface IBasicTradingService {
  int getTotalVolume(String[] symbols);
  String executeTrade(Object[] params);
  String[] getMostActive(  );
}

Now we modify the application BasicTradingClient to include a call to the getMostActive( ) method, and then iterate over the values in the array and print them out. When using GLUE we don't have to cast the return value to a String[] because, unlike the Apache SOAP example, the getMostActive( ) method of the interface is defined to return the proper type. Here's the modified code:

package javasoap.book.ch5;
import electric.registry.RegistryException;
import electric.registry.Registry;
public class BasicTradingClient {
   public static void main(String[] args) throws Exception 
   {
      try {
        IBasicTradingService srv = (IBasicTradingService)Registry.bind(
          "http://georgetown:8004/glue/urn:BasicTradingService.wsdl",
          IBasicTradingService.class);
        String[] stocks = { "MINDSTRM", "MSFT", "SUN" };
        int total = srv.getTotalVolume(stocks);
        System.out.println("Total Volume is " + total);
        Object[] multiParams = { "MINDSTRM", new Integer(100), 
                                    new Boolean(true) };
        String desc = srv.executeTrade(multiParams);
        System.out.println("Trade Description: " + desc);
        String[] actives = srv.getMostActive(  );
        int cnt = actives.length;
        for (int i = 0; i < cnt; i++) {
           System.out.println(actives[i]);
        }
    }
    catch (RegistryException e)
    {
       System.out.println(e);
    }
  }
}

If you run this example, you'll get the following output:

Total Volume is 345000
Trade Description: Buy 100 of MINDSTRM
ABC
DEF
GHI
JKL

GLUE uses the same serialization technique for arrays as return values that we saw earlier for array parameters; it uses a reference to a separately serialized array as the actual return value, and it references the actual array data. The SOAP envelope returned when invoking the getMostActive( ) method is:

<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:getMostActiveResponse   
        xmlns:n='urn:BasicTradingService'>
         <Result href='#id0'/>
      </n:getMostActiveResponse>
      <id0 id='id0' soapenc:root='0' 
        xmlns:ns2='http://www.themindelectric.com/package/java.lang/' 
        xsi:type='soapenc:Array' soapenc:arrayType='xsd:string[4]'>
         <i xsi:type='xsd:string'>ABC</i>
         <i xsi:type='xsd:string'>DEF</i>
         <i xsi:type='xsd:string'>GHI</i>
         <i xsi:type='xsd:string'>JKL</i>
      </id0>
    </soap:Body>
</soap:Envelope>

In the next installment, learn passing custom types as parameters.

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.


View catalog information for Java and SOAP

Return to ONJava.com.