Oracle8i CORBA Developer's Guide and Reference Release 3 (8.1.7) Part Number A83722-01 |
|
The following is a Bank example that demonstrates a simple CORBA application. Included is the README, the IDL, the server code, and the client code. Refer to the demo/corba/basic
directory for the Makefile.
bank demonstrates: This is an Oracle8i-compatible version of the VisiBroker Bank example. The major differences from the Vb example are: (1) There is no server main loop. For Oracle8i the "wait-for-activation" loop is part of the IIOP presentation (MTS server). (2) _boa.connect(object) is used instead of the less portable _boa_obj_is_ready(object) in the server object implementation to register the new Account objects. (3) The client program contains the code necessary to lookup the AccountManager object (published under /test/myBank) and activate it, and to authenticate the client to the server. (Note that object activation and authentication, via NON_SSL_LOGIN, happen "under the covers" so to speak on the lookup() method invocation.) (4) There is also a tie implementation of this example, with the server being AccountManagerImplTie.java.
// Bank.idl module Bank { interface Account { float balance(); }; interface AccountManager { Account open(in string name); }; };
The server code is implemented in the following:
package bankServer; import java.util.*; public class AccountManagerImpl extends Bank._AccountManagerImplBase { 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 Oracle 8i. 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(); }
package bankServer; public class AccountImpl extends Bank._AccountImplBase { public AccountImpl(float balance) { _balance = balance; } public float balance() { return _balance; } private float _balance; }
package bankServer; import java.util.*; import oracle.aurora.AuroraServices.ActivatableObject; public class AccountManagerImplTie implements Bank.AccountManagerOperations, ActivatableObject { 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); org.omg.CORBA.ORB.init().BOA_init().obj_is_ready(account); // Print out the new account. // This just goes to the system trace file for Oracle 8i. System.out.println("Created " + name + "'s account: " + account); // Save the account in the account dictionary. _accounts.put(name, account); } // Return the account. return account; } public org.omg.CORBA.Object _initializeAuroraObject () { return new Bank._tie_AccountManager(this); } private Dictionary _accounts = new Hashtable(); private Random _random = new Random(); }
// Client.java opens the account through the AccountManager class and manages // the account through the Account class */ 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 { if (args.length != 5) { System.out.println("usage: Client serviceURL objectName user password " + "accountName"); System.exit(1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; String name = args [4]; Hashtable env = new Hashtable(); env.put(Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); env.put(Context.SECURITY_PRINCIPAL, user); 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); // 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); } }
|
Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|