Oracle8i Enterprise JavaBeans and CORBA Developer's Guide Release 8.1.5 A64683-01 |
|
The following sections compare implementations of the bank example widely used in VBJ documentation. Both client and server are shown as they would be implemented in Oracle8 i and VBJ. All implementations use inheritance.
// Bank.idl module Bank { interface Account { float balance(); }; interface AccountManager { Account open(in string name); }; };
// Client.java import bankServer.*; import Bank.*; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; public class Client { public static void main (String[] args) throws Exception { String serviceURL = "sess_iiop://localhost:2222"; String objectName = "/test/myBank"; String username = "scott"; String password = "tiger"; Hashtable env = new Hashtable(); env.put(Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); env.put(Context.SECURITY_PRINCIPAL, username); env.put(Context.SECURITY_CREDENTIALS, password); env.put(Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN); Context ic = new InitialContext(env); AccountManager manager = (AccountManager) ic.lookup(serviceURL + objectName); // use args[0] as the account name, or a default. String name = args.length == 1 ? 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); } }
// Client.java public class Client { public static void main(String[] args) { // Initialize the ORB. org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null); // Locate an account manager. Bank.AccountManager manager = Bank.AccountManagerHelper.bind(orb, "BankManager"); // 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); } }
// AccountImpl.java package bankServer; public class AccountImpl extends Bank._AccountImplBase { public AccountImpl(float balance) { _balance = balance; } public float balance() { return _balance; } private float _balance; }
// AccountImpl.java public class AccountImpl extends Bank._AccountImplBase { public AccountImpl(float balance) { _balance = balance; } public float balance() { return _balance; } private float _balance; }
// AccountManagerImpl.java package bankServer; import java.util.*; public class AccountManagerImpl extends Bank._AccountManagerImplBase { public AccountManagerImpl() { super(); } public AccountManagerImpl(String name) { super(name); } public synchronized Bank.Account open(String name) { // Lookup the account in the account dictionary. Bank.Account account = (Bank.Account) _accounts.get(name); // If there was no account in the dictionary, create one. if(account == null) { // Make up the account's balance, between 0 and 1000 dollars. float balance = Math.abs(_random.nextInt()) % 100000 / 100f; // Create the account implementation, given the balance. account = new AccountImpl(balance); _orb().connect (account); // Print out the new account. // This just goes to the system trace file for Aurora. System.out.println("Created " + name + "`s account: " + account); // Save the account in the account dictionary. _accounts.put(name, account); } // Return the account. return account; } private Dictionary _accounts = new Hashtable(); private Random _random = new Random(); }
// AccountManagerImpl.java import java.util.*; public class AccountManagerImpl extends Bank._AccountManagerImplBase { public AccountManagerImpl(String name) { super(name); } public synchronized Bank.Account open(String name) { // Lookup the account in the account dictionary. Bank.Account account = (Bank.Account) _accounts.get(name); // If there was no account in the dictionary, create one. if(account == null) { // Make up the account's balance, between 0 and 1000 dollars. float balance = Math.abs(_random.nextInt()) % 100000 / 100f; // Create the account implementation, given the balance. account = new AccountImpl(balance); // Make the object available to the ORB. _boa().obj_is_ready(account); // Print out the new account. System.out.println("Created " + name + "`s account: " + account); // Save the account in the account dictionary. _accounts.put(name, account); } // Return the account. return account; } private Dictionary _accounts = new Hashtable(); private Random _random = new Random(); }
// Server.java public class Server { public static void main(String[] args) { // Initialize the ORB. org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null); // Initialize the BOA. org.omg.CORBA.BOA boa = orb.BOA_init(); // Create the account manager object. Bank.AccountManager manager = new AccountManagerImpl("BankManager"); // Export the newly created object. boa.obj_is_ready(manager); System.out.println(manager + " is ready."); // Wait for incoming requests boa.impl_is_ready(); } }