import java.net.*; import java.rmi.*; import java.rmi.server.*; import java.util.*; //import Banco.*; /** * 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. **/ public class BancoServer extends UnicastRemoteObject implements Banco { /** * This nested class stores data for a single account with the bank **/ class Conta { String senha; int saldo; Vector transacoes = new Vector(); Conta(String senha) { this.senha = senha; transacoes.addElement("Conta aberta em " + new Date()); } } /** * hashtable com todas as contas abertas **/ Hashtable listaDeContas = new Hashtable(); /** * This nested class stores data for a single account with the bank **/ public BancoServer() throws RemoteException { super(); } /** * 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. **/ public synchronized void abre(String nome, String senha) throws RemoteException, BankingException { // Verifica se a conta ja esta aberta para este usuario if (listaDeContas.get(nome) != null) throw new BankingException("Conta ja existe!"); Conta conta = new Conta(senha); // registrando na Hashtable de contas listaDeContas.put(nome, conta); } /** * This convenience 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. **/ public Conta verifica(String nome, String senha) throws BankingException { synchronized(listaDeContas) { Conta conta = (Conta)listaDeContas.get(nome); if (conta == null) throw new BankingException("Conta nao encontrada!"); if (!senha.equals(conta.senha)) throw new BankingException("Senha invalida!"); return conta; } } /** * Close the named account. This method is synchronized to make it * thread safe, since it manipulates the accounts hashtable. **/ public synchronized Dinheiro fecha(String nome, String senha) throws RemoteException, BankingException { Conta conta; conta = verifica(nome, senha); listaDeContas.remove(nome); synchronized (conta) { int saldo = conta.saldo; conta.saldo = 0; return new Dinheiro(saldo); } } /** Deposit the specified FunnyMoney to the named account */ public void deposito(String nome, String senha, Dinheiro dinheiro) throws RemoteException, BankingException { Conta conta = verifica(nome, senha); synchronized(conta) { conta.saldo += dinheiro.valor; conta.transacoes.addElement("Deposito de R$ " + dinheiro.valor + " em " + new Date()); } } /** Withdraw the specified amount from the named account */ public Dinheiro saque(String nome, String senha, int valor) throws RemoteException, BankingException { Conta conta = verifica(nome, senha); synchronized(conta) { if (conta.saldo < valor) throw new BankingException("Saldo insuficiente!"); conta.saldo -= valor; conta.transacoes.addElement("Saque de R$ " + valor + " em "+new Date()); return new Dinheiro(valor); } } /** Return the current balance in the named account */ public int saldo(String nome, String senha) throws RemoteException, BankingException { Conta conta = verifica(nome, senha); synchronized(conta) { return conta.saldo; } } /** * Return a Vector of strings containing the transaction history * for the named account **/ public Vector historico(String nome, String senha) throws RemoteException, BankingException { Conta conta = verifica(nome, senha); synchronized(conta) { return conta.transacoes; } } /** * 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 { BancoServer banco = new BancoServer(); String nomeBanco = System.getProperty("banco", "BancoDefault"); Naming.rebind(nomeBanco, banco); System.out.println(nomeBanco + " aberto e pronto para clientes em url:" + InetAddress.getLocalHost().getHostName()); } catch (RemoteException ex) { System.err.println("Couldn't contact rmiregistry."); System.err.println("Are you sure you're running rmiregistry?"); System.exit(1); } catch (MalformedURLException ex) { // Shouldn't happen System.exit(1); } catch (Exception e) { System.err.println(e); System.err.println("Uso: java [-Dbanco=] BancoServer"); System.exit(1); } } }