Migrated sql_plugin_t to INIT/METHOD macros
[strongswan.git] / src / libcharon / plugins / sql / sql_plugin.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "sql_plugin.h"
17
18 #include <daemon.h>
19 #include "sql_config.h"
20 #include "sql_cred.h"
21 #include "sql_logger.h"
22
23 typedef struct private_sql_plugin_t private_sql_plugin_t;
24
25 /**
26 * private data of sql plugin
27 */
28 struct private_sql_plugin_t {
29
30 /**
31 * implements plugin interface
32 */
33 sql_plugin_t public;
34
35 /**
36 * database connection instance
37 */
38 database_t *db;
39
40 /**
41 * configuration backend
42 */
43 sql_config_t *config;
44
45 /**
46 * credential set
47 */
48 sql_cred_t *cred;
49
50 /**
51 * bus listener/logger
52 */
53 sql_logger_t *logger;
54 };
55
56 METHOD(plugin_t, destroy, void,
57 private_sql_plugin_t *this)
58 {
59 charon->backends->remove_backend(charon->backends, &this->config->backend);
60 lib->credmgr->remove_set(lib->credmgr, &this->cred->set);
61 charon->bus->remove_listener(charon->bus, &this->logger->listener);
62 this->config->destroy(this->config);
63 this->cred->destroy(this->cred);
64 this->logger->destroy(this->logger);
65 this->db->destroy(this->db);
66 free(this);
67 }
68
69 /*
70 * see header file
71 */
72 plugin_t *sql_plugin_create()
73 {
74 char *uri;
75 private_sql_plugin_t *this;
76
77 uri = lib->settings->get_str(lib->settings, "charon.plugins.sql.database", NULL);
78 if (!uri)
79 {
80 DBG1(DBG_CFG, "sql plugin: database URI not set");
81 return NULL;
82 }
83
84 INIT(this,
85 .public = {
86 .plugin = {
87 .destroy = _destroy,
88 },
89 },
90 .db = lib->db->create(lib->db, uri),
91 );
92
93 if (!this->db)
94 {
95 DBG1(DBG_CFG, "sql plugin failed to connect to database");
96 free(this);
97 return NULL;
98 }
99 this->config = sql_config_create(this->db);
100 this->cred = sql_cred_create(this->db);
101 this->logger = sql_logger_create(this->db);
102
103 charon->backends->add_backend(charon->backends, &this->config->backend);
104 lib->credmgr->add_set(lib->credmgr, &this->cred->set);
105 charon->bus->add_listener(charon->bus, &this->logger->listener);
106
107 return &this->public.plugin;
108 }
109