Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_environment_configuration.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Adaptive Framework Register Environment Configuration
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
9 
15 #include "afw_internal.h"
16 
17 
18 
19 /* Configure environment with a configuration entry. */
20 AFW_DEFINE(void)
22  const afw_object_t *conf,
23  const afw_utf8_t *source_location,
24  afw_xctx_t *xctx)
25 {
26  const afw_utf8_t *type;
27  const afw_environment_conf_type_t *conf_type;
28  const afw_pool_t *p;
29 
30  /* Create new multithreaded pool */
31  p = afw_pool_create_multithreaded(xctx->env->p, xctx);
32 
33  /* Clone conf and source_location to new pool. */
34  conf = afw_object_create_clone(conf, p, xctx);
35  source_location = afw_utf8_clone(source_location, p, xctx);
36 
37  /* Added sourceLocation property to conf. */
39  &afw_s_sourceLocation, source_location, xctx);
40 
41  /* Get type property. */
42  type = afw_object_old_get_property_as_utf8(conf, &afw_s_type, p, xctx);
43  if (!type) {
44  AFW_THROW_ERROR_FZ(general, xctx,
46  "missing type property",
47  AFW_UTF8_FMT_ARG(source_location));
48  }
49 
50  /* Get conf_type and call create function. */
51  conf_type = afw_environment_get_conf_type(type, xctx);
52  if (!conf_type) {
53  AFW_THROW_ERROR_FZ(general, xctx,
55  "invalid type \"%" AFW_UTF8_FMT "\"",
56  AFW_UTF8_FMT_ARG(source_location),
57  AFW_UTF8_FMT_ARG(type));
58  }
59  conf_type->create(type, conf, source_location, p, xctx);
60 }
61 
62 
63 /* Configure environment with list of configuration entries. */
64 AFW_DEFINE(void)
66  const afw_list_t *entry_list,
67  const afw_utf8_t *source_location,
68  afw_xctx_t *xctx)
69 {
70  const afw_iterator_t *iterator;
71  const afw_value_t *value;
72  const afw_object_t *entry;
73  const afw_utf8_t *detail_source_location;
74  int count;
75 
76  /* Default source_location to "Configuration". */
77  if (!source_location) {
78  source_location = &afw_s_Configuration;
79  }
80 
81  /* Process each configuration entry. */
82  for (iterator = NULL, count = 1;; count++) {
83 
84  /* Get next configuration entry. */
85  value = afw_list_get_next_value(entry_list, &iterator,
86  xctx->p, xctx);
87  if (!value) break;
88 
89  /* Source location will include entry number. */
90  detail_source_location = afw_utf8_printf(xctx->env->p, xctx,
91  "%" AFW_UTF8_FMT " entry %d",
92  AFW_UTF8_FMT_ARG(source_location), count);
93 
94  if (!afw_value_is_object(value)) {
95  AFW_THROW_ERROR_FZ(general, xctx,
97  "is not an object",
98  AFW_UTF8_FMT_ARG(detail_source_location));
99  }
100  entry = ((const afw_value_object_t *)value)->internal;
101 
102  /* Configure using entry. */
103  afw_environment_configure_with_object(entry, detail_source_location,
104  xctx);
105  };
106 }
107 
108 
110  const afw_utf8_t *type,
111  const afw_object_t *entry,
112  const afw_utf8_t *source_location,
113  const afw_pool_t *p, afw_xctx_t *xctx)
114 {
115  const afw_utf8_t *extension_id;
116  const afw_utf8_t *module_path;
117  const afw_object_t* manifest;
118 
119  extension_id = afw_object_old_get_property_as_utf8(entry,
120  &afw_s_extensionId, p, xctx);
121 
122  if (!extension_id) {
123  AFW_THROW_ERROR_FZ(general, xctx,
125  "\"extension_id\" required.",
126  AFW_UTF8_FMT_ARG(source_location));
127  }
128 
129  module_path = afw_object_old_get_property_as_utf8(entry,
130  &afw_s_modulePath, p, xctx);
131 
132  /* If module_path is not supplied, see if it is registered. */
133  if (!module_path) {
134  manifest = afw_runtime_get_object(&afw_s__AdaptiveManifest_,
135  extension_id, xctx);
136  if (manifest) {
137  module_path = afw_object_old_get_property_as_string(manifest,
138  &afw_s_modulePath, xctx);
139  }
140  }
141  if (!module_path) {
142  AFW_THROW_ERROR_FZ(general, xctx,
144  "\"modulePath\" needed for \"extension\" %" AFW_UTF8_FMT ".",
145  AFW_UTF8_FMT_ARG(source_location),
146  AFW_UTF8_FMT_ARG(extension_id));
147  }
148 
149  afw_environment_load_extension(extension_id, module_path,
150  entry, xctx);
151 }
152 
153 
154 /* Prepare properties for a conf type. */
155 AFW_DEFINE(const afw_object_t *)
157  const afw_object_t *properties,
158  afw_xctx_t *xctx)
159 {
160  const afw_pool_t *p;
161  const afw_object_t *result;
162  const afw_utf8_t *type;
163  const afw_utf8_t *subtype;
164  const afw_utf8_t *id;
165  const afw_utf8_t *source_location;
166  const afw_environment_conf_type_t *conf_type;
167  const afw_utf8_t *path;
168 
169  /* Use properties p. */
170  p = properties->p;
171 
172  /* Get sourceLocation. Default for now to empty string. */
173  source_location = afw_object_old_get_property_as_string(
174  properties, &afw_s_sourceLocation, xctx);
175  if (!source_location) {
176  source_location = &afw_s_a_empty_string;
177  }
178 
179  /* Get type. */
180  type = afw_object_old_get_property_as_string(properties,
181  &afw_s_type, xctx);
182  if (!type || type->len == 0) {
183  AFW_THROW_ERROR_FZ(general, xctx,
185  " missing type",
186  AFW_UTF8_FMT_ARG(source_location));
187  }
188 
189  /* Get conf_type for type. */
190  conf_type = afw_environment_get_conf_type(type, xctx);
191  if (!conf_type) {
192  AFW_THROW_ERROR_FZ(general, xctx,
194  "type %" AFW_UTF8_FMT
195  " is not valid",
196  AFW_UTF8_FMT_ARG(source_location),
197  AFW_UTF8_FMT_ARG(type));
198  }
199 
200  /* If appropriate, get subtype. */
201  subtype = NULL;
202  if (conf_type->subtype_property_name) {
204  properties, conf_type->subtype_property_name, xctx);
205  if (!subtype || subtype->len == 0) {
206  AFW_THROW_ERROR_FZ(general, xctx,
208  "%" AFW_UTF8_FMT
209  " property required for conf type %" AFW_UTF8_FMT,
210  AFW_UTF8_FMT_ARG(source_location),
212  AFW_UTF8_FMT_ARG(type));
213  }
214  }
215 
216  /* If appropriate, get id. If not present, default to subtype. */
217  id = &afw_s_current;
218  if (conf_type->id_property_name) {
220  properties, conf_type->id_property_name, xctx);
221  if (!id || id->len == 0) {
222  AFW_THROW_ERROR_FZ(general, xctx,
224  " %" AFW_UTF8_FMT
225  " property required for conf type %" AFW_UTF8_FMT,
226  AFW_UTF8_FMT_ARG(source_location),
228  AFW_UTF8_FMT_ARG(type));
229  }
230  }
231 
232  /* Construct path. */
233  path = afw_utf8_printf(p, xctx,
234  "/afw/_AdaptiveConf_%" AFW_UTF8_FMT
235  "%s%" AFW_UTF8_FMT
236  "/%" AFW_UTF8_FMT,
237  AFW_UTF8_FMT_ARG(type),
238  (const char *)((subtype) ? "_" : ""),
239  (int)((subtype) ? (int)subtype->len : 0),
240  (const char *)((subtype) ? (const char *)subtype->s : ""),
241  AFW_UTF8_FMT_ARG(id));
242 
243 
244  /* If defaulting source location, make it path. */
245  if (source_location == &afw_s_a_empty_string) {
246  source_location = path;
248  &afw_s_sourceLocation, source_location, xctx);
249  }
250 
251  /* If not adaptor for afw (which will cause loop), normalize result. */
252  result = properties;
253  if (!afw_utf8_equal(type, &afw_s_adaptor) ||
254  !afw_utf8_equal(id, &afw_s_afw))
255  {
256  result = afw_object_view_create(properties,
257  path, &afw_object_options_composite_normalize_defaults_required,
258  p, xctx);
259  result = afw_object_create_clone(result, p, xctx);
260  if (afw_object_meta_has_errors(result, xctx)) {
261  afw_object_meta_log_errors(result, source_location, xctx);
262  AFW_THROW_ERROR_FZ(general, xctx,
264  " configuration error(s) logged",
265  AFW_UTF8_FMT_ARG(source_location));
266  }
267  }
268 
269 
270  /* Return result. */
271  return result;
272 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
#define afw_value_is_object(A_VALUE)
Macro to determine if value is evaluated object.
#define afw_object_old_get_property_as_string(object, property_name, xctx)
Get property function for data type string value.
afw_object_set_property_as_string(const afw_object_t *object, const afw_utf8_t *property_name, const afw_utf8_t *internal, afw_xctx_t *xctx)
Set property function for data type string values.
#define AFW_UTF8_FMT_ARG(A_STRING)
Convenience Macro for use with AFW_UTF8_FMT to specify arg.
Definition: afw_common.h:605
#define AFW_UTF8_CONTEXTUAL_LABEL_FMT
Format string used for source location.
Definition: afw_common.h:595
struct afw_iterator_s afw_iterator_t
#define AFW_UTF8_FMT
Format string specifier used for afw_utf8_t.
Definition: afw_common.h:588
void afw_environment_internal_extension_conf_type_create_cede_p(const afw_utf8_t *type, const afw_object_t *entry, const afw_utf8_t *source_location, const afw_pool_t *p, afw_xctx_t *xctx)
type=extension conf handler.
const afw_environment_conf_type_t * afw_environment_get_conf_type(const afw_utf8_t *type, afw_xctx_t *xctx)
Get the conf_type associated with type.
afw_environment_prepare_conf_type_properties(const afw_object_t *properties, afw_xctx_t *xctx)
Prepare properties for a conf type.
afw_environment_load_extension(const afw_utf8_t *extension_id, const afw_utf8_t *module_path, const afw_object_t *properties, afw_xctx_t *xctx)
Load and initialize environment extension.
afw_environment_configure_with_object(const afw_object_t *conf, const afw_utf8_t *source_location, afw_xctx_t *xctx)
Configure environment with a configuration entry.
afw_environment_configure_with_object_list(const afw_list_t *entry_list, const afw_utf8_t *source_location, afw_xctx_t *xctx)
Configure environment with list of configuration entries.
#define AFW_THROW_ERROR_FZ(code, xctx, format_z,...)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:319
#define afw_list_get_next_value(instance, iterator, p, xctx)
Call method get_next_value of interface afw_list.
afw_object_meta_has_errors(const afw_object_t *instance, afw_xctx_t *xctx)
Check if object flagged for errors.
afw_object_meta_log_errors(const afw_object_t *instance, const afw_utf8_t *source_location, afw_xctx_t *xctx)
Log meta errors.
afw_object_view_create(const afw_object_t *instance, const afw_utf8_t *entity_path, const afw_object_options_t *options, const afw_pool_t *p, afw_xctx_t *xctx)
Create an object view of an object in specified pool.
afw_object_create_clone(const afw_object_t *object, const afw_pool_t *p, afw_xctx_t *xctx)
Clone an object to a specified pool.
Definition: afw_memory.c:138
afw_object_old_get_property_as_utf8(const afw_object_t *instance, const afw_utf8_t *property_name, const afw_pool_t *p, afw_xctx_t *xctx)
Get an object's property value as a string in specified pool.
Definition: afw_object.c:531
const afw_pool_t * afw_pool_create_multithreaded(const afw_pool_t *parent, afw_xctx_t *xctx)
Create a new multithreaded pool.
afw_runtime_get_object(const afw_utf8_t *object_type_id, const afw_utf8_t *object_id, afw_xctx_t *xctx)
Get a runtime object.
Definition: afw_runtime.c:853
afw_boolean_t afw_utf8_equal(const afw_utf8_t *s1, const afw_utf8_t *s2)
Check to see if a string equals another string.
const afw_utf8_t * afw_utf8_clone(const afw_utf8_t *string, const afw_pool_t *p, afw_xctx_t *xctx)
Clone a utf-8 string into a specific pool.
Definition: afw_utf8.h:347
afw_utf8_printf(const afw_pool_t *p, afw_xctx_t *xctx, const afw_utf8_z_t *format,...)
Create a utf-8 string using a c format string in specified pool.
Definition: afw_utf8.c:459
Struct for afw environment conf type.
const afw_utf8_t * subtype_property_name
Subtype property name for instances of this conf type.
const afw_utf8_t * id_property_name
Id property name for instances of this conf type.
afw_environment_conf_type_create_cede_p_t create
Create function for this type.
Interface afw_list public struct.
Interface afw_object public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
struct for data type object values.
Interface afw_value public struct.
Interface afw_xctx public struct.