/**

 * Created by IntelliJ IDEA.
 * User: sgreene
 * Date: May 22, 2003
 * Time: 8:47:38 PM
 * Modified by : Chandar
 *

 * File  : BankAccountClient .java
 *
 * Overview :
 *     This file defines the client for the Bank Web Service
 */

package oracle.demo.handlerchain;


import oracle.demo.handlerchain.stubs.*;

// JAX-RPC related imports
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Stub;
import java.net.URL;



public class BankAccountClient {

   // variable for demo usernames
    private static String DEMO_USER1 = "DemoUser1";

    private static String DEMO_USER2 = "DemoUser2";

    private static String DEMO_USER3 = "DemoUser3";


   // variable for web service URL
    private String m_serviceURL;

   // variable for service factory
    private ServiceFactory m_serviceFactory = null;

   /*
     * Constructor definition
     */

    public BankAccountClient(String serviceURL) throws Exception{
        // initialize service url
        m_serviceURL = serviceURL;
        // create instance of service factory
        m_serviceFactory = ServiceFactory.newInstance();
    }

   /*
     *  This methods performs various operation on Bank Service
     */
    private void runDemo() throws Exception{

        // demo valid account operations
        demoGoodAccount();

        // demo insufficient funds operation
        demoInsufficientFunds();

        // demo wrong withdrawal operation
        demoWithdrawalCap();
    }

   /*
     *  This methods tries to withdraw more than permissible amount

     *  from the account. The request is intercepted by AccountWithdrawalHandler
     *  which raises an exception
     */
    private void demoWithdrawalCap() throws Exception{
        // get bank service from Service Factory using the service url
        OracleBank bankService = (OracleBank)m_serviceFactory.loadService(
                              new URL(m_serviceURL ),OracleBank.class,null);

        // get a handle to the service
        BankBeanInterface port = bankService.getBankBeanInterfacePort();
        ((Stub)port)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,m_serviceURL);

        // create a new account in bank

        String accountID = port.createAccount(DEMO_USER3,3000.00f);
        System.out.println("Created an account for " + DEMO_USER3 + " with $3000.00");
        System.out.println("Attempting to withdraw 2500.00 from the account."+
                             "  The account cap is 2000.00");
        try{
            // try to withdraw more than permissible amount from bank
            port.withdraw(accountID,2500.00f);
        }
        // catch any exceptions
        catch(Exception ex){
            System.out.println("Unable to withdraw funds.");
            System.out.println("Exception: " + ex.getMessage());
        }
    }


   /*
     *  This methods tries create a new account in the Bank
     *  with insufficient initial balance. The request is intercepted by NewAccountHandler
     *  which raises an exception
     */
    private void demoInsufficientFunds() throws Exception{

        // get bank service from Service Factory using the service url
        OracleBank bankService = (OracleBank)m_serviceFactory.loadService(
                                    new URL(m_serviceURL ),OracleBank.class,null);

        // get a handle to the service
        BankBeanInterface port = bankService.getBankBeanInterfacePort();
        ((Stub)port)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,m_serviceURL);

        System.out.println("Attempting to create an account for " +
                              DEMO_USER2 + " with no initial funds");
        try{
            // try to create an account with 0 initial balance
            port.createAccount(DEMO_USER2,0f);
        }
        // catch any exceptions
        catch(Exception ex){
            System.out.println("Could not createAccount for " + DEMO_USER2);
            System.out.println("Exception: " + ex.getMessage());
        }
    }

   /*
     *  This method performs all the valid operations on the bank
    *

    */
    private void demoGoodAccount() throws Exception{

        // get bank service from Service Factory using the service url
        OracleBank bankService = (OracleBank)m_serviceFactory.loadService(
                                    new URL(m_serviceURL ),OracleBank.class,null);

        // get a handle to the service
        BankBeanInterface port = bankService.getBankBeanInterfacePort();
        ((Stub)port)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,m_serviceURL);

        // create a new account
        String accountID = port.createAccount(DEMO_USER1,2000.50f);
        System.out.println("Created an account for " + DEMO_USER1 + " with $2000.50");
        System.out.println("AccountID for DemoUser is " + accountID);
        System.out.println("Depositing $500.50 into account.");


        // deposit amount in the account
        port.deposit(accountID,500.50f);

        // get current balance in the account
        float balance = port.getBalance(accountID,DEMO_USER1);
        System.out.println("Current balance is now " + balance);
        System.out.println("Withdrawing $250.00 from account");

        // withdraw amount from the account
        port.withdraw(accountID,250.00f);
        balance = port.getBalance(accountID,DEMO_USER1);
    }

   /*
     *  This is the main method of client class
    *
    */
    public static void main(String args[]){

        String serviceURL = null;
       // check if service url is provided as an argument
        if(args.length > 0){
            serviceURL = args[0];
        }
       else{
            System.out.println("no url specified");
            return;
        }
        try{
            // create an instance of client
            BankAccountClient demo = new BankAccountClient(serviceURL);
            // call method to invoke web service methods
            demo.runDemo();
        }
        // catch any exceptions
        catch(Exception ex){
            ex.printStackTrace();
        }

    }
}

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy