Service Code

The complete service code is presented in Example 5-8. The ProductService2 class extends the original ProductService class and uses the same product hashtable. The new getProductInfo( ) method receives a String SKU parameter and checks the product hashtable.

Example 5-8. ProductService2.java
package com.ecerami.soap;

import java.util.Hashtable;

/**
 * A Sample SOAP Service
 * Provides Product Information for requested Stockkeeping Unit (SKU)
 */
public class ProductService2 extends ProductService {

  /**
  *  Provides Product Info for requested SKU.
  *  If SKU is not found, method throws a ProductNotFoundException
  */
  public ProductBean getProductInfo (String sku)
    throws ProductNotFoundException {
    ProductBean product = (ProductBean) products.get(sku);
    if (product==null)
                           throw new ProductNotFoundException ("SKU Not Found:  "+sku);
    return product;
  }
}

If a match is found, the method returns the correct ProductBean. Otherwise, the method throws a ProductNotFoundException. The ProductNotFoundException code is presented in Example 5-9.

Example 5-9. ProductNotFoundException.java
package com.ecerami.soap; import org.apache.soap.Fault; /** * ProductNotFoundException * Encapsulates any exceptions related to retrieving * product/price for Specified Stockkeeping Unit (SKU) */ public class ProductNotFoundException extends Exception { private Fault fault; public ProductNotFoundException (String faultString) { super (faultString); } public ProductNotFoundException (String faultString, Fault ...

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.