Ordinarily, 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
idlj
-generated 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:
HelloServant
to another Java class.
Instructions for compiling and running
the example are provided.
Interface Definition (
Hello.idl
)
module HelloApp { interface Hello { string sayHello(); }; };
Compile the IDL interface with the command:
idlj -fall -ftie Hello.idlThis generates two additional files in a
HelloApp
subdirectory:
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 _HelloTie(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); } } }
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); } } }
idlj -fall -ftie Hello.idl
javac *.java HelloApp/*.java
tnameserv -ORBInitialPort 1050&
java HelloServer -ORBInitialPort 1050
java HelloClient -ORBInitialPort 1050
Home |
Copyright © 1995-98 Sun Microsystems, Inc. All Rights Reserved.