This is a simple example to get you started with Java CORBA programming using POA transient object support in the ORB. IDL will be used to define Object interfaces.
We will use the IDL from the previous Hello World application.
// 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 -poa -compile -ds gensrc -d ../../lib hello.idl
The idl2java compiler, generates the Java bindings for POA
into 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 HelloPOA // inheritance based POA Skeleton HelloPOATie // delegation based POA Skeleton _HelloStub // Stub
The servant extends the HelloPOA
class and implements the
sayHello
method.
package poaHello; public class HelloImpl extends helloWorld.HelloPOA { public String sayHello() { | return "Hello World!\n"; } }
Notice that by extending HelloPOA
, the servant becomes a
org.omg.PortableServer.Servant
. Also notice that it is
NOT a org.omg.CORBA.Object
, so, you need to create an object
reference using the POA APIs before you use any API like the
orb.object_to_string
which takes a CORBA object as a
parameter.
The server creates the ORB, creates a servant, uses the Root POA to create an object reference, stringifies it and writes it out, and then waits for invocations.
The Root POA is a locality constrained object that can be obtained from
the ORB using the orb.resolve_initial_references
with "RootPOA"
as a parameter.
package poaHello; import util.Util; import org.omg.CORBA.ORB; import org.omg.PortableServer.POA; import org.omg.PortableServer.Servant; public class helloServer { public static void main(String[] args) { | try { | | | | // create the jBroker ORB | | ORB orb = ORB.init(args, null); | | | | // create a servant | | Servant hello = new HelloImpl(); | | | | // get the root POA | | POA rootPOA = (POA) orb.resolve_initial_references("RootPOA"); | | | | // create a stringified object reference | | String helloIOR = orb.object_to_string( | | rootPOA.servant_to_reference(hello)); | | | | // write the stringified object reference | | Util.writeIOR(helloIOR, "ior", true); | | | | // activate the Root POA | | rootPOA.the_POAManager().activate(); | | | | // wait for invocations | | System.out.println("waiting for invocations ..."); | | orb.run(); | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
The rootPOA.the_POAManaget().activate
statement is
optional. The orb.run()
call automatically activates the
root POA.
Since the IDL interface hasn't changed, we will use the client from the Hello World example.
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(); | } } }
We can continue to use the HelloImpl2
from the
helloWorld example.
package helloWorld; public class HelloImpl2 implements HelloOperations { public String sayHello() { | return "Hello World!\n"; } }
Here, creating the servant step is different than before. Notice, that
the TIE object actually extends HelloPOA
.
package poaHello; import util.Util; import org.omg.CORBA.ORB; import org.omg.PortableServer.POA; import org.omg.PortableServer.Servant; import helloWorld.HelloImpl2; import helloWorld.HelloPOATie; public class helloServer2 { public static void main(String[] args) { | try { | | | | // create the jBroker ORB | | ORB orb = ORB.init(args, null); | | | | // create a servant | | Servant hello = new HelloPOATie(new HelloImpl2()); | | | | // get the root POA | | POA rootPOA = (POA) orb.resolve_initial_references("RootPOA"); | | | | // create a stringified object reference | | String helloIOR = orb.object_to_string( | | rootPOA.servant_to_reference(hello)); | | | | // write the stringified object reference | | Util.writeIOR(helloIOR, "ior", true); | | | | // activate the Root POA | | rootPOA.the_POAManager().activate(); | | | | // wait for invocations | | System.out.println("waiting for invocations ..."); | | orb.run(); | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
hello.idl
Hello
// Wed Jun 09 14:46:33 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:33 EDT 2004 package helloWorld; public interface HelloOperations { public java.lang.String sayHello(); }
HelloHelper
// Wed Jun 09 14:46:33 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:33 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(); } }
HelloPOA
// Wed Jun 09 14:46:33 EDT 2004 package helloWorld; /** Stream based POA Skeleton for Hello */ public abstract class HelloPOA extends org.omg.PortableServer.Servant implements helloWorld.HelloOperations, 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[] _all_interfaces(org.omg.PortableServer.POA poa, byte[] oid) { | return __ids; } public helloWorld.Hello _this() { | return helloWorld.HelloHelper.narrow(_this_object()); } public helloWorld.Hello _this(org.omg.CORBA.ORB orb) { | return helloWorld.HelloHelper.narrow(_this_object(orb)); } }
HelloPOATie
// Wed Jun 09 14:46:33 EDT 2004 package helloWorld; /** Stream based POA TIE Skeleton for Hello */ public class HelloPOATie extends helloWorld.HelloPOA { private helloWorld.HelloOperations _delegate; public HelloPOATie(helloWorld.HelloOperations delegate) { | this._delegate = delegate; } public helloWorld.HelloOperations _delegate() { | return _delegate; } public void _delegate(helloWorld.HelloOperations delegate) { | _delegate = delegate; } private org.omg.PortableServer.POA _poa; public HelloPOATie(helloWorld.HelloOperations delegate, org.omg.PortableServer.POA poa) { | this._poa = poa; | this._delegate = delegate; } public org.omg.PortableServer.POA _default_POA() { | if (_poa != null) return _poa; | | return super._default_POA(); } 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.
The _servant_preinvoke
and _servant_postinvoke
methods
give the ORB the opportunity to enfore the POA semantics of POA Manager, adapter
activation, object activation etc.
// Wed Jun 09 14:46:33 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 © 2000-2004, Novell, Inc. All rights reserved. |