|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
This lesson introduces the basics of writing a CORBA client application. Here's the completed version ofHelloClient.java.
Applet Note: While this lesson focuses on writing a CORBA client application, many of the steps are identical to those required for writing applets. The major difference is that the applet code appears in theinitmethod rather than inmain. For information on how to set up the applet's HTML page, see Setting Up the HTML File (Applets Only). Here's the complete code for the applet version:HelloApplet.java.
The basic shell of a CORBA client is the same as many Java applications: You import required library packages, declare the application class, define amainmethod, and remember to handle any exceptions.Importing Required Packages
Start your text editor and save a new file titledHelloClient.javato 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.
Applet Note: If you are writing an applet, you also need to importjava.awt.Graphicsandorg.omg.CosNaming.NamingContextPackage.*. The latter package contains special exceptions thrown by the name service.Declaring the Client Class
InHelloClient.java, declare the client class:public class HelloClient { // Add the main method here in the next step. }
Applet Note: In the applet version of this code,HelloApplet.java, you declare the applet class like this:public class HelloApplet extends java.applet.Applet {
// Put the init() method here in the next step.
}Defining a main Method
Every Java application needs amainmethod. Declare it within the scope of theHelloClientclass, as follows:public static void main(String args[]) { // Put the try-catch block here in the next step. }Handling CORBA System Exceptions
Because all CORBA programs can throw CORBA system exceptions at runtime, you will place all of themainfunctionality within atry-catchblock. CORBA programs throw system exceptions whenever trouble occurs during any of the processes involved in invoking the server from the client.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 atry-catchblock: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 anorg.omg.CORBA.ORBobject and initializes it by passing to the object certain information about itself.Inside
HelloClient.java'stry-catchblock, declare and initialize an ORB variable:ORB orb = ORB.init(args, null);The call to the ORB's
initmethod passes in your application's command line arguments, allowing you to set certain properties at runtime.
Once 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 Using Stringified Object References for information on how to get an initial object reference when no naming service is available.Obtaining the Initial Naming Context
The first step in using the naming service is to get the initial naming context. In thetry-catchblock, below your ORB initialization, callorb.resolve_initial_referencesto get an object reference to the name server: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.org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");Narrowing the Object Reference
As with all CORBA object references,objRefis a generic CORBA object. To use it as aNamingContextobject, you must narrow it to its proper type. Add the call tonarrowjust below the previous statement.NamingContext ncRef = NamingContextHelper.narrow(objRef);Here we see the use of an
idltojava-generated helper class, similar in function toHelloHelper. ThencRefobject is now anorg.omg.CosNaming.NamingContextand you can use it to access the naming service and find other services. You will do that in the next step.Finding a Service in Naming
Names can have different structures depending upon the implementation of the naming service. Consequently, CORBA name servers handle complex names by way ofNameComponentobjects. EachNameComponentholds a single part, or element, of the name. An array ofNameComponentobjects 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
NameComponentto hold an identifying string for the Hello server. Add this code directly below the call tonarrow.NameComponent nc = new NameComponent("Hello", "");This statement sets the
idfield ofnc, the newNameComponent, to "Hello" and thekindfield to an empty string.Because the path to the
Helloobject has just one element, create a single-element array out ofnc. TheNamingContext.resolvemethod requires this array for its work:NameComponent path[] = {nc};Finally, pass
pathto the naming service'sresolvemethod to get an object reference to the Hello server and narrow it to aHelloobject:Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));Here you see the
HelloHelperhelper class at work. Theresolvemethod returns a generic CORBA object as you saw above when locating the name service itself. Therefore, you immediately narrow it to aHelloobject, 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.
- Continuing with the try-catch block in
HelloClient.java, enter the following invocation below the call to the name service'sresolvemethod:String hello = helloRef.sayHello();- Finally, add code to print the results of the invocation to standard output:
System.out.println(hello);- Save and close
HelloClient.java.
Now you're ready to write the server code.Tutorial.htmlis provided for displaying your finished applet, but you need to customize a few attributes and parameters.
- Open
Tutorial.htmlin your text editor.- Inside the APPLET tag, enter the path to your project directory as the value for the
CODEBASEattribute.- In the first
PARAMtag, enter the name of the machine where the CORBA name server runs (most likely your local machine name) as the value forORBInitialHost.- Make sure the second
PARAMtag is set to the value ofORBInitialPortthat you are using to run the name server (it's preset to 1050 to work with the default used in the examples in this trail). In any case, it should be a value above 1028.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.