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

advertisement

AddThis Social Bookmark Button

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

Now let's create a StockTrade_ServerSide class to represent the stock trade on the server side. Just to be sure that this class is different from its client-side counterpart, let's eliminate the constructor that takes parameters. And for good measure, let's also change the names of the class variables and the order in which they appear.




package javasoap.book.ch5;
public class StockTrade_ServerSide {
   int      _shares;
   boolean  _buyOrder;
   String   _stock;
   public StockTrade_ServerSide(  ) {
   }
   public String getSymbol(  ) {
      return _stock;
   }
   public void setSymbol(String stock) {
      _stock = stock;
   }
   public boolean isBuy(  ) {
      return _buyOrder;
   }
   public void setBuy(boolean buyOrder) {
      _buyOrder = buyOrder;
   }
   public int getNumShares(  ) {
      return _shares;
   }
   public void setNumShares(int shares) {
      _shares = shares;
   }
}

We can add a new method to the urn:BasicTradingService service called executeStockTrade( ), which takes a stock trade as a parameter. The return value from this method is a string that describes the order. Here's the modified BasicTradingService class. We can take advantage of the executeTrade( ) method that already exists in this class. In the new method, executeStockTrade( ), we build an Object array from the three properties of the trade parameter, and we pass that array to the executeTrade( ) method.


package javasoap.book.ch5;
public class BasicTradingService {
   
   public BasicTradingService(  ) {
   }
   public String executeStockTrade(StockTrade_ServerSide trade) {
      Object[] params = new Object[3];
      params[0] = trade.getSymbol(  );
      params[1] = new Integer(trade.getNumShares(  ));
      params[2] = new Boolean(trade.isBuy(  ));
      return executeTrade(params);
   }
   public String[] getMostActive(  ) {
   
      // get the most actively traded stocks
      String[] actives = { "ABC", "DEF", "GHI", "JKL" };
      return actives;
   }
   public int getTotalVolume(String[] stocks) {
      
      // get the volumes for each stock from some
      // data feed and return the total
      
      int total = 345000; 
      return total;
   }
   public String executeTrade(Object[] params) {
      String result;
      try {
         String stock = (String)params[0];
         Integer numShares = (Integer)params[1];
         Boolean buy = (Boolean)params[2];
         String orderType = "Buy";
         if (false == buy.booleanValue(  )) {
            orderType = "Sell";
         }
         result = (orderType + " " + numShares + " of " + stock);
      }
      catch (ClassCastException e) {
         result = "Bad Parameter Type Encountered";
      }
      return result;
   }
}

To deploy the service, we need to map the custom type to the Java class that implements that type. We need to give the custom type a name and qualify it with an appropriate namespace. This is not unlike the process we'd use to declare a service. This mapping takes place in the deployment descriptor, within the isd:mappings section. Here's the deployment descriptor we'll use to deploy the service in Apache SOAP:


<isd:service 
    xmlns:isd="http://xml.apache.org/xml-soap/deployment"
    id="urn:BasicTradingService">
  <isd:provider 
     type="java"
     scope="Application"
     methods="getTotalVolume getMostActive executeTrade executeStockTrade">
    <isd:java 
       class="javasoap.book.ch5.BasicTradingService" 
       static="false"/>
  </isd:provider>
  
  <isd:faultListener>org.apache.soap.server.DOMFaultListener
  </isd:faultListener>
  <isd:mappings>
    <isd:map  
       encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
       xmlns:x="urn:BasicTradingService" qname="x:StockTrade"
       javaType="javasoap.book.ch5.StockTrade_ServerSide"
       java2XMLClassName="org.apache.soap.encoding.soapenc.BeanSerializer"
       xml2JavaClassName="org.apache.soap.encoding.soapenc.BeanSerializer"/>
  </isd:mappings>    
</isd:service>

This is important, so let's summarize the mapping before we go any further. The mapping of a custom type to a Java class is associated with an encoding style as well as a fully qualified type name. A Java class that implements the custom type is specified, as well as the utility classes used to perform serialization and deserialization. Now let's look at the details of the example.

Pages: 1, 2, 3, 4, 5

Next Pagearrow