ActivatableAccountImplpublic class ActivatableAccountImpl extends java.rmi.activation.Activatable implements AccountActivatableAccountImpl: 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 boolean | checkTransfer(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 void | deposit(float amt)
mBalance += amt;
// Log transaction...
System.out.println("--> Deposited " + amt + " into account " + getName());
System.out.println(" New balance: " + getBalance());
| public float | getBalance()
return mBalance;
| public java.lang.String | getName()
return mName;
| public void | transfer(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 void | transfer(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 void | withdraw(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);
}
|
|