| This is a cut down version of a little dance I do. Its all
from the documentation supplement.
Paul
--
// in some header file
class CORBAFacade {
public:
// dtor
virtual ~CORBAFacade(void);
static bool bad(void);
static bool bad(CORBA::Environment& ev);
static int ORB_init(void);
static CORBA::Object_ptr bind(const char *name);
protected:
static CORBA::Environment ev;
static bool bad(CORBA_Environment& CEnv);
static CORBA_Object ORB_resolve_initial_references(char *name);
static int Init(void);
private:
static ::CORBA_Environment CEnv;
static CORBA_ORB pOrbObj;
static CORBA_Object pNameSvcObj;
static CORBA::Identifier id;
static bool InitDone;
// ctor
CORBAFacade(void);
// disallowed
const CORBAFacade & operator=(const CORBAFacade &right);
// disallowed
int operator==(const CORBAFacade &right) const;
// disallowed
int operator!=(const CORBAFacade &right) const;
};
template<class T>
T *
CORBAFacade_bind(const char *objectName, T* & objPtr)
{
CORBA::Object_ptr p = CORBAFacade::bind(objectName);
CORBA::Environment ev;
if (CORBA::is_nil(p, ev))
objPtr = T::_nil(ev);
else {
objPtr = T::_narrow(p);
CORBA::release(p, ev);
}
return objPtr;
}
// in some cxx file
CORBA::Object_ptr
CORBAFacade::bind(const char *objectName)
{
if (Init()==-1) {
cerr << "init failed" << endl;
return CORBA::Object::_nil(ev);
}
CosNaming_NameComponent name_comp;
name_comp.id = (char *)objectName; // not pretty
name_comp.kind = 0;
CosNaming_Name name;
name._length = 1;
name._maximum = 1;
name._buffer = &name_comp;
CORBA_Object obj = CosNaming_NamingContext_resolve(pNameSvcObj, &CEnv,
&name);
if (bad(CEnv)) {
cerr << "resolve failed" << endl;
return CORBA::Object::_nil(ev);
}
else {
CORBA::Object_ptr p = OBB::DEC_BOA->create_objref_from_c(obj, ev);
CORBA_Object_release(obj, (CORBA_Environment *)0);
if (bad()) {
cerr << "create objref from C failed" << endl;
return CORBA::Object::_nil(ev);
}
else {
return p;
}
}
}
int
CORBAFacade::Init(void)
{
if (InitDone)
return 0;
if (ORB_init()==-1) {
cerr << "ORB_init failed" << endl;
return -1;
}
pNameSvcObj = ORB_resolve_initial_references("NameService");
if (CORBA_Object_is_nil(pNameSvcObj, (CORBA_Environment *)0)) {
cerr << "nameservice lookup failed" << endl;
return -1;
}
else {
InitDone = true;
return 0;
}
}
CORBA_Object
CORBAFacade::ORB_resolve_initial_references(char *name)
{
CORBA_Object o = CORBA_ORB_resolve_initial_references(pOrbObj, name,
&CEnv);
if (bad(CEnv))
return CORBA_OBJECT_NIL;
else
return o;
}
int
CORBAFacade::ORB_init(void)
{
pOrbObj = ::CORBA_ORB_init(0, (char **)0, CORBA_DEC_ORB_ID, &CEnv);
if (bad(CEnv))
return -1;
return 0;
}
bool
CORBAFacade::bad(void)
{
return ev.exception() != 0;
}
bool
CORBAFacade::bad(CORBA::Environment& ev)
{
return ev.exception() != 0;
}
bool
CORBAFacade::bad(CORBA_Environment& CEnv)
{
if (CEnv._major == CORBA_NO_EXCEPTION)
return false;
else {
CORBA_exception_free(&CEnv);
return true;
}
}
[Posted by WWW Notes gateway]
|
| Paul, thanks for your facade. We've done more or less the same
in our current project - that is we've created two template
classes to encapsulate all those things that you want to do
so often with CORBA clients and servers. Below is our version
of get-objref-from-naming-service for ObjectBroker (we call
out to it from one of the template methods).
(To be fair I should acknowledge Gunnar Olerud and Jacques Schouten
who are doing most of the hard work).
John D.
---------------------------------------------------------
// MODULE: ObjFromNaming
//;
// @@DatatypeInclude
#ifndef INC_OBBUSEFULS_HPP
#include "obbusefuls.hpp"
#endif
// @@End
// @@Includes
// @@End
// @@UserEntry
#include <namesvc.h>
// @@End
// @@UserEntry
//
// THIS NEEDS TO BE UPDATED WHEN THE COSS C++ BINDINGS ARE AVAILABLE
//
//
// retrieve an object reference from the advertisement registry given
// the name of the object
//
const CORBA::Object_ptr
ObjFromNaming (const string &objName)
{
// temporary code -
// lookup the object from the adv. reg.
// convert it to a string
// use the C++ bindings to convert the string to an object
CosNaming_NameComponent nameComp;
CosNaming_Name name;
nameComp.id = (char *) objName.c_str();
nameComp.kind = NULL;
name._length = 1;
name._maximum = 1;
name._buffer = & nameComp;
CORBA::Environment ev;
CORBA_Environment local_ev;
const CORBA_Object obj = CosNaming_NamingContext_resolve (
COSNAMING_OBJECT_NAMECONTEXT,
& local_ev,
& name);
if ((local_ev._major != CORBA_NO_EXCEPTION) || (obj == NULL))
{
/**
string errText = OBB_exception_errortext (
& local_ev,
OBB_FORMAT_80);
reportAndExit( "CosNaming_NamingContext_resolve.", errText);
// reportAndExit( "CosNaming_NamingContext_resolve.");
**/
return CORBA::Object::_nil(ev);
}
const CORBA_string strObj = CORBA_ORB_object_to_string (
CORBA_DEC_ORB_OBJECT,
& local_ev,
obj);
if (local_ev._major != CORBA_NO_EXCEPTION)
return CORBA::Object::_nil(ev);
CORBA_Object_release (obj, & local_ev);
const CORBA::Object_ptr objPtr =
CORBA::DEC_ORB->string_to_object (strObj, ev);
CORBA_free (strObj);
if ((ev.exception()) || (CORBA::is_nil (objPtr, ev)))
return CORBA::Object::_nil(ev);
return objPtr;
}
// @@End
|