Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 5 of 6
idlj -fall Hello.idl
idlj is located in your J2SE 1.4 installation's bin directory. This compiler implements the CORBA IDL-to-Java mapping specifications. It produces some Java files as output,
which I briefly describe below:
HelloPOA.java: The abstract class that our server object will extend
HelloHelper.java: Provides auxiliary functionality, notably the narrow() method required to cast CORBA object references to their proper types
HelloHolder.java: Used whenever the Hello interface (type) is an out or an inout parameter to a method defined in IDL
_HelloStub.java: The client stub, providing CORBA functionality for the client
Hello.java and HelloOperations.java: Together provide interfaces that contain the Java version of our IDL interface
As mentioned above, the implementation of the server application consists of two parts: one class that implements the methods from the server interface, and another class that hosts the server object and registers it with the ORB runtime and naming service:
package hello;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.util.Properties;
class HelloImpl extends HelloPOA {
private ORB orb;
public void setORB(ORB orb_val) {
orb = orb_val;
}
// Implement sayHello() method
public String sayHello() {
return "\nHello world !!\n";
}
// Implement shutdown() method
public void shutdown() {
orb.shutdown(false);
}
}
public class HelloServer {
public static void main(String args[]) {
try{
// Create and initialize the ORB
ORB orb = ORB.init(args, System.getProperties());
// Get reference to rootpoa
POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
// Create servant and register it with the ORB
HelloImpl helloImpl = new HelloImpl();
helloImpl.setORB(orb);
// Get object reference from the servant
org.omg.CORBA.Object ref = rootPOA.servant_to_reference(helloImpl);
Hello href = HelloHelper.narrow(ref);
// Get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
// Use NamingContextExt, which is part of the Interoperable
// Naming Service (INS) specification
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
// Bind the Object reference in Naming
String name = "Hello";
NameComponent path[] = ncRef.to_name( name );
ncRef.rebind(path, href);
// Activate the POAManager
rootPOA.the_POAManager().activate();
System.out.println("HelloServer ready and waiting ...");
System.out.println(orb.object_to_string(href));
// Wait for invocations from clients
orb.run();
}
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("HelloServer Exiting ...");
}
}
Here's a sample client to test the server:
package hello;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.util.*;
public class HelloClient
{
static Hello helloImpl;
public static void main(String args[])
{
try{
// Create and initialize the ORB.
ORB orb = ORB.init(args, System.getProperties());
// Get the root naming context.
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
// Use NamingContextExt instead of NamingContext. This is
// part of the Interoperable Naming Service.
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
// Resolve the Object reference in Naming.
String name = "Hello";
helloImpl = HelloHelper.narrow(ncRef.resolve_str(name));
System.out.println("Obtained a handle on server object: " + helloImpl);
System.out.println(helloImpl.sayHello());
helloImpl.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Compile the server and client Java source files. Start the naming service as follows: