RequestProcessingStrategyServantActivator.cpp

Go to the documentation of this file.
00001 // $Id: RequestProcessingStrategyServantActivator.cpp 76995 2007-02-11 12:51:42Z johnnyw $
00002 #include "tao/orbconf.h"
00003 
00004 ACE_RCSID (PortableServer,
00005            Request_Processing,
00006            "$Id: RequestProcessingStrategyServantActivator.cpp 76995 2007-02-11 12:51:42Z johnnyw $")
00007 
00008 #if (TAO_HAS_MINIMUM_POA == 0) && !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
00009 
00010 #include "tao/ORB_Constants.h"
00011 #include "tao/PortableServer/ServantActivatorC.h"
00012 #include "tao/PortableServer/RequestProcessingStrategyServantActivator.h"
00013 #include "tao/PortableServer/Servant_Base.h"
00014 #include "tao/PortableServer/Non_Servant_Upcall.h"
00015 #include "tao/PortableServer/Root_POA.h"
00016 #include "tao/PortableServer/POA_Current_Impl.h"
00017 #include "tao/PortableServer/Servant_Upcall.h"
00018 
00019 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00020 
00021 namespace TAO
00022 {
00023   namespace Portable_Server
00024   {
00025     RequestProcessingStrategyServantActivator::RequestProcessingStrategyServantActivator (void) :
00026       etherealize_objects_ (true)
00027     {
00028     }
00029 
00030     void
00031     RequestProcessingStrategyServantActivator::strategy_cleanup (void)
00032     {
00033       {
00034         Non_Servant_Upcall non_servant_upcall (*this->poa_);
00035         ACE_UNUSED_ARG (non_servant_upcall);
00036 
00037         this->servant_activator_ = PortableServer::ServantActivator::_nil ();
00038       }
00039 
00040       RequestProcessingStrategy::strategy_cleanup ();
00041     }
00042 
00043     PortableServer::ServantManager_ptr
00044     RequestProcessingStrategyServantActivator::get_servant_manager (void)
00045     {
00046       return PortableServer::ServantManager::_duplicate (this->servant_activator_.in ());
00047     }
00048 
00049     void
00050     RequestProcessingStrategyServantActivator::set_servant_manager (
00051       PortableServer::ServantManager_ptr imgr)
00052     {
00053       // This operation sets the default servant manager associated with the
00054       // POA. This operation may only be invoked once after a POA has been
00055       // created. Attempting to set the servant manager after one has already
00056       // been set will result in the BAD_INV_ORDER system exception with
00057       // standard minor code 6 being raised (see 11.3.9.12 of the corba spec)
00058       if (!CORBA::is_nil (this->servant_activator_.in ()))
00059         {
00060           throw ::CORBA::BAD_INV_ORDER (CORBA::OMGVMCID | 6, CORBA::COMPLETED_NO);
00061         }
00062 
00063       this->servant_activator_ =
00064         PortableServer::ServantActivator::_narrow (imgr);
00065 
00066       this->validate_servant_manager (this->servant_activator_.in ());
00067     }
00068 
00069     TAO_SERVANT_LOCATION
00070     RequestProcessingStrategyServantActivator::locate_servant (
00071       const PortableServer::ObjectId &system_id,
00072       PortableServer::Servant &servant)
00073     {
00074       TAO_SERVANT_LOCATION location = TAO_SERVANT_NOT_FOUND;
00075 
00076       location = this->poa_->servant_present (system_id, servant);
00077 
00078       if (location == TAO_SERVANT_NOT_FOUND)
00079         {
00080           if (!CORBA::is_nil (this->servant_activator_.in ()))
00081             {
00082               location = TAO_SERVANT_MANAGER;
00083             }
00084         }
00085 
00086       return location;
00087     }
00088 
00089     PortableServer::Servant
00090     RequestProcessingStrategyServantActivator::locate_servant (
00091       const char * /*operation*/,
00092       const PortableServer::ObjectId &system_id,
00093       TAO::Portable_Server::Servant_Upcall &servant_upcall,
00094       TAO::Portable_Server::POA_Current_Impl &poa_current_impl,
00095       bool &wait_occurred_restart_call
00096       )
00097     {
00098       PortableServer::Servant servant = 0;
00099 
00100       servant = this->poa_->find_servant (system_id,
00101                                           servant_upcall,
00102                                           poa_current_impl);
00103 
00104       if (servant != 0)
00105         {
00106           return servant;
00107         }
00108 
00109       // If the POA has the USE_SERVANT_MANAGER policy, a servant manager
00110       // has been associated with the POA so the POA will invoke incarnate
00111       // or preinvoke on it to find a servant that may handle the
00112       // request. (The choice of method depends on the NON_RETAIN or
00113       // RETAIN policy of the POA.) If no servant manager has been
00114       // associated with the POA, the POA raises the OBJ_ADAPTER system
00115       // exception.
00116       //
00117       // If a servant manager is located and invoked, but the servant
00118       // manager is not directly capable of incarnating the object, it
00119       // (the servant manager) may deal with the circumstance in a variety
00120       // of ways, all of which are the application's responsibility.  Any
00121       // system exception raised by the servant manager will be returned
00122       // to the client in the reply. In addition to standard CORBA
00123       // exceptions, a servant manager is capable of raising a
00124       // ForwardRequest exception. This exception includes an object
00125       // reference.
00126       //
00127 
00128       this->validate_servant_manager (this->servant_activator_.in ());
00129 
00130       servant = this->incarnate_servant (poa_current_impl.object_id ());
00131 
00132       // If the incarnate operation returns a servant that is
00133       // already active for a different Object Id and if the POA
00134       // also has the UNIQUE_ID policy, the incarnate has violated
00135       // the POA policy and is considered to be in error. The POA
00136       // will raise an OBJ_ADAPTER system exception for the
00137       // request.
00138       bool may_activate =
00139         this->poa_->is_servant_activation_allowed (servant,
00140                                                    wait_occurred_restart_call);
00141 
00142       if (!may_activate)
00143         {
00144           // If we are not allowed to activate the servant, throw an exception
00145           // etherealize is not called because the servant is never added to
00146           // the active object map
00147           throw ::CORBA::OBJ_ADAPTER ();
00148         }
00149 
00150       // The POA enters the returned Servant value into the Active
00151       // Object Map so that subsequent requests with the same
00152       // ObjectId value will be delivered directly to that servant
00153       // without invoking the servant manager.  Only run if there
00154       // are no errors or if a restart is not required.
00155       if (!wait_occurred_restart_call)
00156         {
00157           int const result =
00158             this->poa_->
00159               rebind_using_user_id_and_system_id (servant,
00160                                                   poa_current_impl.object_id (),
00161                                                   system_id,
00162                                                   servant_upcall);
00163           if (result != 0)
00164             {
00165               // Throw an exception, etherealize is not called because servant
00166               // is not added to the active object map
00167               throw ::CORBA::OBJ_ADAPTER ();
00168             }
00169 
00170           // Increment the reference count on the servant upcall.
00171           servant_upcall.increment_servant_refcount ();
00172         }
00173       else
00174         {
00175           CORBA::Boolean cleanup_in_progress = 0;
00176           this->etherealize_servant (poa_current_impl.object_id (),
00177                                      servant,
00178                                      cleanup_in_progress);
00179 
00180           // We ended up waiting on a condition variable, the
00181           // POA state may have changed while we are waiting.
00182           // Therefore, we need to restart this call.
00183           return 0;
00184         }
00185 
00186       // Success
00187       return servant;
00188     }
00189 
00190     void
00191     RequestProcessingStrategyServantActivator::etherealize_servant (
00192       const PortableServer::ObjectId& object_id,
00193       PortableServer::Servant servant,
00194       CORBA::Boolean cleanup_in_progress
00195       )
00196     {
00197       CORBA::Boolean remaining_activations =
00198         this->poa_->servant_has_remaining_activations (servant);
00199 
00200       // ATTENTION: Trick locking here, see class header for details
00201       Non_Servant_Upcall non_servant_upcall (*this->poa_);
00202       ACE_UNUSED_ARG (non_servant_upcall);
00203 
00204       // @todo This is not according to the spec. According to 11.3.6.2 at the
00205       // end when etherealize returns a system exception the POA ignores the
00206       // exception
00207       this->servant_activator_->etherealize (object_id,
00208                                              this->poa_,
00209                                              servant,
00210                                              cleanup_in_progress,
00211                                              remaining_activations);
00212     }
00213 
00214     PortableServer::Servant
00215     RequestProcessingStrategyServantActivator::incarnate_servant (
00216       const PortableServer::ObjectId& object_id)
00217     {
00218       PortableServer::Servant servant = 0;
00219 
00220       // ATTENTION: Trick locking here, see class header for details
00221       Non_Servant_Upcall non_servant_upcall (*this->poa_);
00222       ACE_UNUSED_ARG (non_servant_upcall);
00223 
00224       // @@
00225       // Invocations of incarnate on the servant manager are serialized.
00226       // Invocations of etherealize on the servant manager are serialized.
00227       // Invocations of incarnate and etherealize on the servant manager are mutually exclusive.
00228       servant = this->servant_activator_->incarnate (object_id, this->poa_);
00229 
00230       if (servant == 0)
00231         {
00232           throw ::CORBA::OBJ_ADAPTER (CORBA::OMGVMCID | 7, CORBA::COMPLETED_NO);
00233         }
00234       else
00235         {
00236           return servant;
00237         }
00238     }
00239 
00240     void
00241     RequestProcessingStrategyServantActivator::cleanup_servant (
00242       PortableServer::Servant servant,
00243       const PortableServer::ObjectId &user_id
00244       )
00245     {
00246       // If a servant manager is associated with the POA,
00247       // ServantLocator::etherealize will be invoked with the oid and the
00248       // servant. (The deactivate_object operation does not wait for the
00249       // etherealize operation to complete before deactivate_object
00250       // returns.)
00251       //
00252       // Note: If the servant associated with the oid is serving multiple
00253       // Object Ids, ServantLocator::etherealize may be invoked multiple
00254       // times with the same servant when the other objects are
00255       // deactivated. It is the responsibility of the object
00256       // implementation to refrain from destroying the servant while it is
00257       // active with any Id.
00258 
00259       // If the POA has no ServantActivator associated with it, the POA
00260       // implementation calls _remove_ref when all operation invocations
00261       // have completed. If there is a ServantActivator, the Servant is
00262       // consumed by the call to ServantActivator::etherealize instead.
00263 
00264       // @bala, is this order correct, see 11.3.9.17 of the spec, it
00265       // says first remove from the map, then etherealize. not the
00266       // other way around
00267       // @@ Johnny, you are right! This will not show up until a
00268       // thread is trying to activate the object in another thread
00269       // using activate_object_with_id (). A test case is a must for
00270       // atleast this issue.
00271       if (servant)
00272         {
00273           if (this->etherealize_objects_ &&
00274               !CORBA::is_nil (this->servant_activator_.in ()))
00275             {
00276               this->etherealize_servant (user_id,
00277                                          servant,
00278                                          this->poa_->cleanup_in_progress ());
00279             }
00280           else
00281             {
00282               // ATTENTION: Trick locking here, see class header for details
00283               Non_Servant_Upcall non_servant_upcall (*this->poa_);
00284               ACE_UNUSED_ARG (non_servant_upcall);
00285 
00286               servant->_remove_ref ();
00287             }
00288         }
00289 
00290       // This operation causes the association of the Object Id specified
00291       // by the oid parameter and its servant to be removed from the
00292       // Active Object Map.
00293       if (this->poa_->unbind_using_user_id (user_id) != 0)
00294         throw ::CORBA::OBJ_ADAPTER ();
00295     }
00296 
00297     void
00298     RequestProcessingStrategyServantActivator::etherealize_objects (
00299       CORBA::Boolean etherealize_objects)
00300     {
00301       this->etherealize_objects_ = etherealize_objects;
00302     }
00303 
00304     void
00305     RequestProcessingStrategyServantActivator::post_invoke_servant_cleanup(
00306       const PortableServer::ObjectId &/*system_id*/,
00307       const TAO::Portable_Server::Servant_Upcall &/*servant_upcall*/)
00308     {
00309     }
00310   }
00311 }
00312 
00313 TAO_END_VERSIONED_NAMESPACE_DECL
00314 
00315 #endif /* TAO_HAS_MINIMUM_POA == 0 */

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