HomeDigital EditionSys-Con RadioSearch Web Services Cd
B2B Beginning WS Business Process Management Case Studies Content Management Distributing Computing e-Business Electronic Data Interchange Enterprise Industry Insight Integration Interviews Java & Web Services .NET Portal Product Reviews Scalability & Performance Security SOAP Source Code UDDI Wireless WS Standards WS Tips & Techniques WSDL WS Editorials XML

Web Services Made Easy, by Graham Glass
WSJ Vol 01 Issue 01 - pg.61

	


Listing 1
package examples;

/**
 * An interface for getting exchange rates.
 */
public interface IExchange
  {
  /**
   * Set the value, in US dollars, of the specified currency.
   * @param country The country.
   * @param value The value in US dollars.
   */
  void setValue( String country, double value );

  /**
   * Return the value of the specified currency.
   * @param country The country.
   * @return The value in US dollars.
   * @throws ExchangeException If the country is not recognized.
   */
  double getValue( String country ) throws ExchangeException;

  /**
   * Return the exchange rate between two countries.
   * @param country1 The country to convert from.
   * @param country2 The country to convert to.
   * @return The exchange rate.
   * @throws ExchangeException If either country is not recognized.
   */
  double getRate( String country1, String country2 )
    throws ExchangeException;
  }

Listing2
package examples;

import java.util.Hashtable;

/**
 * Simple implementation of IExchange.
 */
public class Exchange implements IExchange
  {
  Hashtable values = new Hashtable(); // in US dollars

  public void setValue( String country, double value )
    {
    values.put( country, new Double( value ) );
    }

  public double getValue( String country )
    throws ExchangeException
    {
    Double value = (Double) values.get( country );

    if( value == null )
      throw new ExchangeException( "country " + country + " not recognized" );

    return value.doubleValue();
    }

  public double getRate( String country1, String country2 )
    throws ExchangeException
    {
    return getValue( country1 ) / getValue( country2 );
    }
  }

Listing3
package examples;

import electric.service.registry.Registry;
import electric.server.http.HTTP;

public class ExchangeServer
  {
  public static void main( String[] args )
    throws Exception
    {
    // start a web server on port 8004, accept messages via /soap
    HTTP.startup( "http://localhost:8004/soap" );

    // initialize an instance of Exchange
    Exchange exchange = new Exchange();
    exchange.setValue( "usa", 1 );
    exchange.setValue( "japan", 0.4 );

    // publish an instance of Exchange
    Registry.publish( "exchange", exchange );
    }
  }

Listing4
package examples;

import electric.registry.Registry;

public class ExchangeClient
  {
  public static void main( String[] args )
    throws Exception
    {
    // bind to web service whose WSDL is at the specified URL
    String url = "http://localhost:8004/soap/exchange.wsdl";
    IExchange exchange = (IExchange) Registry.bind( url, IExchange.class );

    // invoke the web service as if it was a local java object
    double rate = exchange.getRate( "usa", "japan" );
    System.out.println( "usa/japan exchange rate = " + rate );
    }
  }