XTA: API Reference for C language

xta_native_xa_resource.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2019, Christian Ferrari <tiian@users.sourceforge.net>
3  * All rights reserved.
4  *
5  * This file is part of LIXA.
6  *
7  * LIXA is free software: you can redistribute this file and/or modify
8  * it under the terms of the GNU Lesser General Public License version 2.1 as
9  * published by the Free Software Foundation.
10  *
11  * LIXA is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with LIXA. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "config.h"
20 
21 
22 
23 /* system includes */
24 #ifdef HAVE_GLIB_H
25 # include <glib.h>
26 #endif
27 /* LIXA includes */
28 #include "lixa_errors.h"
29 #include "lixa_trace.h"
30 #include "client_conn.h"
31 /* XTA includes */
32 #include "xta_native_xa_resource.h"
33 
34 
35 
36 /* set module trace flag */
37 #ifdef LIXA_TRACE_MODULE
38 # undef LIXA_TRACE_MODULE
39 #endif /* LIXA_TRACE_MODULE */
40 #define LIXA_TRACE_MODULE LIXA_TRACE_MOD_XTA
41 
42 
43 
45  const char *name, const char *switch_file,
46  const char *open_info, const char *close_info)
47 {
48  enum Exception { G_TRY_MALLOC_ERROR
49  , XTA_NATIVE_XA_RESOURCE_INIT_ERROR
50  , NONE } excp;
51  int ret_cod = LIXA_RC_INTERNAL_ERROR;
52  xta_native_xa_resource_t *this = NULL;
53 
54  LIXA_TRACE(("xta_native_xa_resource_new\n"));
55  TRY {
56  /* allocate sufficient memory for the object */
57  if (NULL == (this = (xta_native_xa_resource_t *)g_try_malloc0(
58  sizeof(xta_native_xa_resource_t))))
59  THROW(G_TRY_MALLOC_ERROR);
60  /* initialize the object */
61  if (LIXA_RC_OK != (ret_cod = xta_native_xa_resource_init(
62  this, -1, NULL, name, switch_file,
63  open_info, close_info)))
64  THROW(XTA_NATIVE_XA_RESOURCE_INIT_ERROR);
65 
66  THROW(NONE);
67  } CATCH {
68  switch (excp) {
69  case G_TRY_MALLOC_ERROR:
71  break;
72  case XTA_NATIVE_XA_RESOURCE_INIT_ERROR:
73  break;
74  case NONE:
75  ret_cod = LIXA_RC_OK;
76  break;
77  default:
78  ret_cod = LIXA_RC_INTERNAL_ERROR;
79  } /* switch (excp) */
80  /* recovery step in the event of error */
81  if (NONE > excp) {
82  if (NULL != this) {
83  g_free(this);
84  this = NULL;
85  }
86  } /* if (NONE > excp) */
87  } /* TRY-CATCH */
88  LIXA_TRACE(("xta_native_xa_resource_new/excp=%d/"
89  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
90  return this;
91 }
92 
93 
94 
96  int rmid, const xta_transaction_manager_config_t *config)
97 {
98  enum Exception { G_TRY_MALLOC_ERROR
99  , XTA_NATIVE_XA_RESOURCE_INIT_ERROR
100  , NONE } excp;
101  int ret_cod = LIXA_RC_INTERNAL_ERROR;
102  xta_native_xa_resource_t *this = NULL;
103 
104  LIXA_TRACE(("xta_native_xa_resource_new_by_rmid\n"));
105  TRY {
106  /* allocate sufficient memory for the object */
107  if (NULL == (this = (xta_native_xa_resource_t *)g_try_malloc0(
108  sizeof(xta_native_xa_resource_t))))
109  THROW(G_TRY_MALLOC_ERROR);
110  /* initialize the object */
111  if (LIXA_RC_OK != (ret_cod = xta_native_xa_resource_init(
112  this, rmid, config, NULL, NULL, NULL, NULL)))
113  THROW(XTA_NATIVE_XA_RESOURCE_INIT_ERROR);
114 
115  THROW(NONE);
116  } CATCH {
117  switch (excp) {
118  case G_TRY_MALLOC_ERROR:
119  ret_cod = LIXA_RC_G_TRY_MALLOC_ERROR;
120  break;
121  case XTA_NATIVE_XA_RESOURCE_INIT_ERROR:
122  break;
123  case NONE:
124  ret_cod = LIXA_RC_OK;
125  break;
126  default:
127  ret_cod = LIXA_RC_INTERNAL_ERROR;
128  } /* switch (excp) */
129  } /* TRY-CATCH */
130  if (NONE > excp) {
131  /* memory recovery */
132  g_free(this);
133  this = NULL;
134  }
135  LIXA_TRACE(("xta_native_xa_resource_new_by_rmid/excp=%d/"
136  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
137  return this;
138 }
139 
140 
141 
143 {
144  enum Exception { NONE } excp;
145  int ret_cod = LIXA_RC_INTERNAL_ERROR;
146 
147  LIXA_TRACE(("xta_native_xa_resource_delete\n"));
148  TRY {
149  /* dispose object content */
150  xta_native_xa_resource_clean(xa_resource);
151  /* release memory allocated for the object */
152  g_free(xa_resource);
153 
154  THROW(NONE);
155  } CATCH {
156  switch (excp) {
157  case NONE:
158  ret_cod = LIXA_RC_OK;
159  break;
160  default:
161  ret_cod = LIXA_RC_INTERNAL_ERROR;
162  } /* switch (excp) */
163  } /* TRY-CATCH */
164  LIXA_TRACE(("xta_native_xa_resource_delete/excp=%d/"
165  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
166  return;
167 }
168 
169 
170 
172  xta_native_xa_resource_t *xa_resource,
173  int rmid, const xta_transaction_manager_config_t *config, const char *name,
174  const char *switch_file, const char *open_info, const char *close_info)
175 {
176  enum Exception { NULL_OBJECT
177  , XA_RESOURCE_INIT_ERROR
178  , INVALID_OPTION
179  , XML_STRDUP_ERROR1
180  , XML_STRDUP_ERROR2
181  , CLIENT_CONFIG_LOAD_SWITCH_FILE_ERROR
182  , OUT_OF_RANGE
183  , NONE } excp;
184  int ret_cod = LIXA_RC_INTERNAL_ERROR;
185 
186  LIXA_TRACE(("xta_native_xa_resource_init\n"));
187  TRY {
188  if (NULL == xa_resource)
189  THROW(NULL_OBJECT);
190  /* call parent initializator */
191  if (LIXA_RC_OK != (ret_cod = xta_xa_resource_init(
192  (xta_xa_resource_t *)xa_resource, TRUE)))
193  THROW(XA_RESOURCE_INIT_ERROR);
194  if (rmid < 0) {
195  /* rmid < 0: this is a dynamic definition */
196  xa_resource->xa_resource.dynamic = TRUE;
197  if (NULL == name || NULL == switch_file || NULL == open_info ||
198  NULL == close_info)
199  THROW(INVALID_OPTION);
200  if (strlen(open_info) >= MAXINFOSIZE) {
201  LIXA_TRACE(("xta_native_xa_resource_init: option open_info "
202  "(" SIZE_T_FORMAT ")"
203  "exceeds MAXINFOSIZE %d\n", strlen(open_info),
204  MAXINFOSIZE));
205  } /* if (strlen(open_info) >= MAXINFOSIZE) */
206  if (strlen(close_info) >= MAXINFOSIZE) {
207  LIXA_TRACE(("xta_native_xa_resource_init: option close_info "
208  "(" SIZE_T_FORMAT ")"
209  "exceeds MAXINFOSIZE %d\n", strlen(close_info),
210  MAXINFOSIZE));
211  } /* if (strlen(close_info) >= MAXINFOSIZE) */
212  /* duplicate resource name */
213  if (NULL == (xa_resource->xa_resource.rsrmgr_config.name =
214  xmlCharStrdup(name)))
215  THROW(XML_STRDUP_ERROR1);
216  /* duplicate resource switch_file (path) */
217  if (NULL == (xa_resource->xa_resource.rsrmgr_config.switch_file =
218  xmlCharStrdup(switch_file)))
219  THROW(XML_STRDUP_ERROR2);
220  /* copy open_info */
221  strncpy(xa_resource->xa_resource.rsrmgr_config.xa_open_info,
222  open_info, MAXINFOSIZE);
223  xa_resource->xa_resource.rsrmgr_config.xa_open_info[
224  MAXINFOSIZE-1] = '\0';
225  /* copy close_info */
226  strncpy(xa_resource->xa_resource.rsrmgr_config.xa_close_info,
227  close_info, MAXINFOSIZE);
228  xa_resource->xa_resource.rsrmgr_config.xa_close_info[
229  MAXINFOSIZE-1] = '\0';
230  /* load the switch file for the resource manager */
231  if (LIXA_RC_OK != (ret_cod = client_config_load_switch_file(
232  &xa_resource->xa_resource.act_rsrmgr_config,
233  TRUE)))
234  THROW(CLIENT_CONFIG_LOAD_SWITCH_FILE_ERROR);
235  LIXA_TRACE(("xta_native_xa_resource_init: initialized resource "
236  "name='%s', switch_file='%s', xa_open_info='%s', "
237  "xa_close_info='%s'\n",
238  xa_resource->xa_resource.rsrmgr_config.name,
239  xa_resource->xa_resource.rsrmgr_config.switch_file,
240  xa_resource->xa_resource.rsrmgr_config.xa_open_info,
241  xa_resource->xa_resource.rsrmgr_config.xa_close_info));
242  } else {
243  struct act_rsrmgr_config_s *act_rsrmgr;
244  /* rmid >= 0: get the properties from global configuration that
245  * has been loaded by xta_transaction_manager_new() */
246  xa_resource->xa_resource.dynamic = FALSE;
247  if (rmid >=
248  ((client_config_coll_t *)config)->actconf.rsrmgrs->len) {
249  LIXA_TRACE(("xta_native_xa_resource_init: rmid=%d is out of "
250  "range [0,%u]\n", rmid,
251  ((client_config_coll_t *)config)->
252  actconf.rsrmgrs->len-1));
253  THROW(OUT_OF_RANGE);
254  }
255  act_rsrmgr = &g_array_index(
256  ((client_config_coll_t *)config)->actconf.rsrmgrs,
257  struct act_rsrmgr_config_s, rmid);
258  /* copy it locally to the resource object */
259  xa_resource->xa_resource.act_rsrmgr_config = *act_rsrmgr;
260  }
261 
262  THROW(NONE);
263  } CATCH {
264  switch (excp) {
265  case NULL_OBJECT:
266  ret_cod = LIXA_RC_NULL_OBJECT;
267  break;
268  case XA_RESOURCE_INIT_ERROR:
269  break;
270  case INVALID_OPTION:
271  ret_cod = LIXA_RC_INVALID_OPTION;
272  break;
273  case XML_STRDUP_ERROR1:
274  case XML_STRDUP_ERROR2:
275  ret_cod = LIXA_RC_XML_STRDUP_ERROR;
276  break;
277  case CLIENT_CONFIG_LOAD_SWITCH_FILE_ERROR:
278  break;
279  case OUT_OF_RANGE:
280  ret_cod = LIXA_RC_OUT_OF_RANGE;
281  break;
282  case NONE:
283  ret_cod = LIXA_RC_OK;
284  break;
285  default:
286  ret_cod = LIXA_RC_INTERNAL_ERROR;
287  } /* switch (excp) */
288  } /* TRY-CATCH */
289  LIXA_TRACE(("xta_native_xa_resource_init/excp=%d/"
290  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
291  return ret_cod;
292 }
293 
294 
295 
297 {
298  enum Exception { CLIENT_CONFIG_UNLOAD_SWITCH_FILE_ERROR
299  , NONE } excp;
300  int ret_cod = LIXA_RC_INTERNAL_ERROR;
301 
302  LIXA_TRACE(("xta_native_xa_resource_clean\n"));
303  TRY {
304  /* clean properties only for dynamically created resources */
305  if (xa_resource->xa_resource.dynamic) {
306  /* clean resource name */
307  if (NULL != xa_resource->xa_resource.rsrmgr_config.name) {
308  g_free(xa_resource->xa_resource.rsrmgr_config.name);
309  xa_resource->xa_resource.rsrmgr_config.name = NULL;
310  }
311  /* clean switch_file */
312  if (NULL != xa_resource->xa_resource.rsrmgr_config.switch_file) {
313  g_free(xa_resource->xa_resource.rsrmgr_config.switch_file);
314  xa_resource->xa_resource.rsrmgr_config.switch_file = NULL;
315  }
316  /* clean xa_open_info and xa_close_info */
317  xa_resource->xa_resource.rsrmgr_config.xa_open_info[0] = '\0';
318  xa_resource->xa_resource.rsrmgr_config.xa_close_info[0] = '\0';
319  /* clean pointer from complete to partial structure */
320  xa_resource->xa_resource.act_rsrmgr_config.generic = NULL;
321  /* unload module */
322  if (NULL != xa_resource->xa_resource.act_rsrmgr_config.module) {
323  if (LIXA_RC_OK != (
324  ret_cod = client_config_unload_switch_file(
325  &xa_resource->xa_resource.act_rsrmgr_config)))
326  THROW(CLIENT_CONFIG_UNLOAD_SWITCH_FILE_ERROR);
327  }
328  } /* if (xa_resource->dynamic) */
329  /* clean "base class" (xta_xa_resource_t) properties */
331 
332  THROW(NONE);
333  } CATCH {
334  switch (excp) {
335  case CLIENT_CONFIG_UNLOAD_SWITCH_FILE_ERROR:
336  break;
337  case NONE:
338  ret_cod = LIXA_RC_OK;
339  break;
340  default:
341  ret_cod = LIXA_RC_INTERNAL_ERROR;
342  } /* switch (excp) */
343  } /* TRY-CATCH */
344  LIXA_TRACE(("xta_native_xa_resource_clean/excp=%d/"
345  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
346  return;
347 }
348 
int xta_native_xa_resource_init(xta_native_xa_resource_t *xa_resource, int rmid, const xta_transaction_manager_config_t *config, const char *name, const char *switch_file, const char *open_info, const char *close_info)
#define LIXA_RC_XML_STRDUP_ERROR
Definition: lixa_errors.h:498
#define LIXA_RC_NULL_OBJECT
Definition: lixa_errors.h:139
#define LIXA_RC_INTERNAL_ERROR
Definition: lixa_errors.h:122
int xta_xa_resource_init(xta_xa_resource_t *xa_resource, int native)
#define LIXA_RC_OK
Definition: lixa_errors.h:115
void xta_native_xa_resource_clean(xta_native_xa_resource_t *xa_resource)
void xta_transaction_manager_config_t
xta_native_xa_resource_t * xta_native_xa_resource_new_by_rmid(int rmid, const xta_transaction_manager_config_t *config)
#define LIXA_RC_G_TRY_MALLOC_ERROR
Definition: lixa_errors.h:563
xta_native_xa_resource_t * xta_native_xa_resource_new(const char *name, const char *switch_file, const char *open_info, const char *close_info)
#define MAXINFOSIZE
Definition: xa.h:67
struct act_rsrmgr_config_s act_rsrmgr_config
void xta_xa_resource_clean(xta_xa_resource_t *xa_resource)
#define LIXA_RC_INVALID_OPTION
Definition: lixa_errors.h:163
struct rsrmgr_config_s rsrmgr_config
#define LIXA_RC_OUT_OF_RANGE
Definition: lixa_errors.h:126
void xta_native_xa_resource_delete(xta_native_xa_resource_t *xa_resource)

Copyright © 2009-2019, Christian Ferrari tiian@users.sourceforge.net http://www.tiian.org/