1 Hello IDL Interface
Below is the IDL for the Hello World Application. The interface Hello
has one method, sayHello which takes no parameters and returns a
string. The IDL serves as a contract between the object implementation and
its client.
// hello.idl module helloWorld { interface Hello { | string sayHello(); }; };
idl2java -compile -ds gensrc -d ../../lib hello.idlThe idl2java compiler, generates the Java code to the gensrc directory, and compiles the generated Java code to the ../../lib directory. The following classes are created in the helloWorld Java package. Notice that all the Java class names are derived from the IDL interface Hello's name.
Hello // Java interface corresponding to IDL interface HelloOperations // methods on the Java interface HelloHelper // helper methods for narrow, read, write HelloHolder // holder for out and inout parameters _HelloImplBase // inheritance based Skeleton _HelloTie // delegation based Skeleton _HelloStub // Stub
Notice that by extending _HelloImplBase, the servant also implements the org.omg.CORBA.Object interface. So, you can use the servant also as an object reference.package helloWorld; public class HelloImpl extends _HelloImplBase { public String sayHello() { | return "Hello World!\n"; } }
4 Hello World Server
The server creates the ORB, creates a CORBA object, writes out it's
stringified object reference, and then waits for invocations.
The orb.connect(hello) statement is optional. If you try to marshal or stringify the servant/objref that hasn't been connected, the ORB will automatically connects it up.package helloWorld; import util.Util; import org.omg.CORBA.ORB; public class helloServer { public static void main(String[] args) { | try { | | | | // create the jBroker ORB | | ORB orb = ORB.init(args, null); | | | | // create a servant and register with ORB | | Hello hello = new HelloImpl(); | | orb.connect(hello); | | | | // create a stringified object reference | | String helloIOR = orb.object_to_string(hello); | | | | // write the stringified object reference | | Util.writeIOR(helloIOR, "ior", true); | | | | // wait for invocations | | System.out.println("waiting for invocations ..."); | | orb.run(); | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
package helloWorld; import util.Util; import org.omg.CORBA.ORB; public class helloClient { public static void main(String[] args) { | try { | | | | // create the jBroker ORB | | ORB orb = ORB.init(args, null); | | | | // read the stringified object reference | | String helloIOR = Util.readIOR("ior"); | | | | // narrow the stringified object | | Hello hello = HelloHelper.narrow(orb.string_to_object(helloIOR)); | | | | // invoke method on the object | | System.out.println(hello.sayHello()); | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
The advantage of the inheritance based skeleton is that fewer objects are created at runtime. The main disadvantage is that it takes up the single implementation inheritance slot which may be needed for other things.
6.1 Hello Implementation
The servant implements the HelloOperations interface.package helloWorld; public class HelloImpl2 implements HelloOperations { public String sayHello() { | return "Hello World!\n"; } }6.2 Hello World Server
The server, as before, creates the ORB, creates a CORBA object, writes out it's stringified object reference, and then waits for invocations.Here, creating the servant step is different than before. Notice, that the TIE object actually extends _HelloImplBase.
package helloWorld; import util.Util; import org.omg.CORBA.ORB; public class helloServer2 { public static void main(String[] args) { | try { | | | | // create the jBroker ORB | | ORB orb = ORB.init(args, null); | | | | // create a TIEd servant and register with ORB | | Hello hello = new _HelloTie(new HelloImpl2()); | | orb.connect(hello); | | | | // create a stringified object reference | | String helloIOR = orb.object_to_string(hello); | | | | // write the stringified object reference | | Util.writeIOR(helloIOR, "ior", true); | | | | // wait for invocations | | System.out.println("waiting for invocations ..."); | | orb.run(); | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
Copyright © 2000-2003, Novell, Inc. All rights reserved. |