Programming CORBA with Orbacus on Linux
Activation of servants
There are some important methods in the classes generated by the IDL compiler.
Implicite activation using _this()
The simplest case of object activation is to use the _this() method:
X_ptr X_impl::_this();
The method call creates a CORBA object under the root POA, registers the implementation in the
root POA and creates and returns an object reference for the new object. The caller is responsible
for releasing this reference after use.
The object is created as a transient object, that means it exists only for the lifetime of the
POA in which it was created.
A typical usage is the following sequence:
X_impl* impl = new X_impl();
X_var xv = impl->_this();
This simple method is shown in the server of an example.
Note that you have to activate at least one POA. In simple cases, this may be the root POA
which already exists and just have to be activated, as the example server shows.
Explicite activation using POAs
A POA offers two methods for explicit activation of object implementations. You can
activate the object with or without an object ID:
PortableServer::ObjectId_ptr PortableServer::POA::activate_object(Servant)
PortableServer::POA::activate_object_with_id(CORBA::ObjectID_ptr, Servant)
Both methods increments the reference counter of the servant.
Explicite activation using POAs, without object ID
The methods raises some exceptions, so a typical usage for activation without object IDs
is the following:
PortableServer::POA_var poa = <>get POA from somewhere>
try {
// create implementation
X_impl* impl = new X_impl();
// activate object
PortableServer::ObjectId_var oid;
oid = poa->activate_object(impl);
// get a reference
CORBA::Object_var obj = rootPOA->id_to_reference(oid);
X_var servant = X::_narrow(obj);
if (CORBA::is_nil(servant)){
// handle error
}
} catch (const PortableServer::POA::WrongPolicy& e){
// handle error
} catch (const PortableServer::POA::ServantAlreadyActive&){
// handle error
}
This activation method is shown in the server of an example.
Explicite activation using POAs, with object ID
If you want to specify an object ID, the following is a typical use:
PortableServer::POA_var poa = <>get POA from somewhere>
try {
// create implementation
X_impl* impl = new X_impl();
// create object ID
PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId("ISrv");
// activate object
poa->activate_object_with_id(oid, impl);
// get a reference
X_var servant = impl->_this();
} catch (const PortableServer::POA::WrongPolicy& e){
// handle error
} catch (const PortableServer::POA::ServantAlreadyActive&){
// handle error
} catch (const PortableServer::POA::ObjectAlreadyActive&){
// handle error
}
If the implementation is activated on a POA different than the root POA, you have to override
the method _default_POA() in the implementation to return the different POA on which
the implementation was activated, see an example. If you do not
override this method, calling _this() would errorneously activate the implementation
implicitly on the root POA!
Explicite activation, getting references
A disadvantage of both methods is that they are activate the implementation, but without
returning a reference to it. A reference can be got in two ways. The first uses a conversion
function from the returned object ID and is used in the example.
// create implementation
X_impl* impl = new X_impl();
// activate object
PortableServer::ObjectId_var oid;
oid = poa->activate_object(impl);
// get a reference
CORBA::Object_var obj = rootPOA->id_to_reference(oid);
X_var servant = X::_narrow(obj);
if (CORBA::is_nil(servant)){
// handle error
}
The second use the _this() method to get the reference. If a servant is already
activated, this method does not re-activate it, but simply returns the reference instead:
// create implementation
X_impl* impl = new X_impl();
// activate object
poa->activate_object(impl);
// get a reference
X_var servant = impl->_this();
This method is used in the example. Note that,
if the implementation is activated on a POA different than the root POA, you have to override
the method _default_POA() in the implementation to return the different POA on which
the implementation was activated (see how this is done in the example, Hallo_impl.cpp).
If you do not
override this method, calling _this() would errorneously activate the implementation
implicitly on the root POA!
back to CORBA page
|