00001 // -*- C++ -*- 00002 00003 //==================================================================== 00004 /** 00005 * @file Service_Gestalt.h 00006 * 00007 * $Id: Service_Gestalt.h 84619 2009-02-26 12:26:16Z johnnyw $ 00008 * 00009 * @author Iliyan Jeliazkov <iliyan@ociweb.com> 00010 */ 00011 //==================================================================== 00012 00013 #ifndef ACE_SERVICE_GESTALT_H 00014 #define ACE_SERVICE_GESTALT_H 00015 00016 #include /**/ "ace/pre.h" 00017 00018 #include /**/ "ace/config-all.h" 00019 #include "ace/Default_Constants.h" 00020 00021 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00022 # pragma once 00023 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00024 00025 #include "ace/Auto_Ptr.h" 00026 #include "ace/SString.h" 00027 #include "ace/Unbounded_Queue.h" 00028 #include "ace/Unbounded_Set.h" 00029 #include "ace/Service_Repository.h" 00030 #include "ace/Singleton.h" 00031 #include "ace/OS_NS_signal.h" 00032 #include "ace/Synch_Traits.h" 00033 #include "ace/Atomic_Op.h" 00034 #include "ace/Guard_T.h" 00035 00036 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00037 00038 #if (ACE_USES_CLASSIC_SVC_CONF == 1) 00039 class ACE_Service_Type_Factory; 00040 class ACE_Location_Node; 00041 #else 00042 class ACE_XML_Svc_Conf; 00043 class ACE_DLL; 00044 #endif /* ACE_USES_CLASSIC_SVC_CONF == 1 */ 00045 00046 class ACE_Static_Svc_Descriptor; 00047 class ACE_Svc_Conf_Param; 00048 00049 /** 00050 * @class ACE_Service_Gestalt 00051 * 00052 * @brief Supplies common server operations for dynamic and static 00053 * configuration of services. 00054 * 00055 * The Gestalt embodies the concept of configuration context. On one 00056 * hand, it is a flat namespace, where names correspond to a Service 00057 * Object instance. A Gestalt owns the Service Repository instance, 00058 * which in turn owns the Service Object instances. 00059 * 00060 * Another aspect of a Gestalt is its responsibility for 00061 * record-keeping and accounting for the meta-data, necessary for 00062 * locating, removing or instantiating a service. 00063 * 00064 * A repository underlies an instance of a gestalt and its lifetime 00065 * may or may not be bounded by the lifetime of the gestalt, that owns 00066 * it. This feature is important for the derived classes and the 00067 * Service Config in particular. 00068 * 00069 */ 00070 class ACE_Export ACE_Service_Gestalt 00071 { 00072 private: 00073 /// 00074 /// Not implemented to enforce no copying 00075 // 00076 ACE_UNIMPLEMENTED_FUNC (ACE_Service_Gestalt(const ACE_Service_Gestalt&)) 00077 ACE_UNIMPLEMENTED_FUNC (ACE_Service_Gestalt& operator=(const ACE_Service_Gestalt&)) 00078 00079 public: 00080 enum 00081 { 00082 MAX_SERVICES = ACE_DEFAULT_SERVICE_REPOSITORY_SIZE 00083 }; 00084 00085 enum 00086 { 00087 DEFAULT_SIZE = ACE_DEFAULT_SERVICE_GESTALT_SIZE 00088 }; 00089 00090 /// Constructor either associates the instance with the process-wide 00091 /// singleton instance of ACE_Service_Repository, or creates and 00092 /// manages its own instance of the specified size. 00093 ACE_Service_Gestalt (size_t size = DEFAULT_SIZE, 00094 bool svc_repo_is_owned = true, 00095 bool no_static_svcs = true); 00096 00097 /// Perform user-specified close activities and remove dynamic 00098 /// memory. 00099 ~ACE_Service_Gestalt (void); 00100 00101 /// Dump the state of an object. 00102 void dump (void) const; 00103 00104 /** 00105 * Performs an open without parsing command-line arguments. The 00106 * @a logger_key indicates where to write the logging output, which 00107 * is typically either a STREAM pipe or a socket address. If 00108 * @a ignore_static_svcs is true then static services are not loaded, 00109 * otherwise, they are loaded. If @a ignore_default_svc_conf_file is 00110 * true then the @c svc.conf configuration file will be ignored. 00111 * Returns zero upon success, -1 if the file is not found or cannot 00112 * be opened (errno is set accordingly), otherwise returns the 00113 * number of errors encountered loading the services in the 00114 * specified svc.conf configuration file. If @a ignore_debug_flag is 00115 * true then the application is responsible for setting the 00116 * ACE_Log_Msg::priority_mask appropriately. 00117 */ 00118 int open (const ACE_TCHAR program_name[], 00119 const ACE_TCHAR *logger_key = 0, 00120 bool ignore_static_svcs = true, 00121 bool ignore_default_svc_conf_file = false, 00122 bool ignore_debug_flag = false); 00123 00124 /** 00125 * This is the primary entry point into the ACE_Service_Config (the 00126 * constructor just handles simple initializations). It parses 00127 * arguments passed in from @a argc and @a argv parameters. The 00128 * arguments that are valid in a call to this method include: 00129 * 00130 * - '-b' Option to indicate that we should be a daemon. Note that when 00131 * this option is used, the process will be daemonized before the 00132 * service configuration file(s) are read. During daemonization, 00133 * (on POSIX systems) the current directory will be changed to "/" 00134 * so the caller should either fully specify the file names, or 00135 * execute a @c chroot() to the appropriate directory. 00136 * @sa ACE::daemonize(). 00137 * - '-d' Turn on debugging mode 00138 * - '-f' Specifies a configuration file name other than the default 00139 * svc.conf. Can be specified multiple times to use multiple files. 00140 * - '-k' Specifies the rendezvous point to use for the ACE distributed 00141 * logger. 00142 * - '-y' Explicitly enables the use of static services. This flag 00143 * overrides the @a ignore_static_svcs parameter value. 00144 * - '-n' Explicitly disables the use of static services. This flag 00145 * overrides the @a ignore_static_svcs parameter value. 00146 * - '-p' Specifies a pathname which is used to store the process id. 00147 * - '-s' Specifies a signal number other than SIGHUP to trigger reprocessing 00148 * of the configuration file(s). Ignored for platforms that do not 00149 * have POSIX signals, such as Windows. 00150 * - '-S' Specifies a service directive string. Enclose the string in quotes 00151 * and escape any embedded quotes with a backslash. This option 00152 * specifies service directives without the need for a configuration 00153 * file. 00154 * 00155 * @param argc The number of commandline arguments. 00156 * @param argv The array with commandline arguments 00157 * @param logger_key Indicates where to write the logging output, 00158 * which is typically either a STREAM pipe or a 00159 * socket address. 00160 * @param ignore_static_svcs If true then static services are not loaded, 00161 * otherwise, they are loaded. 00162 * @param ignore_default_svc_conf_file If false then the @c svc.conf 00163 * configuration file will be ignored. 00164 * @param ignore_debug_flag If false then the application is responsible 00165 * for setting the @c ACE_Log_Msg::priority_mask 00166 * appropriately. 00167 * 00168 * @retval -1 The configuration file is not found or cannot 00169 * be opened (errno is set accordingly). 00170 * @retval 0 Success. 00171 * @retval >0 The number of errors encountered while processing 00172 * the service configuration file(s). 00173 */ 00174 int open (int argc, 00175 ACE_TCHAR *argv[], 00176 const ACE_TCHAR *logger_key = 0, 00177 bool ignore_static_svcs = true, 00178 bool ignore_default_svc_conf_file = false, 00179 bool ignore_debug_flag = false); 00180 00181 /// Has it been opened? Returns the difference between the times 00182 /// open and close have been called on this instance 00183 int is_opened (void); 00184 00185 /// Declare the dynamic allocation hooks. 00186 ACE_ALLOC_HOOK_DECLARE; 00187 00188 /// Process one service configuration @a directive, which is passed as 00189 /// a string. Returns the number of errors that occurred. 00190 int process_directive (const ACE_TCHAR directive[]); 00191 00192 /// Process one static service definition. 00193 /** 00194 * Load a new static service. 00195 * 00196 * @param ssd Service descriptor, see the document of 00197 * ACE_Static_Svc_Descriptor for more details. 00198 * 00199 * @param force_replace If set the new service descriptor replaces 00200 * any previous instance in the repository. 00201 * 00202 * @return Returns -1 if the service cannot be 'loaded'. 00203 */ 00204 int process_directive (const ACE_Static_Svc_Descriptor &ssd, 00205 bool force_replace = false); 00206 00207 /// Process a file containing a list of service configuration 00208 /// directives. 00209 int process_file (const ACE_TCHAR file[]); 00210 00211 /** 00212 * Locate an entry with @a name in the table. If @a ignore_suspended 00213 * is set then only consider services marked as resumed. If the 00214 * caller wants the located entry, pass back a pointer to the 00215 * located entry via @a srp. If @a name is not found, -1 is returned. 00216 * If @a name is found, but it is suspended and the caller wants to 00217 * ignore suspended services a -2 is returned. 00218 */ 00219 int find (const ACE_TCHAR name[], 00220 const ACE_Service_Type **srp = 0, 00221 bool ignore_suspended = true) const; 00222 00223 /** 00224 * Handle the command-line options intended for the 00225 * ACE_Service_Gestalt. Note that @c argv[0] is assumed to be the 00226 * program name. 00227 * 00228 * The arguments that are valid in a call to this method are 00229 * - '-d' Turn on debugging mode 00230 * - '-f' Option to read in the list of svc.conf file names 00231 * - '-k' Option to read a wide string where in the logger output can 00232 * be written 00233 * - '-y' Turn on the flag for a repository of statically 00234 * linked services 00235 * - '-n' Need not have a repository of statically linked services 00236 * - '-S' Option to read in the list of services on the command-line 00237 * Please observe the difference between options '-f' that looks 00238 * for a list of files and here a list of services. 00239 */ 00240 int parse_args (int argc, ACE_TCHAR *argv[]); 00241 00242 /** 00243 * Process (or re-process) service configuration requests that are 00244 * provided in the svc.conf file(s). Returns the number of errors 00245 * that occurred. 00246 */ 00247 int process_directives (bool ignore_default_svc_conf_file); 00248 00249 /// Tidy up and perform last rites when ACE_Service_Config is shut 00250 /// down. This method calls @c close_svcs. Returns 0. 00251 int close (void); 00252 00253 /// Registers a service descriptor for a static service object 00254 int insert (ACE_Static_Svc_Descriptor *stsd); 00255 00256 // = Utility methods. 00257 00258 #if (ACE_USES_CLASSIC_SVC_CONF == 1) 00259 /// Dynamically link the shared object file and retrieve a pointer to 00260 /// the designated shared object in this file. Also account for the 00261 /// possiblity to have static services registered when loading the DLL, by 00262 /// ensuring that the dynamic sevice is registered before any of its 00263 /// subordibnate static services. Thus avoiding any finalization order 00264 /// problems. 00265 int initialize (const ACE_Service_Type_Factory *, 00266 const ACE_TCHAR *parameters); 00267 #endif /* (ACE_USES_CLASSIC_SVC_CONF == 1) */ 00268 00269 /// Dynamically link the shared object file and retrieve a pointer to 00270 /// the designated shared object in this file. 00271 /// @deprecated 00272 /// @note This is error-prone in the presense of dynamic services, 00273 /// which in turn initialize their own static services. This method 00274 /// will allow those static services to register *before* the dynamic 00275 /// service that owns them. Upon finalization of the static services 00276 /// the process will typically crash, because the dynamic service's 00277 /// DLL may have been already released, together with the memory in 00278 /// which the static services reside. It may not crash, for 00279 /// instance, when the first static service to register is the same 00280 /// as the dynamic service being loaded. You should be so lucky! 00281 int initialize (const ACE_Service_Type *, 00282 const ACE_TCHAR *parameters); 00283 00284 /// Initialize and activate a statically @a svc_name service. 00285 int initialize (const ACE_TCHAR *svc_name, 00286 const ACE_TCHAR *parameters); 00287 00288 /// Resume a @a svc_name that was previously suspended or has not yet 00289 /// been resumed (e.g., a static service). 00290 int resume (const ACE_TCHAR svc_name[]); 00291 00292 /** 00293 * Suspend @a svc_name. Note that this will not unlink the service 00294 * from the daemon if it was dynamically linked, it will mark it as 00295 * being suspended in the Service Repository and call the @c suspend() 00296 * member function on the appropriate ACE_Service_Object. A 00297 * service can be resumed later on by calling the @c resume() member 00298 * function... 00299 */ 00300 int suspend (const ACE_TCHAR svc_name[]); 00301 00302 /// Totally remove @a svc_name from the daemon by removing it 00303 /// from the ACE_Reactor, and unlinking it if necessary. 00304 int remove (const ACE_TCHAR svc_name[]); 00305 00306 /** 00307 * Using the supplied name, finds and (if needed) returns a pointer to a 00308 * static service descriptor. Returns 0 for success and -1 for failure 00309 */ 00310 int find_static_svc_descriptor (const ACE_TCHAR* name, 00311 ACE_Static_Svc_Descriptor **ssd = 0) const; 00312 00313 struct Processed_Static_Svc 00314 { 00315 Processed_Static_Svc (const ACE_Static_Svc_Descriptor *); 00316 ~Processed_Static_Svc (void); 00317 ACE_TCHAR * name_; 00318 const ACE_Static_Svc_Descriptor *assd_; 00319 }; 00320 00321 /// Get the current ACE_Service_Repository held by this object. 00322 ACE_Service_Repository* current_service_repository (void); 00323 00324 protected: 00325 00326 int parse_args_i (int, ACE_TCHAR *argv[], 00327 bool& ignore_default_svc_conf_file); 00328 00329 /** 00330 * Performs an open without parsing command-line arguments. The 00331 * @a logger_key indicates where to write the logging output, which 00332 * is typically either a STREAM pipe or a socket address. If 00333 * @a ignore_default_svc_conf_file is non-0 then the "svc.conf" file 00334 * will be ignored. If @a ignore_debug_flag is non-0 then the 00335 * application is responsible for setting the 00336 * @c ACE_Log_Msg::priority_mask() appropriately. Returns number of 00337 * errors that occurred on failure and 0 otherwise. 00338 */ 00339 int open_i (const ACE_TCHAR program_name[], 00340 const ACE_TCHAR *logger_key = 0, 00341 bool ignore_static_svcs = true, 00342 bool ignore_default_svc_conf_file = false, 00343 bool ignore_debug_flag = false); 00344 00345 /// Initialize the @c svc_conf_file_queue_ if necessary. 00346 int init_svc_conf_file_queue (void); 00347 00348 /// Add the default statically-linked services to the 00349 /// ACE_Service_Repository. 00350 int load_static_svcs (void); 00351 00352 /// Process service configuration requests that were provided on the 00353 /// command-line. Returns the number of errors that occurred. 00354 int process_commandline_directives (void); 00355 00356 /// Process a static directive without also inserting its descriptor 00357 /// the global table. This avoids multiple additions when processing 00358 /// directives in non-global gestalts. 00359 int process_directive_i (const ACE_Static_Svc_Descriptor &ssd, 00360 bool force_replace = false); 00361 00362 #if (ACE_USES_CLASSIC_SVC_CONF == 1) 00363 /// This is the implementation function that process_directives() 00364 /// and process_directive() both call. Returns the number of errors 00365 /// that occurred. 00366 int process_directives_i (ACE_Svc_Conf_Param *param); 00367 #else 00368 /// Helper function to dynamically link in the XML Service Configurator 00369 /// parser. 00370 ACE_XML_Svc_Conf* get_xml_svc_conf (ACE_DLL &d); 00371 #endif /* ACE_USES_CLASSIC_SVC_CONF == 1 */ 00372 00373 /// Dynamically link the shared object file and retrieve a pointer to 00374 /// the designated shared object in this file. 00375 int initialize_i (const ACE_Service_Type *sr, const ACE_TCHAR *parameters); 00376 00377 const ACE_Static_Svc_Descriptor* find_processed_static_svc (const ACE_TCHAR*); 00378 void add_processed_static_svc (const ACE_Static_Svc_Descriptor *); 00379 00380 /// Performs the common initialization tasks for a new or previously 00381 /// closed instance. Must not be virtual, as it is called from the 00382 /// constructor. 00383 int init_i (void); 00384 00385 protected: 00386 00387 /// Maintain a queue of services to be configured from the 00388 /// command-line. 00389 typedef ACE_Unbounded_Queue<ACE_TString> ACE_SVC_QUEUE; 00390 typedef ACE_Unbounded_Queue_Iterator<ACE_TString> ACE_SVC_QUEUE_ITERATOR; 00391 00392 /// Maintain a set of the statically linked service descriptors. 00393 typedef ACE_Unbounded_Set<ACE_Static_Svc_Descriptor *> 00394 ACE_STATIC_SVCS; 00395 00396 typedef ACE_Unbounded_Set_Iterator<ACE_Static_Svc_Descriptor *> 00397 ACE_STATIC_SVCS_ITERATOR; 00398 00399 typedef ACE_Unbounded_Set<Processed_Static_Svc *> 00400 ACE_PROCESSED_STATIC_SVCS; 00401 00402 typedef ACE_Unbounded_Set_Iterator<Processed_Static_Svc *> 00403 ACE_PROCESSED_STATIC_SVCS_ITERATOR; 00404 00405 friend class ACE_Dynamic_Service_Base; 00406 friend class ACE_Service_Object; 00407 friend class ACE_Service_Config; 00408 friend class ACE_Service_Config_Guard; 00409 00410 protected: 00411 00412 /// Do we own the service repository instance, or have only been 00413 /// given a ptr to the singleton? 00414 bool svc_repo_is_owned_; 00415 00416 /// Repository size is necessary, so that we can close (which may 00417 /// destroy the repository instance), and then re-open again. 00418 size_t svc_repo_size_; 00419 00420 /// Keep track of the number of times the instance has been 00421 /// initialized (opened). "If so, we can't allow <yyparse> to be called since 00422 /// it's not reentrant" is the original motivation, but that does not seem 00423 /// to be the case anymore. This variable is incremented by the 00424 /// <ACE_Service_Gestalt::open> method and decremented by the 00425 /// <ACE_Service_Gestalt::close> method. 00426 int is_opened_; 00427 00428 /// Indicates where to write the logging output. This is typically 00429 /// either a STREAM pipe or a socket 00430 const ACE_TCHAR *logger_key_; 00431 00432 /// Should we avoid loading the static services? 00433 bool no_static_svcs_; 00434 00435 /// Queue of services specified on the command-line. 00436 ACE_SVC_QUEUE* svc_queue_; 00437 00438 /** 00439 * Queue of svc.conf files specified on the command-line. 00440 * @@ This should probably be made to handle unicode filenames... 00441 */ 00442 ACE_SVC_QUEUE* svc_conf_file_queue_; 00443 00444 /// The service repository to hold the services. 00445 ACE_Service_Repository* repo_; 00446 00447 /// Repository of statically linked services. 00448 ACE_STATIC_SVCS* static_svcs_; 00449 00450 /// Repository of statically linked services for which process 00451 /// directive was called, but the service is not already a member of 00452 /// the static_svcs_ list. 00453 ACE_PROCESSED_STATIC_SVCS* processed_static_svcs_; 00454 00455 /// Support for intrusive reference counting 00456 ACE_Atomic_Op<ACE_SYNCH_MUTEX, long> refcnt_; 00457 00458 public: 00459 static void intrusive_add_ref (ACE_Service_Gestalt*); 00460 static void intrusive_remove_ref (ACE_Service_Gestalt*); 00461 00462 }; /* class ACE_Service_Gestalt */ 00463 00464 00465 /** 00466 * @class ACE_Service_Type_Dynamic_Guard 00467 * 00468 * @brief A forward service declaration guard. 00469 * 00470 * Helps to resolve an issue with hybrid services, i.e. dynamic 00471 * services, accompanied by static services in the same DLL. Only 00472 * automatic instances of this class are supposed to exist. Those are 00473 * created during (dynamic) service initialization and serve to: 00474 * 00475 * (a) Ensure the service we are loading is ordered last in the 00476 * repository, following any other services it may cause to register, 00477 * as part of its own registration. This is a common case when 00478 * loading dynamic services from DLLs - there are often static 00479 * initializers, which register static services. 00480 * 00481 * (b) The SDG instance destructor detects if the dynamic service 00482 * initialized successfully and "fixes-up" all the newly registered 00483 * static services to hold a reference to the DLL, from which they 00484 * have originated. 00485 */ 00486 class ACE_Export ACE_Service_Type_Dynamic_Guard 00487 { 00488 public: 00489 ACE_Service_Type_Dynamic_Guard (ACE_Service_Repository &r, 00490 ACE_TCHAR const *name); 00491 00492 ~ACE_Service_Type_Dynamic_Guard (void); 00493 00494 private: 00495 ACE_Service_Repository & repo_; 00496 size_t repo_begin_; 00497 ACE_TCHAR const * const name_; 00498 00499 # if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0) 00500 ACE_Guard< ACE_Recursive_Thread_Mutex > repo_monitor_; 00501 #endif 00502 }; 00503 00504 00505 ACE_END_VERSIONED_NAMESPACE_DECL 00506 00507 #if defined (__ACE_INLINE__) 00508 #include "ace/Service_Gestalt.inl" 00509 #endif /* __ACE_INLINE__ */ 00510 00511 00512 #include /**/ "ace/post.h" 00513 00514 #endif /* ACE_SERVICE_GESTALT_H */
1.6.1