Common Object Request Broker Architecture
module Bank { interface Account { float balance(); }; interface AccountManager { Account open(in string name); }; };
interface Bank.Account extends interface CORBA.Object interface Bank.AccountManager extends interface CORBA.Object class Bank._sk_Account implements interface Bank.Account class Bank._sk_AccountManager implements interface Bank.AccountManager class Bank.Account_var class Bank.AccountManager_var
import java.util.*; class Account extends Bank._sk_Account { private float _balance; Account(float balance) { _balance = balance; } public float balance() throws CORBA.SystemException { return _balance; } }
class AccountManager extends Bank._sk_AccountManager { private Dictionary _accounts = new Hashtable(); private Random _random = new Random(); AccountManager(String name) { super(name); } public Bank.Account open(String name) throws CORBA.SystemException { // Lookup the account in the account dictionary. Bank.Account account = ( Bank.Account ) _accounts.get( name ); if(account == null) { // Create a new account with between 0 and 1000 dollars. float balance = Math.abs(_random.nextInt()) % 100000 / 100f; account = new Account(balance); System.out.println("Created " + name + "'s account: " + account); // Export the new object reference. CORBA.ORB.init().BOA_init().obj_is_ready(account); // Save the account in the account dictionary. _accounts.put(name, account); } // Return the account. return account; } }
public class Server { public static void main(String[] args) { try { // Initialize some CORBA Stuff CORBA.ORB orb = CORBA.ORB.init(); CORBA.BOA boa = orb.BOA_init(); // Create the account manager object. AccountManager manager = new AccountManager("Post-Modern Bank"); // Export the newly create object. boa.obj_is_ready(manager); System.out.println(manager + " is ready."); // Wait for incoming requests boa.impl_is_ready(); } catch(CORBA.SystemException e) { System.err.println(e); } } }
public class Client { public static void main(String args[]) { try { // Initialize the ORB. CORBA.ORB orb = CORBA.ORB.init(); // Locate an account manager. Bank.AccountManager manager = Bank.AccountManager_var.bind("Post-Modern Bank"); // use args[0] as the account name, or a default. String name = args.length > 0 ? args[0] : "Jack B. Quick"; // Request the account manager to open a named account. Bank.Account account = manager.open(name); // Get the balance of the account. float balance = account.balance(); // Print out the balance. System.out.println ("The balance in " + name + "'s account is $" + balance); } catch(CORBA.SystemException e) { System.err.println(e); } } }
prompt> java Client The balance in Jack B. Quick's account is $71.62 prompt> java Client The balance in Jack B. Quick's account is $71.62 prompt> java Client "Jack Sprat" The balance in Jack Sprat's account is $322.38 prompt> java Client "Jack Sprat" The balance in Jack Sprat's account is $322.38 prompt> java Client The balance in Jack B. Quick's account is $71.62
See previous two slides