[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference pamsrc::objectbroker_development

Title:ObjectBroker Development - BEA Systems' CORBA
Notice:See note 2 for kit locations; note 4 for training
Moderator:RECV::GUMBELd
Created:Thu Dec 27 1990
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2482
Total number of notes:13057

2458.0. "C++ example dealing with CosNaming on OBB 2.7" by LEMAN::NAUFFRAY () Thu Mar 20 1997 05:37

    Hi,
    
       Does somebody know a pointer to a little C++ example + 
    associated make file using the CosNaming Service other than 
    the inventory example with OBB v2.7. 
    If so, I'm interested in order to avoid do it if already existing
    somewhere.
    
       Thanks a lot.
       James
    
T.RTitleUserPersonal
Name
DateLines
2458.1NETRIX::"[email protected]"Paul HealyFri Mar 21 1997 06:11184
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]
2458.2not the complete example, James, but still...LEMAN::DONALDSONFroggisattva! Froggisattva!Fri Mar 21 1997 09:3395
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

2458.3Many thanks John & PaulLEMAN::NAUFFRAYTue Mar 25 1997 10:011