Oracle8i CORBA Developer's Guide and Reference
Release 3 (8.1.7)

Part Number A83722-01

Library

Solution Area

Contents

Index

Go to previous page Go to beginning of chapter Go to next page

Pure CORBA Client

This example uses CORBA Naming Service to retrieve any objects instead of JNDI.

README

Overview
========


This example is a variant of the VisiBroker for Java "bank" example,
which simply creates and publishes a factory CORBA object
AccountManager that generates Account objects with some random
balance. The Account object has a method, balance(), that returns the
account "balance".

This example differs from the other basic CORBA examples in this set
in that it does not use JNDI to lookup and activate the published
object. It uses the CosNaming name service instead, along with the
classes that are part of the oracle.aurora.AuroraServices package.


Source files
============


bank.idl
--------

The CORBA IDL for the example.  Defines a single interface Bank with
two interfaces: Account and AccountManager. The AccountManager has a
single method, open(), that creates an Account object, and returns it
to the caller. The Account class has a single method, balance(), that
returns the random balance in the account.

bankServer.AccountManagerImpl.java
----------------------------------

The Java code that implements the AccountManager class.

bankServer.AccountImpl.java
---------------------------

The Java code that implements the Account class.

Client.java
-----------

You invoke the client program from a command prompt, and pass it five
arguments. For example:

% java -classpath LIBs Client localhost 2481 ORCL scott tiger

where LIBs is the classpath that must include

$ORACLE_HOME/lib/aurora_client.jar
#If using Java 2, use classes12.zip instead of classes111.zip
$ORACLE_HOME/jdbc/lib/classes111.zip
$ORACLE_HOME/lib/vbjorb.jar
$ORACLE_HOME/lib/vbjapp.jar
$JAVA_HOME/lib/classes.zip

(Note: for NT users, the environment variables might be %ORACLE_HOME% and
%JAVA_HOME%.)


The client program looks up and accesses the published AccountManager
using the following steps:

(1) Get argument values from the invocation that set:
    (a) the hostname of the server machine
    (b) the GIOP listener port on that server
    (c) the database SID for the Oracle server
    (d) the username in the instance
    (e) the password

(2) Uses the standard resolve_initial_references() method on the ORB to
obtain the NameService of 8i. There are various properties that
control how this initial name service is obtained, as explained in the
doc and in the example code. The Oracle name service object is an instance of a
PublishingContext, which is an Oracle-specific extension to the CosNaming
NamingContext interface. But you can resolve arbitrary references to
any service also using resolve_initial_references().

(The PublishingContext class, along with the other Oracle-specific classes
used in this example, is documented in the JavaDoc that accompanies this
EJB/CORBA product.)


(3) Gets and uses a server login object, which is published under the standard
name /etc/login in all Java-enabled Oracle8i databases. This is done in the
following steps:

     (a) Set the /etc directory and the login object name as members of a
           CosNaming NameComponent array.
     (b) Using this array, resolve the component as a Java Object.
     (c) Narrow it to be a published object type.
     (d) Activate and get the object using the Oracle-specific
         activate_no_helper() method.
     (e) Narrow it to an Oracle LoginServer object.
     (f) Create a client login proxy object.
     (g) Authenticate the client using the login object.


(4) Lookup and activate an AccountManager class, which is published (by the
Makefile) as /test/bank. The steps are the same as those for the login object.

(You can appreciate now how much the Oracle JNDI lookup() method is doing for
you, as it performs steps (2), (3), and (4) in one invocation.)

(5) Get a new Account object, call the balance() method on it to get the
"balance", and print the value to the client console.


Output
======

The printed output is something like:

The balance in Jack.B.Quick's account is $786.68

The actual balance amount is a random number, and will be different
each time you run this program.


Compiling and Running the Example
=================================


UNIX
----

Enter the command 'make all' or simply 'make' in the shell to compile,
load, and deploy the objects, and run the client program.  Other
targets are 'run' and 'clean'.

Make sure that a shell environment variable ORACLE_HOME is set to
point to the home location of the Oracle installation. This is
operating system dependent, so see the Installation documentation that
came with your system for the location. Also, review the README file
for the Oracle database, and the README file for the CORBA/EJB server
(the Oracle8i ORB), for additional up-to-date information.


Windows NT
----------

On Windows NT, run the batch file makeit.bat from a DOS command prompt
to compile, load, and deploy the objects. Run the batch file runit.bat
to run the client program, and see the results.


Make sure that the environment variables %ORACLE_HOME%, %CLASSPATH%,
and %SERVICE% are set appropriately for the DOS command window. You
can set these as either user or system environment variables from the
Control Panel. Double click on System in the Control Panel then on
the Environment tab to set these variables. Start a new DOS window
after setting environment variable values.


See the Installation documentation that came with your Oracle8i system
for the values of these variables. Also, review the README file for
the Oracle database, and the README file for the CORBA/EJB server (the
Oracle8i ORB), for additional up-to-date information.

You can also set an environment variable %JAVA_HOME% to point to the
root of your Java JDK. For example, SET JAVA_HOME=C:\JDK1.1.6.

Bank.IDL

// Bank.idl

module Bank {
  interface Account { float balance(); };
  interface AccountManager { Account open(in string name); };
};

Server Code

AccountManagerImpl.java

package bankServer;

// import the idl-generated classes
import Bank.*;

