1 #include <opensrf/osrf_settings.h>
3 osrf_host_config* config = NULL;
6 @brief Fetch a specified string from an already-loaded configuration.
7 @param format A printf-style format string. Subsequent parameters, if any, will be formatted
8 and inserted into the format string.
9 @return If the value is found, a pointer to a newly-allocated string containing the value;
12 The format string, after expansion, defines a search path through a configuration previously
13 loaded and stored as a jsonObject.
15 The configuration must have been already been loaded via a call to osrf_settings_retrieve()
16 (probably via a call to osrfSystemBootstrap()). Otherwise this function will call exit()
19 The calling code is responsible for freeing the string.
21 char* osrf_settings_host_value(const char* format, ...) {
22 VA_LIST_TO_STRING(format);
25 const char * msg = "NULL config pointer; looking for config_context ";
26 fprintf( stderr, "osrf_settings_host_value: %s\"%s\"\n",
28 osrfLogError( OSRF_LOG_MARK, "%s\"%s\"", msg, VA_BUF );
32 jsonObject* o = jsonObjectFindPath(config->config, VA_BUF);
33 char* val = jsonObjectToSimpleString(o);
39 @brief Fetch a specified subset of an already-loaded configuration.
40 @param format A printf-style format string. Subsequent parameters, if any, will be formatted
41 and inserted into the format string.
42 @return If the value is found, a pointer to a newly created jsonObject containing the
43 specified subset; otherwise NULL.
45 The format string, after expansion, defines a search path through a configuration previously
46 loaded and stored as a jsonObject.
48 The configuration must have been already been loaded via a call to osrf_settings_retrieve()
49 (probably via a call to osrfSystemBootstrap()). Otherwise this function will call exit()
52 The calling code is responsible for freeing the jsonObject.
54 jsonObject* osrf_settings_host_value_object(const char* format, ...) {
55 VA_LIST_TO_STRING(format);
58 const char * msg = "config pointer is NULL; looking for config context ";
59 fprintf( stderr, "osrf_settings_host_value_object: %s\"%s\"\n",
61 osrfLogError( OSRF_LOG_MARK, "%s\"%s\"", msg, VA_BUF );
65 return jsonObjectFindPath(config->config, VA_BUF);
69 int osrf_settings_retrieve(const char* hostname) {
73 osrfAppSession* session = osrfAppSessionClientInit("opensrf.settings");
74 jsonObject* params = jsonNewObject(NULL);
75 jsonObjectPush(params, jsonNewObject(hostname));
76 int req_id = osrfAppSessionMakeRequest(
77 session, params, "opensrf.settings.host_config.get", 1, NULL );
78 osrfMessage* omsg = osrfAppSessionRequestRecv( session, req_id, 60 );
79 jsonObjectFree(params);
82 osrfLogError( OSRF_LOG_MARK, "No osrfMessage received from host %s (timeout?)", hostname);
83 } else if(!omsg->_result_content) {
84 osrfMessageFree(omsg);
87 "NULL or non-existent osrfMessage result content received from host %s, "
88 "broken message or no settings for host",
92 config = osrf_settings_new_host_config(hostname);
93 config->config = jsonObjectClone(omsg->_result_content);
94 osrfMessageFree(omsg);
97 osrf_app_session_request_finish( session, req_id );
98 osrfAppSessionFree( session );
101 osrfLogError( OSRF_LOG_MARK, "Unable to load config for host %s", hostname);
109 osrf_host_config* osrf_settings_new_host_config(const char* hostname) {
110 if(!hostname) return NULL;
111 osrf_host_config* c = safe_malloc(sizeof(osrf_host_config));
112 c->hostname = strdup(hostname);
117 void osrf_settings_free_host_config(osrf_host_config* c) {
121 jsonObjectFree(c->config);