/src/strongswan/src/libstrongswan/database/database_factory.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2008 Martin Willi |
3 | | * |
4 | | * Copyright (C) secunet Security Networks AG |
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. |
15 | | */ |
16 | | |
17 | | #include "database_factory.h" |
18 | | |
19 | | #include <collections/linked_list.h> |
20 | | #include <threading/mutex.h> |
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; |
33 | | |
34 | | /** |
35 | | * list of registered database_t implementations |
36 | | */ |
37 | | linked_list_t *databases; |
38 | | |
39 | | /** |
40 | | * mutex to lock access to databases |
41 | | */ |
42 | | mutex_t *mutex; |
43 | | }; |
44 | | |
45 | | METHOD(database_factory_t, create, database_t*, |
46 | | private_database_factory_t *this, char *uri) |
47 | 0 | { |
48 | 0 | enumerator_t *enumerator; |
49 | 0 | database_t *database = NULL; |
50 | 0 | database_constructor_t create; |
51 | |
|
52 | 0 | this->mutex->lock(this->mutex); |
53 | 0 | enumerator = this->databases->create_enumerator(this->databases); |
54 | 0 | while (enumerator->enumerate(enumerator, &create)) |
55 | 0 | { |
56 | 0 | database = create(uri); |
57 | 0 | if (database) |
58 | 0 | { |
59 | 0 | break; |
60 | 0 | } |
61 | 0 | } |
62 | 0 | enumerator->destroy(enumerator); |
63 | 0 | this->mutex->unlock(this->mutex); |
64 | 0 | return database; |
65 | 0 | } |
66 | | |
67 | | METHOD(database_factory_t, add_database, void, |
68 | | private_database_factory_t *this, database_constructor_t create) |
69 | 0 | { |
70 | 0 | this->mutex->lock(this->mutex); |
71 | 0 | this->databases->insert_last(this->databases, create); |
72 | 0 | this->mutex->unlock(this->mutex); |
73 | 0 | } |
74 | | |
75 | | METHOD(database_factory_t, remove_database, void, |
76 | | private_database_factory_t *this, database_constructor_t create) |
77 | 0 | { |
78 | 0 | this->mutex->lock(this->mutex); |
79 | 0 | this->databases->remove(this->databases, create, NULL); |
80 | 0 | this->mutex->unlock(this->mutex); |
81 | 0 | } |
82 | | |
83 | | METHOD(database_factory_t, destroy, void, |
84 | | private_database_factory_t *this) |
85 | 3.92k | { |
86 | 3.92k | this->databases->destroy(this->databases); |
87 | 3.92k | this->mutex->destroy(this->mutex); |
88 | 3.92k | free(this); |
89 | 3.92k | } |
90 | | |
91 | | /* |
92 | | * see header file |
93 | | */ |
94 | | database_factory_t *database_factory_create() |
95 | 3.92k | { |
96 | 3.92k | private_database_factory_t *this; |
97 | | |
98 | 3.92k | INIT(this, |
99 | 3.92k | .public = { |
100 | 3.92k | .create = _create, |
101 | 3.92k | .add_database = _add_database, |
102 | 3.92k | .remove_database = _remove_database, |
103 | 3.92k | .destroy = _destroy, |
104 | 3.92k | }, |
105 | 3.92k | .databases = linked_list_create(), |
106 | 3.92k | .mutex = mutex_create(MUTEX_TYPE_DEFAULT), |
107 | 3.92k | ); |
108 | | |
109 | 3.92k | return &this->public; |
110 | 3.92k | } |
111 | | |