Oracle8i Enterprise JavaBeans and CORBA Developer's Guide Release 8.1.5 A64683-01 |
|
Here is the README file for the session examples:
The examples in the session/ directories demonstrate various CORBA programming techniques that you can use to create and manage sessions in Oracle8i. The examples are short, and each example shows just one, or at the most a few aspects of Oracle8i CORBA session handling. The examples are mostly slight variants on the basic helloworld example. None of these examples do any database access. You should first study the 'explicit' example. This example shows you how to use JNDI to connect and activate a CORBA object by doing each step of the process explicitly. In the other, basic/ examples, things such as authentication are done automatically for you, for example when you specify NON_SSL_LOGIN as the authentication method in the Initial Context. Running the Examples ==================== To run the examples, you must have access to an Oracle8i database server that hosts the Oracle8i server-side Java VM. The SCOTT schema must have write access to the CORBA name space starting at the 'test' directory, which is true of the install database. The tables that support the publishing directories are established when your Oracle8i system with the Java server option is built. You can use the Session Shell to verify the presence of the test directory. See the Oracle8i EJB and CORBA Developer's Guide for information about the Session Shell. You must also have the INIT.ORA, tnsnames.ora, and listener.ora files configured properly to accept both standard TTC as well as IIOP incoming connections. This is done for you in the install test database. See the Net8 Administrator's Guide for information about setting up these files. For simplicity. most of these examples connect directly to the dispatcher port. Your production code should use the listener for better scalability. Each example publishes one or more objects in the database. To lookup and activate the published object, the client uses the Oracle8i JNDI interface to the CosNaming implementation. The Makefiles or batch files provided with the examples expect that you have the java and javac commands from the Sun JDK 1.1.x (with x >= 3) in your PATH. They also expect that your CLASSPATH contains the Java runtime classes (classes.zip) corresponding to your java interpreter. The makefiles/batch files take care of adding the ORACLE specific jar and zip files to your CLASSPATH. For reference here is a list of jar and zip files that the makefiles/batch files use: ORACLE_HOME/lib/aurora_client.jar # Oracle 8i ORB runtime ORACLE_HOME/lib/aurora.jar # Oracle 8i in-the-database runtime ORACLE_HOME/jdbc/lib/classes111.zip # for JDBC examples ORACLE_HOME/sqlj/lib/translator.zip # for SQLJ examples ORACLE_HOME/lib/vbjapp.jar # Inprise VisiBroker library ORACLE_HOME/lib/vbjorb.jar # VisiBroker library ORACLE_HOME/lib/vbj30ssl.jar # required if you modify any # client code to use SSL The example programs are: explicit - shows how to get the JNDI initial context, authenticate the client explicitly using a login server objct and a client proxy login object (and stub), create a session "by hand", and so on. Study this example carefully. clientserverserver - create a new session from within a server object. timeout - client sets the session timeout value from the server object. sharedsession - client writes an object reference to a file, and a second client reads the ref, and uses it to invoke a method on the object in the session started by the first client. twosessions - client creates two separate sessions explicitly, and invokes a method on an object in each session. twosessionsbyname - client creates two separate named sessions, and activates a separate object in each session. This example uses the SessionCtx login method to authenticate the client, rather than the fully explicit login object activation used in the twosessions example. The code in the examples is not always commented, but each of the examples has its own readme file. The readme explains what the code does, and points out any special features used in the example. Each of these examples has been tested on Solaris 2.6 and Windows NT 4.0. If you have problems compiling or running the examples on these or on another supported platform, please inform your Oracle support representative.
Overview ======== Demonstrates how a client can activate a CORBA server object explicitly, and the use of the login object for client authentication. Compare this example to the ../examples/corba/basic/helloworld case. In the basic example, only three client-side call are made to lookup and activate a server object, and then invoke one of its methods: Context ic = new InitialContext(env); Hello hello = (Hello) ic.lookup(serviceURL + objectName); System.out.println(hello.helloWorld ()); This example makes explicit much that is handled "under the covers" in the simple helloworld example. You should study this example before going on to the other examples in this CORBA sessions directory. Source files ============ hello.idl --------- The CORBA IDL for the example. The IDL for the Hello interface simply defines one method: interface Hello wstring helloWorld() which must be implemented by the helloServer.HelloImpl.java code. Client.java ----------- You invoke the client program from a command line prompt, and pass it four arguments, the - service URL (service ID, hostname, and port) - name of the published object to lookup and instantiate - username - password that authenticate the client to the Oracle8i database server For example: % java -classpath LIBs Client sess_iiop://localhost:2481:ORCL \ /test/myHello scott tiger where LIBs is the classpath that must include $ORACLE_HOME/lib/aurora_client.jar $ORACLE_HOME/jdbc/lib/classes111.zip $ORACLE_HOME/lib/vbjorb.jar $ORACLE_HOME/lib/vbjapp.jar $JAVA_HOME/lib/classes.zip The client code performs the following steps: - gets a JNDI Context (InitialContext()) - looks up the service URL to get a ServiceCtx (service context) object - creates a session context. This activates a new session in the server. - activates a login server object - creates a new client-side login object - authenticates the client (login.authenticate()) - activates a Hello object - invokes the helloWorld() method on the Hello object, and print the results The printed output is: Hello World! helloServer/HelloImpl.java -------------------------- This file implements the method specified in the hello.idl file: helloWorld(). It simple returns the greeting to the client. 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.
module hello { interface Hello { wstring helloWorld (); }; };
import hello.Hello; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.jndi.sess_iiop.SessionCtx; import oracle.aurora.AuroraServices.LoginServer; import oracle.aurora.client.Login; 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 != 4) { System.out.println ("usage: Client serviceURL objectName user password"); System.exit (1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; // Prepare a simplified Initial Context as we are going to do // everything by hand Hashtable env = new Hashtable (); env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); Context ic = new InitialContext (env); // Get a SessionCtx that represents a database instance ServiceCtx service = (ServiceCtx)ic.lookup (serviceURL); // Create a session in the instance. The session name must start by a : SessionCtx session = (SessionCtx)service.createSubcontext (":session1"); // Activate the LoginServer object at the well known name etc/login LoginServer login_server = (LoginServer)session.activate ("etc/login"); // Create the login client and authenticate with the login protocol Login login = new Login(login_server); login.authenticate (user, password, null); // Activate the Hello object and call its helloWorld method Hello hello = (Hello)session.activate (objectName); System.out.println (hello.helloWorld ()); } }
package helloServer; import hello.*; import oracle.aurora.AuroraServices.ActivatableObject; public class HelloImpl extends _HelloImplBase implements ActivatableObject { public String helloWorld () { return "Hello World!"; } public org.omg.CORBA.Object _initializeAuroraObject () { return this; } }
clientserverserver demonstrates: (1) A CORBA server object that instantiates a second session in the same server, and calls methods on it. The basic structure of this example is a client program that instantiates a server object, then invokes a method on it that sets a String to "Hello World!". The client then invokes the getOtherHello() method on the server object. This method takes the authentication and service identifier information from the client, and creates a second server object *in a different session*. Source files ============ hello.idl --------- The CORBA IDL for the example. Defines an interface, Hello, with 4 methods: interface Hello wstring helloWorld(); void setMessage ( in wstring message); void getOtherHello ( in wstring user, in wstring password, in wstring objectURL) raises (AccessError); wstring otherHelloWorld() and one exception: AccessError. Client.java ----------- The client looks up and instantiates a Hello CORBA server object. The client then invokes setMessage() on this object to set its message variable. Next the client invokes getOtherHello(), to have the first CORBA server object create a second Hello object. The first server Hello object will set a different message in the message instance variable. The client finally calls otherHelloWorld() on the first object, which indirectly returns the message set in the second object. The result of all this is that client prints: Hello World! Hello from the Other Hello Object on its console. helloServer/HelloImpl.java -------------------------- This server class implements the four methods specified in hello.idl: setMessage() simply sets the class variable message to the input parameter. helloWorld() returns to the client whatever String setMessage set. getOtherHello() takes three parameters: a username, password, and a service URL (e.g. "sess_iiop://<hostname>:<dispatcher_port>"). It then instantiates a second Hello server object, and sets its message variable to "Hello from the Other Hello Object". otherHelloWorld() invokes the helloWorld() method on the second object, and returns its message string to the client. Compiling and Running the Example ================================= On UNIX, enter the command 'make all' or just simply 'make' in the shell to compile, load, and publish the objects, and run the client program. Other targets are 'make compile', 'make load', 'make publish', and 'make run'. On Windows NT, use the batch file to compile, load, publish and run.
module hello { exception AccessError { wstring message; }; interface Hello { wstring helloWorld (); void setMessage (in wstring message); void getOtherHello (in wstring user, in wstring password, in wstring objectURL) raises (AccessError); wstring otherHelloWorld (); }; };
import hello.Hello; 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 != 4) { System.out.println ("usage: Client serviceURL objectName user password"); System.exit (1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; 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); // Activate a Hello in the 8i server // This creates a first session in the server Hello hello = (Hello)ic.lookup (serviceURL + objectName); hello.setMessage ("Hello World!"); System.out.println (hello.helloWorld ()); // Ask the first Hello to activate another Hello in the same server // This creates another session used by the first session hello.getOtherHello (user, password, serviceURL + objectName); System.out.println (hello.otherHelloWorld ()); } }
package helloServer; import hello.*; import oracle.aurora.AuroraServices.ActivatableObject; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.AuroraServices.ActivatableObject; import javax.naming.*; import java.util.*; public class HelloImpl extends _HelloImplBase implements ActivatableObject { String message; Hello otherHello; public String helloWorld () { return message; } public void setMessage (String message) { this.message = message; } public void getOtherHello (String user, String password, String URL) throws AccessError { try { 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); otherHello = (Hello)ic.lookup (URL); otherHello.setMessage ("Hello from the Other Hello Object"); } catch (Exception e) { e.printStackTrace (); throw new AccessError (e.toString ()); } } public String otherHelloWorld () { if (otherHello != null) return otherHello.helloWorld (); else return "otherHello not accessed yet"; } public org.omg.CORBA.Object _initializeAuroraObject () { return this; } }
Overview ======== Timeout shows you how to set the session timeout from a server object. For testing the timeout, a second client is provided. The second client is authenticated using a login IOR that the first client writes to a file.. The basic structure of this example is a client program that instantiates two server objects in separate sessions. Compare this example with the ..corba/session/clientserverserver example, in which the client instantiates a server object, and that server object then instantiates a second server object in a different session. Source files ============ hello.idl --------- The CORBA IDL interface Hello wstring helloWorld () void setMessage (in wstring message) void setTimeOut (in long seconds) Client.java ----------- You invoke the client program from a command line prompt, and pass it seven arguments: - the service URL (service ID, hostname, and port) - the name of the published object to lookup and instantiate - a username - a password that authenticates the client to the Oracle8i database - the name of a file that the client writes the hello IOR into - the name of a file that the client writes the login IOR into - the session timeout value in seconds For example: % java -classpath LIBs Client sess_iiop://localhost:2222 scott tiger hello.ior login.ior 30 where LIBs is the classpath that must include $ORACLE_HOME/lib/aurora_client.jar $ORACLE_HOME/jdbc/lib/classes111.zip $ORACLE_HOME/lib/vbjorb.jar $ORACLE_HOME/lib/vbjapp.jar $JAVA_HOME/lib/classes.zip This first client gets a reference to a Hello object, and sets its message instance variable to "As created by Client1.java". It then sets the session timeout to the number of seconds passed as the sixth parameter. Next, the client writes the stringified hello IOR and login IOR to the file named in the fifth and sixth parameter, then exits. The session remains alive, on account of the timeout parameter. This client program prints Client1: As created by Client1 Set session timeout to 30 seconds Client1: wrote the login IOR Client1: exiting... on its console. Client2.java ------------ The Client2 program reads the IOR for the hello object, and the IOR for the login object. These were written to files by Client1. The login IOR is required because the client uses NON_SSL_LOGIN as the authentication mechanism. This requires that the client2 program get a reference to a login server object, and then instantiate a client-side proxy object to communicate with the server-side login object, in order to authenticate. helloServer/HelloImpl.java -------------------------- Implements the methods specified in hello.idl: String helloWorld() void setMessage(String message) void setTimeOut(int seconds) 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.
module hello { interface Hello { wstring helloWorld (); void setMessage (in wstring message); void setTimeOut (in long seconds); }; };
import hello.Hello; import java.io.*; import javax.naming.*; import java.util.Hashtable; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.client.*; import oracle.aurora.AuroraServices.*; public class Client1 { public static void main (String[] args) throws Exception { if (args.length != 7) { System.out.println ("usage: Client serviceURL objectName user password iorfile loginfile timeout"); System.exit(1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; String iorfile = args [4]; String loginfile = args [5]; int timeout = Integer.parseInt(args [6]); 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); Hello hello = (Hello) ic.lookup(serviceURL + objectName); hello.setMessage("As created by Client1"); System.out.println("Client1: " + hello.helloWorld()); // Make the session survive timeout seconds after its last connection // is dropped. hello.setTimeOut(timeout); System.out.println("Set session timeout to " + timeout + " seconds"); // Write the IOR to a file for Client2.java to access our session OutputStream os = new FileOutputStream(iorfile); org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(); String ior = orb.object_to_string(hello); os.write(ior.getBytes()); os.close(); // create an ior for login object LoginServer lserver = (LoginServer) (ic.lookup(serviceURL + "/etc/login")); String loginior = orb.object_to_string(lserver); OutputStream ls = new FileOutputStream(loginfile); ls.write(loginior.getBytes()); ls.close(); System.out.println("Client1: wrote the login IOR"); System.out.println("Client1: exiting..."); } }
import hello.Hello; import hello.HelloHelper; import java.io.*; import javax.naming.*; import java.util.Hashtable; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.client.*; import oracle.aurora.AuroraServices.*; public class Client2 { public static void main (String[] args) throws Exception { if (args.length != 4) { System.out.println("usage: Client2 user password iorfile loginfile"); System.exit(1); } String user = args [0]; String password = args [1]; String iorfile = args [2]; String loginfile = args [3]; // Initialize the ORB for accessing objects in 8i // You have to initialize the ORB that way. // You will be authenticated using the login IOR read // from the file. org.omg.CORBA.ORB orb = ServiceCtx.init(null, null, null, false, null); // Read the ior from iorfile InputStream is = new FileInputStream(iorfile); byte[] iorbytes = new byte [is.available()]; is.read(iorbytes); is.close(); String ior = new String(iorbytes); System.out.println("Client2: Got the hello IOR"); // Read the login IOR from the loginfile. FileInputStream ls = new FileInputStream(loginfile); byte[] loginbytes = new byte [ls.available()]; ls.read(loginbytes); ls.close(); String loginior = new String(loginbytes); System.out.println("Client2: got the login IOR."); // Try to authenticate try { org.omg.CORBA.Object lobj = orb.string_to_object(loginior); LoginServer lserver = LoginServerHelper.narrow(lobj); org.omg.CORBA.BindOptions lbo = new org.omg.CORBA.BindOptions( false, false); lserver._bind_options(lbo); Login login = new Login(lserver); boolean result = login.authenticate(user, password, null); } catch (Exception e) { System.out.println("Login failed: " + e.getMessage()); System.exit(1); } System.out.println("Client2: authenticated."); // Access the object from the ior and print its message Hello hello = HelloHelper.narrow(orb.string_to_object(ior)); System.out.println("Client2: " + hello.helloWorld()); // Disconnect from the object by exiting System.out.println("Client2: exiting..."); } }
package helloServer; import hello.*; import oracle.aurora.AuroraServices.ActivatableObject; import oracle.aurora.net.Presentation; public class HelloImpl extends _HelloImplBase implements ActivatableObject { String message; public String helloWorld () { return message; } public void setMessage (String message) { this.message = message; } public void setTimeOut (int seconds) { Presentation.sessionTimeout (seconds); } public org.omg.CORBA.Object _initializeAuroraObject () { return this; } }
Overview ======== Sharedsession client1 writes an object reference out to a file. The second client reads the IOR, and uses it to access an object in the same session started by the first client. Sources ======= hello.idl --------- The CORBA IDL for the example. Specifies one interface with two methods: interface Hello wstring helloWorld() void setMessage(in wstring message) Client1.java ------------ There are two client programs in this example. You invoke the first client program (Client1.class) from a command line prompt, and pass it six arguments: - the service URL (service ID, hostname, and port) - the name of a published object to lookup and instantiate - a username (e.g. SCOTT) - a password (e.g. TIGER) - a filename in which to save the hello IOR from this client - a filename in which to save the login IOR This client should be run in the background. Use & in a UNIX shell, or START in NT. For example: % java -classpath LIBs Client sess_iiop://localhost:2222 scott tiger hello.ior login.ior & where LIBs is the classpath that must include $ORACLE_HOME/lib/aurora_client.jar $ORACLE_HOME/jdbc/lib/classes111.zip $ORACLE_HOME/lib/vbjorb.jar $ORACLE_HOME/lib/vbjapp.jar $JAVA_HOME/lib/classes.zip The client looks up and activates a Hello object, then sets its message instance variable to "As created by Client1". The client then writes the stringified IOR to the file specified on the command line. (Note that a client-side ORB has to be specifically activated ( org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init();) to get access to the object_to_string() ORB method.) Then the client loops invoking helloWorld() on its Hello object. At some point, the second client will have changed the message in the object, and that will be visible in the first client's output. The first client then sleeps for 20 seconds, before exiting. Client2.java ------------ You invoke the second client program (Client2.class) from a command line prompt, and pass it four arguments: - a username (e.g. SCOTT) - a password (e.g. TIGER) - a filename from which to read the hello IOR from client1 - a filename from which to read the login IOR from client1 This client sleeps for 5 seconds, then tries to read the hello IOR from the file written by client1. When read, client2 then reads the login IOR, and authenticates itself to the session. The client then sets the message instance variable to "Client2 was here and modified the message". The first client, still running, will print this new message out. helloServer/HelloImpl.java -------------------------- This source file implements the two methods specified in the hello.idl file: setMessage() to set the instance variable message, and helloWorld() to return the value set in message. Compiling and Running the Example ================================= 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.
module hello { interface Hello { wstring helloWorld (); void setMessage (in wstring message); }; };
import hello.Hello; import java.io.*; import javax.naming.*; import java.util.Hashtable; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.client.*; import oracle.aurora.AuroraServices.*; public class Client1 { public static void main (String[] args) throws Exception { if (args.length != 6) { System.out.println ("usage: Client serviceURL objectName user password " + "loginfile iorfile"); System.exit(1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; String loginIORFile = args [4]; String helloIORFile = args [5]; Hashtable env = new Hashtable(); env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); Context ic = new InitialContext (env); LoginServer lserver = (LoginServer)ic.lookup (serviceURL + "/etc/login"); new Login (lserver).authenticate (user, password, null); Hello hello = (Hello)ic.lookup (serviceURL + objectName); hello.setMessage ("As created by Client1"); writeIOR (lserver, loginIORFile); writeIOR (hello, helloIORFile); int i; for (i = 0; i < 10; i++) { System.out.println ("Client1: " + i + ": " + hello.helloWorld ()); Thread.sleep (4000); } System.out.println("Client1: exiting..."); } static public void writeIOR (org.omg.CORBA.Object object, String iorFile) throws Exception { org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (); String ior = orb.object_to_string (object); OutputStream os = new FileOutputStream (iorFile); os.write (ior.getBytes ()); os.close (); } }
import hello.Hello; import hello.HelloHelper; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import javax.naming.*; import java.util.Hashtable; import java.io.*; import oracle.aurora.client.*; import oracle.aurora.AuroraServices.*; public class Client2 { public static void main (String[] args) throws Exception { if (args.length != 4) { System.out.println("usage: Client2 user password loginfile hellofile"); System.exit(1); } String user = args [0]; String password = args [1]; String loginIORfile = args [2]; String helloIORfile = args [3]; // Initialize the ORB for accessing objects in 8i // You have to initialize the ORB that way. // You will be authenticated using the login object IOR retrieved // from the loginfile, so the parameters are null. org.omg.CORBA.ORB orb = ServiceCtx.init (null, null, null, false, null); // Read the IORs from the IOR files String loginIOR = getIOR (loginIORfile); String helloIOR = getIOR (helloIORfile); // Authenticate with the login Object LoginServer lserver = LoginServerHelper.narrow (orb.string_to_object (loginIOR)); lserver._bind_options (new org.omg.CORBA.BindOptions (false, false)); Login login = new Login (lserver); login.authenticate (user, password, null); System.out.println("Client2: authenticated."); // Access the Hello object from its ior and change its message Hello hello = HelloHelper.narrow (orb.string_to_object (helloIOR)); hello.setMessage ("Client2 was here and modified the message"); System.out.println ("Client2: " + hello.helloWorld()); System.out.println("Client2: exiting..."); } // Read an IOR from an IOR file. static String getIOR (String iorFile) throws Exception { // Loop until the ior file is available InputStream is = null; int i; for (i = 0; i < 10; i++) { try { is = new FileInputStream(iorFile); } catch (FileNotFoundException e) {} Thread.sleep(1000); } if (is == null){ System.out.println("Client2 timed out before finding " + iorFile); System.exit(1); } byte[] iorbytes = new byte [is.available ()]; is.read (iorbytes); is.close (); String ior = new String (iorbytes); System.out.println("Client2: got the IOR from " + iorFile); return ior; } }
package helloServer; import hello.*; import oracle.aurora.AuroraServices.ActivatableObject; public class HelloImpl extends _HelloImplBase implements ActivatableObject { String message; public String helloWorld () { return message; } public void setMessage (String message) { this.message = message; } public org.omg.CORBA.Object _initializeAuroraObject () { return this; } }
Overview ======== Twosessions demostrates a client that instantiates two separate sessions in the server, and calls methods on objects in each session. It also demos use of the login object for client authentication. Compare this example to the ../examples/corba/session/clientserverserver example, in which the client instantiates a server object, and that server object then instantiates a second server object in a different session. Source files ============ hello.idl --------- The CORBA IDL for the example. The IDL for the Hello object simply defines two methods: interface Hello wstring helloWorld (); void setMessage (in wstring message); which must be implemented by the helloServer.HelloImpl.java code. Client.java ----------- You invoke the client program from a command line prompt, and pass it four arguments: the service URL (service ID, hostname, and port), the name of the published object to lookup and instantiate, and a username and password that authenticate the client to the Oracle8i database server. For example: % java -classpath LIBs Client sess_iiop://localhost:2222 scott tiger where LIBs is the classpath that must include $ORACLE_HOME/lib/aurora_client.jar $ORACLE_HOME/jdbc/lib/classes111.zip $ORACLE_HOME/lib/vbjorb.jar $ORACLE_HOME/lib/vbjapp.jar $JAVA_HOME/lib/classes.zip The client first obtains a service context in the normal way, by getting a JNDI Context object, and looking up the service context on it, using the service URL (e.g., sess_iiop://localhost:2222). The service context is then used to create new named sessions, :session1 and :session2. On each session, a login server object is instantiated, then a login client is obtained, and the authenticate() method on the login client is used to authenticate the client. Note that this form of authentication is what happens automatically when a server object is instantiated, and the JNDI context is obtained by passing in the username, password, optional database role, and the value NON_SSL_LOGIN in the environmentg hashtable. In this example, because the sessions are instantiated overtly, it is necessary to also do the authentication overtly. After session instantiation and authentication, a Hello object is instantiated in each session, the helloWorld() method is invoked on each, and the returned String is printed on the console. The printed output is: Hello from Session1 Hello from Session2 helloServer/HelloImpl.java -------------------------- This source file implements the two methods specified in the hello.idl file: setMessage() to set the instance variable message, and helloWorld() to return the value set in message. 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.
module hello { interface Hello { wstring helloWorld (); void setMessage (in wstring message); }; };
import hello.Hello; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.jndi.sess_iiop.SessionCtx; import oracle.aurora.AuroraServices.LoginServer; import oracle.aurora.client.Login; 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 != 4) { System.out.println ("usage: Client serviceURL objectName user password"); System.exit (1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; // Prepare a simplified Initial Context as we are going to do // everything by hand Hashtable env = new Hashtable (); env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); Context ic = new InitialContext (env); // Get a SessionCtx that represents a database instance ServiceCtx service = (ServiceCtx)ic.lookup (serviceURL); // Create and authenticate a first session in the instance. SessionCtx session1 = (SessionCtx)service.createSubcontext (":session1"); LoginServer login_server1 = (LoginServer)session1.activate ("etc/login"); Login login1 = new Login (login_server1); login1.authenticate (user, password, null); // Create and authenticate a second session in the instance. SessionCtx session2 = (SessionCtx)service.createSubcontext (":session2"); LoginServer login_server2 = (LoginServer)session2.activate ("etc/login"); Login login2 = new Login (login_server2); login2.authenticate (user, password, null); // Activate one Hello object in each session Hello hello1 = (Hello)session1.activate (objectName); Hello hello2 = (Hello)session2.activate (objectName); // Verify that the objects are indeed different hello1.setMessage ("Hello from Session1"); hello2.setMessage ("Hello from Session2"); System.out.println (hello1.helloWorld ()); System.out.println (hello2.helloWorld ()); } }
package helloServer; import hello.*; import oracle.aurora.AuroraServices.ActivatableObject; public class HelloImpl extends _HelloImplBase implements ActivatableObject { String message; public String helloWorld () { return message; } public void setMessage (String message) { this.message = message; } public org.omg.CORBA.Object _initializeAuroraObject () { return this; } }
Overview ======== Twosessionbyname shows a client that creates two separate server sessions by name, and then does a JNDI lookup() on the sessions, using the names. Compare this example to ../examples/corba/session/twosessions/*. In the twosessionsbyname example, the session name is used to do a short-hand lookup and instantiation of server object by using the session name in the URL parameter of the lookup() method. In the twosessions example, two sessions are created by name, but the names are not used. Sources ======= Client.java ----------- You invoke the client program from a command line prompt, and pass it four arguments: the service URL (service ID, hostname, and port), the name of the published object to lookup and instantiate, and a username and password that authenticate the client to the Oracle8i database server. For example: % java -classpath LIBs Client sess_iiop://localhost:2222 scott tiger where LIBs is the classpath that must include $ORACLE_HOME/lib/aurora_client.jar $ORACLE_HOME/jdbc/lib/classes111.zip $ORACLE_HOME/lib/vbjorb.jar $ORACLE_HOME/lib/vbjapp.jar $JAVA_HOME/lib/classes.zip The client instantiates two sessions by name, then two Hello objects, one in each session, then verifies that the object are different by setting the message instance variable in each object to a different value, and calling helloWorld() on each object, and printing the result. The output of the client program is: Hello from Session1 Hello from Session2 helloServer/HelloImpl.java -------------------------- This source file implements the two methods specified in the hello.idl file: setMessage() to set the instance variable message, and helloWorld() to return the value set in message. 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.
module hello { interface Hello { wstring helloWorld (); void setMessage (in wstring message); }; };
import hello.Hello; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.jndi.sess_iiop.SessionCtx; import oracle.aurora.AuroraServices.LoginServer; import oracle.aurora.client.Login; 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 != 4) { System.out.println ("usage: Client serviceURL objectName user password"); System.exit (1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; // Prepare a simplified Initial Context as we are going to do // everything by hand Hashtable env = new Hashtable (); env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); Context ic = new InitialContext (env); // Get a SessionCtx that represents a database instance ServiceCtx service = (ServiceCtx)ic.lookup (serviceURL); // Create the 2 sessions SessionCtx session1 = (SessionCtx)service.createSubcontext (":session1"); SessionCtx session2 = (SessionCtx)service.createSubcontext (":session2"); // Login the sessions using the shortcut login method session1.login (user, password, null); session2.login (user, password, null); // Activate the objects by usign the fully specified URL that contains // the session name Hello hello1 = (Hello)ic.lookup (serviceURL + "/:session1" + objectName); Hello hello2 = (Hello)ic.lookup (serviceURL + "/:session2" + objectName); // Verify that the objects are indeed different hello1.setMessage ("Hello from Session1"); hello2.setMessage ("Hello from Session2"); System.out.println (hello1.helloWorld ()); System.out.println (hello2.helloWorld ()); } }
package helloServer; import hello.*; import oracle.aurora.AuroraServices.ActivatableObject; public class HelloImpl extends _HelloImplBase implements ActivatableObject { String message; public String helloWorld () { return message; } public void setMessage (String message) { this.message = message; } public org.omg.CORBA.Object _initializeAuroraObject () { return this; } }