]>
Commit | Line | Data |
---|---|---|
552cc11b MW |
1 | /* |
2 | * Copyright (C) 2008 Martin Willi | |
19ef2aec TB |
3 | * |
4 | * Copyright (C) secunet Security Networks AG | |
552cc11b MW |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2 of the License, or (at your | |
9 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * for more details. | |
552cc11b MW |
15 | */ |
16 | ||
17 | #include "database_factory.h" | |
18 | ||
12642a68 | 19 | #include <collections/linked_list.h> |
eba64cef | 20 | #include <threading/mutex.h> |
552cc11b MW |
21 | |
22 | typedef struct private_database_factory_t private_database_factory_t; | |
23 | ||
24 | /** | |
25 | * private data of database_factory | |
26 | */ | |
27 | struct private_database_factory_t { | |
28 | ||
29 | /** | |
30 | * public functions | |
31 | */ | |
32 | database_factory_t public; | |
7daf5226 | 33 | |
552cc11b MW |
34 | /** |
35 | * list of registered database_t implementations | |
36 | */ | |
37 | linked_list_t *databases; | |
7daf5226 | 38 | |
552cc11b MW |
39 | /** |
40 | * mutex to lock access to databases | |
41 | */ | |
42 | mutex_t *mutex; | |
43 | }; | |
44 | ||
b59fd53f AS |
45 | METHOD(database_factory_t, create, database_t*, |
46 | private_database_factory_t *this, char *uri) | |
552cc11b MW |
47 | { |
48 | enumerator_t *enumerator; | |
49 | database_t *database = NULL; | |
50 | database_constructor_t create; | |
7daf5226 | 51 | |
552cc11b MW |
52 | this->mutex->lock(this->mutex); |
53 | enumerator = this->databases->create_enumerator(this->databases); | |
54 | while (enumerator->enumerate(enumerator, &create)) | |
55 | { | |
56 | database = create(uri); | |
57 | if (database) | |
58 | { | |
59 | break; | |
60 | } | |
61 | } | |
62 | enumerator->destroy(enumerator); | |
63 | this->mutex->unlock(this->mutex); | |
64 | return database; | |
65 | } | |
66 | ||
b59fd53f AS |
67 | METHOD(database_factory_t, add_database, void, |
68 | private_database_factory_t *this, database_constructor_t create) | |
552cc11b MW |
69 | { |
70 | this->mutex->lock(this->mutex); | |
71 | this->databases->insert_last(this->databases, create); | |
72 | this->mutex->unlock(this->mutex); | |
73 | } | |
74 | ||
b59fd53f AS |
75 | METHOD(database_factory_t, remove_database, void, |
76 | private_database_factory_t *this, database_constructor_t create) | |
552cc11b MW |
77 | { |
78 | this->mutex->lock(this->mutex); | |
79 | this->databases->remove(this->databases, create, NULL); | |
80 | this->mutex->unlock(this->mutex); | |
81 | } | |
82 | ||
b59fd53f AS |
83 | METHOD(database_factory_t, destroy, void, |
84 | private_database_factory_t *this) | |
552cc11b MW |
85 | { |
86 | this->databases->destroy(this->databases); | |
87 | this->mutex->destroy(this->mutex); | |
88 | free(this); | |
89 | } | |
90 | ||
91 | /* | |
92 | * see header file | |
93 | */ | |
94 | database_factory_t *database_factory_create() | |
95 | { | |
b59fd53f AS |
96 | private_database_factory_t *this; |
97 | ||
98 | INIT(this, | |
99 | .public = { | |
100 | .create = _create, | |
101 | .add_database = _add_database, | |
102 | .remove_database = _remove_database, | |
103 | .destroy = _destroy, | |
104 | }, | |
105 | .databases = linked_list_create(), | |
106 | .mutex = mutex_create(MUTEX_TYPE_DEFAULT), | |
107 | ); | |
7daf5226 | 108 | |
552cc11b MW |
109 | return &this->public; |
110 | } | |
111 |