FileDocCategorySizeDatePackage
ActivatableAccountImpl.javaAPI DocExample5970Thu Dec 15 21:45:22 GMT 2005com.oreilly.jent.rmi

ActivatableAccountImpl

public class ActivatableAccountImpl extends java.rmi.activation.Activatable implements Account
ActivatableAccountImpl: Activatable implementation of the Account remote interface.

Fields Summary
private float
mBalance
private String
mName
Constructors Summary
public ActivatableAccountImpl(String name)


  // "Regular" constructor used to create a "pre-activated" server
    
        
    // Register and export object (on random open port)
    // Note that we're 
    super(null, new MarshalledObject(new AccountState(name, 0f)), false, 0);
    mName = name;
  
protected ActivatableAccountImpl(java.rmi.activation.ActivationID id, MarshalledObject arg)

    // Export this object with the given activation id, on random port
    super(id, 0);
    System.out.println("Activating an account");
    // Check incoming data (account state) passed in with activation request
    try {
      Object oarg = arg.get();
      if (oarg instanceof AccountState) {
        AccountState s = (AccountState)oarg;
        // Set our name and balance based on incoming state
        mName = s.name;
        mBalance = s.balance;
      }
      else {
        System.out.println("Unknown argument type received on activation: " +
                           oarg.getClass().getName());
      }
    }
    catch(Exception e) {
      System.out.println("Error retrieving argument to activation");
    }
  
Methods Summary
private booleancheckTransfer(Account src, float amt)

    boolean approved = false;
    try {
      if (src.getBalance() >= amt) {
        approved = true;
      }
    }
    catch (RemoteException re) {
      // If some remote exception occurred, then the transfer is still
      // compromised, so return false
      approved = false;
    }
    return approved;
  
public voiddeposit(float amt)

    mBalance += amt;
    // Log transaction...
    System.out.println("--> Deposited " + amt + " into account " + getName());
    System.out.println("    New balance: " + getBalance());
  
public floatgetBalance()

    return mBalance;
  
public java.lang.StringgetName()

    return mName;
  
public voidtransfer(float amt, Account src)

    if (checkTransfer(src, amt)) {
      src.withdraw(amt);
      this.deposit(amt);
      // Log transaction...
      System.out.println("--> Transferred " + amt + " from account " + getName());
      System.out.println("    New balance: " + getBalance());
    }
    else {
      throw new InsufficientFundsException("Source account balance is less " +
                                           "than the requested transfer.");
    }
  
public voidtransfer(java.util.List amts, java.util.List srcs)

    ListIterator amtCurs = amts.listIterator();
    ListIterator srcCurs = srcs.listIterator();
    // Iterate through the accounts and the amounts to be
    // transferred from each (assumes amounts are given as Float
    // objects)
    while (amtCurs.hasNext() && srcCurs.hasNext()) {
      Float amt = (Float)amtCurs.next();
      Account src = (Account)srcCurs.next();
      // Make the transaction
      this.transfer(amt.floatValue(), src);
    }
  
public voidwithdraw(float amt)

    if (mBalance >= amt) {
      mBalance -= amt;
      // Log transaction...
      System.out.println("--> Withdrew " + amt + " from account " + getName());
      System.out.println("    New balance: " + getBalance());
    }
    else {
      throw new InsufficientFundsException("Withdrawal request of " + amt +
					   " exceeds balance of " + mBalance);
    }