Storable_Naming_Context.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 //=============================================================================
00003 /**
00004  *  @file   Storable_Naming_Context.h
00005  *
00006  *  $Id: Storable_Naming_Context.h 77276 2007-02-21 08:26:36Z johnnyw $
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, const char * kind);
00176 
00177   /**
00178    * Find the binding containing <id> and <kind> in the table, and
00179    * pass binding's type and object back to the caller by reference.
00180    * Return 0 on success and -1 on failure.  Note: a 'duplicated' object
00181    * reference is assigned to <obj>, so the caller is responsible for
00182    * its deallocation.
00183    */
00184   virtual int find (const char * id,
00185                     const char * kind,
00186                     CORBA::Object_ptr & obj,
00187                     CosNaming::BindingType &type);
00188 
00189 private:
00190 
00191   /// Helper: factors common code from <bind> and <rebind>.
00192   int shared_bind (const char *id,
00193                    const char *kind,
00194                    CORBA::Object_ptr obj,
00195                    CosNaming::BindingType type,
00196                    int rebind);
00197 
00198   /// Hash map used for storage.
00199   HASH_MAP map_;
00200 
00201   CORBA::ORB_var orb_;
00202 
00203 };
00204 
00205 /**
00206  * @class TAO_Storable_Naming_Context
00207  *
00208  * @brief This class plays a role of a 'ConcreteImplementor' in the
00209  * Bridge pattern architecture of the CosNaming::NamingContext
00210  * implementation.
00211  *
00212  * This class provides a  implementation of the
00213  * NamingContext functionality, i.e., the state can be preserved
00214  * across process boundaries.  Derives from
00215  * TAO_Hash_Naming_Context and uses TAO_Storable_Bindings_Map to
00216  * store name to object bindings.
00217  */
00218 class TAO_Naming_Serv_Export TAO_Storable_Naming_Context : public TAO_Hash_Naming_Context
00219 {
00220 
00221 public:
00222 
00223   /// Underlying data structure - typedef for ease of use.
00224   typedef TAO_Storable_Bindings_Map::HASH_MAP HASH_MAP;
00225 
00226   // = Initialization and termination methods.
00227 
00228   /// Constructor.
00229   TAO_Storable_Naming_Context (CORBA::ORB_ptr orb,
00230                                PortableServer::POA_ptr poa,
00231                                const char *poa_id,
00232                                TAO_Naming_Service_Persistence_Factory *factory,
00233                                const ACE_TCHAR *persistence_directory,
00234                                size_t hash_table_size = ACE_DEFAULT_MAP_SIZE);
00235 
00236   /// Destructor.
00237   virtual ~TAO_Storable_Naming_Context (void);
00238 
00239   // = Utility methods.
00240   /**
00241    * This utility method factors out the code needed to create a new
00242    * Storable Naming Context servant and activate it under the
00243    * specified POA with the specified id.  This function is static so
00244    * that the code can be used, both from inside the class (e.g.,
00245    * <new_context>), and from outside (e.g., Naming_Utils.cpp).
00246    */
00247   static CosNaming::NamingContext_ptr make_new_context (
00248                                CORBA::ORB_ptr orb,
00249                                PortableServer::POA_ptr poa,
00250                                const char *poa_id,
00251                                size_t context_size,
00252                                TAO_Naming_Service_Persistence_Factory *factory,
00253                                const ACE_TCHAR *persistence_directory,
00254                                TAO_Storable_Naming_Context **new_context);
00255 
00256   // = Methods not implemented in TAO_Hash_Naming_Context.
00257 
00258   static CosNaming::NamingContext_ptr recreate_all(
00259                               CORBA::ORB_ptr orb,
00260                               PortableServer::POA_ptr poa,
00261                               const char *poa_id,
00262                               size_t context_size,
00263                               int reentering,
00264                               TAO_Naming_Service_Persistence_Factory *factory,
00265                               const ACE_TCHAR *persistence_directory,
00266                               int use_redundancy);
00267 
00268 
00269   /**
00270    * This operation returns a new naming context implemented by the
00271    * same naming server in which the operation was invoked.  The
00272    * context is not bound.
00273    */
00274   virtual CosNaming::NamingContext_ptr new_context (void);
00275 
00276   /**
00277    * Returns at most the requested number of bindings <how_many> in
00278    * <bl>.  If the naming context contains additional bindings, they
00279    * are returned with a BindingIterator.  In the naming context does
00280    * not contain any additional bindings <bi> returned as null.
00281    */
00282   virtual void list (CORBA::ULong how_many,
00283                      CosNaming::BindingList_out &bl,
00284                      CosNaming::BindingIterator_out &bi);
00285 
00286 
00287   virtual void rebind (const CosNaming::Name& n,
00288                        CORBA::Object_ptr obj);
00289 
00290   /**
00291    * Create a binding for name <n> and object <obj> in the naming
00292    * context.  Compound names are treated as follows: ctx->bind (<c1;
00293    * c2; c3; cn>, obj) = (ctx->resolve (<c1; c2; cn-1>))->bind (<cn>,
00294    * obj) if the there already exists a binding for the specified
00295    * name, <AlreadyBound> exception is thrown.  Naming contexts should
00296    * be bound using <bind_context> and <rebind_context> in order to
00297    * participate in name resolution later.
00298    */
00299   virtual void bind (const CosNaming::Name &n,
00300                      CORBA::Object_ptr obj);
00301 
00302 
00303   /**
00304    * This is the version of <bind> specifically for binding naming
00305    * contexts, so that they will participate in name resolution when
00306    * compound names are passed to be resolved.
00307    */
00308   virtual void bind_context (const CosNaming::Name &n,
00309                              CosNaming::NamingContext_ptr nc);
00310 
00311   /**
00312    * This is a version of <rebind> specifically for naming contexts,
00313    * so that they can participate in name resolution when compound
00314    * names are passed.
00315    */
00316    virtual void rebind_context (const CosNaming::Name &n,
00317                                 CosNaming::NamingContext_ptr nc);
00318 
00319   /**
00320    * Return object reference that is bound to the name.  Compound name
00321    * resolve is defined as follows: ctx->resolve (<c1; c2; cn>) =
00322    * ctx->resolve (<c1; c2 cn-1>)->resolve (<cn>) The naming service
00323    * does not return the type of the object.  Clients are responsible
00324    * for "narrowing" the object to the appropriate type.
00325    */
00326   virtual CORBA::Object_ptr resolve (const CosNaming::Name &n);
00327 
00328   /**
00329    * Remove the name binding from the context.  When compound names
00330    * are used, unbind is defined as follows: ctx->unbind (<c1; c2;
00331    * cn>) = (ctx->resolve (<c1; c2; cn-1>))->unbind (<cn>)
00332    */
00333   virtual void unbind (const CosNaming::Name &n);
00334 
00335   /**
00336    * This operation creates a new context and binds it to the name
00337    * supplied as an argument.  The newly-created context is
00338    * implemented by the same server as the context in which it was
00339    * bound (the name argument excluding the last component).
00340    */
00341   virtual CosNaming::NamingContext_ptr bind_new_context (
00342                                 const CosNaming::Name &n);
00343 
00344   /**
00345    * Delete the naming context.  The user should take care to <unbind> any
00346    * bindings in which the given context is bound to some names, to
00347    * avoid dangling references when invoking <destroy> operation.
00348    * NOTE: <destory> is a no-op on the root context.
00349    * NOTE: after <destroy> is invoked on a Naming Context, all
00350    * BindingIterators associated with that Naming Context are also destroyed.
00351    */
00352   virtual void destroy (void);
00353 
00354 protected:
00355 
00356   /// Global counter used for generation of POA ids for children Naming
00357   /// Contexts.
00358   static ACE_UINT32 gcounter_;
00359 
00360   /// Counter used for generation of transients
00361   ACE_UINT32 counter_;
00362 
00363   /**
00364    * A pointer to the underlying data structure used to store name
00365    * bindings. While our superclass (TAO_Hash_Naming_Context) also
00366    * maintains a pointer to the data structure, keeping this pointer
00367    * around saves us from the need to downcast when invoking
00368    * non-virtual methods.
00369    */
00370   TAO_Storable_Bindings_Map *storable_context_;
00371 
00372   CORBA::ORB_var orb_;
00373 
00374   ACE_CString name_;
00375 
00376   PortableServer::POA_var poa_;
00377 
00378   TAO_Naming_Service_Persistence_Factory *factory_;
00379 
00380   /// The directory in which to store the files
00381   ACE_CString persistence_directory_;
00382 
00383   /// Save the hash table initial size
00384   size_t hash_table_size_;
00385 
00386   /// Disk time that match current memory state
00387   time_t last_changed_;
00388 
00389   /// Flag to tell use whether we are redundant or not
00390   static int redundant_;
00391 
00392   static const char * root_name_;
00393 
00394   /// The pointer to the global file used to allocate new contexts
00395   static ACE_Auto_Ptr<TAO_Storable_Base> gfl_;
00396 
00397 /**
00398  * @class File_Open_Lock_and_Check
00399  *
00400  * @brief Helper class for the TAO_Storable_Naming_Context.
00401  *
00402  * Guard class for the TAO_Storable_Naming_Context.  It opens
00403  * a file for read/write and sets a lock on it.  It then checks
00404  * if the file has changed and re-reads it if it has.
00405  *
00406  * The destructor insures that the lock gets released.
00407  *
00408  * <pre>
00409  * How to use this class:
00410  *   File_Open_Lock_and_Check flck(this, name_len > 1 ? "r" : "rw");
00411  * </pre>
00412  */
00413 class File_Open_Lock_and_Check
00414 {
00415 public:
00416 
00417   /// Constructor - we always need the object which we guard.
00418   File_Open_Lock_and_Check(TAO_Storable_Naming_Context * context,
00419                                 const char * mode);
00420 
00421   /// Destructor
00422   ~File_Open_Lock_and_Check(void);
00423 
00424   /// Releases the lock, closes the file, and deletes the I/O stream.
00425   void release(void);
00426 
00427   /// Returns the stream to read/write on
00428   TAO_Storable_Base & peer(void);
00429 
00430 private:
00431   /// Default constructor
00432   File_Open_Lock_and_Check(void);
00433 
00434   /// A flag to keep us from trying to close things more than once.
00435   int closed_;
00436 
00437   /// We need to save the pointer to our parent for cleaning up
00438   TAO_Storable_Naming_Context * context_;
00439 
00440   /// The pointer to the actual file I/O (bridge pattern)
00441   TAO_Storable_Base *fl_;
00442 
00443   /// The flags that we were opened with
00444   int rwflags_;
00445 
00446   /// Symbolic values for the flags in the above
00447   enum{ mode_write = 1, mode_read = 2, mode_create = 4 };
00448 }; // end of embedded class File_Open_Lock_and_Check
00449 
00450   friend class File_Open_Lock_and_Check;
00451 
00452   int load_map(File_Open_Lock_and_Check *flck);
00453 
00454   void Write(TAO_Storable_Base& wrtr);
00455 
00456 };
00457 
00458 TAO_END_VERSIONED_NAMESPACE_DECL
00459 
00460 #include /**/ "ace/post.h"
00461 #endif /* TAO_STORABLE_NAMING_CONTEXT_H */

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