FileDocCategorySizeDatePackage
Account3_Impl2.javaAPI DocExample2975Thu Nov 08 00:22:34 GMT 2001com.ora.rmibook.chapter12.bank

Account3_Impl2

public class Account3_Impl2 extends UnicastRemoteObject implements Account3

Fields Summary
private static final int
TIMER_DURATION
private Money
_balance
private String
_currentClient
private int
_timeLeftUntilLockIsReleased
Constructors Summary
public Account3_Impl2(Money startingBalance)


      
          
        _balance = startingBalance;
        _timeLeftUntilLockIsReleased = 0;
        (Account3_Impl2_LockThread.getSingleton()).addAccount(this);
        //	register with the lock-expiration thread
    
Methods Summary
private voidcheckAccess()

        String clientHost = wrapperAroundGetClientHost();

        if (null == _currentClient) {
            _currentClient = clientHost;
        } else {
            if (!_currentClient.equals(clientHost)) {
                throw new LockedAccountException();
            }
        }
        resetCounter();
        return;
    
private voidcheckForNegativeAmount(Money amount)

        int cents = amount.getCents();

        if (0 > cents) {
            throw new NegativeAmountException();
        }
    
private voidcheckForOverdraft(Money amount)

        if (amount.greaterThan(_balance)) {
            throw new OverdraftException(false);
        }
        return;
    
protected synchronized voiddecrementLockTimer(int amountToDecrement)

        _timeLeftUntilLockIsReleased -= amountToDecrement;
        if (_timeLeftUntilLockIsReleased < 0) {
            _currentClient = null;
        }
    
public synchronized MoneygetBalance()

        checkAccess();
        return _balance;
    
public synchronized voidmakeDeposit(Money amount)

        checkAccess();
        checkForNegativeAmount(amount);
        _balance.add(amount);
        return;
    
public synchronized voidmakeWithdrawal(Money amount)

        checkAccess();
        checkForNegativeAmount(amount);
        checkForOverdraft(amount);
        _balance.subtract(amount);
        return;
    
private voidresetCounter()

        _timeLeftUntilLockIsReleased = TIMER_DURATION;
    
private java.lang.StringwrapperAroundGetClientHost()

        String clientHost = null;

        try {
            clientHost = getClientHost();
        } catch (ServerNotActiveException ignored) {
        }
        return clientHost;