Moved data structures to new collections subfolder
[strongswan.git] / src / libstrongswan / database / database_factory.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 "database_factory.h"
17
18 #include <collections/linked_list.h>
19 #include <threading/mutex.h>
20
21 typedef struct private_database_factory_t private_database_factory_t;
22
23 /**
24 * private data of database_factory
25 */
26 struct private_database_factory_t {
27
28 /**
29 * public functions
30 */
31 database_factory_t public;
32
33 /**
34 * list of registered database_t implementations
35 */
36 linked_list_t *databases;
37
38 /**
39 * mutex to lock access to databases
40 */
41 mutex_t *mutex;
42 };
43
44 METHOD(database_factory_t, create, database_t*,
45 private_database_factory_t *this, char *uri)
46 {
47 enumerator_t *enumerator;
48 database_t *database = NULL;
49 database_constructor_t create;
50
51 this->mutex->lock(this->mutex);
52 enumerator = this->databases->create_enumerator(this->databases);
53 while (enumerator->enumerate(enumerator, &create))
54 {
55 database = create(uri);
56 if (database)
57 {
58 break;
59 }
60 }
61 enumerator->destroy(enumerator);
62 this->mutex->unlock(this->mutex);
63 return database;
64 }
65
66 METHOD(database_factory_t, add_database, void,
67 private_database_factory_t *this, database_constructor_t create)
68 {
69 this->mutex->lock(this->mutex);
70 this->databases->insert_last(this->databases, create);
71 this->mutex->unlock(this->mutex);
72 }
73
74 METHOD(database_factory_t, remove_database, void,
75 private_database_factory_t *this, database_constructor_t create)
76 {
77 this->mutex->lock(this->mutex);
78 this->databases->remove(this->databases, create, NULL);
79 this->mutex->unlock(this->mutex);
80 }
81
82 METHOD(database_factory_t, destroy, void,
83 private_database_factory_t *this)
84 {
85 this->databases->destroy(this->databases);
86 this->mutex->destroy(this->mutex);
87 free(this);
88 }
89
90 /*
91 * see header file
92 */
93 database_factory_t *database_factory_create()
94 {
95 private_database_factory_t *this;
96
97 INIT(this,
98 .public = {
99 .create = _create,
100 .add_database = _add_database,
101 .remove_database = _remove_database,
102 .destroy = _destroy,
103 },
104 .databases = linked_list_create(),
105 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
106 );
107
108 return &this->public;
109 }
110