class WorldServant extends _WorldImplBase
{
Note that a CORBA::string maps to a Java String, and that
because Java is a "pointer-less, garbage-collected" language, we
need not worry about allocating memory for our return value as
we did in the C++ server.
public String hello() {
return "Greetings from Java World server!!";
}
}
So much for the servant. Now we'll define the public class
that contains our obligatory main method in which we'll
instantiate our servant. Recall that all functions in Java
(including main) must exist within a class. Recall also that
only one class in any source file may be declared public.
public class HelloServer
{
public static void main( String args[] ) {
Exceptions are an inherent part of the Java language, so we
don't have to worry about supporting "exception-less" Java
compilers as we do in C++. We wrap all our main functionality
within a try block.
try {
As with all CORBA applications, we must initialize our ORB.
In Java, we do this by calling the ORB static method,
init. Its first parameter is the array of command-line
arguments passed to main, and its second parameter
is for application-specific properties. These properties
allow a different vendor's ORB implementation to be "plugged in."
Note also the fully qualified package name: all entities from the CORBA IDL module reside in the org.omg.CORBA package.
// Initialize our ORB
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
Let's not waste any time creating our servant!
// Instantiate a servant
WorldServant world = new WorldServant();
Currently, no Java environments support the POA -- the mapping
for it has not been standardized, but it shouldn't be too long.
What that means, basically, is that Java servers only support
"transient" objects. Transient objects only live within the
server process in which they were created. Once that server dies,
the object may never be accessed again. The same is not true of
"persistent" CORBA objects, but they are beyond the scope of this
tutorial. Java's lack of persistent objects makes it a
less-than-ideal language for creating CORBA servers, but it is
perfectly appropriate for building CORBA clients.
An object implementation may be explicitly connected to the ORB by calling the ORB's connect() method, thus allowing the ORB to forward incoming requests to it. Similarly, one may call disconnect() to cause the ORB to reject requests for the object.
// Connect the servant to the ORB
orb.connect(world);
We must somehow publish our object reference so that clients
may find us. Our somewhat crude approach is identical to our C++
server in that we invoke the ORB's
object_to_string() method and write the result to
stdout.
// Write stringified reference to stdout
String str = orb.object_to_string(world);
System.out.println(str);
Because there is no ORB::run() method in the current Java
server-side mapping, we must employ a bit of trickery to
prevent our server application from exiting before any client
gets a chance to access it. Here are a couple of other
alternatives you might consider:
// Force ourself to hang out, allowing clients to be serviced
java.lang.Object sync = new java.lang.Object();
synchronized(sync) {
sync.wait();
}
That concludes our try block. Our simple server simply
catches any old Exception and dumps it to stderr.
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
Let's continue by writing a Java client.
Back to... [ Corba Tutorials ]