Object_Adapter.cpp

Go to the documentation of this file.
00001 // $Id: Object_Adapter.cpp 81203 2008-04-01 14:17:09Z vzykov $
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 81203 2008-04-01 14:17:09Z vzykov $")
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.pi_reply_status () == PortableInterceptor::LOCATION_FORWARD)
00384     {
00385       forward_to = 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 (request.is_forwarded ())
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 (request.is_forwarded ())
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.reply_status (GIOP::LOCATION_FORWARD);
00794           request.pi_reply_status (PortableInterceptor::LOCATION_FORWARD);
00795           request.forward_location (forward_to.ptr ());
00796           if (sri_adapter != 0)
00797             {
00798               sri_adapter->send_other (request,
00799                                        0,  // args
00800                                        0,  // nargs
00801                                        0,  // servant_upcall
00802                                        0,  // exceptions
00803                                        0   // nexceptions
00804                                       );
00805             }
00806         }
00807     }
00808   catch ( ::CORBA::Exception& ex)
00809     {
00810       // Just assume the current exception is a system exception, the
00811       // status can only change when the interceptor changes this
00812       // and this is only done when the sri_adapter is available. If we
00813       // don't have an sri_adapter we just rethrow the exception
00814       PortableInterceptor::ReplyStatus status =
00815         PortableInterceptor::SYSTEM_EXCEPTION;
00816 
00817       if (sri_adapter != 0)
00818         {
00819           request.caught_exception (&ex);
00820 
00821           sri_adapter->send_exception (request,
00822                                        0,  // args
00823                                        0,  // nargs
00824                                        0,  // servant_upcall
00825                                        0,  // exceptions
00826                                        0);   // nexceptions
00827 
00828           status = request.pi_reply_status ();
00829         }
00830 
00831       // Only re-throw the exception if it hasn't been transformed by
00832       // the send_exception() interception point (e.g. to a
00833       // LOCATION_FORWARD).
00834       if (status == PortableInterceptor::SYSTEM_EXCEPTION
00835           || status == PortableInterceptor::USER_EXCEPTION)
00836         {
00837           throw;
00838         }
00839     }
00840 #endif  /* TAO_HAS_INTERCEPTORS == 1 */
00841 
00842   return result;
00843 }
00844 
00845 const char *
00846 TAO_Object_Adapter::name (void) const
00847 {
00848   return TAO_OBJID_ROOTPOA;
00849 }
00850 
00851 CORBA::Object_ptr
00852 TAO_Object_Adapter::root (void)
00853 {
00854   return CORBA::Object::_duplicate (this->root_);
00855 }
00856 
00857 CORBA::Object_ptr
00858 TAO_Object_Adapter::create_collocated_object (TAO_Stub *stub,
00859                                               const TAO_MProfile &mp)
00860 {
00861   TAO_ServantBase *sb = this->get_collocated_servant (mp);
00862 
00863   // Set the servant ORB.  Do not duplicate the ORB here since
00864   // TAO_Stub::servant_orb() duplicates it.
00865   stub->servant_orb (this->orb_core_.orb ());
00866 
00867   // It is ok to create a collocated object even when <sb> is
00868   // zero. This constructor will set the stub collocated indicator and
00869   // the strategized proxy broker if required.
00870   CORBA::Object_ptr x;
00871   ACE_NEW_RETURN (x,
00872                   CORBA::Object (stub,
00873                                  1,
00874                                  sb),
00875                   CORBA::Object::_nil ());
00876 
00877   // Success.
00878   return x;
00879 }
00880 
00881 CORBA::Long
00882 TAO_Object_Adapter::initialize_collocated_object (TAO_Stub *stub)
00883 {
00884   // If we have been forwarded: use the forwarded profiles
00885   const TAO_MProfile &mp = stub->forward_profiles () ? *(stub->forward_profiles ())
00886                                                      : stub->base_profiles ();
00887 
00888   TAO_ServantBase *sb = this->get_collocated_servant (mp);
00889 
00890   // Set the servant ORB.  Do not duplicate the ORB here since
00891   // TAO_Stub::servant_orb() duplicates it.
00892   stub->servant_orb (this->orb_core_.orb ());
00893 
00894   // It is ok to set the object as a collocated object even when
00895   // <sb> is zero.
00896   stub->collocated_servant (sb);
00897 
00898   // Mark the stub as collocated. This will set the strategized object
00899   // proxy broker if required.
00900   stub->is_collocated (true);
00901 
00902   // Return 0 (success) iff we found a servant.
00903   return ! sb;
00904 }
00905 
00906 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00907 void
00908 TAO_Object_Adapter::release_poa_manager_factory (
00909                                      TAO_POAManager_Factory *factory)
00910 {
00911   if (factory != 0)
00912     {
00913       factory->remove_all_poamanagers ();
00914       ::CORBA::release (factory);
00915     }
00916 }
00917 #endif
00918 
00919 TAO_ServantBase *
00920 TAO_Object_Adapter::get_collocated_servant (const TAO_MProfile &mp)
00921 {
00922   for (TAO_PHandle j = 0;
00923        j != mp.profile_count ();
00924        ++j)
00925     {
00926       const TAO_Profile *profile = mp.get_profile (j);
00927       TAO::ObjectKey_var objkey = profile->_key ();
00928 
00929       if (objkey->length() < TAO_Root_POA::TAO_OBJECTKEY_PREFIX_SIZE
00930           || ACE_OS::memcmp (objkey->get_buffer (),
00931                              &TAO_Root_POA::objectkey_prefix[0],
00932                              TAO_Root_POA::TAO_OBJECTKEY_PREFIX_SIZE) != 0)
00933         continue;
00934 
00935       TAO_ServantBase *servant = 0;
00936 
00937       try
00938         {
00939           this->find_servant (objkey.in (), servant);
00940         }
00941       catch (const ::CORBA::Exception&)
00942         {
00943         }
00944 
00945       return servant;
00946     }
00947 
00948   return 0;
00949 }
00950 
00951 // ****************************************************************
00952 
00953 TAO_Object_Adapter::Hint_Strategy::~Hint_Strategy (void)
00954 {
00955 }
00956 
00957 void
00958 TAO_Object_Adapter::Hint_Strategy::object_adapter (TAO_Object_Adapter *oa)
00959 {
00960   this->object_adapter_ = oa;
00961 }
00962 
00963 TAO_Object_Adapter::Active_Hint_Strategy::Active_Hint_Strategy (CORBA::ULong map_size)
00964   : persistent_poa_system_map_ (map_size)
00965 {
00966 }
00967 
00968 TAO_Object_Adapter::Active_Hint_Strategy::~Active_Hint_Strategy (void)
00969 {
00970 }
00971 
00972 int
00973 TAO_Object_Adapter::Active_Hint_Strategy::find_persistent_poa (
00974   const poa_name &system_name,
00975   TAO_Root_POA *&poa)
00976 {
00977   poa_name folded_name;
00978   int result = this->persistent_poa_system_map_.recover_key (system_name,
00979                                                              folded_name);
00980 
00981   if (result == 0)
00982     {
00983       result = this->persistent_poa_system_map_.find (system_name, poa);
00984       if (result != 0
00985           || folded_name != poa->folded_name ())
00986         {
00987           result =
00988             this->object_adapter_->persistent_poa_name_map_->find (folded_name,
00989                                                                    poa);
00990           if (result != 0)
00991             {
00992               result = this->object_adapter_->activate_poa (folded_name, poa);
00993             }
00994         }
00995     }
00996 
00997   return result;
00998 }
00999 
01000 int
01001 TAO_Object_Adapter::Active_Hint_Strategy::bind_persistent_poa (
01002   const poa_name &folded_name,
01003   TAO_Root_POA *poa,
01004   poa_name_out system_name)
01005 {
01006   poa_name name = folded_name;
01007   int result = this->persistent_poa_system_map_.bind_modify_key (poa,
01008                                                                  name);
01009 
01010   if (result == 0)
01011     {
01012       result =
01013         this->object_adapter_->persistent_poa_name_map_->bind (folded_name,
01014                                                                poa);
01015 
01016       if (result != 0)
01017         this->persistent_poa_system_map_.unbind (name);
01018       else
01019         ACE_NEW_RETURN (system_name,
01020                         poa_name (name),
01021                         -1);
01022     }
01023 
01024   return result;
01025 }
01026 
01027 int
01028 TAO_Object_Adapter::Active_Hint_Strategy::unbind_persistent_poa (
01029   const poa_name &folded_name,
01030   const poa_name &system_name)
01031 {
01032   int result = this->persistent_poa_system_map_.unbind (system_name);
01033 
01034   if (result == 0)
01035     result =
01036       this->object_adapter_->persistent_poa_name_map_->unbind (folded_name);
01037 
01038   return result;
01039 }
01040 
01041 TAO_Object_Adapter::No_Hint_Strategy::~No_Hint_Strategy (void)
01042 {
01043 }
01044 
01045 int
01046 TAO_Object_Adapter::No_Hint_Strategy::find_persistent_poa (
01047   const poa_name &system_name,
01048   TAO_Root_POA *&poa)
01049 {
01050   int result =
01051     this->object_adapter_->persistent_poa_name_map_->find (system_name,
01052                                                            poa);
01053   if (result != 0)
01054     {
01055       result =
01056         this->object_adapter_->activate_poa (system_name, poa);
01057     }
01058 
01059   return result;
01060 }
01061 
01062 int
01063 TAO_Object_Adapter::No_Hint_Strategy::bind_persistent_poa (
01064   const poa_name &folded_name,
01065   TAO_Root_POA *poa,
01066   poa_name_out system_name)
01067 {
01068   int result =
01069     this->object_adapter_->persistent_poa_name_map_->bind (folded_name, poa);
01070   if (result == 0)
01071     ACE_NEW_RETURN (system_name,
01072                     poa_name (folded_name),
01073                     -1);
01074   return result;
01075 }
01076 
01077 int
01078 TAO_Object_Adapter::No_Hint_Strategy::unbind_persistent_poa (
01079   const poa_name & folded_name,
01080   const poa_name & /* system_name */)
01081 {
01082   return this->object_adapter_->persistent_poa_name_map_->unbind (folded_name);
01083 }
01084 
01085 TAO_Object_Adapter::poa_name_iterator::poa_name_iterator (
01086   int begin,
01087   CORBA::ULong size,
01088   const CORBA::Octet *folded_buffer)
01089   : size_ (size),
01090     folded_buffer_ (folded_buffer),
01091     last_separator_ ((CORBA::ULong) ~0)
01092 {
01093   if (begin)
01094     {
01095       this->position_ = (CORBA::ULong) ~0;
01096       this->operator++ ();
01097     }
01098   else
01099     this->position_ = this->size_;
01100 }
01101 
01102 bool
01103 TAO_Object_Adapter::poa_name_iterator::operator== (const poa_name_iterator &rhs) const
01104 {
01105   return this->position_ == rhs.position_;
01106 }
01107 
01108 bool
01109 TAO_Object_Adapter::poa_name_iterator::operator!= (const poa_name_iterator &rhs) const
01110 {
01111   return !this->operator== (rhs);
01112 }
01113 
01114 ACE_CString
01115 TAO_Object_Adapter::poa_name_iterator::operator* () const
01116 {
01117   CORBA::ULong start_at =
01118     this->last_separator_ +
01119     TAO_Root_POA::name_separator_length ();
01120 
01121   CORBA::ULong how_many =
01122     this->position_
01123     - this->last_separator_
01124     - TAO_Root_POA::name_separator_length ();
01125 
01126   return ACE_CString (reinterpret_cast <const char *>
01127                                        (&this->folded_buffer_[start_at]),
01128                       how_many);
01129 }
01130 
01131 TAO_Object_Adapter::poa_name_iterator &
01132 TAO_Object_Adapter::poa_name_iterator::operator++ (void)
01133 {
01134   for (this->last_separator_ = this->position_;
01135        ;
01136        )
01137     {
01138       ++this->position_;
01139       if (this->position_ < this->size_)
01140         {
01141           if (this->folded_buffer_[this->position_] == TAO_Root_POA::name_separator ())
01142             break;
01143         }
01144       else
01145         break;
01146     }
01147 
01148   return *this;
01149 }
01150 
01151 TAO_Object_Adapter::iteratable_poa_name::iteratable_poa_name (
01152   const poa_name &folded_name)
01153   : folded_name_ (folded_name)
01154 {
01155 }
01156 
01157 TAO_Object_Adapter::iteratable_poa_name::iterator
01158 TAO_Object_Adapter::iteratable_poa_name::begin (void) const
01159 {
01160   return iterator (1,
01161                    this->folded_name_.length (),
01162                    this->folded_name_.get_buffer ());
01163 }
01164 
01165 TAO_Object_Adapter::iteratable_poa_name::iterator
01166 TAO_Object_Adapter::iteratable_poa_name::end (void) const
01167 {
01168   return iterator (0,
01169                    this->folded_name_.length (),
01170                    this->folded_name_.get_buffer ());
01171 }
01172 
01173 void
01174 TAO_Object_Adapter::wait_for_non_servant_upcalls_to_complete (void)
01175 {
01176   // Check if a non-servant upcall is in progress.  If a non-servant
01177   // upcall is in progress, wait for it to complete.  Unless of
01178   // course, the thread making the non-servant upcall is this thread.
01179   while (this->enable_locking_ &&
01180          this->non_servant_upcall_in_progress_ &&
01181          ! ACE_OS::thr_equal (this->non_servant_upcall_thread_,
01182                               ACE_OS::thr_self ()))
01183     {
01184       // If so wait...
01185       int const result = this->non_servant_upcall_condition_.wait ();
01186       if (result == -1)
01187         throw ::CORBA::OBJ_ADAPTER ();
01188     }
01189 }
01190 
01191 void
01192 TAO_Object_Adapter::wait_for_non_servant_upcalls_to_complete_no_throw (void)
01193 {
01194   // Non-exception throwing version.
01195   try
01196     {
01197       this->wait_for_non_servant_upcalls_to_complete ();
01198     }
01199   catch (const ::CORBA::Exception&)
01200     {
01201       ACE_ERROR ((LM_ERROR,
01202                   "TAO_Object_Adapter::wait_for_non_servant_upcalls_to_complete "
01203                   "threw exception it should not have!\n"));
01204     }
01205 }
01206 
01207 void
01208 TAO_Object_Adapter::servant_dispatcher (TAO_Servant_Dispatcher *dispatcher)
01209 {
01210   if (this->servant_dispatcher_)
01211     delete this->servant_dispatcher_;
01212 
01213   this->servant_dispatcher_ = dispatcher;
01214 }
01215 
01216 void
01217 TAO_Object_Adapter::do_dispatch (TAO_ServerRequest& req,
01218                                  TAO::Portable_Server::Servant_Upcall& upcall)
01219 {
01220   upcall.servant ()->_dispatch(req, &upcall);
01221 }
01222 
01223 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:40:54 2010 for TAO_PortableServer by  doxygen 1.4.7