Oracle8i CORBA Developer's Guide and Reference Release 3 (8.1.7) Part Number A83722-01 |
|
The following example shows how to use the IFR. Soft copy is located at demo/corba/basic/bankWithIFR
.
// Bank.idl module Bank { interface Account { float balance(); }; interface AccountManager { Account open(in string name); }; };
The server code is implemented in the AccountManager
, Account
, and TIE classes.
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(); }
The client code is facilitated in the following:
import bankServer.*; import Bank.*; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; import org.omg.CORBA.Repository; 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); System.out.println("Calling the implicit method get_interface()"); org.omg.CORBA.InterfaceDef intf = (org.omg.CORBA.InterfaceDef) account._get_interface_def(); System.out.println("intf = " + intf.name()); System.out.println("Now explicitly looking up for IFR and printing the"); System.out.println("whole repository"); System.out.println(""); Repository rep = (Repository)ic.lookup(serviceURL + "/etc/ifr"); new PrintIDL(org.omg.CORBA.ORB.init()).print(rep); } }
import java.io.PrintStream; import java.util.Vector; import java.io.DataInputStream; import org.omg.CORBA.Repository; public class PrintIDL { private static org.omg.CORBA.ORB _orb; private static PrintStream _out = System.out; private static int _indent; public PrintIDL (org.omg.CORBA.ORB orb) { _orb = orb; } private void println(Object o) { for(int i = 0; i < _indent; i++) { _out.print(" "); } _out.println(o); } private String toIdl(org.omg.CORBA.IDLType idlType) { org.omg.CORBA.Contained contained = org.omg.CORBA.ContainedHelper.narrow(idlType); return contained == null ? idlType.type().toString() : contained.absolute_name(); } public void print(org.omg.CORBA.Container container) throws org.omg.CORBA.UserException { org.omg.CORBA.Contained[] contained = container.contents(org.omg.CORBA.DefinitionKind.dk_all, true); for(int i = 0; i < contained.length; i++) { { org.omg.CORBA.ContainedPackage.Description description =
contained[i].describe(); org.omg.CORBA.portable.OutputStream output =
_orb.create_output_stream(); org.omg.CORBA.ContainedPackage.DescriptionHelper.write(output,
description); org.omg.CORBA.portable.InputStream input =
output.create_input_stream(); org.omg.CORBA.ContainedPackage.Description description2 = org.omg.CORBA.ContainedPackage.DescriptionHelper.read(input); org.omg.CORBA.Any any1 = _orb.create_any(); org.omg.CORBA.ContainedPackage.DescriptionHelper.insert(any1,
description); org.omg.CORBA.Any any2 = _orb.create_any(); org.omg.CORBA.ContainedPackage.DescriptionHelper.insert(any2,
description2); if(!any1.equals(any1) || !any1.equals(any2) || !any2.equals(any2) || !any2.equals(any1)) { System.out.println("\n*** The desriptions were not equal (1) *** \n"); } org.omg.CORBA.ContainedPackage.Description description3 = org.omg.CORBA.ContainedPackage.DescriptionHelper.extract(any2); if(description.kind != description2.kind || !description.value.equals(description3.value)) { System.out.println("\n*** The desriptions were not equal (2) *** \n"); } } switch(contained[i].def_kind().value()) { case org.omg.CORBA.DefinitionKind._dk_Attribute: printAttribute(org.omg.CORBA.AttributeDefHelper.narrow(contained[i])); break; case org.omg.CORBA.DefinitionKind._dk_Constant: printConstant(org.omg.CORBA.ConstantDefHelper.narrow(contained[i])); break; case org.omg.CORBA.DefinitionKind._dk_Exception: printException(org.omg.CORBA.ExceptionDefHelper.narrow(contained[i])); break; case org.omg.CORBA.DefinitionKind._dk_Interface: printInterface(org.omg.CORBA.InterfaceDefHelper.narrow(contained[i])); break; case org.omg.CORBA.DefinitionKind._dk_Module: printModule(org.omg.CORBA.ModuleDefHelper.narrow(contained[i])); break; case org.omg.CORBA.DefinitionKind._dk_Operation: printOperation(org.omg.CORBA.OperationDefHelper.narrow(contained[i])); break; case org.omg.CORBA.DefinitionKind._dk_Alias: printAlias(org.omg.CORBA.AliasDefHelper.narrow(contained[i])); break; case org.omg.CORBA.DefinitionKind._dk_Struct: printStruct(org.omg.CORBA.StructDefHelper.narrow(contained[i])); break; case org.omg.CORBA.DefinitionKind._dk_Union: printUnion(org.omg.CORBA.UnionDefHelper.narrow(contained[i])); break; case org.omg.CORBA.DefinitionKind._dk_Enum: printEnum(org.omg.CORBA.EnumDefHelper.narrow(contained[i])); break; case org.omg.CORBA.DefinitionKind._dk_none: case org.omg.CORBA.DefinitionKind._dk_all: case org.omg.CORBA.DefinitionKind._dk_Typedef: case org.omg.CORBA.DefinitionKind._dk_Primitive: case org.omg.CORBA.DefinitionKind._dk_String: case org.omg.CORBA.DefinitionKind._dk_Sequence: case org.omg.CORBA.DefinitionKind._dk_Array: default: break; } } } private void printConstant(org.omg.CORBA.ConstantDef def) throws
org.omg.CORBA.UserException { println("const " + toIdl(def.type_def()) + " " + def.name() + " = " +
def.value() + ";"); } private void printStruct(org.omg.CORBA.StructDef def) throws
org.omg.CORBA.UserException { println("struct " + def.name() + " {"); _indent++; org.omg.CORBA.StructMember[] members = def.members(); for(int j = 0; j < members.length; j++) { println(toIdl(members[j].type_def) + " " + members[j].name + ";"); } _indent--; println("};"); } private void printUnion(org.omg.CORBA.UnionDef def) throws org.omg.CORBA.UserException { println("union " + def.name() + " switch(" + toIdl(def.discriminator_type_def()) + ") {"); org.omg.CORBA.UnionMember[] members = def.members(); int default_index = def.type().default_index(); _indent++; for(int j = 0; j < members.length; j++) { if(j == default_index) { println("default:"); } else { println("case " + members[j].label + ":"); } _indent++; println(toIdl(members[j].type_def) + " " + members[j].name + ";"); _indent--; } _indent--; println("};"); } private void printException(org.omg.CORBA.ExceptionDef def) throws
org.omg.CORBA.UserException { println("exception " + def.name() + " {"); _indent++; org.omg.CORBA.StructMember[] members = def.members(); for(int j = 0; j < members.length; j++) { println(toIdl(members[j].type_def) + " " + members[j].name + ";"); } _indent--; println("};"); } private void printEnum(org.omg.CORBA.EnumDef def) throws
org.omg.CORBA.UserException { org.omg.CORBA.TypeCode type = def.type(); println("enum " + type.name() + " {"); _indent++; int count = type.member_count(); for(int j = 0; j < count; j++) { println(type.member_name(j) + ((j == count - 1) ? "" : ",")); } _indent--; println("};"); } private void printAlias(org.omg.CORBA.AliasDef def) throws
org.omg.CORBA.UserException { org.omg.CORBA.IDLType idlType = def.original_type_def(); String arrayBounds = ""; while(true) { // This is a little strange, since the syntax of typedef'ed // arrays is stupid. org.omg.CORBA.ArrayDef arrayDef =
org.omg.CORBA.ArrayDefHelper.narrow(idlType); if(arrayDef == null) { break; } arrayBounds += "[" + arrayDef.length() + "]"; idlType = arrayDef.element_type_def(); } println("typedef " + toIdl(idlType) + " " + def.name() + arrayBounds + ";"); } private void printAttribute(org.omg.CORBA.AttributeDef def) throws
org.omg.CORBA.UserException { String readonly = def.mode() == org.omg.CORBA.AttributeMode.ATTR_READONLY ? "readonly " : ""; println(readonly + "attribute " + toIdl(def.type_def()) + " " + def.name() +
";"); } private void printOperation(org.omg.CORBA.OperationDef def) throws
org.omg.CORBA.UserException { String oneway = def.mode() == org.omg.CORBA.OperationMode.OP_ONEWAY ? "oneway " : ""; println(oneway + toIdl(def.result_def()) + " " + def.name() + "("); _indent++; org.omg.CORBA.ParameterDescription[] parameters = def.params(); for(int k = 0; k < parameters.length; k++) { String[] mode = { "in", "out", "inout" }; String comma = k == parameters.length - 1 ? "" : ","; println(mode[parameters[k].mode.value()] + " " +
toIdl(parameters[k].type_def) + " " + parameters[k].name + comma); } _indent--; org.omg.CORBA.ExceptionDef[] exceptions = def.exceptions(); if(exceptions.length > 0) { println(") raises ("); _indent++; for(int k = 0; k < exceptions.length; k++) { String comma = k == exceptions.length - 1 ? "" : ","; println(exceptions[k].absolute_name() + comma); } _indent--; } println(");"); } private void printInterface(org.omg.CORBA.InterfaceDef idef) throws
org.omg.CORBA.UserException { String superList = ""; { org.omg.CORBA.InterfaceDef[] base_interfaces = idef.base_interfaces(); if(base_interfaces.length > 0) { superList += " :"; for(int j = 0; j < base_interfaces.length; j++) { String comma = j == base_interfaces.length - 1 ? "" : ","; superList += " " + base_interfaces[j].absolute_name() + comma; } } } println("interface " + idef.name() + superList + " {"); _indent++; print(idef); _indent--; println("};"); } private void printModule(org.omg.CORBA.ModuleDef def) throws
org.omg.CORBA.UserException { println("module " + def.name() + " {"); _indent++; print(def); _indent--; println("};"); } }
|
Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|