FileDocCategorySizeDatePackage
RemoteBankServer.javaAPI DocExample6604Sat Jan 24 10:44:40 GMT 2004je3.rmi

RemoteBankServer

public class RemoteBankServer extends UnicastRemoteObject implements RemoteBank
This class implements the remote methods defined by the RemoteBank interface. It has a serious shortcoming, though: all account data is lost when the server goes down.

Fields Summary
Map
accounts
This hashtable stores all open accounts and maps from account name to Account object. Methods that use this object will be synchronized to prevent concurrent access by more than one thread.
Constructors Summary
public RemoteBankServer()
This constructor doesn't do anything, but because the superclass constructor throws an exception, the exception must be declared here

    
                             
         super(); 
Methods Summary
public synchronized FunnyMoneycloseAccount(java.lang.String name, java.lang.String password)
Close the named account. This method is synchronized to make it thread safe, since it manipulates the accounts hashtable.

        Account acct;
        acct = verify(name, password);
        accounts.remove(name);
        // Before changing the balance or transactions of any account, we first
        // have to obtain a lock on that account to be thread safe.
        synchronized (acct) {
            int balance = acct.balance;
            acct.balance = 0;
            return new FunnyMoney(balance);
        }
    
public voiddeposit(java.lang.String name, java.lang.String password, FunnyMoney money)
Deposit the specified FunnyMoney to the named account

        Account acct = verify(name, password);
        synchronized(acct) { 
            acct.balance += money.amount; 
            acct.transactions.add("Deposited " + money.amount + 
				  " on " + new Date());
        }
    
public intgetBalance(java.lang.String name, java.lang.String password)
Return the current balance in the named account

        Account acct = verify(name, password);
        synchronized(acct) { return acct.balance; }
    
public java.util.ListgetTransactionHistory(java.lang.String name, java.lang.String password)
Return a Vector of strings containing the transaction history for the named account

        Account acct = verify(name, password);
        synchronized(acct) { return acct.transactions; }
    
public static voidmain(java.lang.String[] args)
The main program that runs this RemoteBankServer. Create a RemoteBankServer object and give it a name in the registry. Read a system property to determine the name, but use "FirstRemote" as the default name. This is all that is necessary to set up the service. RMI takes care of the rest.

        try {
            // Create a bank server object
            RemoteBankServer bank = new RemoteBankServer();
            // Figure out what to name it
            String name = System.getProperty("bankname", "FirstRemote");
            // Name it that
            Naming.rebind(name, bank);
            // Tell the world we're up and running
            System.out.println(name + " is open and ready for customers.");
        }
        catch (Exception e) {
            System.err.println(e);
            System.err.println("Usage: java [-Dbankname=<name>] " +
		            "je3.rmi.RemoteBankServer");
            System.exit(1); // Force exit because there may be RMI threads
        }
    
public synchronized voidopenAccount(java.lang.String name, java.lang.String password)
Open a bank account with the specified name and password This method is synchronized to make it thread safe, since it manipulates the accounts hashtable.

        // Check if there is already an account under that name
        if (accounts.get(name) != null) 
            throw new BankingException("Account already exists.");
        // Otherwise, it doesn't exist, so create it.
        Account acct = new Account(password);
        // And register it
        accounts.put(name, acct);
    
synchronized je3.rmi.RemoteBankServer$Accountverify(java.lang.String name, java.lang.String password)
This internal method is not a remote method. Given a name and password it checks to see if an account with that name and password exists. If so, it returns the Account object. Otherwise, it throws an exception. This method is synchronized because it uses the accounts hashtable.

        Account acct = (Account)accounts.get(name);
        if (acct == null) throw new BankingException("No such account");
        if (!password.equals(acct.password))
            throw new BankingException("Invalid password");
        return acct;
    
public FunnyMoneywithdraw(java.lang.String name, java.lang.String password, int amount)
Withdraw the specified amount from the named account

        Account acct = verify(name, password);
        synchronized(acct) {
            if (acct.balance < amount) 
                throw new BankingException("Insufficient Funds");
            acct.balance -= amount;
            acct.transactions.add("Withdrew " + amount + " on "+new Date());
            return new FunnyMoney(amount);
        }