This is a simple example to get you started with Java CORBA programming using simple transient object support in the ORB. IDL will be used to define Object interfaces.
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(); }; };
Run the idl2java
compiler on the hello.idl
file to generate the Java
bindings from the above IDL.
idl2java -compile -ds gensrc -d ../../lib hello.idl
The 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
The servant extends the _HelloImplBase
class and implements the
sayHello
method.
package helloWorld; public class HelloImpl extends _HelloImplBase { public String sayHello() { | return "Hello World!\n"; } }
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.
The server creates the ORB, creates a CORBA object, writes out it's stringified object reference, and then waits for invocations.
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(); | } } }
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 connect it up.
The client creates the ORB, reads the stringified object reference of
the Hello
object, narrows it to the Hello
type, and
then invokes the sayHello
method on it.
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 idl2java compiler generates both inheritance as well as delegation based skeletons. Above, we saw how to write the servant using the inheritance based skeleton. Here we will use the delegation based skeleton. It is often referred as the TIE approach.
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.
Hello Implementation
The servant implements the
HelloOperations
interface.package helloWorld; public class HelloImpl2 implements HelloOperations { public String sayHello() { | return "Hello World!\n"; } }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(); | } } }Code Generated from hello.idl
Hello
// Wed Jun 09 14:46:39 EDT 2004 package helloWorld; public interface Hello extends org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity, helloWorld.HelloOperations { }
HelloOperations
// Wed Jun 09 14:46:39 EDT 2004 package helloWorld; public interface HelloOperations { public java.lang.String sayHello(); }
HelloHelper
// Wed Jun 09 14:46:39 EDT 2004 package helloWorld; /** Helper for Hello */ public final class HelloHelper { // get the singleton ORB private static final org.omg.CORBA.ORB _orb() { | return org.omg.CORBA.ORB.init(); } // read Hello public static helloWorld.Hello read(org.omg.CORBA.portable.InputStream _is) { | return helloWorld.HelloHelper.narrow(_is.read_Object()); } // write Hello public static void write(org.omg.CORBA.portable.OutputStream _os, helloWorld.Hello _value) { | _os.write_Object(_value); } private static org.omg.CORBA.TypeCode _type; // typecode for Hello public static org.omg.CORBA.TypeCode type() { | if (_type == null) { | | synchronized(org.omg.CORBA.TypeCode.class) { | | | if (_type == null) { | | | | _type = _orb().create_interface_tc(id(), "Hello"); | | | } | | } | } | | return _type; } // insert Hello public static void insert(org.omg.CORBA.Any any, helloWorld.Hello value) { | org.omg.CORBA.portable.OutputStream os = any.create_output_stream(); | any.type(type()); | write(os, value); | any.read_value(os.create_input_stream(), type()); } // extract Hello public static helloWorld.Hello extract(org.omg.CORBA.Any any) { | return read(any.create_input_stream()); } // repository Id for Hello public static String id() { | return "IDL:helloWorld/Hello:1.0"; } // narrow to Hello public static helloWorld.Hello narrow(org.omg.CORBA.Object _object) { | if (_object == null) return null; | | // check for expected Stub | if (_object.getClass() == _stubClass) | return (helloWorld.Hello) _object; | | // test for type | if (!_object._is_a(id())) | throw new org.omg.CORBA.BAD_PARAM(); | | // create the stub | helloWorld._HelloStub stub = null; | try { | | stub = (helloWorld._HelloStub) _stubClass.newInstance(); | } catch (Exception ex) { | | throw new org.omg.CORBA.UNKNOWN(ex.toString()); | } | | // set the delegate in the stub | org.omg.CORBA.portable.Delegate delegate = | ((org.omg.CORBA.portable.ObjectImpl) _object)._get_delegate(); | stub._set_delegate(delegate); | | return stub; } private static java.lang.Class _stubClass = helloWorld._HelloStub.class; }
HelloHolder
// Wed Jun 09 14:46:39 EDT 2004 package helloWorld; /** Holder for Hello */ public final class HelloHolder implements org.omg.CORBA.portable.Streamable { // instance variable public helloWorld.Hello value; // constructors public HelloHolder() {} public HelloHolder(helloWorld.Hello value) { | this.value = value; } // _read method public void _read(org.omg.CORBA.portable.InputStream is) { | value = helloWorld.HelloHelper.read(is); } // _write method public void _write(org.omg.CORBA.portable.OutputStream os) { | helloWorld.HelloHelper.write(os, value); } // _type method public org.omg.CORBA.TypeCode _type() { | return helloWorld.HelloHelper.type(); } }
_HelloImplBase
// Wed Jun 09 14:46:39 EDT 2004 package helloWorld; /** Stream based Skeleton for Hello */ public abstract class _HelloImplBase extends org.omg.CORBA.portable.ObjectImpl implements helloWorld.Hello, org.omg.CORBA.portable.InvokeHandler { // the method dispatch table private static final java.util.Map _mtable = new java.util.HashMap(); static { | _mtable.put("sayHello", new java.lang.Integer(0)); } // dispatch to invoke handler public org.omg.CORBA.portable.OutputStream _invoke(String _method, org.omg.CORBA.portable.InputStream _is, org.omg.CORBA.portable.ResponseHandler _rh) { | org.omg.CORBA.portable.OutputStream _os = null; | // get the method Id | | java.lang.Integer _methodId = (java.lang.Integer) _mtable.get(_method); | if (_methodId == null) throw new org.omg.CORBA.BAD_OPERATION(); | | // invoke the method | switch (_methodId.intValue()) | { | | case 0: { | | | | | | // invoke the method | | | java.lang.String _result = this.sayHello(); | | | | | | // create the output stream | | | _os = _rh.createReply(); | | | | | | // marshal the result | | | _os.write_string(_result); | | } | | break; | } | | // return the output stream | return _os; } private static final java.lang.String __ids[] = { | "IDL:helloWorld/Hello:1.0" }; public java.lang.String[] _ids() { | return __ids; } }
_HelloTie
// Wed Jun 09 14:46:39 EDT 2004 package helloWorld; /** Stream based TIE Skeleton for Hello */ public class _HelloTie extends helloWorld._HelloImplBase { private helloWorld.HelloOperations _delegate; public _HelloTie(helloWorld.HelloOperations delegate) { | this._delegate = delegate; } public helloWorld.HelloOperations _delegate() { | return _delegate; } public void _delegate(helloWorld.HelloOperations delegate) { | _delegate = delegate; } public java.lang.String sayHello() { | return _delegate.sayHello(); } }
_HelloStub
The idl2java compiler by default generates stubs that are enabled for local invocation. For locally running objects, marshaling/unmarshaling of parameters and IIOP is bypassed to do a direct call on the servant object. You can disable this by using the -nolocal flag on the idl2java compiler.
// Wed Jun 09 14:46:39 EDT 2004 package helloWorld; /** Stub for Hello */ public class _HelloStub extends org.omg.CORBA.portable.ObjectImpl implements helloWorld.Hello { public _HelloStub() { | super(); } public java.lang.String sayHello() { | _local_stub: | | if (_get_delegate().is_local(this)) { | | | | // get the Servant Object | | org.omg.CORBA.portable.ServantObject _servObj = | | _servant_preinvoke("sayHello", helloWorld.HelloOperations.class); | | if (_servObj == null) break _local_stub; | | | | try { | | | | | | // invoke on the servant | | | return ((helloWorld.HelloOperations) _servObj.servant).sayHello(); | | | | | } finally { | | | _servant_postinvoke(_servObj); | | } | } | | org.omg.CORBA.portable.InputStream _is = null; | | try { | | try { | | | // create a request | | | org.omg.CORBA.portable.OutputStream _os = | | | _request("sayHello", true); | | | | | | // do the invocation | | | _is = _invoke(_os); | | | | | | // unmarshal the result | | | java.lang.String _result = _is.read_string(); | | | | | | // return the result | | | return _result; | | | | | } catch (org.omg.CORBA.portable.ApplicationException _ex) { | | | | | | // read the exception id | | | _is = _ex.getInputStream(); | | | String _id = _ex.getId(); | | | | | | // no matching exception found | | | throw new org.omg.CORBA.UNKNOWN(_id); | | | | | } catch (org.omg.CORBA.portable.RemarshalException _rex) { | | | return sayHello(); | | } | } finally { | | _releaseReply(_is); | } } private static final java.lang.String __ids[] = { | "IDL:helloWorld/Hello:1.0" }; public java.lang.String[] _ids() { | return __ids; } }
Copyright © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.