import java.util.Dictionary;
import java.util.Random;
import java.util.Hashtable;

// Corba specific imports
import org.omg.CORBA.Object;

// Aurora-orb specific imports
import oracle.aurora.AuroraServices.ActivatableObject;

public class AccountManagerImpl
       extends _AccountManagerImplBase
       implements ActivatableObject
{
  private Dictionary _accounts = new Hashtable ();
  private Random _random = new Random ();

  // Constructors
  public AccountManagerImpl () { super (); }        
  public AccountManagerImpl (String name) { super (name); }        

  public Object  _initializeAuroraObject () {
    return new AccountManagerImpl ("BankManager");
  }

  public synchronized Account open (String name) {
    // Lookup the account in the account dictionary.
    Account account = (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.
      _orb ().connect (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;
  }
}

AccountImpl.java

package bankServer;

import Bank.*;

public class AccountImpl extends _AccountImplBase {
  private float _balance;

  public AccountImpl () { _balance = (float) 100000.00; }
  public AccountImpl (float balance) { _balance = balance; }        
  public float balance () { return _balance; }        
}

Client.java

import java.lang.Exception;

import org.omg.CORBA.Object;
import org.omg.CORBA.SystemException;
import org.omg.CosNaming.NameComponent;

import oracle.aurora.client.Login;
import oracle.aurora.AuroraServices.LoginServer;
import oracle.aurora.AuroraServices.LoginServerHelper;
import oracle.aurora.AuroraServices.PublishedObject;
import oracle.aurora.AuroraServices.PublishingContext;
import oracle.aurora.AuroraServices.PublishedObjectHelper;
import oracle.aurora.AuroraServices.PublishingContextHelper;

import Bank.Account;
import Bank.AccountManager;
import Bank.AccountManagerHelper;

public class Client {
  public static void main(String args[]) throws Exception {
    // Parse the args
    if (args.length < 4 || args.length > 5 ) {
      System.out.println ("usage: Client host port username password <sid>");
      System.exit(1);
    }
    String host = args[0];
    String port = args[1];
    String username = args[2];
    String password = args[3];
    String sid = null;
    if(args.length == 5)
      sid  = args[4];

    // Declarations for an account and manager
    Account account = null;
    AccountManager manager = null;
    com.visigenic.vbroker.orb.ORB orb;
    PublishingContext rootCtx = null;

    // access the Aurora Names Service
    try {
      // Initialize the ORB
      String initref;
      initref = (sid == null) ? "iioploc://" + host + ":" + port :
	"iioploc://" + host + ":" + port + ":" + sid;
      System.setProperty("ORBDefaultInitRef", initref);

      /*
       * Alternatively the following individual properties can be set
       * which take precedence over the URL above
      System.setProperty("ORBBootHost", host);
      System.setProperty("ORBBootPort", port);
      if(sid != null)
	System.setProperty("ORACLE_SID", sid);
       */

      /*
       * Some of the other properties that you can set
       System.setProperty("ORBNameServiceBackCompat", "false");
       System.setProperty("USE_SERVICE_NAME", "true");
       System.setProperty("ORBUseSSL", "true");
       System.setProperty("TRANSPORT_TYPE", "sess_iiop");
       */

      orb = oracle.aurora.jndi.orb_dep.Orb.init();
      // Get the Name service Object reference
      rootCtx = PublishingContextHelper.narrow(orb.resolve_initial_references(
							  "NameService"));
      // Get the pre-published login object reference
      PublishedObject loginPubObj = null;    
      LoginServer serv = null;
      NameComponent[] nameComponent = new NameComponent[2];
      nameComponent[0] = new NameComponent ("etc", "");
      nameComponent[1] = new NameComponent ("login", "");

      // Lookup this object in the Name service
      Object loginCorbaObj = rootCtx.resolve (nameComponent);

      // Make sure it is a published object
      loginPubObj = PublishedObjectHelper.narrow (loginCorbaObj);

      // create and activate this object (non-standard call)
      loginCorbaObj = loginPubObj.activate_no_helper ();
      serv = LoginServerHelper.narrow (loginCorbaObj);
      
      // Create a client login proxy object and authenticate to the DB
      Login login = new Login (serv);
      login.authenticate (username, password, null);
      
      // Now create and get the bank object reference
      PublishedObject bankPubObj = null;
      nameComponent[0] = new NameComponent ("test", "");
      nameComponent[1] = new NameComponent ("bank", "");

      // Lookup this object in the name service
      Object bankCorbaObj = rootCtx.resolve (nameComponent);

      // Make sure it is a published object
      bankPubObj = PublishedObjectHelper.narrow (bankCorbaObj);

      // create and activate this object (non-standard call)
      bankCorbaObj = bankPubObj.activate_no_helper ();
      manager = AccountManagerHelper.narrow (bankCorbaObj);
      
      account = manager.open ("Jack.B.Quick");

      float balance = account.balance ();
      System.out.println ("The balance in Jack.B.Quick's account is $" 
                          + balance);      
    } catch (SystemException e) {
      System.out.println ("Caught System Exception: " + e);
      e.printStackTrace ();            
    } catch (Exception e) {
      System.out.println ("Caught Unknown Exception: " + e);
      e.printStackTrace ();            
    }
  }
}


Go to previous page
Go to beginning of chapter
Go to next page
Oracle
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.

Library

Solution Area

Contents

Index