23.3 RMI-IIOPバンクのサンプル

Accountインタフェースは,java.rmi.Remoteインタフェースを継承し,AccountImplクラスによってインプリメントされます(コードサンプル23-2参照)。

Clientクラス(コードサンプル23-3参照)は,まず,アカウントを生成するために各アカウント用にAccountDataオブジェクトを生成して,それらをAccountManagerに渡すことによって,指定のすべてのAccountオブジェクトを適切な残高で生成します。そして,生成されたアカウントで残高が正しいかどうかを確認します。クライアントはAccountManagerに,アカウントすべてのリストを照会し,各アカウントの貸方に$10.00を記入します。クライアントはアカウントの新しい残高が正確であるかどうかを検証します。

コードサンプルは,Borland Enterprise Server VisiBrokerをインストールしたディレクトリのexamples/vbe/rmi-iiopに入っています。
コードサンプル23-2 Accountインタフェースのインプリメント

public class AccountImpl extends Bank.AccountPOA {
  public AccountImpl(Bank.AccountData data) {
     _name = data.getName();
     _balance = data.getBalance();
  }
  public String name() throws java.rmi.RemoteException {
     return _name;
  }
  public float getBalance() throws java.rmi.RemoteException {
     return _balance;
  }
  public void setBalance(float balance) throws
                                        java.rmi.RemoteException {
     _balance = balance;
  }
  private float _balance;
  private String _name;
}

コードサンプル23-3 Clientクラス

public class Client {
  public static void main(String[ ] args) {
     try {
        // Initialize the ORB.
        org.omg.CORBA.ORB orb =
                          org.omg.CORBA.ORB.init(args,null);
        // Get the manager Id
        byte[ ] managerId = "RMIBankManager".getBytes();
        // Locate an account manager. Give the full POA
        // name and the servant ID.
        Bank.AccountManager manager =
                        Bank.AccountManagerHelper.bind(orb,
                                "/rmi_bank_poa", managerId);
        // Use any number of argument pairs to indicate
        // name, balance of accounts to create
        if (args.length == 0 || args.length % 2 != 0) {
           args = new String[2];
           args[0] = "Jack B. Quick";
           args[1] = "123.23";
        }
        int i = 0;
        while (i < args.length) {
           String name = args[i++];
           float balance;
           try {
              balance = new Float(args[i++]).floatValue();
           } catch (NumberFormatException n) {
              balance = 0;
           }
           Bank.AccountData data =
                        new Bank.AccountData(name, balance);
           Bank.Account account = manager.create(data);
           System.out.println("Created account for " +
              name + " with opening balance of $" + balance);
        }
        java.util.Hashtable accounts =
                                      manager.getAccounts();
        for (java.util.Enumeration e = accounts.elements();
                                     e.hasMoreElements();) {
           Bank.Account account =
                 Bank.AccountHelper.narrow(
                     (org.omg.CORBA.Object)e.nextElement());
           String name = account.name();
           float balance = account.getBalance();
           System.out.println("Current balance in " + name +
                               "'s account is $" + balance);
           System.out.println("Crediting $10 to " + name +
                                             "'s account.");
           account.setBalance(balance + (float)10.0);
           balance = account.getBalance();
           System.out.println("New balance in " + name +
                               "'s account is $" + balance);
        }
     } catch (java.rmi.RemoteException e){
        System.err.println(e);
     }
  }
}