Java Reference
In-Depth Information
Example 16−2: RemoteBankServer.java (continued)
{
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);
}
}
/** Return the current balance in the named account */
public int getBalance(String name, String password)
throws RemoteException, BankingException
{
Account acct = verify(name, password);
synchronized(acct) { return acct.balance; }
}
/**
* Return a Vector of strings containing the transaction history
* for the named account
**/
public List getTransactionHistory(String name, String password)
throws RemoteException, BankingException
{
Account acct = verify(name, password);
synchronized(acct) { return acct.transactions; }
}
/**
* 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.
**/
public static void main(String[] args) {
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>] " +
"com.davidflanagan.examples.rmi.RemoteBankServer");
System.exit(1); // Force exit because there may be RMI threads
}
}
}
Search WWH ::




Custom Search