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

Account2_Impl

public class Account2_Impl extends UnicastRemoteObject implements Account2

Fields Summary
private Money
_balance
private String
_currentClient
Constructors Summary
public Account2_Impl(Money startingBalance)

        _balance = startingBalance;
    
Methods Summary
private booleanbecomeOwner()

        String clientHost = wrapperAroundGetClientHost();

        if (null != _currentClient) {
            if (_currentClient.equals(clientHost)) {
                return true;
            }
        } else {
            _currentClient = clientHost;
            return true;
        }
        return false;
    
private voidcheckAccess()

        String clientHost = wrapperAroundGetClientHost();

        if ((null != _currentClient) && (_currentClient.equals(clientHost))) {
            return;
        }
        throw new LockedAccountException();
    
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;
    
public synchronized MoneygetBalance()

        checkAccess();
        return _balance;
    
public synchronized voidgetLock()

        if (false == becomeOwner()) {
            throw new LockedAccountException();
        }
        return;
    
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;
    
public synchronized voidreleaseLock()

        String clientHost = wrapperAroundGetClientHost();

        if ((null != _currentClient) && (_currentClient.equals(clientHost))) {
            _currentClient = null;
        }
    
private java.lang.StringwrapperAroundGetClientHost()

        String clientHost = null;

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