Bindings_Iterator_T.cpp

Go to the documentation of this file.
00001 // $Id: Bindings_Iterator_T.cpp 77276 2007-02-21 08:26:36Z johnnyw $
00002 
00003 #ifndef TAO_BINDINGS_ITERATOR_T_CPP
00004 #define TAO_BINDINGS_ITERATOR_T_CPP
00005 
00006 #include "orbsvcs/Naming/Bindings_Iterator_T.h"
00007 
00008 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00009 # pragma once
00010 #endif /* ACE_LACKS_PRAGMA_ONCE */
00011 
00012 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00013 
00014 template <class ITERATOR, class TABLE_ENTRY>
00015 TAO_Bindings_Iterator<ITERATOR, TABLE_ENTRY>::TAO_Bindings_Iterator (
00016   TAO_Hash_Naming_Context *context,
00017   ITERATOR *hash_iter,
00018   PortableServer::POA_ptr poa,
00019   TAO_SYNCH_RECURSIVE_MUTEX &lock)
00020   : destroyed_ (false),
00021     context_ (context),
00022     hash_iter_ (hash_iter),
00023     lock_ (lock),
00024     poa_ (PortableServer::POA::_duplicate (poa))
00025 
00026 {
00027 }
00028 
00029 template <class ITERATOR, class TABLE_ENTRY>
00030 TAO_Bindings_Iterator<ITERATOR, TABLE_ENTRY>::~TAO_Bindings_Iterator (void)
00031 {
00032   delete hash_iter_;
00033 
00034   // Since we are going away, decrement the reference count on the
00035   // Naming Context we were iterating over.
00036   context_->interface ()->_remove_ref ();
00037 }
00038 
00039 // Return the Default POA of this Servant
00040 template <class ITERATOR, class TABLE_ENTRY> PortableServer::POA_ptr
00041 TAO_Bindings_Iterator<ITERATOR, TABLE_ENTRY>::_default_POA ()
00042 {
00043   return PortableServer::POA::_duplicate (this->poa_.in ());
00044 }
00045 
00046 template <class ITERATOR, class TABLE_ENTRY> CORBA::Boolean
00047 TAO_Bindings_Iterator<ITERATOR, TABLE_ENTRY>::next_one (
00048     CosNaming::Binding_out b)
00049 {
00050   CosNaming::Binding *binding = 0;
00051 
00052   // Allocate a binding to be returned (even if there no more
00053   // bindings, we need to allocate an out parameter.)
00054   ACE_NEW_THROW_EX (binding,
00055                     CosNaming::Binding,
00056                     CORBA::NO_MEMORY ());
00057 
00058   b = binding;
00059 
00060   ACE_GUARD_THROW_EX (TAO_SYNCH_RECURSIVE_MUTEX,
00061                       ace_mon,
00062                       this->lock_,
00063                       CORBA::INTERNAL ());
00064 
00065   // Check to make sure this object is still valid.
00066   if (this->destroyed_)
00067     throw CORBA::OBJECT_NOT_EXIST ();
00068 
00069   // If the context we are iterating over has been destroyed,
00070   // self-destruct.
00071   if (context_->destroyed ())
00072     {
00073       destroy ();
00074 
00075       throw CORBA::OBJECT_NOT_EXIST ();
00076     }
00077 
00078   // If there are no more bindings.
00079   if (hash_iter_->done ())
00080     {
00081       b->binding_type = CosNaming::nobject;
00082       b->binding_name.length (0);
00083       return 0;
00084     }
00085   else
00086     {
00087       // Return a binding.
00088       TABLE_ENTRY *hash_entry = 0;
00089       hash_iter_->next (hash_entry);
00090 
00091       if (populate_binding (hash_entry, *binding) == 0)
00092         throw CORBA::NO_MEMORY ();
00093 
00094       hash_iter_->advance ();
00095       return 1;
00096     }
00097 }
00098 
00099 template <class ITERATOR, class TABLE_ENTRY> CORBA::Boolean
00100 TAO_Bindings_Iterator<ITERATOR, TABLE_ENTRY>::next_n (
00101     CORBA::ULong how_many,
00102     CosNaming::BindingList_out bl)
00103 {
00104   // We perform an allocation before obtaining the lock so that an out
00105   // parameter is allocated in case we fail to obtain the lock.
00106   ACE_NEW_THROW_EX (bl,
00107                     CosNaming::BindingList (0),
00108                     CORBA::NO_MEMORY ());
00109   // Obtain the lock.
00110   ACE_GUARD_THROW_EX (TAO_SYNCH_RECURSIVE_MUTEX,
00111                       ace_mon,
00112                       this->lock_,
00113                       CORBA::INTERNAL ());
00114 
00115   // Check to make sure this object is still valid.
00116   if (this->destroyed_)
00117     throw CORBA::OBJECT_NOT_EXIST ();
00118 
00119   // If the context we are iterating over has been destroyed,
00120   // self-destruct.
00121   if (context_->destroyed ())
00122     {
00123       destroy ();
00124 
00125       throw CORBA::OBJECT_NOT_EXIST ();
00126     }
00127 
00128   // Check for illegal parameter values.
00129   if (how_many == 0)
00130     throw CORBA::BAD_PARAM ();
00131 
00132   // If there are no more bindings...
00133   if (hash_iter_->done ())
00134       return 0;
00135   else
00136     {
00137       // Initially assume that the iterator has the requested number of
00138       // bindings.
00139       bl->length (how_many);
00140 
00141       TABLE_ENTRY *hash_entry = 0;
00142 
00143       // Iterate and populate the BindingList.
00144       for (CORBA::ULong i = 0; i < how_many; i++)
00145         {
00146           hash_iter_->next (hash_entry);
00147 
00148           if (populate_binding (hash_entry, bl[i]) == 0)
00149             throw CORBA::NO_MEMORY ();
00150 
00151           if (hash_iter_->advance () == 0)
00152             {
00153               // If no more bindings are left, reset length to the actual
00154               // number of bindings populated, and get out of the loop.
00155               bl->length (i + 1);
00156               break;
00157             }
00158         }
00159       return 1;
00160     }
00161 }
00162 
00163 template <class ITERATOR, class TABLE_ENTRY> void
00164 TAO_Bindings_Iterator<ITERATOR, TABLE_ENTRY>::destroy (void)
00165 {
00166   ACE_GUARD_THROW_EX (TAO_SYNCH_RECURSIVE_MUTEX,
00167                       ace_mon,
00168                       this->lock_,
00169                       CORBA::INTERNAL ());
00170 
00171   // Check to make sure this object is still valid.
00172   if (this->destroyed_)
00173     throw CORBA::OBJECT_NOT_EXIST ();
00174 
00175   // Mark the object invalid.
00176   this->destroyed_ = true;
00177 
00178   PortableServer::ObjectId_var id =
00179     poa_->servant_to_id (this);
00180 
00181   poa_->deactivate_object (id.in ());
00182 }
00183 
00184 template <class ITERATOR, class TABLE_ENTRY> int
00185 TAO_Bindings_Iterator<ITERATOR, TABLE_ENTRY>::populate_binding (
00186   TABLE_ENTRY *hash_entry,
00187   CosNaming::Binding &b)
00188 {
00189   b.binding_type = hash_entry->int_id_.type_;
00190   b.binding_name.length (1);
00191 
00192   // Here we perform a check before assignment to make sure
00193   // CORBA::string_dup is not called on 0 pointer, since the spec does
00194   // not say what should happen in that case.
00195   if (hash_entry->ext_id_.id () != 0)
00196     {
00197       b.binding_name[0].id =
00198         hash_entry->ext_id_.id ();
00199       if (b.binding_name[0].id.in () == 0)
00200         return 0;
00201     }
00202   if (hash_entry->ext_id_.kind () != 0)
00203     {
00204       b.binding_name[0].kind =
00205         hash_entry->ext_id_.kind ();
00206       if (b.binding_name[0].kind.in () == 0)
00207         return 0;
00208     }
00209   return 1;
00210 }
00211 
00212 TAO_END_VERSIONED_NAMESPACE_DECL
00213 
00214 #endif /* TAO_BINDINGS_ITERATOR_T_CPP */

Generated on Sun Jan 27 16:15:29 2008 for TAO_CosNaming by doxygen 1.3.6