00001 #include "orbsvcs/Naming/Storable_Naming_Context.h"
00002 #include "orbsvcs/Naming/Bindings_Iterator_T.h"
00003
00004 #include "tao/debug.h"
00005
00006
00007
00008
00009
00010 #ifdef _MSC_VER
00011 # pragma warning(disable: 4786)
00012
00013
00014 #endif
00015
00016 #include "ace/Auto_Ptr.h"
00017 #include "ace/OS_NS_stdio.h"
00018
00019 ACE_RCSID (Naming,
00020 Storable_Naming_Context,
00021 "$Id: Storable_Naming_Context.cpp 80501 2008-01-25 18:55:41Z pollackj $")
00022
00023
00024 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00025
00026 const char * TAO_Storable_Naming_Context::root_name_;
00027 ACE_UINT32 TAO_Storable_Naming_Context::gcounter_;
00028 ACE_Auto_Ptr<TAO_Storable_Base> TAO_Storable_Naming_Context::gfl_;
00029 int TAO_Storable_Naming_Context::redundant_;
00030
00031 TAO_Storable_IntId::TAO_Storable_IntId (void)
00032 : ref_ (CORBA::string_dup ("")),
00033 type_ (CosNaming::nobject)
00034 {
00035 }
00036
00037 TAO_Storable_IntId::TAO_Storable_IntId (const char * ior,
00038 CosNaming::BindingType type)
00039 : ref_ (ior),
00040 type_ (type)
00041 {
00042 }
00043
00044 TAO_Storable_IntId::TAO_Storable_IntId (const TAO_Storable_IntId &rhs)
00045 {
00046 ref_ = rhs.ref_;
00047 type_ = rhs.type_;
00048 }
00049
00050 TAO_Storable_IntId::~TAO_Storable_IntId (void)
00051 {
00052 }
00053
00054 void
00055 TAO_Storable_IntId::operator= (const TAO_Storable_IntId &rhs)
00056 {
00057
00058 if (&rhs == this)
00059 return;
00060
00061 type_ = rhs.type_;
00062 ref_ = rhs.ref_;
00063 }
00064
00065 TAO_Storable_ExtId::TAO_Storable_ExtId (void)
00066 : id_ (CORBA::string_dup ("")),
00067 kind_ (CORBA::string_dup (""))
00068 {
00069 }
00070
00071 TAO_Storable_ExtId::TAO_Storable_ExtId (const char *id, const char *kind)
00072 : id_ (id)
00073 , kind_ (kind)
00074 {
00075 }
00076
00077 TAO_Storable_ExtId::TAO_Storable_ExtId (const TAO_Storable_ExtId &rhs)
00078 {
00079 id_ = rhs.id_;
00080 kind_ = rhs.kind_;
00081 }
00082
00083 TAO_Storable_ExtId::~TAO_Storable_ExtId (void)
00084 {
00085 }
00086
00087 void
00088 TAO_Storable_ExtId::operator= (const TAO_Storable_ExtId &rhs)
00089 {
00090
00091 if (&rhs == this)
00092 return;
00093
00094 id_ = rhs.id_;
00095 kind_ = rhs.kind_;
00096 }
00097
00098 bool
00099 TAO_Storable_ExtId::operator== (const TAO_Storable_ExtId &rhs) const
00100 {
00101 return (ACE_OS::strcmp (id_.in(), rhs.id_.in()) == 0)
00102 && (ACE_OS::strcmp (kind_.in(), rhs.kind_.in()) == 0);
00103 }
00104
00105 bool
00106 TAO_Storable_ExtId::operator!= (const TAO_Storable_ExtId &rhs) const
00107 {
00108 return (ACE_OS::strcmp (id_.in(), rhs.id_.in()) != 0)
00109 || (ACE_OS::strcmp (kind_.in(), rhs.kind_.in()) != 0);
00110 }
00111
00112 u_long
00113 TAO_Storable_ExtId::hash (void) const
00114 {
00115
00116
00117
00118
00119 ACE_CString temp (id_.in());
00120 temp += kind_.in();
00121
00122 return temp.hash ();
00123 }
00124
00125 const char *
00126 TAO_Storable_ExtId::id (void)
00127 {
00128 return id_.in();
00129 }
00130
00131 const char *
00132 TAO_Storable_ExtId::kind (void)
00133 {
00134 return kind_.in();
00135 }
00136
00137 int
00138 TAO_Storable_Bindings_Map::unbind (const char *id, const char *kind)
00139 {
00140 ACE_TRACE("unbind");
00141 TAO_Storable_ExtId name (id, kind);
00142 return this->map_.unbind (name);
00143 }
00144
00145 int
00146 TAO_Storable_Bindings_Map::bind (const char *id,
00147 const char *kind,
00148 CORBA::Object_ptr obj,
00149 CosNaming::BindingType type)
00150 {
00151 ACE_TRACE("bind");
00152 return this->shared_bind (id, kind, obj, type, 0);
00153 }
00154
00155 int
00156 TAO_Storable_Bindings_Map::rebind (const char *id,
00157 const char *kind,
00158 CORBA::Object_ptr obj,
00159 CosNaming::BindingType type)
00160 {
00161 ACE_TRACE("rebind");
00162 return this->shared_bind (id, kind, obj, type, 1);
00163 }
00164
00165 int
00166 TAO_Storable_Bindings_Map::find (const char *id,
00167 const char *kind,
00168 CORBA::Object_ptr & obj,
00169 CosNaming::BindingType &type)
00170 {
00171 ACE_TRACE("find");
00172 TAO_Storable_ExtId name (id, kind);
00173 TAO_Storable_IntId entry;
00174
00175 if (this->map_.find (name, entry) != 0)
00176 {
00177 return -1;
00178 }
00179 else
00180 {
00181 obj = orb_->string_to_object (entry.ref_.in());
00182 type = entry.type_;
00183
00184 return 0;
00185 }
00186 }
00187
00188 TAO_Storable_Bindings_Map::TAO_Storable_Bindings_Map (size_t hash_table_size,
00189 CORBA::ORB_ptr orb)
00190 : map_ (hash_table_size),
00191 orb_(CORBA::ORB::_duplicate (orb))
00192 {
00193 ACE_TRACE("TAO_Storable_Bindings_Map");
00194 }
00195
00196 TAO_Storable_Bindings_Map::~TAO_Storable_Bindings_Map (void)
00197 {
00198 ACE_TRACE("~TAO_Storable_Bindings_Map");
00199 }
00200
00201 TAO_Storable_Bindings_Map::HASH_MAP &
00202 TAO_Storable_Bindings_Map::map (void)
00203 {
00204 ACE_TRACE("map");
00205 return map_;
00206 }
00207
00208 size_t
00209 TAO_Storable_Bindings_Map::current_size (void)
00210 {
00211 ACE_TRACE("current_size");
00212 return map_.current_size ();
00213 }
00214
00215 size_t
00216 TAO_Storable_Bindings_Map::total_size (void)
00217 {
00218 ACE_TRACE("total_size");
00219 return map_.total_size ();
00220 }
00221
00222 int
00223 TAO_Storable_Bindings_Map::shared_bind (const char * id,
00224 const char * kind,
00225 CORBA::Object_ptr obj,
00226 CosNaming::BindingType type,
00227 int rebind)
00228 {
00229 ACE_TRACE("shared_bind");
00230 TAO_Storable_ExtId new_name (id, kind);
00231 CORBA::String_var ior = orb_->object_to_string(obj);
00232 TAO_Storable_IntId new_entry (ior.in(), type);
00233 TAO_Storable_IntId old_entry;
00234
00235 if (rebind == 0)
00236 {
00237
00238 return this->map_.bind (new_name, new_entry);
00239 }
00240 else
00241
00242 {
00243
00244 if (this->map_.find (new_name,
00245 old_entry) == 0
00246 && type != old_entry.type_)
00247 return -2;
00248
00249 else
00250 return this->map_.rebind (new_name, new_entry);
00251 }
00252 }
00253
00254 void TAO_Storable_Naming_Context::Write(TAO_Storable_Base& wrtr)
00255 {
00256 ACE_TRACE("Write");
00257 TAO_NS_Persistence_Header header;
00258
00259 header.size (static_cast<unsigned int> (storable_context_->current_size()));
00260 header.destroyed (destroyed_);
00261
00262 wrtr << header;
00263
00264 if (0u == header.size ())
00265 return;
00266
00267 ACE_Hash_Map_Iterator<TAO_Storable_ExtId,TAO_Storable_IntId,
00268 ACE_Null_Mutex> it = storable_context_->map().begin();
00269 ACE_Hash_Map_Iterator<TAO_Storable_ExtId,TAO_Storable_IntId,
00270 ACE_Null_Mutex> itend = storable_context_->map().end();
00271
00272 ACE_Hash_Map_Entry<TAO_Storable_ExtId,TAO_Storable_IntId> ent = *it;
00273
00274 while (!(it == itend))
00275 {
00276 TAO_NS_Persistence_Record record;
00277
00278 ACE_CString name;
00279 CosNaming::BindingType bt = (*it).int_id_.type_;
00280 if (bt == CosNaming::ncontext)
00281 {
00282 CORBA::Object_var
00283 obj = orb_->string_to_object ((*it).int_id_.ref_.in ());
00284 if (obj->_is_collocated ())
00285 {
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 PortableServer::ObjectId_var
00306 oid = poa_->reference_to_id (obj.in ());
00307 CORBA::String_var
00308 nm = PortableServer::ObjectId_to_string (oid.in ());
00309 const char
00310 *newname = nm.in ();
00311 name.set (newname);
00312 record.type (TAO_NS_Persistence_Record::LOCAL_NCONTEXT);
00313 }
00314 else
00315 {
00316
00317
00318
00319
00320
00321
00322
00323
00324 name.set ((*it).int_id_.ref_.in ());
00325 record.type (TAO_NS_Persistence_Record::REMOTE_NCONTEXT);
00326 }
00327 }
00328 else
00329 {
00330 name.set ((*it).int_id_.ref_.in ());
00331 record.type (TAO_NS_Persistence_Record::OBJREF);
00332 }
00333 record.ref(name);
00334
00335 const char *myid = (*it).ext_id_.id();
00336 ACE_CString id(myid);
00337 record.id(id);
00338
00339 const char *mykind = (*it).ext_id_.kind();
00340 ACE_CString kind(mykind);
00341 record.kind(kind);
00342
00343 wrtr << record;
00344 it.advance();
00345 }
00346 }
00347
00348
00349 int
00350 TAO_Storable_Naming_Context::load_map(File_Open_Lock_and_Check *flck)
00351 {
00352 ACE_TRACE("load_map");
00353
00354 TAO_Storable_Bindings_Map *bindings_map;
00355
00356
00357 ACE_NEW_THROW_EX (bindings_map,
00358 TAO_Storable_Bindings_Map (hash_table_size_,orb_.in()),
00359 CORBA::NO_MEMORY ());
00360
00361
00362
00363 TAO_NS_Persistence_Header header;
00364 TAO_NS_Persistence_Record record;
00365
00366
00367 flck->peer() >> header;
00368 if (!flck->peer ().good ())
00369 {
00370 flck->peer ().clear ();
00371 throw CORBA::INTERNAL ();
00372 }
00373
00374
00375 this->destroyed_ = header.destroyed();
00376
00377
00378 for (unsigned int i= 0u; i<header.size(); ++i)
00379 {
00380 flck->peer() >> record;
00381 if (!flck->peer ().good ())
00382 {
00383 flck->peer ().clear ();
00384 throw CORBA::INTERNAL ();
00385 }
00386
00387 if (TAO_NS_Persistence_Record::LOCAL_NCONTEXT == record.type ())
00388 {
00389 PortableServer::ObjectId_var
00390 id = PortableServer::string_to_ObjectId (record.ref ().c_str ());
00391 const char
00392 *intf = interface_->_interface_repository_id ();
00393 CORBA::Object_var
00394 objref = poa_->create_reference_with_id (id.in (), intf);
00395 bindings_map->bind ( record.id ().c_str (),
00396 record.kind ().c_str (),
00397 objref.in (),
00398 CosNaming::ncontext );
00399 }
00400 else
00401 {
00402 CORBA::Object_var
00403 objref = orb_->string_to_object (record.ref ().c_str ());
00404 bindings_map->bind ( record.id ().c_str (),
00405 record.kind ().c_str (),
00406 objref.in (),
00407 ((TAO_NS_Persistence_Record::REMOTE_NCONTEXT == record.type ())
00408 ? CosNaming::ncontext
00409 : CosNaming::nobject ));
00410 }
00411 }
00412 storable_context_ = bindings_map;
00413 context_ = storable_context_;
00414 return 0;
00415 }
00416
00417 TAO_Storable_Naming_Context::
00418 File_Open_Lock_and_Check::File_Open_Lock_and_Check(
00419 TAO_Storable_Naming_Context * context,
00420 const char * mode)
00421 :closed_(1),
00422 context_(context)
00423 {
00424 ACE_TRACE("File_Open_Lock_and_Check");
00425
00426 rwflags_ = 0;
00427 for( unsigned int i = 0; i<ACE_OS::strlen(mode); i++ )
00428 {
00429 switch (mode[i])
00430 {
00431 case 'r': rwflags_ |= mode_read;
00432 break;
00433 case 'w': rwflags_ |= mode_write;
00434 break;
00435 case 'c': rwflags_ |= mode_create;
00436 break;
00437 default: rwflags_ = -1;
00438 }
00439 }
00440 if( rwflags_ <= 0 )
00441 {
00442 errno = EINVAL;
00443 throw CORBA::PERSIST_STORE();
00444 }
00445
00446
00447 ACE_CString file_name(context->persistence_directory_);
00448 file_name += "/";
00449 file_name += context->name_;
00450
00451
00452 fl_ = context->factory_->create_stream(file_name, ACE_TEXT_CHAR_TO_TCHAR(mode));
00453 if (TAO_Storable_Naming_Context::redundant_)
00454 {
00455 if (fl_->open() != 0)
00456 {
00457 delete fl_;
00458 throw CORBA::PERSIST_STORE();
00459 }
00460
00461
00462 if (fl_ -> flock(0, 0, 0) != 0)
00463 {
00464 fl_->close();
00465 delete fl_;
00466 throw CORBA::INTERNAL();
00467 }
00468
00469
00470
00471 closed_ = 0;
00472
00473 if ( ! (rwflags_ & mode_create) )
00474 {
00475
00476 time_t new_last_changed = fl_->last_changed();
00477 if( new_last_changed > context->last_changed_ )
00478 {
00479 context->last_changed_ = new_last_changed;
00480
00481 delete context->storable_context_;
00482
00483 context->load_map(this);
00484 }
00485 }
00486 }
00487 else if ( ! context->storable_context_ || (rwflags_ & mode_write) )
00488 {
00489 if (fl_->open() != 0)
00490 {
00491 delete fl_;
00492 throw CORBA::PERSIST_STORE();
00493 }
00494
00495
00496
00497 closed_ = 0;
00498
00499 if(!context->storable_context_)
00500 {
00501
00502 context->load_map(this);
00503 }
00504 }
00505 else
00506 {
00507
00508 delete fl_;
00509 }
00510 }
00511
00512 void
00513 TAO_Storable_Naming_Context::
00514 File_Open_Lock_and_Check::release(void)
00515 {
00516 ACE_TRACE("release");
00517 if ( ! closed_ )
00518 {
00519
00520 if(TAO_Storable_Naming_Context::redundant_)
00521 {
00522 if( rwflags_ & mode_write )
00523 context_->last_changed_ = fl_->last_changed();
00524 fl_->funlock(0, 0, 0);
00525 }
00526 fl_->close();
00527 delete fl_;
00528 closed_ = 1;
00529 }
00530 }
00531
00532 TAO_Storable_Naming_Context::
00533 File_Open_Lock_and_Check::~File_Open_Lock_and_Check(void)
00534 {
00535 ACE_TRACE("~File_Open_Lock_and_Check");
00536 this->release();
00537 }
00538
00539 TAO_Storable_Base &
00540 TAO_Storable_Naming_Context::File_Open_Lock_and_Check::peer(void)
00541 {
00542 ACE_TRACE("peer");
00543 return *fl_;
00544 }
00545
00546 TAO_Storable_Naming_Context::TAO_Storable_Naming_Context (
00547 CORBA::ORB_ptr orb,
00548 PortableServer::POA_ptr poa,
00549 const char *poa_id,
00550 TAO_Naming_Service_Persistence_Factory *factory,
00551 const ACE_TCHAR *persistence_directory,
00552 size_t hash_table_size)
00553 : TAO_Hash_Naming_Context (poa,
00554 poa_id),
00555 counter_ (0),
00556 storable_context_ (0),
00557 orb_(CORBA::ORB::_duplicate (orb)),
00558 name_ (poa_id),
00559 poa_ (PortableServer::POA::_duplicate (poa)),
00560 factory_(factory),
00561 persistence_directory_ (ACE_TEXT_ALWAYS_CHAR(persistence_directory)),
00562 hash_table_size_(hash_table_size),
00563 last_changed_(0)
00564 {
00565 ACE_TRACE("TAO_Storable_Naming_Context");
00566 }
00567
00568 TAO_Storable_Naming_Context::~TAO_Storable_Naming_Context (void)
00569 {
00570 ACE_TRACE("~TAO_Storable_Naming_Context");
00571
00572
00573
00574
00575 if (this->destroyed_)
00576 {
00577
00578 ACE_CString file_name (this->persistence_directory_);
00579 file_name += "/";
00580 file_name += this->name_;
00581
00582
00583 ACE_Auto_Ptr<TAO_Storable_Base>
00584 fl (
00585 this->factory_->create_stream(file_name.c_str(),
00586 ACE_TEXT("r"))
00587 );
00588 if (fl.get())
00589 {
00590 if (TAO_debug_level > 5)
00591 ACE_DEBUG ((LM_DEBUG, "(%P|%t) NameService: removing file %s\n",
00592 file_name.fast_rep()));
00593 fl->remove ();
00594 }
00595 }
00596 }
00597
00598 CosNaming::NamingContext_ptr
00599 TAO_Storable_Naming_Context::make_new_context (
00600 CORBA::ORB_ptr orb,
00601 PortableServer::POA_ptr poa,
00602 const char *poa_id,
00603 size_t context_size,
00604 TAO_Naming_Service_Persistence_Factory *factory,
00605 const ACE_TCHAR *persistence_directory,
00606 TAO_Storable_Naming_Context **new_context)
00607 {
00608 ACE_TRACE("make_new_context");
00609
00610 CosNaming::NamingContext_var result;
00611
00612
00613
00614 TAO_Storable_Naming_Context *context_impl = 0;
00615 ACE_NEW_THROW_EX (context_impl,
00616 TAO_Storable_Naming_Context (orb,
00617 poa,
00618 poa_id,
00619 factory,
00620 persistence_directory,
00621 context_size),
00622 CORBA::NO_MEMORY ());
00623
00624
00625
00626 ACE_Auto_Basic_Ptr<TAO_Storable_Naming_Context> temp (context_impl);
00627
00628 TAO_Naming_Context *context = 0;
00629 ACE_NEW_THROW_EX (context,
00630 TAO_Naming_Context (context_impl),
00631 CORBA::NO_MEMORY ());
00632
00633
00634 context_impl->interface (context);
00635
00636
00637
00638 temp.release ();
00639 PortableServer::ServantBase_var s = context;
00640
00641
00642 PortableServer::ObjectId_var id =
00643 PortableServer::string_to_ObjectId (poa_id);
00644
00645
00646
00647
00648
00649
00650 try
00651 {
00652 poa->activate_object_with_id (id.in (), context);
00653 }
00654 catch (const PortableServer::POA::ObjectAlreadyActive&)
00655 {
00656 throw CosNaming::NamingContext::AlreadyBound();
00657 }
00658
00659
00660 result = context->_this ();
00661
00662
00663 *new_context = context_impl;
00664
00665 return result._retn ();
00666 }
00667
00668 CosNaming::NamingContext_ptr
00669 TAO_Storable_Naming_Context::new_context (void)
00670 {
00671 ACE_TRACE("new_context");
00672 ACE_GUARD_THROW_EX (ACE_SYNCH_RECURSIVE_MUTEX,
00673 ace_mon,
00674 this->lock_,
00675 CORBA::INTERNAL ());
00676
00677 {
00678
00679 File_Open_Lock_and_Check flck(this, "r");
00680
00681
00682
00683 if (this->destroyed_)
00684 throw CORBA::OBJECT_NOT_EXIST ();
00685 }
00686
00687 TAO_NS_Persistence_Global global;
00688
00689
00690 if(redundant_)
00691 {
00692
00693 if (gfl_->open() != 0)
00694 {
00695 delete gfl_.release();
00696 throw CORBA::PERSIST_STORE();
00697 }
00698 if (gfl_ -> flock(0, 0, 0) != 0)
00699 throw CORBA::INTERNAL();
00700
00701 *gfl_.get() >> global;
00702 if (!gfl_.get ()->good () &&
00703 gfl_.get ()->rdstate () != TAO_Storable_Base::eofbit)
00704 {
00705 gfl_.get ()->clear ();
00706 throw CORBA::INTERNAL ();
00707 }
00708 gcounter_ = global.counter();
00709
00710 }
00711 char poa_id[BUFSIZ];
00712 ACE_OS::sprintf (poa_id,
00713 "%s_%d",
00714 root_name_,
00715 gcounter_++);
00716
00717 global.counter(gcounter_);
00718 *gfl_.get() << global;
00719 if(redundant_)
00720 {
00721
00722 if (gfl_ -> flock(0, 0, 0) != 0)
00723 throw CORBA::INTERNAL();
00724 gfl_->close();
00725 }
00726
00727
00728 TAO_Storable_Naming_Context *new_context = 0;
00729 CosNaming::NamingContext_var result =
00730 make_new_context (this->orb_.in (),
00731 this->poa_.in (),
00732 poa_id,
00733 this->storable_context_->total_size (),
00734 this->factory_,
00735 ACE_TEXT_CHAR_TO_TCHAR (this->persistence_directory_.c_str ()),
00736 &new_context);
00737
00738
00739 ACE_NEW_THROW_EX (new_context->storable_context_,
00740 TAO_Storable_Bindings_Map (hash_table_size_,orb_.in ()),
00741 CORBA::NO_MEMORY ());
00742 new_context->context_ = new_context->storable_context_;
00743
00744 File_Open_Lock_and_Check flck(new_context, "wc");
00745 new_context->Write(flck.peer());
00746
00747 return result._retn ();
00748 }
00749
00750 void
00751 TAO_Storable_Naming_Context::rebind (const CosNaming::Name& n,
00752 CORBA::Object_ptr obj)
00753 {
00754 ACE_TRACE("rebind");
00755
00756 CORBA::ULong name_len = n.length ();
00757
00758
00759 if (name_len == 0)
00760 throw CosNaming::NamingContext::InvalidName();
00761
00762
00763 ACE_GUARD_THROW_EX (ACE_SYNCH_RECURSIVE_MUTEX, ace_mon,
00764 this->lock_,
00765 CORBA::INTERNAL ());
00766
00767
00768 File_Open_Lock_and_Check flck(this, name_len > 1 ? "r" : "rw");
00769
00770
00771
00772 if (this->destroyed_)
00773 throw CORBA::OBJECT_NOT_EXIST ();
00774
00775
00776
00777
00778 if (name_len > 1)
00779 {
00780
00781 flck.release();
00782
00783 CosNaming::NamingContext_var context =
00784 get_context (n);
00785
00786 CosNaming::Name simple_name;
00787 simple_name.length (1);
00788 simple_name[0] = n[name_len - 1];
00789 context->rebind (simple_name, obj);
00790 }
00791
00792
00793 else
00794 {
00795 int result = this->context_->rebind (n[0].id,
00796 n[0].kind,
00797 obj,
00798 CosNaming::nobject);
00799
00800
00801 if (result == -1)
00802 throw CORBA::INTERNAL ();
00803
00804 else if (result == -2)
00805 throw CosNaming::NamingContext::NotFound(
00806 CosNaming::NamingContext::not_object,
00807 n);
00808
00809 this->Write(flck.peer());
00810 }
00811 }
00812
00813 void
00814 TAO_Storable_Naming_Context::bind_context (const CosNaming::Name &n,
00815 CosNaming::NamingContext_ptr nc)
00816 {
00817 ACE_TRACE("TAO_Storable_Naming_Context::bind_context");
00818
00819 CORBA::ULong name_len = n.length ();
00820
00821
00822 if (name_len == 0)
00823 throw CosNaming::NamingContext::InvalidName();
00824
00825
00826 if (CORBA::is_nil (nc))
00827 throw CORBA::BAD_PARAM ();
00828
00829
00830 ACE_GUARD_THROW_EX (ACE_SYNCH_RECURSIVE_MUTEX, ace_mon,
00831 this->lock_,
00832 CORBA::INTERNAL ());
00833
00834
00835 File_Open_Lock_and_Check flck(this, name_len > 1 ? "r" : "rw");
00836
00837
00838
00839 if (this->destroyed_)
00840 throw CORBA::OBJECT_NOT_EXIST ();
00841
00842
00843
00844
00845 if (name_len > 1)
00846 {
00847
00848 flck.release();
00849
00850 CosNaming::NamingContext_var context =
00851 get_context (n);
00852
00853 CosNaming::Name simple_name;
00854 simple_name.length (1);
00855 simple_name[0] = n[name_len - 1];
00856 context->bind_context (simple_name, nc);
00857 }
00858
00859 else
00860 {
00861
00862 int result = this->context_->bind (n[0].id,
00863 n[0].kind,
00864 nc,
00865 CosNaming::ncontext);
00866 if (result == 1)
00867 throw CosNaming::NamingContext::AlreadyBound();
00868
00869
00870 else if (result == -1)
00871 throw CORBA::INTERNAL ();
00872
00873 this->Write(flck.peer());
00874 }
00875 }
00876
00877 void
00878 TAO_Storable_Naming_Context::rebind_context (const CosNaming::Name &n,
00879 CosNaming::NamingContext_ptr nc)
00880 {
00881 ACE_TRACE("rebind_context");
00882
00883 CORBA::ULong name_len = n.length ();
00884
00885
00886 if (name_len == 0)
00887 throw CosNaming::NamingContext::InvalidName();
00888
00889
00890 ACE_GUARD_THROW_EX (ACE_SYNCH_RECURSIVE_MUTEX, ace_mon,
00891 this->lock_,
00892 CORBA::INTERNAL ());
00893
00894
00895 File_Open_Lock_and_Check flck(this, name_len > 1 ? "r" : "rw");
00896
00897
00898
00899 if (this->destroyed_)
00900 throw CORBA::OBJECT_NOT_EXIST ();
00901
00902
00903
00904
00905 if (name_len > 1)
00906 {
00907
00908 flck.release();
00909
00910 CosNaming::NamingContext_var context =
00911 get_context (n);
00912
00913 CosNaming::Name simple_name;
00914 simple_name.length (1);
00915 simple_name[0] = n[name_len - 1];
00916 context->rebind_context (simple_name, nc);
00917 }
00918
00919
00920 else
00921 {
00922 int result = this->context_->rebind (n[0].id,
00923 n[0].kind,
00924 nc,
00925 CosNaming::ncontext);
00926
00927 if (result == -1)
00928 throw CORBA::INTERNAL ();
00929 else if (result == -2)
00930 throw CosNaming::NamingContext::NotFound(
00931 CosNaming::NamingContext::not_context,
00932 n);
00933
00934 this->Write(flck.peer());
00935 }
00936 }
00937
00938 CORBA::Object_ptr
00939 TAO_Storable_Naming_Context::resolve (const CosNaming::Name& n)
00940 {
00941 ACE_TRACE("resolve");
00942
00943 CORBA::ULong name_len = n.length ();
00944
00945
00946 if (name_len == 0)
00947 throw CosNaming::NamingContext::InvalidName();
00948
00949
00950 ACE_GUARD_THROW_EX (ACE_SYNCH_RECURSIVE_MUTEX, ace_mon, this->lock_,
00951 CORBA::INTERNAL ());
00952
00953
00954 File_Open_Lock_and_Check flck(this, "r");
00955
00956
00957
00958 if (this->destroyed_)
00959 throw CORBA::OBJECT_NOT_EXIST ();
00960
00961
00962 flck.release();
00963
00964
00965 CosNaming::BindingType type;
00966
00967 CORBA::Object_ptr obj = CORBA::Object::_nil ();
00968
00969 if (this->context_->find (n[0].id,
00970 n[0].kind,
00971 obj,
00972 type) == -1)
00973 throw CosNaming::NamingContext::NotFound(
00974 CosNaming::NamingContext::missing_node,
00975 n);
00976
00977
00978 CORBA::Object_var result = obj;
00979
00980
00981
00982 if (name_len > 1)
00983 {
00984 CosNaming::NamingContext_var context =
00985 CosNaming::NamingContext::_nil ();
00986
00987 if (type == CosNaming::ncontext)
00988 {
00989
00990 context = CosNaming::NamingContext::_narrow (result.in ());
00991 }
00992 else
00993
00994 throw CosNaming::NamingContext::NotFound(
00995 CosNaming::NamingContext::not_context,
00996 n);
00997
00998
00999 if (CORBA::is_nil (context.in ()))
01000 throw CosNaming::NamingContext::NotFound(
01001 CosNaming::NamingContext::not_context,
01002 n);
01003 else
01004 {
01005
01006
01007
01008
01009
01010
01011
01012 CosNaming::Name rest_of_name
01013 (n.maximum () - 1,
01014 n.length () - 1,
01015 const_cast<CosNaming::NameComponent*> (n.get_buffer ()) + 1);
01016
01017
01018 return context->resolve (rest_of_name);
01019 }
01020 }
01021
01022
01023 return result._retn ();
01024 }
01025
01026 void
01027 TAO_Storable_Naming_Context::unbind (const CosNaming::Name& n)
01028 {
01029 ACE_TRACE("unbind");
01030
01031 CORBA::ULong name_len = n.length ();
01032
01033
01034 if (name_len == 0)
01035 throw CosNaming::NamingContext::InvalidName();
01036
01037
01038 ACE_GUARD_THROW_EX (ACE_SYNCH_RECURSIVE_MUTEX, ace_mon,
01039 this->lock_,
01040 CORBA::INTERNAL ());
01041
01042
01043 File_Open_Lock_and_Check flck(this, name_len > 1 ? "r" : "rw");
01044
01045
01046
01047 if (this->destroyed_)
01048 throw CORBA::OBJECT_NOT_EXIST ();
01049
01050
01051
01052
01053 if (name_len > 1)
01054 {
01055
01056 flck.release();
01057
01058 CosNaming::NamingContext_var context =
01059 get_context (n);
01060
01061 CosNaming::Name simple_name;
01062 simple_name.length (1);
01063 simple_name[0] = n[name_len - 1];
01064 context->unbind (simple_name);
01065 }
01066
01067
01068 else
01069 {
01070 if (this->context_->unbind (n[0].id,
01071 n[0].kind) == -1)
01072 throw CosNaming::NamingContext::NotFound(
01073 CosNaming::NamingContext::missing_node,
01074 n);
01075
01076 this->Write(flck.peer());
01077 }
01078 }
01079
01080 CosNaming::NamingContext_ptr
01081 TAO_Storable_Naming_Context::bind_new_context (const CosNaming::Name& n)
01082 {
01083 ACE_TRACE("bind_new_context");
01084
01085 CORBA::ULong name_len = n.length ();
01086
01087
01088 if (name_len == 0)
01089 throw CosNaming::NamingContext::InvalidName();
01090
01091 ACE_GUARD_THROW_EX (ACE_SYNCH_RECURSIVE_MUTEX,
01092 ace_mon,
01093 this->lock_,
01094 CORBA::INTERNAL ());
01095
01096
01097
01098 if (this->destroyed_)
01099 throw CORBA::OBJECT_NOT_EXIST ();
01100
01101
01102 File_Open_Lock_and_Check flck(this, name_len > 1 ? "r" : "rw");
01103
01104
01105
01106 if (this->destroyed_)
01107 throw CORBA::OBJECT_NOT_EXIST ();
01108
01109
01110
01111
01112 if (name_len > 1)
01113 {
01114
01115 flck.release();
01116
01117 CosNaming::NamingContext_var context =
01118 get_context (n);
01119
01120 CosNaming::Name simple_name;
01121 simple_name.length (1);
01122 simple_name[0] = n[name_len - 1];
01123 return context->bind_new_context (simple_name);
01124 }
01125
01126
01127
01128 flck.release();
01129
01130
01131 CosNaming::NamingContext_var result =
01132 CosNaming::NamingContext::_nil ();
01133
01134
01135 result = new_context ();
01136
01137
01138 try
01139 {
01140 bind_context (n,
01141 result.in ());
01142 }
01143 catch (const CORBA::Exception&)
01144 {
01145 {
01146 try
01147 {
01148 result->destroy ();
01149 }
01150 catch (const CORBA::Exception&)
01151 {
01152
01153 }
01154 }
01155
01156 throw;
01157 }
01158 return result._retn ();
01159 }
01160
01161 void
01162 TAO_Storable_Naming_Context::destroy (void)
01163 {
01164 ACE_TRACE("destroy");
01165 ACE_GUARD_THROW_EX (ACE_SYNCH_RECURSIVE_MUTEX,
01166 ace_mon,
01167 this->lock_,
01168 CORBA::INTERNAL ());
01169
01170
01171 File_Open_Lock_and_Check flck(this, "rw");
01172
01173
01174
01175 if (this->destroyed_)
01176 throw CORBA::OBJECT_NOT_EXIST ();
01177
01178 if (this->context_->current_size () != 0)
01179 throw CosNaming::NamingContext::NotEmpty();
01180
01181
01182 if (root ())
01183 return;
01184
01185 else
01186 {
01187 this->destroyed_ = 2;
01188
01189
01190
01191
01192
01193 PortableServer::POA_var poa =
01194 this->_default_POA ();
01195
01196 PortableServer::ObjectId_var id =
01197 PortableServer::string_to_ObjectId (poa_id_.fast_rep ());
01198
01199
01200 poa->deactivate_object (id.in ());
01201
01202 this->Write(flck.peer());
01203 }
01204 }
01205
01206
01207 void
01208 TAO_Storable_Naming_Context::bind (const CosNaming::Name& n,
01209 CORBA::Object_ptr obj)
01210 {
01211 ACE_TRACE("TAO_Storable_Naming_Context::bind");
01212
01213 CORBA::ULong name_len = n.length ();
01214
01215
01216 if (name_len == 0)
01217 throw CosNaming::NamingContext::InvalidName();
01218
01219
01220 ACE_GUARD_THROW_EX (ACE_SYNCH_RECURSIVE_MUTEX, ace_mon,
01221 this->lock_,
01222 CORBA::INTERNAL ());
01223
01224
01225 File_Open_Lock_and_Check flck(this, name_len > 1 ? "r" : "rw");
01226
01227
01228
01229 if (this->destroyed_)
01230 throw CORBA::OBJECT_NOT_EXIST ();
01231
01232
01233
01234
01235 if (name_len > 1)
01236 {
01237
01238 flck.release();
01239
01240 CosNaming::NamingContext_var context =
01241 get_context (n);
01242
01243 CosNaming::Name simple_name;
01244 simple_name.length (1);
01245 simple_name[0] = n[name_len - 1];
01246 context->bind (simple_name, obj);
01247 }
01248
01249 else
01250 {
01251
01252 int result = this->context_->bind (n[0].id,
01253 n[0].kind,
01254 obj,
01255 CosNaming::nobject);
01256 if (result == 1)
01257 throw CosNaming::NamingContext::AlreadyBound();
01258
01259
01260 else if (result == -1)
01261 throw CORBA::INTERNAL ();
01262
01263 this->Write(flck.peer());
01264 }
01265 }
01266
01267
01268 void
01269 TAO_Storable_Naming_Context::list (CORBA::ULong how_many,
01270 CosNaming::BindingList_out &bl,
01271 CosNaming::BindingIterator_out &bi)
01272 {
01273 ACE_TRACE("list");
01274
01275
01276 bi = CosNaming::BindingIterator::_nil ();
01277 ACE_NEW_THROW_EX (bl,
01278 CosNaming::BindingList (0),
01279 CORBA::NO_MEMORY ());
01280
01281
01282 ACE_GUARD_THROW_EX (ACE_SYNCH_RECURSIVE_MUTEX,
01283 ace_mon,
01284 this->lock_,
01285 CORBA::INTERNAL ());
01286
01287
01288 File_Open_Lock_and_Check flck(this, "r");
01289
01290
01291
01292 if (this->destroyed_)
01293 throw CORBA::OBJECT_NOT_EXIST ();
01294
01295
01296 flck.release();
01297
01298
01299 HASH_MAP::ITERATOR *hash_iter = 0;
01300 ACE_NEW_THROW_EX (hash_iter,
01301 HASH_MAP::ITERATOR (storable_context_->map ()),
01302 CORBA::NO_MEMORY ());
01303
01304
01305
01306 ACE_Auto_Basic_Ptr<HASH_MAP::ITERATOR> temp (hash_iter);
01307
01308
01309
01310 typedef ACE_Hash_Map_Manager<TAO_Storable_ExtId,
01311 TAO_Storable_IntId,
01312 ACE_Null_Mutex>::ITERATOR ITER_DEF;
01313 typedef ACE_Hash_Map_Manager<TAO_Storable_ExtId,
01314 TAO_Storable_IntId,
01315 ACE_Null_Mutex>::ENTRY ENTRY_DEF;
01316
01317
01318 typedef TAO_Bindings_Iterator<ITER_DEF, ENTRY_DEF> ITER_SERVANT;
01319
01320
01321 ITER_SERVANT *bind_iter = 0;
01322
01323
01324 CORBA::ULong n;
01325
01326
01327 if (this->context_->current_size () > how_many)
01328 n = how_many;
01329 else
01330 n = static_cast<CORBA::ULong> (this->context_->current_size ());
01331
01332
01333 bl->length (n);
01334
01335 ENTRY_DEF *hash_entry = 0;
01336
01337 for (CORBA::ULong i = 0; i < n; i++)
01338 {
01339 hash_iter->next (hash_entry);
01340 hash_iter->advance ();
01341
01342 if (ITER_SERVANT::populate_binding (hash_entry, bl[i]) == 0)
01343 throw CORBA::NO_MEMORY();
01344 }
01345
01346
01347
01348
01349
01350 if (this->context_->current_size () <= how_many)
01351 return;
01352 else if (redundant_)
01353 {
01354
01355 ACE_UNUSED_ARG (bind_iter);
01356 throw CORBA::NO_IMPLEMENT ();
01357 }
01358 else
01359 {
01360
01361 ACE_NEW_THROW_EX (bind_iter,
01362 ITER_SERVANT (this, hash_iter,
01363 this->poa_.in (), this->lock_),
01364 CORBA::NO_MEMORY ());
01365
01366
01367
01368 temp.release ();
01369 PortableServer::ServantBase_var iter = bind_iter;
01370
01371
01372
01373 interface_->_add_ref ();
01374
01375
01376
01377 char poa_id[BUFSIZ];
01378 ACE_OS::snprintf (poa_id,
01379 BUFSIZ,
01380 "%s_%d",
01381 this->poa_id_.c_str (),
01382 this->gcounter_++);
01383 PortableServer::ObjectId_var id =
01384 PortableServer::string_to_ObjectId (poa_id);
01385
01386 this->poa_->activate_object_with_id (id.in (),
01387 bind_iter);
01388 bi = bind_iter->_this ();
01389 }
01390 }
01391
01392 TAO_END_VERSIONED_NAMESPACE_DECL
01393
01394 #include "orbsvcs/Naming/Naming_Service_Container.h"
01395
01396 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
01397
01398 CosNaming::NamingContext_ptr TAO_Storable_Naming_Context::recreate_all(
01399 CORBA::ORB_ptr orb,
01400 PortableServer::POA_ptr poa,
01401 const char *poa_id,
01402 size_t context_size,
01403 int reentering,
01404 TAO_Naming_Service_Persistence_Factory *factory,
01405 const ACE_TCHAR *persistence_directory,
01406 int use_redundancy)
01407 {
01408 ACE_TRACE("recreate_all");
01409
01410 ACE_UNUSED_ARG (reentering);
01411
01412
01413 redundant_ = use_redundancy;
01414
01415
01416 root_name_ = poa_id;
01417
01418
01419 TAO_Storable_Naming_Context *new_context = 0;
01420 CosNaming::NamingContext_var result =
01421 make_new_context (orb,
01422 poa,
01423 poa_id,
01424 context_size,
01425 factory,
01426 persistence_directory,
01427 &new_context);
01428
01429
01430 ACE_TString file_name(persistence_directory);
01431 file_name += ACE_TEXT("/");
01432 file_name += ACE_TEXT_CHAR_TO_TCHAR(poa_id);
01433 ACE_Auto_Ptr<TAO_Storable_Base> fl (factory->create_stream(ACE_TEXT_ALWAYS_CHAR(file_name.c_str()), ACE_TEXT("r")));
01434 if (fl->exists())
01435 {
01436
01437 File_Open_Lock_and_Check flck(new_context, "r");
01438 }
01439 else
01440 {
01441
01442 ACE_NEW_THROW_EX (new_context->storable_context_,
01443 TAO_Storable_Bindings_Map (context_size,orb),
01444 CORBA::NO_MEMORY ());
01445 new_context->context_ = new_context->storable_context_;
01446 File_Open_Lock_and_Check flck(new_context, "wc");
01447 new_context->Write(flck.peer());
01448 }
01449
01450
01451 file_name += ACE_TEXT("_global");
01452
01453
01454 gfl_.reset(factory->create_stream(ACE_TEXT_ALWAYS_CHAR(file_name.c_str()), ACE_TEXT("crw")));
01455 if (gfl_->open() != 0)
01456 {
01457 delete gfl_.release();
01458 throw CORBA::PERSIST_STORE();
01459 }
01460
01461
01462 TAO_NS_Persistence_Global global;
01463 *gfl_.get() >> global;
01464 if (!gfl_.get ()->good () &&
01465 gfl_.get ()->rdstate () != TAO_Storable_Base::eofbit)
01466 {
01467 gfl_.get ()->clear ();
01468 throw CORBA::INTERNAL ();
01469 }
01470 gcounter_ = global.counter();
01471 if(redundant_) gfl_->close();
01472
01473 return result._retn ();
01474 }
01475
01476 TAO_END_VERSIONED_NAMESPACE_DECL