3936565a1134a78dd44fc565c54c6f99939eb4ef
[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 <utils/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 /**
45 * Implementation of database_factory_t.create.
46 */
47 static database_t* create(private_database_factory_t *this, char *uri)
48 {
49 enumerator_t *enumerator;
50 database_t *database = NULL;
51 database_constructor_t create;
52
53 this->mutex->lock(this->mutex);
54 enumerator = this->databases->create_enumerator(this->databases);
55 while (enumerator->enumerate(enumerator, &create))
56 {
57 database = create(uri);
58 if (database)
59 {
60 break;
61 }
62 }
63 enumerator->destroy(enumerator);
64 this->mutex->unlock(this->mutex);
65 return database;
66 }
67
68 /**
69 * Implementation of database_factory_t.add_database.
70 */
71 static void add_database(private_database_factory_t *this,
72 database_constructor_t create)
73 {
74 this->mutex->lock(this->mutex);
75 this->databases->insert_last(this->databases, create);
76 this->mutex->unlock(this->mutex);
77 }
78
79 /**
80 * Implementation of database_factory_t.remove_database.
81 */
82 static void remove_database(private_database_factory_t *this,
83 database_constructor_t create)
84 {
85 this->mutex->lock(this->mutex);
86 this->databases->remove(this->databases, create, NULL);
87 this->mutex->unlock(this->mutex);
88 }
89
90 /**
91 * Implementation of database_factory_t.destroy
92 */
93 static void destroy(private_database_factory_t *this)
94 {
95 this->databases->destroy(this->databases);
96 this->mutex->destroy(this->mutex);
97 free(this);
98 }
99
100 /*
101 * see header file
102 */
103 database_factory_t *database_factory_create()
104 {
105 private_database_factory_t *this = malloc_thing(private_database_factory_t);
106
107 this->public.create = (database_t*(*)(database_factory_t*, char *url))create;
108 this->public.add_database = (void(*)(database_factory_t*, database_constructor_t))add_database;
109 this->public.remove_database = (void(*)(database_factory_t*, database_constructor_t))remove_database;
110 this->public.destroy = (void(*)(database_factory_t*))destroy;
111
112 this->databases = linked_list_create();
113 this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
114
115 return &this->public;
116 }
117