Hello World with Implementation InheritanceOrdinarily, servant classes must inherit from the ImplBase class generated by the idlj compiler. This is inadequate for servant classes that need to inherit functionality from another Java class. The Java programming language allows a class only one superclass and the generated ImplBase class occupies this position. Example 4 illustrates how a servant class can inherit (an implementation) from any Java class. In this example, the HelloServant class inherits its entire implementation from another Java class HelloBasic. At runtime, method requests for HelloServant are delegated to another idljgenerated class. Example 4 is identical to Example 1 except for implementation inheritance enhancements. This page only discusses the code necessary for these enhancements. This section contains:
Interface Definition (Hello.idl)
module HelloApp
{ interface Hello { string sayHello(); }; }; Compile the IDL interface with the command:
idlj -fall Hello.idl
The files generated by this command are discussed in the basic tutorial. Then use the compiler again to generate the Tie bindings:
idlj -fallTIE Hello.idl
The second command generates an additional file in a HelloApp subdirectory: Hello_Tie.java This class acts as the skeleton, receiving invocations from the ORB and delegating them to the servant that actually does the work. Implementing the Server (HelloServer.java)
// Copyright and License
import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; class HelloBasic { public String sayHello() { return "\nHello world !!\n"; } } class HelloServant extends HelloBasic implements HelloOperations { } public class HelloServer { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // create servant and register it with the ORB HelloServant servant = new HelloServant(); Hello helloRef = new Hello_Tie(servant); orb.connect(helloRef); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // bind the Object Reference in Naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; ncRef.rebind(path, helloRef); // wait for invocations from clients java.lang.Object sync = new java.lang.Object(); synchronized (sync) { sync.wait(); } } catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } } } Implementing the Client (HelloClient.java)
// Copyright and License
import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CORBA.*; public class HelloClient { 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"); 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 results String hello = helloRef.sayHello(); System.out.println(hello); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } } Building and Running Hello World The following instructions assume you can use port 1050 for the Java IDL name server. Substitute a different port if necessary. Note that for ports below 1024, you need root access on UNIX machines, and administrator privileges on Windows95 and NT. Note also that the instructions use a slash (/) for path names. Windows95 and NT users should subtitute a backslash (\).
idlj -fall Hello.idl
Then run the idlj compiler again to generate the Tie classes:
idlj -fallTIE Hello.idl
javac *.java HelloApp/*.java
tnameserv -ORBInitialPort 1050&
java HelloServer -ORBInitialPort 1050
java HelloClient -ORBInitialPort 1050
|