The New CORBA 10 Step Recipe
http://java.sun.com/j2se/1.4.2/docs/guide/idl/jidlExample.html
Following is a Hello Corba Example that can be found in the
JDK 1.4 distribution. This 'recipe' uses new CORBA features
including the POA Adapter extension and the ORB daemon
in place of tnameserv.
1. Write the
IDL interface
module
HelloApp
{
interface
Hello
{
string sayHello(); // This
line is an operation statement.
oneway void shutdown(); // This line is another
};
};
2.
Translate
using idlj
> idlj
Hello.idl
//
generates
HelloApp directory with CORBA class suite
3. Compile the
Generated Classes
> javac
HelloApp/*.java
4.
Create
Implementation Class
//
extends
HelloPOA rather than the Implementation Base class
import
HelloApp.*;
import
org.omg.CosNaming.*;
import
org.omg.CosNaming.NamingContextPackage.*;
import
org.omg.CORBA.*;
import
org.omg.PortableServer.*;
import
org.omg.PortableServer.POA;
class HelloImpl
extends HelloPOA {
private
ORB orb;
public
void setORB(ORB orb_val) {
orb = orb_val;
}
//
implement sayHello() method
public
String sayHello() {java
return "\nHello world !!\n";
}
//
implement shutdown() method
public
void shutdown() {
orb.shutdown(false);
}
}
5.
Create Server
//
HelloServer.java
//
Copyright and
License
import
HelloApp.*;
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;
public class
HelloServer {
public
static void main(String args[]) {
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// get reference to rootpoa & activate the POAManager
POA rootpoa =
POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
// 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);
System.out.println("HelloServer ready and waiting ...");
// wait for invocations from clients
orb.run();
}
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("HelloServer Exiting ...");
}
}
6.
Create Client
//
Copyright and
License
import
HelloApp.*;
import
org.omg.CosNaming.*;
import
org.omg.CosNaming.NamingContextPackage.*;
import
org.omg.CORBA.*;
public class
HelloClient
{
static
Hello helloImpl;
public
static void main(String args[])
{
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// 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) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
}
7. Compile
classes
> javac HelloImpl.java
> javac HelloServer.java
> javac HelloClient.java
8. Start orbd
Usage: orbd
<options>
where <options> includes:
-port
Activation Port where the ORBD should be started, default 1049
(optional)
-defaultdb
Directory for ORBD files, default "./orb.db" (optional)
-serverid
Server Id for ORBD, default 1 (optional)
-ORBInitialPort
Initial Port (required)
-ORBInitialHost
Initial HostName (required)
// To start orbd from a UNIX command shell, enter:
> orbd -ORBInitialPort 1050&
// From an MS-DOS system prompt (Windows),
enter:
> start orbd -ORBInitialPort 1050 // note 'start'
Note that 1050 is the port on which you want the name server to run.
The -ORBInitialPort argument is a required command-line argument.
9.
Start server
> java HelloServer -ORBInitialPort 1050 -ORBInitialHost localhost&
// on Linux it worked with or without the ampersand
10. start client
> java HelloClient -ORBInitialPort 1050 -ORBInitialHost localhost