Professional Documents
Culture Documents
Getting started
You will need two things: version 1.2 of the JDK software and the idltojava compiler.
The JDK provides the API and ORB needed to enable CORBA-based distributed object
interaction. The idltojava compiler uses the IDL-to-Java mapping to convert IDL
interface definitions to corresponding Java interfaces, classes, and methods, which you
can then use to implement your client and server code.
module HelloApp
{
interface Hello
{
string sayHello();
};
};
OMG IDL is a purely declarative language designed for specifying programminglanguage-independent operational interfaces for distributed applications. OMG specifies
a mapping from IDL to several different programming languages, including C, C++,
Smalltalk, COBOL, Ada, and Java. When mapped, each statement in OMG IDL is
translated to a corresponding statement in the programming language of choice. You can
use the tool idltojava to map an IDL interface to Java and implement the client class.
When you map the same IDL to C++ and implement the server in that language, the Java
client and C++ server interoperate through the ORB as though they were written in the
same language.
Steps needed to write the IDL for the Hello World application
Step 1: Declaring the CORBA IDL Module
A CORBA module is a namespace that acts as a container for related interfaces and
declarations. It corresponds closely to a Java package. Each module statement in an
IDL file is mapped to a Java package statement.
module HelloApp {
// Add subsequent lines of code here.
};
Step 2: Declaring the Interface
Like Java interfaces, CORBA interfaces declare the API contract that an object has with
other objects. Each interface statement in the IDL maps to a Java interface statement
when mapped.
In your Hello.idl file, enter the interface statement:
module HelloApp {
interface Hello // Add
{
// these
// four
};
// lines.
};
When you compile the IDL, this statement will generate an interface statement in the
Java code. Your client and server classes will implement the Hello interface in different
ways.
Step 3: Declaring the Operations
CORBA operations are the behavior that servers promise to perform on behalf of clients
that invoke them. Each operation statement in the IDL generates a corresponding
method statement in the generated Java interface.
In your Hello.idl file, enter the operation statement:
module HelloApp {
interface Hello
{
string sayHello(); // Add this line.
};
// notice the semicolon
};
//notice the semicolon
Because our little Hello World application has only a single operation, Hello.idl is now
complete.
Mapping Hello.idl from IDL to Java
The tool idltojava reads OMG IDL files and creates the required Java files. The idltojava
defaults are set up so that if you need both client and server files (as you do for our Hello
World program), you simply enter the tool name and the name of your IDL file:
1.Go to a command line prompt.
2.Change directory to the location of your Hello.idl file.
3.Enter the compiler command:
idltojava Hello.idl
If you list the contents of the directory, you will see that a directory called HelloApp has
been created and that it contains five files. Open Hello.java in your text editor. It looks
like this:
/* Hello.java as generated by idltojava */
package HelloApp;
public interface Hello
extends org.omg.CORBA.Object {
String sayHello();
}
With an interface this simple, it is easy to see how the IDL statements map to the
generated Java statements.
Mapping of IDL Statements to Java Statements
IDL Statement
Java Statement
module HelloApp
package HelloApp;
interface Hello
public interface Hello
string sayHello();
String sayHello();
The single surprising item is the extends statement. All CORBA objects are derived from
org.omg.CORBA.Object to ensure required CORBA functionality. The required code is
generated by idltojava; you do not need to do any mapping yourself.
HelloHolder.java
This final class holds a public instance member of type Hello. It provides
operations for out and inout arguments, which CORBA has but which do not map easily
to Java's semantics.
When you write the IDL interface, you do all the programming required to generate all
these files for your distributed application. The only additional work required is the
actual implementation of client and server classes.
Troubleshooting
Error Message: "idltojava" not found
If you try to run idltojava on the file Hello.idl and the system cannot find idltojava, it
is most likely not in your executable path. Make certain that the location of idltojava
is in your path, and try again.
Error Message: preprocessor failed
idltojava uses a C/C++ preprocessor by default. You can change the default by setting
two environment variables, CPP and CPPARGS. If you do not want to use a
preprocessor, you can turn it off by using the command:
idltojava -fno-cpp Hello.idl
System.out.println(hello);
3.Save and close HelloClient.java.
}
}
class HelloServant extends _HelloImplBase
{
public String sayHello()
{
return "\nHello world!!\n";
}
}
2.This statement sets the id field of nc, the new NameComponent, to "Hello" and the
kind component to the empty string. Because the path to the Hello has a single element,
create the single-element array that NamingContext.resolve requires for its work:
NameComponent path[] = {nc};
3.Finally, pass path and the servant object to the naming service, binding the servant
object to the "Hello" id:
ncRef.rebind(path, helloRef);
Now, when the client calls resolve("Hello") on the initial naming context, the naming
service returns an object reference to the Hello servant.
Waiting for Invocation
The server is ready; it simply needs to wait around for a client to request its service.
To achieve that, enter the following code at the end of (but within) the try-catch block:
java.lang.Object sync = new java.lang.Object();
synchronized(sync) {
sync.wait();
}
This form of Object.wait requires HelloServer to remain alive (though quiescent) until
an invocation comes from the ORB. Because of its placement in main, after an invocation
completes and sayHello returns, the server will wait again.
Troubleshooting
Specifying ORB Initial Port
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.
Class Definition Not Found Error
If the Java compiler (javac) throws a NoClassDefFoundError, try using -cp
(classpath) command line option when compiling the source files.
javac cp . *.java HelloApp\*.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 the init method rather than in main. Here's the complete code
for the applet version: HelloApplet.java
// The package containing our stubs.
import HelloApp.*;
// HelloClient will use the naming service.
import org.omg.CosNaming.*;
// The package containing special exceptions thrown by the name service.
import org.omg.CosNaming.NamingContextPackage.*;
// All CORBA applications need these classes.
import org.omg.CORBA.*;
// Needed for the applet.
import java.awt.Graphics;
public class HelloApplet extends java.applet.Applet
{
public void init()
{
try{
// Create and initialize the ORB
ORB orb = ORB.init(this, null);
// Get the root naming context
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// Resolve the object reference in naming
NameComponent nc = new NameComponent("Hello", "");
NameComponent path[] = {nc};
Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));
// Call the Hello server object and print the results
message = helloRef.sayHello();
} catch(Exception e) {
System.out.println("HelloApplet exception: " + e);
e.printStackTrace(System.out);
}
}
String message = "";
public void paint(Graphics g)
{
g.drawString(message, 25, 50);
}
}
Setting Up the HTML File (Applets Only)
Tutorial.html is provided for displaying your finished applet, but you need to
customize a few attributes and
parameters.
1.Open Tutorial.html in your text editor.
2.Inside the APPLET tag, enter the path to your project directory as the value for the
CODEBASE attribute.
3.In the first PARAM tag, enter the name of the machine where the CORBA name
server runs (most likely your local machine name) as the value for ORBInitialHost.
4.Make sure the second PARAM tag is set to the value of ORBInitialPort that you
are using to run thename 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.