This lesson introduces the basics of writing a CORBA client application. Included in this lesson are:
To see a completed version of HelloClient.java, follow the link.
The basic shell of a CORBA client is the same as many Java applications: You import required library packages, declare the application class, define a main() method, and remember to handle any exceptions.
Start your text editor and save a new file titled HelloClient.java to your project directory.
Import the packages required for the client class:
import HelloApp.*; // The package containing our stubs. import org.omg.CosNaming.*; // HelloClient will use the naming service. import org.omg.CORBA.*; // All CORBA applications need these classes.
In HelloClient.java, declare the client class:
public class HelloClient { // Add the main() method here in the next step. }
Every Java application needs a main() method. Declare it within the scope of the HelloClient class, as follows:
public static void main(String args[]) { // Put the try-catch block here in the next step. }
Because all CORBA programs can throw CORBA system exceptions at runtime, you will place all of the main() functionality within a try-catch block. CORBA programs throw system exceptions whenever trouble occurs during any of the processes (marshaling, unmarshaling, upcall) involved in invocation.
Our exception handler simply prints the name of the exception and its stack trace to standard output so you can see what kind of thing has gone wrong.
Inside main(), set up a try-catch block:
try{ // Add the rest of the HelloClient code here. } catch(Exception e) { System.out.println("ERROR : " + e); e.printStackTrace(System.out); }
A CORBA client needs a local ORB object to perform all of its marshaling and IIOP work. Every client instantiates an org.omg.CORBA.ORB object and initializes it by passing to the object certain information about itself.
If you closed HelloClient.java, open it now.
Inside the try-catch block, declare and initialize an ORB variable:
ORB orb = ORB.init(args, null);
The call to the ORB's init() method passes in your application's command line arguments, allowing you to set certain properties at runtime.
Now that the application has an ORB, it can ask the ORB to locate the actual service it needs, in this case the Hello server. There are a number of ways for a CORBA client to get an initial object reference; our client application will use the COS Naming Service specified by OMG and provided with Java IDL. See Lesson 5 for information on how to get an initial object reference when there is no naming service available.
The first step in using the naming service is to get the initial naming context. In the try-catch block, below your ORB initialization, call orb.resolve_initial_references() to get an object reference to the name server:
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
The string "NameService" is defined for all CORBA ORBs. When you pass in that string, the ORB returns the initial naming context, an object reference to the name service.
As with all CORBA object references, objRef is a generic CORBA object. To use it as a NamingContext object, you must narrow it to its proper type. Add the call to narrow() just below the previous statement.
NamingContext ncRef = NamingContextHelper.narrow(objRef);
Here we see the use of an idltojava-generated helper class, similar in function to HelloHelper. The ncRef object is now an org.omg.CosNaming.NamingContext and you can use it to access the naming service and find other services. You will do that in the next step.
Names can have different structures depending upon the implementation of the naming service. Consequently, CORBA name servers handle complex names by way of NameComponent objects. Each NameComponent holds a single part, or element, of the name. An array of NameComponent objects can hold a fully specified path to an object on any computer file or disk system.
To find the Hello server, you first need a NameComponent to hold an identifying string for the Hello server. If you closed HelloClient.java, open it now. Add this code directly below the call to narrow().
NameComponent nc = new NameComponent("Hello", "");
This statement sets the id field of nc to "Hello" and the kind field to an empty string.
Because the path to the Hello object has just one element, create a single-element array out of nc. The NamingContext.resolve() method requires this array for its work:
NameComponent path[] = {nc};
Finally, pass path to the naming service's resolve() method to get an object reference to the Hello server and narrow it to a Hello object:
Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));
Here you see the HelloHelper helper class at work. The resolve() method returns a generic CORBA object as you saw above when locating the name service itself. Therefore, you immediately narrow it to a Hello object, which is the object reference you need to perform the rest of your work.
CORBA invocations look like a method call on a local object. The complications of marshaling parameters to the wire, routing them to the server-side ORB, unmarshaling, and placing the upcall to the server method are completely transparent to the client programmer. Because so much is done for you by generated code, invocation is really the easiest part of CORBA programming.
String Hello = helloRef.sayHello();
System.out.println(Hello);
To run HelloClient, you need some server files that you have not yet created. These files are provided for you in [Path_to_JDK]/docs/guide/idl/tutorial/app. Copy them as needed to build your project directory.
Windows users note that you should substitute backslashes (\) for the slashes (/) in all paths in this document.
Your project directory should look like this:
Application |-HelloClient.java |-HelloServant.class |-HelloServer.class |-HelloApp |-_HelloImplBase.class |-_HelloStub.class |-Hello.class |-HelloHelper.class |-HelloHolder.class
javac HelloClient.java
tnameserv -ORBInitialPort 1050 &
java HelloServer -ORBInitialPort 1050 &
java HelloClient -ORBInitialPort 1050
The string prints to the command line:
Hello world!!
Remember to stop both server processes before continuing to the next lesson.
The default ORB Initial Port is port 900. If you prefer, you can omit the port specifications if you start the name server on port 900. Using Solaris software, you must become root to start a process on a port under 1024. Remember to exit from root access before continuing with the tutorial if you choose to use this port for your name server.
Previous lesson | Next lesson | Tutorial home | HelloClient.java
Home |