3 @brief Facility for retrieving server configuration settings.
5 #include <opensrf/osrf_settings.h>
8 @brief Stores a copy of server configuration settings as a jsonObject.
10 It also stores the host name of the settings server which supplied the configuration
11 settings. In practice nothing uses the stored copy of the host name.
13 struct osrf_host_config_ {
14 /** @brief The host name of the settings server */
16 /** @brief The configuration settings as a jsonObject */
20 static osrf_host_config* osrf_settings_new_host_config(const char* hostname);
22 static osrf_host_config* config = NULL;
25 @brief Fetch a specified string from an already-loaded configuration.
26 @param format A printf-style format string. Subsequent parameters, if any, will be formatted
27 and inserted into the format string.
28 @return If the value is found, a pointer to a newly-allocated string containing the value;
31 The format string, after expansion, defines a search path through a configuration previously
32 loaded and stored as a jsonObject.
34 The configuration must have been already been loaded via a call to osrf_settings_retrieve()
35 (probably via a call to osrfSystemBootstrap()). Otherwise this function will call exit()
38 The calling code is responsible for freeing the string.
40 char* osrf_settings_host_value(const char* format, ...) {
41 VA_LIST_TO_STRING(format);
44 const char * msg = "NULL config pointer; looking for config_context ";
45 fprintf( stderr, "osrf_settings_host_value: %s\"%s\"\n",
47 osrfLogError( OSRF_LOG_MARK, "%s\"%s\"", msg, VA_BUF );
51 jsonObject* o = jsonObjectFindPath(config->config, VA_BUF);
52 char* val = jsonObjectToSimpleString(o);
58 @brief Fetch a specified subset of an already-loaded configuration.
59 @param format A printf-style format string. Subsequent parameters, if any, will be formatted
60 and inserted into the format string.
61 @return If the value is found, a pointer to a newly created jsonObject containing the
62 specified subset; otherwise NULL.
64 The format string, after expansion, defines a search path through a configuration previously
65 loaded and stored as a jsonObject.
67 The configuration must have been already been loaded via a call to osrf_settings_retrieve()
68 (probably via a call to osrfSystemBootstrap()). Otherwise this function will call exit()
71 The calling code is responsible for freeing the jsonObject.
73 jsonObject* osrf_settings_host_value_object(const char* format, ...) {
74 VA_LIST_TO_STRING(format);
77 const char * msg = "config pointer is NULL; looking for config context ";
78 fprintf( stderr, "osrf_settings_host_value_object: %s\"%s\"\n",
80 osrfLogError( OSRF_LOG_MARK, "%s\"%s\"", msg, VA_BUF );
84 return jsonObjectFindPath(config->config, VA_BUF);
89 @brief Look up the configuration settings and cache them for future reference.
90 @param hostname The host name for the settings server.
91 @return Zero if successful, or -1 if not.
93 The configuration settings come from a settings server. This arrangement is intended for
94 use by servers, so that all server settings can be stored in a single location. Typically
95 a client process (that is not also a server in its own right) will read its own
96 configuration file locally.
98 The settings are cached as a jsonObject for future lookups by the functions
99 osrf_settings_host_value() and osrf_settings_host_value_object().
101 The calling code is responsible for freeing the cached settings by calling
102 osrf_settings_free_host_config().
104 int osrf_settings_retrieve(const char* hostname) {
108 osrfAppSession* session = osrfAppSessionClientInit("opensrf.settings");
109 jsonObject* params = jsonNewObject(NULL);
110 jsonObjectPush(params, jsonNewObject(hostname));
111 int req_id = osrfAppSessionSendRequest(
112 session, params, "opensrf.settings.host_config.get", 1 );
113 osrfMessage* omsg = osrfAppSessionRequestRecv( session, req_id, 60 );
114 jsonObjectFree(params);
117 osrfLogError( OSRF_LOG_MARK, "No osrfMessage received from host %s (timeout?)", hostname);
118 } else if(!omsg->_result_content) {
119 osrfMessageFree(omsg);
122 "NULL or non-existent osrfMessage result content received from host %s, "
123 "broken message or no settings for host",
127 config = osrf_settings_new_host_config(hostname);
128 config->config = jsonObjectClone(omsg->_result_content);
129 osrfMessageFree(omsg);
132 osrf_app_session_request_finish( session, req_id );
133 osrfAppSessionFree( session );
136 osrfLogError( OSRF_LOG_MARK, "Unable to load config for host %s", hostname);
145 @brief Allocate and initialize an osrf_host_config for a given host name.
146 @param hostname Pointer to a host name.
147 @return Pointer to a newly created osrf_host_config.
149 static osrf_host_config* osrf_settings_new_host_config(const char* hostname) {
150 if(!hostname) return NULL;
151 osrf_host_config* c = safe_malloc(sizeof(osrf_host_config));
152 c->hostname = strdup(hostname);
158 @brief Deallocate an osrf_host_config and its contents.
159 @param c A pointer to the osrf_host_config to be deallocated.
161 void osrf_settings_free_host_config(osrf_host_config* c) {
168 jsonObjectFree(c->config);