Object_Adapter.cpp

Go to the documentation of this file.
00001 // $Id: Object_Adapter.cpp 78787 2007-07-05 08:02:28Z johnnyw $
00002 
00003 // -- PortableServer Include --
00004 #include "tao/PortableServer/Object_Adapter.h"
00005 #include "tao/PortableServer/Non_Servant_Upcall.h"
00006 #include "tao/PortableServer/Servant_Upcall.h"
00007 #include "tao/PortableServer/Root_POA.h"
00008 #include "tao/PortableServer/Regular_POA.h"
00009 #include "tao/PortableServer/Creation_Time.h"
00010 #include "tao/PortableServer/POA_Guard.h"
00011 #include "tao/PortableServer/Default_Servant_Dispatcher.h"
00012 #include "tao/PortableServer/Collocated_Object_Proxy_Broker.h"
00013 #include "tao/PortableServer/POAManager.h"
00014 #include "tao/PortableServer/POAManagerFactory.h"
00015 #include "tao/PortableServer/Servant_Base.h"
00016 
00017 // -- ACE Include --
00018 #include "ace/Auto_Ptr.h"
00019 #include "ace/Log_Msg.h"
00020 #include "ace/OS_NS_string.h"
00021 
00022 // -- TAO Include --
00023 #include "tao/PortableInterceptorC.h"
00024 #include "tao/ORB.h"
00025 #include "tao/ORB_Core.h"
00026 #include "tao/TSS_Resources.h"
00027 #include "tao/TAO_Server_Request.h"
00028 #include "tao/Stub.h"
00029 #include "tao/Profile.h"
00030 #include "tao/MProfile.h"
00031 #include "tao/debug.h"
00032 #include "tao/PortableInterceptor.h"
00033 #include "tao/ORBInitializer_Registry.h"
00034 #include "tao/Thread_Lane_Resources_Manager.h"
00035 #include "tao/Thread_Lane_Resources.h"
00036 #include "tao/Protocols_Hooks.h"
00037 #include "tao/ServerRequestInterceptor_Adapter.h"
00038 
00039 #if !defined (__ACE_INLINE__)
00040 # include "tao/PortableServer/Object_Adapter.inl"
00041 #endif /* __ACE_INLINE__ */
00042 
00043 #include "tao/PortableServer/ThreadPolicy.h"
00044 #include "tao/PortableServer/LifespanPolicy.h"
00045 #include "tao/PortableServer/IdAssignmentPolicy.h"
00046 #include "tao/PortableServer/IdUniquenessPolicy.h"
00047 #include "tao/PortableServer/ImplicitActivationPolicy.h"
00048 #include "tao/PortableServer/RequestProcessingPolicy.h"
00049 #include "tao/PortableServer/ServantRetentionPolicy.h"
00050 
00051 ACE_RCSID (PortableServer,
00052            Object_Adapter,
00053            "$Id: Object_Adapter.cpp 78787 2007-07-05 08:02:28Z johnnyw $")
00054 
00055 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00056 namespace PortableServer
00057 {
00058   class POAManagerFactory;
00059   typedef POAManagerFactory *POAManagerFactory_ptr;
00060 }
00061 #endif
00062 
00063 // Timeprobes class
00064 #include "tao/Timeprobe.h"
00065 
00066 #if defined (ACE_ENABLE_TIMEPROBES)
00067 
00068 static const char *TAO_Object_Adapter_Timeprobe_Description[] =
00069 {
00070   "Object_Adapter::dispatch_servant - start",
00071   "Object_Adapter::dispatch_servant - end",
00072 
00073   "POA::parse_key - start",
00074   "POA::parse_key - end",
00075 
00076   "Object_Adapter::find_poa - start",
00077   "Object_Adapter::find_poa - end",
00078 
00079   "POA::locate_servant - start",
00080   "POA::locate_servant - end",
00081 
00082   "Servant::_dispatch - start",
00083   "Servant::_dispatch - end",
00084 };
00085 
00086 enum
00087 {
00088   // Timeprobe description table start key
00089   TAO_OBJECT_ADAPTER_DISPATCH_SERVANT_START = 200,
00090   TAO_OBJECT_ADAPTER_DISPATCH_SERVANT_END,
00091 
00092   TAO_POA_PARSE_KEY_START,
00093   TAO_POA_PARSE_KEY_END,
00094 
00095   TAO_OBJECT_ADAPTER_FIND_POA_START,
00096   TAO_OBJECT_ADAPTER_FIND_POA_END,
00097 
00098   TAO_POA_LOCATE_SERVANT_START,
00099   TAO_POA_LOCATE_SERVANT_END,
00100 
00101   TAO_SERVANT_DISPATCH_START,
00102   TAO_SERVANT_DISPATCH_END
00103 };
00104 
00105 // Setup Timeprobes
00106 ACE_TIMEPROBE_EVENT_DESCRIPTIONS (TAO_Object_Adapter_Timeprobe_Description,
00107                                   TAO_OBJECT_ADAPTER_DISPATCH_SERVANT_START);
00108 
00109 #endif /* ACE_ENABLE_TIMEPROBES */
00110 
00111 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00112 
00113 /* static */
00114 CORBA::ULong TAO_Object_Adapter::transient_poa_name_size_ = 0;
00115 
00116 void
00117 TAO_Object_Adapter::set_transient_poa_name_size (const TAO_Server_Strategy_Factory::Active_Object_Map_Creation_Parameters &creation_parameters)
00118 {
00119   if (TAO_Object_Adapter::transient_poa_name_size_ == 0)
00120     {
00121       switch (creation_parameters.poa_lookup_strategy_for_transient_id_policy_)
00122         {
00123 #if (TAO_HAS_MINIMUM_POA_MAPS == 0)
00124         case TAO_LINEAR:
00125           TAO_Object_Adapter::transient_poa_name_size_ =
00126             sizeof (CORBA::ULong);
00127           break;
00128         case TAO_DYNAMIC_HASH:
00129           TAO_Object_Adapter::transient_poa_name_size_ =
00130             sizeof (CORBA::ULong);
00131           break;
00132 #endif /* TAO_HAS_MINIMUM_POA_MAPS == 0 */
00133         case TAO_ACTIVE_DEMUX:
00134         default:
00135           TAO_Object_Adapter::transient_poa_name_size_ =
00136             static_cast <CORBA::ULong>(
00137                              ACE_Active_Map_Manager_Key::size ());
00138           break;
00139         }
00140     }
00141 }
00142 
00143 TAO_Object_Adapter::TAO_Object_Adapter (const TAO_Server_Strategy_Factory::Active_Object_Map_Creation_Parameters &creation_parameters,
00144                                         TAO_ORB_Core &orb_core)
00145   : hint_strategy_ (0),
00146     servant_dispatcher_ (0),
00147     persistent_poa_name_map_ (0),
00148     transient_poa_map_ (0),
00149     orb_core_ (orb_core),
00150     enable_locking_ (orb_core_.server_factory ()->enable_poa_locking ()),
00151     thread_lock_ (),
00152     lock_ (TAO_Object_Adapter::create_lock (enable_locking_,
00153                                             thread_lock_)),
00154     reverse_lock_ (*lock_),
00155     non_servant_upcall_condition_ (thread_lock_),
00156     non_servant_upcall_in_progress_ (0),
00157     non_servant_upcall_nesting_level_ (0),
00158     non_servant_upcall_thread_ (ACE_OS::NULL_thread),
00159     root_ (0),
00160 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00161     poa_manager_factory_ (0),
00162 #endif
00163     default_validator_ (orb_core),
00164     default_poa_policies_ ()
00165 {
00166   TAO_Object_Adapter::set_transient_poa_name_size (creation_parameters);
00167 
00168   Hint_Strategy *hint_strategy = 0;
00169   if (creation_parameters.use_active_hint_in_poa_names_)
00170     ACE_NEW (hint_strategy,
00171              Active_Hint_Strategy (creation_parameters.poa_map_size_));
00172   else
00173     ACE_NEW (hint_strategy,
00174              No_Hint_Strategy);
00175 
00176   // Give ownership to the auto pointer.
00177   auto_ptr<Hint_Strategy> new_hint_strategy (hint_strategy);
00178 
00179   new_hint_strategy->object_adapter (this);
00180 
00181   persistent_poa_name_map *ppnm = 0;
00182   switch (creation_parameters.poa_lookup_strategy_for_persistent_id_policy_)
00183     {
00184     case TAO_LINEAR:
00185 #if (TAO_HAS_MINIMUM_POA_MAPS == 0)
00186       ACE_NEW (ppnm,
00187                persistent_poa_name_linear_map (creation_parameters.poa_map_size_));
00188 
00189       break;
00190 #else
00191       ACE_ERROR ((LM_ERROR,
00192                   "linear option for -ORBPersistentidPolicyDemuxStrategy "
00193                   "not supported with minimum POA maps. "
00194                   "Ingoring option to use default... \n"));
00195       /* FALL THROUGH */
00196 #endif /* TAO_HAS_MINIMUM_POA_MAPS == 0 */
00197     case TAO_DYNAMIC_HASH:
00198     default:
00199       ACE_NEW (ppnm,
00200                persistent_poa_name_hash_map (creation_parameters.poa_map_size_));
00201       break;
00202     }
00203   // Give ownership to the auto pointer.
00204   auto_ptr<persistent_poa_name_map> new_persistent_poa_name_map (ppnm);
00205 
00206   transient_poa_map *tpm = 0;
00207   switch (creation_parameters.poa_lookup_strategy_for_transient_id_policy_)
00208     {
00209 #if (TAO_HAS_MINIMUM_POA_MAPS == 0)
00210     case TAO_LINEAR:
00211       ACE_NEW (tpm,
00212                transient_poa_linear_map (creation_parameters.poa_map_size_));
00213       break;
00214     case TAO_DYNAMIC_HASH:
00215       ACE_NEW (tpm,
00216                transient_poa_hash_map (creation_parameters.poa_map_size_));
00217       break;
00218 #else
00219     case TAO_LINEAR:
00220     case TAO_DYNAMIC_HASH:
00221       ACE_ERROR ((LM_ERROR,
00222                   "linear and dynamic options for -ORBTransientidPolicyDemuxStrategy "
00223                   "are not supported with minimum POA maps. "
00224                   "Ingoring option to use default... \n"));
00225       /* FALL THROUGH */
00226 #endif /* TAO_HAS_MINIMUM_POA_MAPS == 0 */
00227     case TAO_ACTIVE_DEMUX:
00228     default:
00229       ACE_NEW (tpm,
00230                transient_poa_active_map (creation_parameters.poa_map_size_));
00231       break;
00232     }
00233   // Give ownership to the auto pointer.
00234   auto_ptr<transient_poa_map> new_transient_poa_map (tpm);
00235 
00236   this->hint_strategy_ =
00237     new_hint_strategy.release ();
00238   this->persistent_poa_name_map_ =
00239     new_persistent_poa_name_map.release ();
00240   this->transient_poa_map_ =
00241     new_transient_poa_map.release ();
00242 }
00243 
00244 void
00245 TAO_Object_Adapter::init_default_policies (TAO_POA_Policy_Set &policies)
00246 {
00247   // Initialize the default policies.
00248 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00249 
00250   TAO::Portable_Server::ThreadPolicy thread_policy (PortableServer::ORB_CTRL_MODEL);
00251   policies.merge_policy (&thread_policy);
00252 
00253 #endif /* TAO_HAS_MINIMUM_POA == 0 */
00254 
00255 #if !defined (CORBA_E_MICRO)
00256   // Lifespan policy.
00257   TAO::Portable_Server::LifespanPolicy lifespan_policy (PortableServer::TRANSIENT);
00258   policies.merge_policy (&lifespan_policy);
00259 #endif
00260 
00261 #if !defined (CORBA_E_MICRO)
00262   // ID uniqueness policy.
00263   TAO::Portable_Server::IdUniquenessPolicy id_uniqueness_policy (PortableServer::UNIQUE_ID);
00264   policies.merge_policy (&id_uniqueness_policy);
00265 #endif
00266 
00267 #if !defined (CORBA_E_MICRO)
00268   // ID assignment policy.
00269   TAO::Portable_Server::IdAssignmentPolicy id_assignment_policy (PortableServer::SYSTEM_ID);
00270   policies.merge_policy (&id_assignment_policy);
00271 #endif
00272 
00273 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00274   // Implicit activation policy.
00275   TAO::Portable_Server::ImplicitActivationPolicy implicit_activation_policy
00276     (PortableServer::NO_IMPLICIT_ACTIVATION);
00277   policies.merge_policy (&implicit_activation_policy);
00278 
00279   // Servant retention policy.
00280   TAO::Portable_Server::ServantRetentionPolicy servant_retention_policy
00281     (PortableServer::RETAIN);
00282   policies.merge_policy (&servant_retention_policy);
00283 
00284   // Request processing policy.
00285   TAO::Portable_Server::RequestProcessingPolicy request_processing_policy
00286     (PortableServer::USE_ACTIVE_OBJECT_MAP_ONLY);
00287   policies.merge_policy (&request_processing_policy);
00288 #endif /* TAO_HAS_MINIMUM_POA == 0 */
00289 #if defined (CORBA_E_MICRO)
00290   ACE_UNUSED_ARG (policies);
00291 #endif
00292 }
00293 
00294 TAO_Object_Adapter::~TAO_Object_Adapter (void)
00295 {
00296   delete this->hint_strategy_;
00297   delete this->persistent_poa_name_map_;
00298   delete this->transient_poa_map_;
00299   delete this->lock_;
00300 
00301   delete this->servant_dispatcher_;
00302 
00303   // This cleanup may have already occurred in the close() method.  If
00304   // that is the case then this won't cause any harm since root_ and
00305   // poa_manager_factory_ would have been set to zero.  But, if close
00306   // wasn't called, then these would be leaked.  It may be better if
00307   // these pointers had a corresponding _var version so that this cleanup
00308   // could be automatic.
00309   ::CORBA::release (this->root_);
00310 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00311   release_poa_manager_factory (this->poa_manager_factory_);
00312 #endif
00313 }
00314 
00315 /* static */
00316 ACE_Lock *
00317 TAO_Object_Adapter::create_lock (int enable_locking,
00318                                  TAO_SYNCH_MUTEX &thread_lock)
00319 {
00320 #if defined (ACE_HAS_THREADS)
00321   if (enable_locking)
00322     {
00323       ACE_Lock *the_lock = 0;
00324       ACE_NEW_RETURN (the_lock,
00325                       ACE_Lock_Adapter<TAO_SYNCH_MUTEX> (thread_lock),
00326                       0);
00327       return the_lock;
00328     }
00329 #else
00330   ACE_UNUSED_ARG (enable_locking);
00331   ACE_UNUSED_ARG (thread_lock);
00332 #endif /* ACE_HAS_THREADS */
00333 
00334   ACE_Lock *the_lock = 0;
00335   ACE_NEW_RETURN (the_lock,
00336                   ACE_Lock_Adapter<ACE_SYNCH_NULL_MUTEX> (),
00337                   0);
00338   return the_lock;
00339 }
00340 
00341 int
00342 TAO_Object_Adapter::dispatch_servant (const TAO::ObjectKey &key,
00343                                       TAO_ServerRequest &req,
00344                                       CORBA::Object_out forward_to)
00345 {
00346   ACE_FUNCTION_TIMEPROBE (TAO_OBJECT_ADAPTER_DISPATCH_SERVANT_START);
00347 
00348   // This object is magical, i.e., it has a non-trivial constructor
00349   // and destructor.
00350   TAO::Portable_Server::Servant_Upcall servant_upcall (&this->orb_core_);
00351 
00352   // Set up state in the POA et al (including the POA Current), so
00353   // that we know that this servant is currently in an upcall.
00354   const char *operation = req.operation ();
00355   int result = servant_upcall.prepare_for_upcall (key, operation, forward_to);
00356 
00357   if (result != TAO_Adapter::DS_OK)
00358     return result;
00359 
00360   // Preprocess request.
00361   if (req.collocated ())
00362     {
00363       servant_upcall.pre_invoke_collocated_request ();
00364     }
00365   else
00366     {
00367       servant_upcall.pre_invoke_remote_request (req);
00368     }
00369 
00370   // Servant dispatch.
00371   {
00372     ACE_FUNCTION_TIMEPROBE (TAO_SERVANT_DISPATCH_START);
00373 
00374     do_dispatch (req, servant_upcall);
00375   }
00376 
00377 #if TAO_HAS_INTERCEPTORS == 1
00378   // ServerInterceptor might have raised ForwardRequest. In case of
00379   // remote calls invocations the LocationForwardReply would have been
00380   // sent in earlier stage, but in colocal scenario no message is sent
00381   // and the LocationForward object must be passed over here to
00382   // calling operation's mem-space.
00383   if (req.collocated() && req.reply_status () == PortableInterceptor::LOCATION_FORWARD)
00384     {
00385       forward_to = CORBA::Object::_duplicate (req.forward_location ());
00386       result = TAO_Adapter::DS_FORWARD;
00387     }
00388 #endif
00389 
00390   return result;
00391 }
00392 
00393 void
00394 TAO_Object_Adapter::locate_poa (const TAO::ObjectKey &key,
00395                                 PortableServer::ObjectId &system_id,
00396                                 TAO_Root_POA *&poa)
00397 {
00398   TAO_Object_Adapter::poa_name poa_system_name;
00399   CORBA::Boolean is_root = false;
00400   CORBA::Boolean is_persistent = false;
00401   CORBA::Boolean is_system_id = false;
00402   TAO::Portable_Server::Temporary_Creation_Time poa_creation_time;
00403 
00404   int result = 0;
00405 
00406   {
00407     ACE_FUNCTION_TIMEPROBE (TAO_POA_PARSE_KEY_START);
00408 
00409     result = TAO_Root_POA::parse_key (key,
00410                                       poa_system_name,
00411                                       system_id,
00412                                       is_root,
00413                                       is_persistent,
00414                                       is_system_id,
00415                                       poa_creation_time);
00416   }
00417 
00418   if (result != 0)
00419     throw ::CORBA::OBJ_ADAPTER ();
00420 
00421   {
00422     ACE_FUNCTION_TIMEPROBE (TAO_OBJECT_ADAPTER_FIND_POA_START);
00423 
00424     result = this->find_poa (poa_system_name,
00425                              is_persistent,
00426                              is_root,
00427                              poa_creation_time,
00428                              poa);
00429   }
00430 
00431   if (result != 0)
00432     throw ::CORBA::OBJECT_NOT_EXIST (CORBA::OMGVMCID | 2, CORBA::COMPLETED_NO);
00433 }
00434 
00435 int
00436 TAO_Object_Adapter::activate_poa (const poa_name &folded_name,
00437                                   TAO_Root_POA *&poa)
00438 {
00439   int result = -1;
00440 
00441 #if (TAO_HAS_MINIMUM_POA == 0) &&  !defined (CORBA_E_MICRO)
00442 
00443   iteratable_poa_name ipn (folded_name);
00444   iteratable_poa_name::iterator iterator = ipn.begin ();
00445   iteratable_poa_name::iterator end = ipn.end ();
00446 
00447   TAO_Root_POA *parent = this->root_;
00448   if (parent == 0 || parent->name () != *iterator)
00449     throw ::CORBA::OBJ_ADAPTER ();
00450   else
00451     ++iterator;
00452 
00453   for (;
00454        iterator != end;
00455        ++iterator)
00456     {
00457       TAO_Root_POA *current = 0;
00458 
00459       try
00460         {
00461           current = parent->find_POA_i (*iterator, 1);
00462         }
00463       catch (const PortableServer::POA::AdapterNonExistent&)
00464         {
00465           return -1;
00466         }
00467 
00468       parent = current;
00469     }
00470 
00471   poa = parent;
00472   result = 0;
00473 #else
00474   ACE_UNUSED_ARG (folded_name);
00475   ACE_UNUSED_ARG (poa);
00476 #endif /* TAO_HAS_MINIMUM_POA == 0 */
00477 
00478   return result;
00479 }
00480 
00481 int
00482 TAO_Object_Adapter::find_transient_poa (const poa_name &system_name,
00483                                         CORBA::Boolean root,
00484                                         const TAO::Portable_Server::Temporary_Creation_Time &poa_creation_time,
00485                                         TAO_Root_POA *&poa)
00486 {
00487   int result = 0;
00488 
00489   if (root)
00490     {
00491       poa = this->root_;
00492     }
00493   else
00494     {
00495       result = this->transient_poa_map_->find (system_name, poa);
00496     }
00497 
00498   if (poa == 0
00499       || (result == 0 && !poa->validate_lifespan (false, poa_creation_time)))
00500     result = -1;
00501 
00502   return result;
00503 }
00504 
00505 int
00506 TAO_Object_Adapter::bind_poa (const poa_name &folded_name,
00507                               TAO_Root_POA *poa,
00508                               poa_name_out system_name)
00509 {
00510   if (poa->persistent ())
00511     return this->bind_persistent_poa (folded_name, poa, system_name);
00512   else
00513     return this->bind_transient_poa (poa, system_name);
00514 }
00515 
00516 int
00517 TAO_Object_Adapter::unbind_poa (TAO_Root_POA *poa,
00518                                 const poa_name &folded_name,
00519                                 const poa_name &system_name)
00520 {
00521   if (poa->persistent ())
00522     return this->unbind_persistent_poa (folded_name, system_name);
00523   else
00524     return this->unbind_transient_poa (system_name);
00525 }
00526 
00527 int
00528 TAO_Object_Adapter::locate_servant_i (const TAO::ObjectKey &key)
00529 {
00530   ACE_FUNCTION_TIMEPROBE (TAO_POA_LOCATE_SERVANT_START);
00531 
00532   PortableServer::ObjectId id;
00533   TAO_Root_POA *poa = 0;
00534 
00535   this->locate_poa (key, id, poa);
00536 
00537   PortableServer::Servant servant = 0;
00538   TAO_SERVANT_LOCATION servant_location =
00539     poa->locate_servant_i (id, servant);
00540 
00541   switch (servant_location)
00542     {
00543     case TAO_SERVANT_FOUND:
00544       // Optimistic attitude
00545     case TAO_DEFAULT_SERVANT:
00546     case TAO_SERVANT_MANAGER:
00547       return 0;
00548 
00549     case TAO_SERVANT_NOT_FOUND:
00550       return -1;
00551     }
00552 
00553   return -1;
00554 }
00555 
00556 TAO_SERVANT_LOCATION
00557 TAO_Object_Adapter::find_servant_i (const TAO::ObjectKey &key,
00558                                     PortableServer::Servant &servant)
00559 {
00560   PortableServer::ObjectId id;
00561   TAO_Root_POA *poa = 0;
00562 
00563   this->locate_poa (key, id, poa);
00564 
00565   return poa->locate_servant_i (id, servant);
00566 }
00567 
00568 void
00569 TAO_Object_Adapter::open (void)
00570 {
00571   // Add in the default POA policies to the default list.
00572   this->init_default_policies (this->default_poa_policies ());
00573 
00574   // If a POA extension hasn't changed the servant dispatcher, initialize the
00575   // default one.
00576   if (this->servant_dispatcher_ == 0)
00577     {
00578       ACE_NEW (this->servant_dispatcher_,
00579                TAO_Default_Servant_Dispatcher);
00580     }
00581 
00582 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00583   ACE_NEW_THROW_EX (this->poa_manager_factory_,
00584                     TAO_POAManager_Factory (*this),
00585                     CORBA::NO_MEMORY ());
00586 
00587   ::CORBA::PolicyList policy;
00588   PortableServer::POAManager_var poa_manager
00589     = poa_manager_factory_->create_POAManager (TAO_DEFAULT_ROOTPOAMANAGER_NAME,
00590                                               policy);
00591 #else
00592   PortableServer::POAManager_ptr poa_manager_ptr;
00593   ::CORBA::PolicyList policy_list;
00594   ACE_NEW_THROW_EX (poa_manager_ptr,
00595                     TAO_POA_Manager (*this, TAO_DEFAULT_ROOTPOAMANAGER_NAME),
00596                     CORBA::NO_MEMORY
00597                     (CORBA::SystemException::_tao_minor_code (0, ENOMEM),
00598                       CORBA::COMPLETED_NO));
00599 
00600   PortableServer::POAManager_var poa_manager = poa_manager_ptr;
00601   // Keep reference of POAManager in TAO_Object_Adapter so the POAManager
00602   // object is destructed after RootPOA is destructed.
00603   the_poa_manager_ = poa_manager;
00604 
00605 #endif
00606 
00607   // This makes sure that the default resources are open when the Root
00608   // POA is created.
00609   this->orb_core_.thread_lane_resources_manager ().open_default_resources ();
00610 
00611   TAO_POA_Policy_Set policies (this->default_poa_policies ());
00612 
00613 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00614   // Specify the implicit activation policy since it should
00615   // be different from the default.  Note that merge_policy
00616   // takes a const reference and makes its own copy of the
00617   // policy.  (Otherwise, we'd have to allocate the policy
00618   // on the heap.)
00619   // Implicit activation policy.
00620   TAO::Portable_Server::ImplicitActivationPolicy implicit_activation_policy
00621     (PortableServer::IMPLICIT_ACTIVATION);
00622   policies.merge_policy (&implicit_activation_policy);
00623 #endif /* TAO_HAS_MINIMUM_POA == 0 */
00624 
00625   // Merge policies from the ORB level.
00626   this->validator ().merge_policies (policies.policies ());
00627 
00628   // If any of the policy objects specified are not valid for the ORB
00629   // implementation, if conflicting policy objects are specified, or
00630   // if any of the specified policy objects require prior
00631   // administrative action that has not been performed, an
00632   // InvalidPolicy exception is raised containing the index in the
00633   // policies parameter value of the first offending policy object.
00634   policies.validate_policies (this->validator (), this->orb_core_);
00635 
00636   // Construct a new POA
00637   TAO_Root_POA::String root_poa_name (TAO_DEFAULT_ROOTPOA_NAME);
00638   this->root_ =
00639     this->servant_dispatcher_->create_Root_POA (root_poa_name,
00640                                                 poa_manager.in (),
00641                                                 policies,
00642                                                 this->lock (),
00643                                                 this->thread_lock (),
00644                                                 this->orb_core_,
00645                                                 this);
00646 
00647   // The Object_Adapter will keep a reference to the Root POA so that
00648   // on its destruction, it can check whether the Root POA has been
00649   // destroyed yet or not.
00650   this->root_->_add_ref ();
00651 
00652   // Lock access for the duration of this transaction.
00653   TAO::Portable_Server::POA_Guard poa_guard (*this->root_);
00654 
00655   // Iterate over the registered IOR interceptors so that they may be
00656   // given the opportunity to add tagged components to the profiles
00657   // for this servant.
00658   this->root_->establish_components ();
00659 }
00660 
00661 void
00662 TAO_Object_Adapter::close (int wait_for_completion)
00663 {
00664   this->check_close (wait_for_completion);
00665 
00666   // Shutting down the ORB causes all object adapters to be destroyed,
00667   // since they cannot exist in the absence of an ORB. Shut down is
00668   // complete when all ORB processing (including request processing
00669   // and object deactivation or other operations associated with
00670   // object adapters) has completed and the object adapters have been
00671   // destroyed. In the case of the POA, this means that all object
00672   // etherealizations have finished and root POA has been destroyed
00673   // (implying that all descendent POAs have also been destroyed).
00674   TAO_Root_POA *root = 0;
00675 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00676   TAO_POAManager_Factory* factory = 0;
00677 #endif
00678   {
00679     ACE_GUARD (ACE_Lock, ace_mon, this->lock ());
00680     if (this->root_ == 0)
00681       return;
00682     root = this->root_;
00683     this->root_ = 0;
00684 
00685 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00686     if (this->poa_manager_factory_ == 0)
00687       return;
00688     factory = this->poa_manager_factory_;
00689     this->poa_manager_factory_ = 0;
00690 #endif
00691   }
00692   CORBA::Boolean etherealize_objects = true;
00693   root->destroy (etherealize_objects, wait_for_completion);
00694   ::CORBA::release (root);
00695 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00696   release_poa_manager_factory (factory);
00697 #endif
00698 }
00699 
00700 void
00701 TAO_Object_Adapter::check_close (int wait_for_completion)
00702 {
00703   TAO_Root_POA::check_for_valid_wait_for_completions (this->orb_core (),
00704                                                  wait_for_completion);
00705 }
00706 
00707 int
00708 TAO_Object_Adapter::priority (void) const
00709 {
00710   return 0;
00711 }
00712 
00713 int
00714 TAO_Object_Adapter::dispatch (TAO::ObjectKey &key,
00715                               TAO_ServerRequest &request,
00716                               CORBA::Object_out forward_to)
00717 {
00718   if (key.length() < TAO_Root_POA::TAO_OBJECTKEY_PREFIX_SIZE
00719       || ACE_OS::memcmp (key.get_buffer (),
00720                          &TAO_Root_POA::objectkey_prefix[0],
00721                          TAO_Root_POA::TAO_OBJECTKEY_PREFIX_SIZE) != 0)
00722     {
00723       return TAO_Adapter::DS_MISMATCHED_KEY;
00724     }
00725 
00726   int result = 0;
00727 
00728 #if TAO_HAS_INTERCEPTORS == 1
00729   TAO::ServerRequestInterceptor_Adapter *sri_adapter =
00730     orb_core_.serverrequestinterceptor_adapter ();
00731 
00732   try
00733     {
00734       if (sri_adapter != 0)
00735         {
00736 #if TAO_HAS_EXTENDED_FT_INTERCEPTORS == 1
00737           CORBA::OctetSeq_var ocs;
00738           sri_adapter->tao_ft_interception_point (request,
00739                                                  0,  // args
00740                                                  0,  // nargs
00741                                                  0,  // servant_upcall
00742                                                  0,  // exceptions
00743                                                  0, // nexceptions
00744                                                  ocs.out ());
00745 
00746           /// If we have a cached result, just go ahead and send the reply
00747           /// and let us  return
00748           if (ocs.ptr () != 0)
00749             {
00750               // request.result_seq (
00751               request.send_cached_reply (ocs.inout ());
00752 
00753               return TAO_Adapter::DS_OK;
00754             }
00755 
00756           // If a PortableInterceptor::ForwardRequest exception was
00757           // thrown, then set the forward_to object reference and return
00758           // with the appropriate return status.
00759           forward_to.ptr () = request.forward_location ();
00760           if (!CORBA::is_nil (request.forward_location ()))
00761             {
00762               return TAO_Adapter::DS_FORWARD;
00763             }
00764 #endif /*TAO_HAS_EXTENDED_FT_INTERCEPTORS*/
00765 
00766           // The receive_request_service_contexts() interception point
00767           // must be invoked before the operation is dispatched to the
00768           // servant.
00769           sri_adapter->receive_request_service_contexts (request,
00770                                                          0,  // args
00771                                                          0,  // nargs
00772                                                          0,  // servant_upcall
00773                                                          0,  // exceptions
00774                                                          0);   // nexceptions
00775 
00776           // If a PortableInterceptor::ForwardRequest exception was
00777           // thrown, then set the forward_to object reference and return
00778           // with the appropriate return status.
00779           forward_to.ptr () = request.forward_location ();
00780           if (!CORBA::is_nil (request.forward_location ()))
00781             {
00782               return TAO_Adapter::DS_FORWARD;
00783             }
00784         }
00785 #endif  /* TAO_HAS_INTERCEPTORS == 1 */
00786 
00787       result = this->dispatch_servant (key, request, forward_to);
00788 
00789 #if TAO_HAS_INTERCEPTORS == 1
00790 
00791       if (result == TAO_Adapter::DS_FORWARD)
00792         {
00793           request.forward_location (forward_to.ptr ());
00794           if (sri_adapter != 0)
00795             {
00796               sri_adapter->send_other (request,
00797                                        0,  // args
00798                                        0,  // nargs
00799                                        0,  // servant_upcall
00800                                        0,  // exceptions
00801                                        0   // nexceptions
00802                                       );
00803             }
00804         }
00805     }
00806   catch ( ::CORBA::Exception& ex)
00807     {
00808       // Just assume the current exception is a system exception, the
00809       // status can only change when the interceptor changes this
00810       // and this is only done when the sri_adapter is available. If we
00811       // don't have an sri_adapter we just rethrow the exception
00812       PortableInterceptor::ReplyStatus status =
00813         PortableInterceptor::SYSTEM_EXCEPTION;
00814 
00815       if (sri_adapter != 0)
00816         {
00817           request.caught_exception (&ex);
00818 
00819           sri_adapter->send_exception (request,
00820                                        0,  // args
00821                                        0,  // nargs
00822                                        0,  // servant_upcall
00823                                        0,  // exceptions
00824                                        0);   // nexceptions
00825 
00826           status = request.reply_status ();
00827         }
00828 
00829       // Only re-throw the exception if it hasn't been transformed by
00830       // the send_exception() interception point (e.g. to a
00831       // LOCATION_FORWARD).
00832       if (status == PortableInterceptor::SYSTEM_EXCEPTION
00833           || status == PortableInterceptor::USER_EXCEPTION)
00834         {
00835           throw;
00836         }
00837     }
00838 #endif  /* TAO_HAS_INTERCEPTORS == 1 */
00839 
00840   return result;
00841 }
00842 
00843 const char *
00844 TAO_Object_Adapter::name (void) const
00845 {
00846   return TAO_OBJID_ROOTPOA;
00847 }
00848 
00849 CORBA::Object_ptr
00850 TAO_Object_Adapter::root (void)
00851 {
00852   return CORBA::Object::_duplicate (this->root_);
00853 }
00854 
00855 CORBA::Object_ptr
00856 TAO_Object_Adapter::create_collocated_object (TAO_Stub *stub,
00857                                               const TAO_MProfile &mp)
00858 {
00859   TAO_ServantBase *sb = this->get_collocated_servant (mp);
00860 
00861   // Set the servant ORB.  Do not duplicate the ORB here since
00862   // TAO_Stub::servant_orb() duplicates it.
00863   stub->servant_orb (this->orb_core_.orb ());
00864 
00865   // It is ok to create a collocated object even when <sb> is
00866   // zero. This constructor will set the stub collocated indicator and
00867   // the strategized proxy broker if required.
00868   CORBA::Object_ptr x;
00869   ACE_NEW_RETURN (x,
00870                   CORBA::Object (stub,
00871                                  1,
00872                                  sb),
00873                   CORBA::Object::_nil ());
00874 
00875   // Success.
00876   return x;
00877 }
00878 
00879 CORBA::Long
00880 TAO_Object_Adapter::initialize_collocated_object (TAO_Stub *stub)
00881 {
00882   // If we have been forwarded: use the forwarded profiles
00883   const TAO_MProfile &mp = stub->forward_profiles () ? *(stub->forward_profiles ())
00884                                                      : stub->base_profiles ();
00885 
00886   TAO_ServantBase *sb = this->get_collocated_servant (mp);
00887 
00888   // Set the servant ORB.  Do not duplicate the ORB here since
00889   // TAO_Stub::servant_orb() duplicates it.
00890   stub->servant_orb (this->orb_core_.orb ());
00891 
00892   // It is ok to set the object as a collocated object even when
00893   // <sb> is zero.
00894   stub->collocated_servant (sb);
00895 
00896   // Mark the stub as collocated. This will set the strategized object
00897   // proxy broker if required.
00898   stub->is_collocated (true);
00899 
00900   // Return 0 (success) iff we found a servant.
00901   return ! sb;
00902 }
00903 
00904 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00905 void
00906 TAO_Object_Adapter::release_poa_manager_factory (
00907                                      TAO_POAManager_Factory *factory)
00908 {
00909   if (factory != 0)
00910     {
00911       factory->remove_all_poamanagers ();
00912       ::CORBA::release (factory);
00913     }
00914 }
00915 #endif
00916 
00917 TAO_ServantBase *
00918 TAO_Object_Adapter::get_collocated_servant (const TAO_MProfile &mp)
00919 {
00920   for (TAO_PHandle j = 0;
00921        j != mp.profile_count ();
00922        ++j)
00923     {
00924       const TAO_Profile *profile = mp.get_profile (j);
00925       TAO::ObjectKey_var objkey = profile->_key ();
00926 
00927       if (objkey->length() < TAO_Root_POA::TAO_OBJECTKEY_PREFIX_SIZE
00928           || ACE_OS::memcmp (objkey->get_buffer (),
00929                              &TAO_Root_POA::objectkey_prefix[0],
00930                              TAO_Root_POA::TAO_OBJECTKEY_PREFIX_SIZE) != 0)
00931         continue;
00932 
00933       TAO_ServantBase *servant = 0;
00934 
00935       try
00936         {
00937           this->find_servant (objkey.in (), servant);
00938         }
00939       catch (const ::CORBA::Exception&)
00940         {
00941         }
00942 
00943       return servant;
00944     }
00945 
00946   return 0;
00947 }
00948 
00949 // ****************************************************************
00950 
00951 TAO_Object_Adapter::Hint_Strategy::~Hint_Strategy (void)
00952 {
00953 }
00954 
00955 void
00956 TAO_Object_Adapter::Hint_Strategy::object_adapter (TAO_Object_Adapter *oa)
00957 {
00958   this->object_adapter_ = oa;
00959 }
00960 
00961 TAO_Object_Adapter::Active_Hint_Strategy::Active_Hint_Strategy (CORBA::ULong map_size)
00962   : persistent_poa_system_map_ (map_size)
00963 {
00964 }
00965 
00966 TAO_Object_Adapter::Active_Hint_Strategy::~Active_Hint_Strategy (void)
00967 {
00968 }
00969 
00970 int
00971 TAO_Object_Adapter::Active_Hint_Strategy::find_persistent_poa (
00972   const poa_name &system_name,
00973   TAO_Root_POA *&poa)
00974 {
00975   poa_name folded_name;
00976   int result = this->persistent_poa_system_map_.recover_key (system_name,
00977                                                              folded_name);
00978 
00979   if (result == 0)
00980     {
00981       result = this->persistent_poa_system_map_.find (system_name, poa);
00982       if (result != 0
00983           || folded_name != poa->folded_name ())
00984         {
00985           result =
00986             this->object_adapter_->persistent_poa_name_map_->find (folded_name,
00987                                                                    poa);
00988           if (result != 0)
00989             {
00990               result = this->object_adapter_->activate_poa (folded_name, poa);
00991             }
00992         }
00993     }
00994 
00995   return result;
00996 }
00997 
00998 int
00999 TAO_Object_Adapter::Active_Hint_Strategy::bind_persistent_poa (
01000   const poa_name &folded_name,
01001   TAO_Root_POA *poa,
01002   poa_name_out system_name)
01003 {
01004   poa_name name = folded_name;
01005   int result = this->persistent_poa_system_map_.bind_modify_key (poa,
01006                                                                  name);
01007 
01008   if (result == 0)
01009     {
01010       result =
01011         this->object_adapter_->persistent_poa_name_map_->bind (folded_name,
01012                                                                poa);
01013 
01014       if (result != 0)
01015         this->persistent_poa_system_map_.unbind (name);
01016       else
01017         ACE_NEW_RETURN (system_name,
01018                         poa_name (name),
01019                         -1);
01020     }
01021 
01022   return result;
01023 }
01024 
01025 int
01026 TAO_Object_Adapter::Active_Hint_Strategy::unbind_persistent_poa (
01027   const poa_name &folded_name,
01028   const poa_name &system_name)
01029 {
01030   int result = this->persistent_poa_system_map_.unbind (system_name);
01031 
01032   if (result == 0)
01033     result =
01034       this->object_adapter_->persistent_poa_name_map_->unbind (folded_name);
01035 
01036   return result;
01037 }
01038 
01039 TAO_Object_Adapter::No_Hint_Strategy::~No_Hint_Strategy (void)
01040 {
01041 }
01042 
01043 int
01044 TAO_Object_Adapter::No_Hint_Strategy::find_persistent_poa (
01045   const poa_name &system_name,
01046   TAO_Root_POA *&poa)
01047 {
01048   int result =
01049     this->object_adapter_->persistent_poa_name_map_->find (system_name,
01050                                                            poa);
01051   if (result != 0)
01052     {
01053       result =
01054         this->object_adapter_->activate_poa (system_name, poa);
01055     }
01056 
01057   return result;
01058 }
01059 
01060 int
01061 TAO_Object_Adapter::No_Hint_Strategy::bind_persistent_poa (
01062   const poa_name &folded_name,
01063   TAO_Root_POA *poa,
01064   poa_name_out system_name)
01065 {
01066   int result =
01067     this->object_adapter_->persistent_poa_name_map_->bind (folded_name, poa);
01068   if (result == 0)
01069     ACE_NEW_RETURN (system_name,
01070                     poa_name (folded_name),
01071                     -1);
01072   return result;
01073 }
01074 
01075 int
01076 TAO_Object_Adapter::No_Hint_Strategy::unbind_persistent_poa (
01077   const poa_name & folded_name,
01078   const poa_name & /* system_name */)
01079 {
01080   return this->object_adapter_->persistent_poa_name_map_->unbind (folded_name);
01081 }
01082 
01083 TAO_Object_Adapter::poa_name_iterator::poa_name_iterator (
01084   int begin,
01085   CORBA::ULong size,
01086   const CORBA::Octet *folded_buffer)
01087   : size_ (size),
01088     folded_buffer_ (folded_buffer),
01089     last_separator_ ((CORBA::ULong) ~0)
01090 {
01091   if (begin)
01092     {
01093       this->position_ = (CORBA::ULong) ~0;
01094       this->operator++ ();
01095     }
01096   else
01097     this->position_ = this->size_;
01098 }
01099 
01100 bool
01101 TAO_Object_Adapter::poa_name_iterator::operator== (const poa_name_iterator &rhs) const
01102 {
01103   return this->position_ == rhs.position_;
01104 }
01105 
01106 bool
01107 TAO_Object_Adapter::poa_name_iterator::operator!= (const poa_name_iterator &rhs) const
01108 {
01109   return !this->operator== (rhs);
01110 }
01111 
01112 ACE_CString
01113 TAO_Object_Adapter::poa_name_iterator::operator* () const
01114 {
01115   CORBA::ULong start_at =
01116     this->last_separator_ +
01117     TAO_Root_POA::name_separator_length ();
01118 
01119   CORBA::ULong how_many =
01120     this->position_
01121     - this->last_separator_
01122     - TAO_Root_POA::name_separator_length ();
01123 
01124   return ACE_CString (reinterpret_cast <const char *>
01125                                        (&this->folded_buffer_[start_at]),
01126                       how_many);
01127 }
01128 
01129 TAO_Object_Adapter::poa_name_iterator &
01130 TAO_Object_Adapter::poa_name_iterator::operator++ (void)
01131 {
01132   for (this->last_separator_ = this->position_;
01133        ;
01134        )
01135     {
01136       ++this->position_;
01137       if (this->position_ < this->size_)
01138         {
01139           if (this->folded_buffer_[this->position_] == TAO_Root_POA::name_separator ())
01140             break;
01141         }
01142       else
01143         break;
01144     }
01145 
01146   return *this;
01147 }
01148 
01149 TAO_Object_Adapter::iteratable_poa_name::iteratable_poa_name (
01150   const poa_name &folded_name)
01151   : folded_name_ (folded_name)
01152 {
01153 }
01154 
01155 TAO_Object_Adapter::iteratable_poa_name::iterator
01156 TAO_Object_Adapter::iteratable_poa_name::begin (void) const
01157 {
01158   return iterator (1,
01159                    this->folded_name_.length (),
01160                    this->folded_name_.get_buffer ());
01161 }
01162 
01163 TAO_Object_Adapter::iteratable_poa_name::iterator
01164 TAO_Object_Adapter::iteratable_poa_name::end (void) const
01165 {
01166   return iterator (0,
01167                    this->folded_name_.length (),
01168                    this->folded_name_.get_buffer ());
01169 }
01170 
01171 void
01172 TAO_Object_Adapter::wait_for_non_servant_upcalls_to_complete (void)
01173 {
01174   // Check if a non-servant upcall is in progress.  If a non-servant
01175   // upcall is in progress, wait for it to complete.  Unless of
01176   // course, the thread making the non-servant upcall is this thread.
01177   while (this->enable_locking_ &&
01178          this->non_servant_upcall_in_progress_ &&
01179          ! ACE_OS::thr_equal (this->non_servant_upcall_thread_,
01180                               ACE_OS::thr_self ()))
01181     {
01182       // If so wait...
01183       int const result = this->non_servant_upcall_condition_.wait ();
01184       if (result == -1)
01185         throw ::CORBA::OBJ_ADAPTER ();
01186     }
01187 }
01188 
01189 void
01190 TAO_Object_Adapter::wait_for_non_servant_upcalls_to_complete_no_throw (void)
01191 {
01192   // Non-exception throwing version.
01193   try
01194     {
01195       this->wait_for_non_servant_upcalls_to_complete ();
01196     }
01197   catch (const ::CORBA::Exception&)
01198     {
01199       ACE_ERROR ((LM_ERROR,
01200                   "TAO_Object_Adapter::wait_for_non_servant_upcalls_to_complete "
01201                   "threw exception it should not have!\n"));
01202     }
01203 }
01204 
01205 void
01206 TAO_Object_Adapter::servant_dispatcher (TAO_Servant_Dispatcher *dispatcher)
01207 {
01208   if (this->servant_dispatcher_)
01209     delete this->servant_dispatcher_;
01210 
01211   this->servant_dispatcher_ = dispatcher;
01212 }
01213 
01214 void
01215 TAO_Object_Adapter::do_dispatch (TAO_ServerRequest& req,
01216                                  TAO::Portable_Server::Servant_Upcall& upcall)
01217 {
01218   upcall.servant ()->_dispatch(req, &upcall);
01219 }
01220 
01221 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Sun Jan 27 13:23:42 2008 for TAO_PortableServer by doxygen 1.3.6