2 //=============================================================================
4 * @file Servant_Locator.cpp
6 * Implementation of ServantLocator class, used with a POA
7 * having a NON_RETAIN policy.
9 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
11 //=============================================================================
14 #include "Servant_Locator.h"
15 #include "ace/OS_NS_string.h"
17 // Initialization.The dllname is used by the Loactor to load it into
18 // memory. The factory function is the point of entry into the dll and
19 // is used for obtaining the servant. The garbage_collection_function
20 // is used to kill the servant.
22 ServantLocator::ServantLocator (CORBA::ORB_ptr orb
,
23 const ACE_TCHAR
*dllname
,
24 const ACE_TCHAR
*factory_function
,
25 const ACE_TCHAR
*garbage_collection_function
)
26 : orb_ (CORBA::ORB::_duplicate (orb
))
28 // The dll is opened using the dllname passed.
29 if (this->dll_
.open (dllname
) == -1)
32 this->dll_
.error ()));
34 // Obtain the symbol for the function that will get the servant
38 // Cannot go from void* to function pointer directly. Cast the void*
41 void *symbol
= this->dll_
.symbol (factory_function
);
42 intptr_t function
= reinterpret_cast<intptr_t> (symbol
);
45 reinterpret_cast<SERVANT_FACTORY
> (function
);
47 // Obtain the symbol for the function which will destroy the
49 symbol
= this->dll_
.symbol (garbage_collection_function
);
50 function
= reinterpret_cast<intptr_t> (symbol
);
52 servant_garbage_collector_
=
53 reinterpret_cast<SERVANT_GARBAGE_COLLECTOR
> (function
);
56 // This method associates an servant with the ObjectID.
58 PortableServer::Servant
59 ServantLocator::preinvoke (const PortableServer::ObjectId
&oid
,
60 PortableServer::POA_ptr poa
,
61 const char * /* operation */,
62 PortableServer::ServantLocator::Cookie
&cookie
)
64 PortableServer::Servant servant
=
65 (*servant_supplier_
) (oid
,
70 // Return the servant as the cookie , used as a check when
71 // postinvoke is called on this ServantLocator.
77 throw CORBA::OBJECT_NOT_EXIST ();
80 // Since the servant gets invoked per operation, the servant has to be
81 // destroyed per operation too. This is accomplished in the
85 ServantLocator::postinvoke (const PortableServer::ObjectId
&oid
,
86 PortableServer::POA_ptr poa
,
87 const char * /* operation */,
88 PortableServer::ServantLocator::Cookie cookie
,
89 PortableServer::Servant servant
)
91 // Check the passed servant with the cookie.
92 PortableServer::Servant my_servant
=
93 reinterpret_cast<PortableServer::Servant
> (cookie
);
95 ACE_ASSERT (servant
== my_servant
);
97 // Invoke the garbage_collection_function.
98 (*servant_garbage_collector_
) (oid
,
102 // To avoid warning about unused variable with ACE_NDEBUG.
103 ACE_UNUSED_ARG (my_servant
);