This is a simple example to get you started with Java CORBA programming using simple transient object support in the ORB. Java RMI will be used to define Object interfaces.
package helloWorld2; public interface Hello extends java.rmi.Remote { String sayHello() throws java.rmi.RemoteException; }
package helloWorld2; import java.rmi.RemoteException; import javax.rmi.PortableRemoteObject; public class HelloImpl extends PortableRemoteObject implements Hello { public HelloImpl() throws RemoteException {} public String sayHello() throws RemoteException { | return "Hello World!\n"; } }
package helloWorld2; import util.Util; import org.omg.CORBA.ORB; import org.omg.CORBA.Object; import javax.rmi.PortableRemoteObject; public class helloServer { public static void main(String[] args) { | try { | | | | // create the jBroker ORB | | ORB orb = ORB.init(args, null); | | | | // create a servant | | Hello hello = new HelloImpl(); | | | | // create a stringified object reference | | String helloIOR = orb.object_to_string((Object) | | PortableRemoteObject.toStub(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 helloWorld2; 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 = (Hello) orb.string_to_object(helloIOR); | | | | // invoke method on the object | | System.out.println(hello.sayHello()); | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
Note: When an object is unmarshaled or destringified, the ORB automatically finds the right stub for it. This makes it possible to use Java casts instead of using the narrow method on javax.rmi.PortableRemoteObject. The use of narrow method is ofcourse the standard thing to do. For example,
above can be replaced byHello hello = (Hello) orb.string_to_object(helloIOR);
if desired.Hello hello = (Hello) PortableRemoteObject.narrow(orb.string_to_object( helloIOR), Hello.class);
To generate stubs and skeletons, run the rmi2iiop compiler. The rmi2iiop compiler works with compiled Java class files. It is customary to run the compiler against the remote implementation instead of the remote interface.
rmi2iiop -keepgenerated -ds gensrc -d ../../lib helloWorld2.HelloImplThe following Java classes are genereted in the helloWorld2 package.
_Hello_Stub // the Stub _HelloImpl_Tie // the delegation based Skeleton
Copyright © 2000-2003, Novell, Inc. All rights reserved. |