]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_settings.c
97c4fae11374567f7de1d2a98ed8c68425f10745
[OpenSRF.git] / src / libopensrf / osrf_settings.c
1 #include <opensrf/osrf_settings.h> 
2
3 osrf_host_config* config = NULL;
4
5 /**
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;
10                 otherwise NULL.
11
12         The format string, after expansion, defines a search path through a configuration previously
13         loaded and stored as a jsonObject.
14
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()
17         immediately.
18
19         The calling code is responsible for freeing the string.
20 */
21 char* osrf_settings_host_value(const char* format, ...) {
22         VA_LIST_TO_STRING(format);
23
24         if( ! config ) {
25                 const char * msg = "NULL config pointer; looking for config_context ";
26                 fprintf( stderr, "osrf_settings_host_value: %s\"%s\"\n",
27                         msg, VA_BUF );
28                 osrfLogError( OSRF_LOG_MARK, "%s\"%s\"", msg, VA_BUF );
29                 exit( 99 );
30         }
31
32         jsonObject* o = jsonObjectFindPath(config->config, VA_BUF);
33         char* val = jsonObjectToSimpleString(o);
34         jsonObjectFree(o);
35         return val;
36 }
37
38 /**
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.
44
45         The format string, after expansion, defines a search path through a configuration previously
46         loaded and stored as a jsonObject.
47
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()
50         immediately.
51
52         The calling code is responsible for freeing the jsonObject.
53  */
54 jsonObject* osrf_settings_host_value_object(const char* format, ...) {
55         VA_LIST_TO_STRING(format);
56
57         if( ! config ) {
58                 const char * msg = "config pointer is NULL; looking for config context ";
59                 fprintf( stderr, "osrf_settings_host_value_object: %s\"%s\"\n",
60                         msg, VA_BUF );
61                 osrfLogError( OSRF_LOG_MARK, "%s\"%s\"", msg, VA_BUF );
62                 exit( 99 );
63         }
64
65         return jsonObjectFindPath(config->config, VA_BUF);
66 }
67
68
69 int osrf_settings_retrieve(const char* hostname) {
70
71         if(!config) {
72
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);
80
81                 if(!omsg) {
82                         osrfLogError( OSRF_LOG_MARK, "No osrfMessage received from host %s (timeout?)", hostname);
83                 } else if(!omsg->_result_content) {
84                         osrfMessageFree(omsg);
85                         osrfLogError(
86                                 OSRF_LOG_MARK,
87                         "NULL or non-existent osrfMessage result content received from host %s, "
88                                 "broken message or no settings for host",
89                                 hostname
90                         );
91                 } else {
92                         config = osrf_settings_new_host_config(hostname);
93                         config->config = jsonObjectClone(omsg->_result_content);
94                         osrfMessageFree(omsg);
95                 }
96
97                 osrf_app_session_request_finish( session, req_id );
98                 osrfAppSessionFree( session );
99
100                 if(!config) {
101                         osrfLogError( OSRF_LOG_MARK, "Unable to load config for host %s", hostname);
102                         return -1;
103                 }
104         }
105
106         return 0;
107 }
108
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);
113         c->config = NULL;
114         return c;
115 }
116
117 void osrf_settings_free_host_config(osrf_host_config* c) {
118         if(!c) c = config;
119         if(!c) return;
120         free(c->hostname);
121         jsonObjectFree(c->config);      
122         free(c);
123 }