00001 /* -*- C++ -*- */ 00002 //============================================================================= 00003 /** 00004 * @file Storable_Naming_Context.h 00005 * 00006 * Storable_Naming_Context.h,v 1.10 2006/03/14 06:14:34 jtc Exp 00007 * 00008 * @author Bruce Trask <trask_b@ociweb.com> 00009 */ 00010 //============================================================================= 00011 00012 00013 #ifndef TAO_STORABLE_NAMING_CONTEXT_H 00014 #define TAO_STORABLE_NAMING_CONTEXT_H 00015 #include /**/ "ace/pre.h" 00016 00017 #include "orbsvcs/Naming/Hash_Naming_Context.h" 00018 #include "ace/Hash_Map_Manager.h" 00019 #include "ace/Auto_Ptr.h" 00020 00021 #include "orbsvcs/Naming/Storable.h" 00022 00023 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00024 # pragma once 00025 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00026 00027 TAO_BEGIN_VERSIONED_NAMESPACE_DECL 00028 00029 class TAO_Naming_Serv_Export TAO_Storable_IntId 00030 { 00031 public: 00032 // = Initialization and termination methods. 00033 /// Constructor. 00034 TAO_Storable_IntId (void); 00035 00036 /// Constructor. 00037 TAO_Storable_IntId (/* in */ const char * ior, 00038 CosNaming::BindingType type /* = CosNaming::nobject */); 00039 00040 /// Copy constructor. 00041 TAO_Storable_IntId (const TAO_Storable_IntId & rhs); 00042 00043 /// Destructor. 00044 ~TAO_Storable_IntId (void); 00045 00046 /// Assignment operator. 00047 void operator= (const TAO_Storable_IntId & rhs); 00048 00049 // = Data members. 00050 00051 /// Stringified IOR to be stored in a Persistent Naming Context. 00052 CORBA::String_var ref_; 00053 00054 /// Binding type for <ref_>. 00055 CosNaming::BindingType type_; 00056 }; 00057 00058 class TAO_Naming_Serv_Export TAO_Storable_ExtId 00059 { 00060 public: 00061 // = Initialization and termination methods. 00062 00063 /// Constructor. 00064 TAO_Storable_ExtId (void); 00065 00066 /// Constructor. 00067 TAO_Storable_ExtId (/* in */ const char *id, 00068 /* in */ const char *kind); 00069 00070 /// Copy constructor. 00071 TAO_Storable_ExtId (const TAO_Storable_ExtId & rhs); 00072 00073 /// Destructor. 00074 ~TAO_Storable_ExtId (void); 00075 00076 // = Assignment and comparison methods. 00077 00078 /// Assignment operator (does copy memory). 00079 void operator= (const TAO_Storable_ExtId & rhs); 00080 00081 /// Equality comparison operator (must match both id_ and kind_). 00082 bool operator== (const TAO_Storable_ExtId &rhs) const; 00083 00084 /// Inequality comparison operator. 00085 bool operator!= (const TAO_Storable_ExtId &rhs) const; 00086 00087 /// <hash> function is required in order for this class to be usable by 00088 /// ACE_Hash_Map_Manager. 00089 u_long hash (void) const; 00090 00091 // = Data members. 00092 00093 /// <id> portion of the name to be associated with some object 00094 /// reference in a Storable Naming Context. 00095 CORBA::String_var id_; 00096 00097 /// <kind> portion of the name to be associated with some object 00098 /// reference in a Storable Naming Context. 00099 CORBA::String_var kind_; 00100 00101 // Accessors. 00102 // follow the mapping rules! 00103 00104 const char * id (void); 00105 const char * kind (void); 00106 00107 }; 00108 00109 00110 /** 00111 * @class TAO_Storable_Bindings_Map 00112 * 00113 * @brief Provides hash-table-based transient storage for name to object 00114 * bindings in a Naming Context. 00115 * 00116 * A thin wrapper on top of ACE_Hash_Map_Manager. Supports 00117 * TAO_Bindings_Map interface. Used by TAO_Transient_Naming_Context. 00118 */ 00119 class TAO_Naming_Serv_Export TAO_Storable_Bindings_Map : public TAO_Bindings_Map 00120 { 00121 public: 00122 00123 /// Underlying data structure - typedef for ease of use. 00124 typedef ACE_Hash_Map_Manager<TAO_Storable_ExtId, 00125 TAO_Storable_IntId, 00126 ACE_Null_Mutex> HASH_MAP; 00127 00128 // = Initialization and termination methods. 00129 00130 /// Constructor. 00131 TAO_Storable_Bindings_Map (size_t hash_table_size, CORBA::ORB_ptr orb); 00132 00133 /// Destructor. 00134 virtual ~TAO_Storable_Bindings_Map (void); 00135 00136 // = Accessors. 00137 00138 /// Get a reference to the underlying hash map. 00139 HASH_MAP &map (void); 00140 00141 /// Return the size of the underlying hash table. 00142 size_t total_size (void); 00143 00144 /// Return current number of entries (name bindings) in the 00145 /// underlying hash map. 00146 virtual size_t current_size (void); 00147 00148 // = Name bindings manipulation methods. 00149 00150 /** 00151 * Add a binding with the specified parameters to the table. 00152 * Return 0 on success and -1 on failure, 1 if there already is a 00153 * binding with <id> and <kind>. 00154 */ 00155 virtual int bind (const char *id, 00156 const char *kind, 00157 CORBA::Object_ptr obj, 00158 CosNaming::BindingType type); 00159 00160 /** 00161 * Overwrite a binding containing <id> and <kind> (or create a new 00162 * one if one doesn't exist) with the specified parameters. Return 00163 * 0 or 1 on success. Return -1 or -2 on failure. (-2 is returned 00164 * if the new and old bindings differ in type). 00165 */ 00166 virtual int rebind (const char *id, 00167 const char *kind, 00168 CORBA::Object_ptr obj, 00169 CosNaming::BindingType type); 00170 00171 /** 00172 * Remove a binding containing <id> and <kind> from the table. 00173 * Return 0 on success and -1 on failure. 00174 */ 00175 virtual int unbind (const char * id, 00176 const char * kind); 00177 00178 /** 00179 * Find the binding containing <id> and <kind> in the table, and 00180 * pass binding's type and object back to the caller by reference. 00181 * Return 0 on success and -1 on failure. Note: a 'duplicated' object 00182 * reference is assigned to <obj>, so the caller is responsible for 00183 * its deallocation. 00184 */ 00185 virtual int find (const char * id, 00186 const char * kind, 00187 CORBA::Object_ptr & obj, 00188 CosNaming::BindingType &type); 00189 00190 private: 00191 00192 /// Helper: factors common code from <bind> and <rebind>. 00193 int shared_bind (const char *id, 00194 const char *kind, 00195 CORBA::Object_ptr obj, 00196 CosNaming::BindingType type, 00197 int rebind); 00198 00199 /// Hash map used for storage. 00200 HASH_MAP map_; 00201 00202 CORBA::ORB_var orb_; 00203 00204 }; 00205 00206 /** 00207 * @class TAO_Storable_Naming_Context 00208 * 00209 * @brief This class plays a role of a 'ConcreteImplementor' in the 00210 * Bridge pattern architecture of the CosNaming::NamingContext 00211 * implementation. 00212 * 00213 * This class provides a implementation of the 00214 * NamingContext functionality, i.e., the state can be preserved 00215 * across process boundaries. Derives from 00216 * TAO_Hash_Naming_Context and uses TAO_Storable_Bindings_Map to 00217 * store name to object bindings. 00218 */ 00219 class TAO_Naming_Serv_Export TAO_Storable_Naming_Context : public TAO_Hash_Naming_Context 00220 { 00221 00222 public: 00223 00224 /// Underlying data structure - typedef for ease of use. 00225 typedef TAO_Storable_Bindings_Map::HASH_MAP HASH_MAP; 00226 00227 // = Initialization and termination methods. 00228 00229 /// Constructor. 00230 TAO_Storable_Naming_Context (CORBA::ORB_ptr orb, 00231 PortableServer::POA_ptr poa, 00232 const char *poa_id, 00233 TAO_Naming_Service_Persistence_Factory *factory, 00234 const ACE_TCHAR *persistence_directory, 00235 size_t hash_table_size = ACE_DEFAULT_MAP_SIZE); 00236 00237 /// Destructor. 00238 virtual ~TAO_Storable_Naming_Context (void); 00239 00240 // = Utility methods. 00241 /** 00242 * This utility method factors out the code needed to create a new 00243 * Storable Naming Context servant and activate it under the 00244 * specified POA with the specified id. This function is static so 00245 * that the code can be used, both from inside the class (e.g., 00246 * <new_context>), and from outside (e.g., Naming_Utils.cpp). 00247 */ 00248 static CosNaming::NamingContext_ptr make_new_context ( 00249 CORBA::ORB_ptr orb, 00250 PortableServer::POA_ptr poa, 00251 const char *poa_id, 00252 size_t context_size, 00253 TAO_Naming_Service_Persistence_Factory *factory, 00254 const ACE_TCHAR *persistence_directory, 00255 TAO_Storable_Naming_Context **new_context 00256 ACE_ENV_ARG_DECL); 00257 00258 // = Methods not implemented in TAO_Hash_Naming_Context. 00259 00260 static CosNaming::NamingContext_ptr recreate_all( 00261 CORBA::ORB_ptr orb, 00262 PortableServer::POA_ptr poa, 00263 const char *poa_id, 00264 size_t context_size, 00265 int reentering, 00266 TAO_Naming_Service_Persistence_Factory *factory, 00267 const ACE_TCHAR *persistence_directory, 00268 int use_redundancy 00269 ACE_ENV_ARG_DECL); 00270 00271 00272 /** 00273 * This operation returns a new naming context implemented by the 00274 * same naming server in which the operation was invoked. The 00275 * context is not bound. 00276 */ 00277 virtual CosNaming::NamingContext_ptr new_context (ACE_ENV_SINGLE_ARG_DECL); 00278 00279 /** 00280 * Returns at most the requested number of bindings <how_many> in 00281 * <bl>. If the naming context contains additional bindings, they 00282 * are returned with a BindingIterator. In the naming context does 00283 * not contain any additional bindings <bi> returned as null. 00284 */ 00285 virtual void list (CORBA::ULong how_many, 00286 CosNaming::BindingList_out &bl, 00287 CosNaming::BindingIterator_out &bi 00288 ACE_ENV_ARG_DECL); 00289 00290 00291 virtual void rebind (const CosNaming::Name& n, 00292 CORBA::Object_ptr obj 00293 ACE_ENV_ARG_DECL); 00294 00295 /** 00296 * Create a binding for name <n> and object <obj> in the naming 00297 * context. Compound names are treated as follows: ctx->bind (<c1; 00298 * c2; c3; cn>, obj) = (ctx->resolve (<c1; c2; cn-1>))->bind (<cn>, 00299 * obj) if the there already exists a binding for the specified 00300 * name, <AlreadyBound> exception is thrown. Naming contexts should 00301 * be bound using <bind_context> and <rebind_context> in order to 00302 * participate in name resolution later. 00303 */ 00304 virtual void bind (const CosNaming::Name &n, 00305 CORBA::Object_ptr obj 00306 ACE_ENV_ARG_DECL); 00307 00308 00309 /** 00310 * This is the version of <bind> specifically for binding naming 00311 * contexts, so that they will participate in name resolution when 00312 * compound names are passed to be resolved. 00313 */ 00314 virtual void bind_context (const CosNaming::Name &n, 00315 CosNaming::NamingContext_ptr nc 00316 ACE_ENV_ARG_DECL); 00317 00318 /** 00319 * This is a version of <rebind> specifically for naming contexts, 00320 * so that they can participate in name resolution when compound 00321 * names are passed. 00322 */ 00323 virtual void rebind_context (const CosNaming::Name &n, 00324 CosNaming::NamingContext_ptr nc 00325 ACE_ENV_ARG_DECL); 00326 00327 /** 00328 * Return object reference that is bound to the name. Compound name 00329 * resolve is defined as follows: ctx->resolve (<c1; c2; cn>) = 00330 * ctx->resolve (<c1; c2 cn-1>)->resolve (<cn>) The naming service 00331 * does not return the type of the object. Clients are responsible 00332 * for "narrowing" the object to the appropriate type. 00333 */ 00334 virtual CORBA::Object_ptr resolve (const CosNaming::Name &n 00335 ACE_ENV_ARG_DECL); 00336 00337 /** 00338 * Remove the name binding from the context. When compound names 00339 * are used, unbind is defined as follows: ctx->unbind (<c1; c2; 00340 * cn>) = (ctx->resolve (<c1; c2; cn-1>))->unbind (<cn>) 00341 */ 00342 virtual void unbind (const CosNaming::Name &n 00343 ACE_ENV_ARG_DECL); 00344 00345 /** 00346 * This operation creates a new context and binds it to the name 00347 * supplied as an argument. The newly-created context is 00348 * implemented by the same server as the context in which it was 00349 * bound (the name argument excluding the last component). 00350 */ 00351 virtual CosNaming::NamingContext_ptr bind_new_context ( 00352 const CosNaming::Name &n 00353 ACE_ENV_ARG_DECL); 00354 00355 /** 00356 * Delete the naming context. The user should take care to <unbind> any 00357 * bindings in which the given context is bound to some names, to 00358 * avoid dangling references when invoking <destroy> operation. 00359 * NOTE: <destory> is a no-op on the root context. 00360 * NOTE: after <destroy> is invoked on a Naming Context, all 00361 * BindingIterators associated with that Naming Context are also destroyed. 00362 */ 00363 virtual void destroy (ACE_ENV_SINGLE_ARG_DECL); 00364 00365 protected: 00366 00367 /// Global counter used for generation of POA ids for children Naming 00368 /// Contexts. 00369 static ACE_UINT32 gcounter_; 00370 00371 /// Counter used for generation of transients 00372 ACE_UINT32 counter_; 00373 00374 /** 00375 * A pointer to the underlying data structure used to store name 00376 * bindings. While our superclass (TAO_Hash_Naming_Context) also 00377 * maintains a pointer to the data structure, keeping this pointer 00378 * around saves us from the need to downcast when invoking 00379 * non-virtual methods. 00380 */ 00381 TAO_Storable_Bindings_Map *storable_context_; 00382 00383 CORBA::ORB_var orb_; 00384 00385 ACE_CString name_; 00386 00387 PortableServer::POA_var poa_; 00388 00389 TAO_Naming_Service_Persistence_Factory *factory_; 00390 00391 /// The directory in which to store the files 00392 ACE_CString persistence_directory_; 00393 00394 /// Save the hash table initial size 00395 size_t hash_table_size_; 00396 00397 /// Disk time that match current memory state 00398 time_t last_changed_; 00399 00400 /// Flag to tell use whether we are redundant or not 00401 static int redundant_; 00402 00403 static const char * root_name_; 00404 00405 /// The pointer to the global file used to allocate new contexts 00406 static ACE_Auto_Ptr<TAO_Storable_Base> gfl_; 00407 00408 /** 00409 * @class File_Open_Lock_and_Check 00410 * 00411 * @brief Helper class for the TAO_Storable_Naming_Context. 00412 * 00413 * Guard class for the TAO_Storable_Naming_Context. It opens 00414 * a file for read/write and sets a lock on it. It then checks 00415 * if the file has changed and re-reads it if it has. 00416 * 00417 * The destructor insures that the lock gets released. 00418 * 00419 * <pre> 00420 * How to use this class: 00421 * File_Open_Lock_and_Check flck(this, name_len > 1 ? "r" : "rw"); 00422 ACE_CHECK; 00423 * </pre> 00424 */ 00425 class File_Open_Lock_and_Check 00426 { 00427 public: 00428 00429 /// Constructor - we always need the object which we guard. 00430 File_Open_Lock_and_Check(TAO_Storable_Naming_Context * context, 00431 const char * mode 00432 ACE_ENV_ARG_DECL); 00433 00434 /// Destructor 00435 ~File_Open_Lock_and_Check(void); 00436 00437 /// Releases the lock, closes the file, and deletes the I/O stream. 00438 void release(void); 00439 00440 /// Returns the stream to read/write on 00441 TAO_Storable_Base & peer(void); 00442 00443 private: 00444 /// Default constructor 00445 File_Open_Lock_and_Check(void); 00446 00447 /// A flag to keep us from trying to close things more than once. 00448 int closed_; 00449 00450 /// We need to save the pointer to our parent for cleaning up 00451 TAO_Storable_Naming_Context * context_; 00452 00453 /// The pointer to the actual file I/O (bridge pattern) 00454 TAO_Storable_Base *fl_; 00455 00456 /// The flags that we were opened with 00457 int rwflags_; 00458 00459 /// Symbolic values for the flags in the above 00460 enum{ mode_write = 1, mode_read = 2, mode_create = 4 }; 00461 }; // end of embedded class File_Open_Lock_and_Check 00462 00463 friend class File_Open_Lock_and_Check; 00464 00465 int load_map(File_Open_Lock_and_Check *flck ACE_ENV_ARG_DECL); 00466 00467 void Write(TAO_Storable_Base& wrtr); 00468 00469 }; 00470 00471 TAO_END_VERSIONED_NAMESPACE_DECL 00472 00473 #include /**/ "ace/post.h" 00474 #endif /* TAO_STORABLE_NAMING_CONTEXT_H */