Service Code

The next step is to write the service code. (See Example 5-4.) The ProductService constructor creates two sample ProductBeans and loads these into the product hashtable. The getProduct( ) method searches for a specific SKU string and returns the corresponding ProductBean. Again, we assume that the user always specifies a valid, current SKU.

Example 5-4. ProductService.java
package com.ecerami.soap;

import java.util.Hashtable;

/**
 * A Sample SOAP Service
 * Provides Product Information for requested Stockkeeping Unit (SKU)
 */
public class ProductService {
  protected Hashtable products;   // Product "Database"

  /**
   * Constructor
   * Load product database with two sample products
   */
  public ProductService (  ) {
    products = new Hashtable(  );
    ProductBean product1 = new ProductBean
      ("Red Hat Linux", "Red Hat Linux Operating System",
      54.99, "A358185");
    ProductBean product2 = new ProductBean
      ("McAfee PGP", "McAfee PGP Personal Privacy",
      19.99, "A358565");
    products.put(product1.getSKU(  ), product1);
    products.put(product2.getSKU(  ), product2);
  }

  /**
  *  Provides Product Info for requested SKU.
  *  We assume that the client always specifies a valid, current SKU
  */
  public ProductBean getProduct (String sku) {
    ProductBean product = (ProductBean) products.get(sku);
    return product;
  }
}

You can deploy the ProductService in the same manner as in the previous examples. Nonetheless, you will need to fill in a few additional fields of information for the type-mapping registry. The Apache SOAP type-mapping ...

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.