Service Code

We will examine the service code first. (See Example 5-1.) The PriceListService constructor creates a product hashtable of two current products. To keep the code simple, the prices are hardcoded. The getPriceList( ) method expects an array of string SKUs and generates a corresponding array of doubles. We assume that the client always requests current, valid SKUs.

Example 5-1. PriceListService.java
package com.ecerami.soap;

import java.util.Hashtable;

/**
 * A Sample SOAP Service
 * Provides a Price List for specified list of SKUs
 */
public class PriceListService {
  protected Hashtable products;   // Product "Database"

  /**
   * Zero Argument Constructor
   * Load product database with two sample products
   */
  public PriceListService (  ) {
    products = new Hashtable(  );
    //  Red Hat Linux
    products.put("A358185", new Double (54.99));
    //  McAfee PGP Personal Privacy
    products.put("A358565", new Double (19.99));
  }

  /**
  *  Provides Price List for specified SKUs.
  *  We assume that the client always specifies valid, current SKUs
  */
  public double[] getPriceList (String sku[]) {
    double prices[] = new double [sku.length];
    for (int i=0; i<sku.length; i++) {
      Double price = (Double) products.get(sku[i]);
      prices[i] = price.doubleValue(  );
    }
    return prices;
  }
}

Get Web Services Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.