merged the modularization branch (credentials) back to trunk
[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 * $Id$
16 */
17
18 #include "database_factory.h"
19
20 #include <utils/linked_list.h>
21 #include <utils/mutex.h>
22
23 typedef struct private_database_factory_t private_database_factory_t;
24
25 /**
26 * private data of database_factory
27 */
28 struct private_database_factory_t {
29
30 /**
31 * public functions
32 */
33 database_factory_t public;
34
35 /**
36 * list of registered database_t implementations
37 */
38 linked_list_t *databases;
39
40 /**
41 * mutex to lock access to databases
42 */
43 mutex_t *mutex;
44 };
45
46 /**
47 * Implementation of database_factory_t.create.
48 */
49 static database_t* create(private_database_factory_t *this, char *uri)
50 {
51 enumerator_t *enumerator;
52 database_t *database = NULL;
53 database_constructor_t create;
54
55 this->mutex->lock(this->mutex);
56 enumerator = this->databases->create_enumerator(this->databases);
57 while (enumerator->enumerate(enumerator, &create))
58 {
59 database = create(uri);
60 if (database)
61 {
62 break;
63 }
64 }
65 enumerator->destroy(enumerator);
66 this->mutex->unlock(this->mutex);
67 return database;
68 }
69
70 /**
71 * Implementation of database_factory_t.add_database.
72 */
73 static void add_database(private_database_factory_t *this,
74 database_constructor_t create)
75 {
76 this->mutex->lock(this->mutex);
77 this->databases->insert_last(this->databases, create);
78 this->mutex->unlock(this->mutex);
79 }
80
81 /**
82 * Implementation of database_factory_t.remove_database.
83 */
84 static void remove_database(private_database_factory_t *this,
85 database_constructor_t create)
86 {
87 this->mutex->lock(this->mutex);
88 this->databases->remove(this->databases, create, NULL);
89 this->mutex->unlock(this->mutex);
90 }
91
92 /**
93 * Implementation of database_factory_t.destroy
94 */
95 static void destroy(private_database_factory_t *this)
96 {
97 this->databases->destroy(this->databases);
98 this->mutex->destroy(this->mutex);
99 free(this);
100 }
101
102 /*
103 * see header file
104 */
105 database_factory_t *database_factory_create()
106 {
107 private_database_factory_t *this = malloc_thing(private_database_factory_t);
108
109 this->public.create = (database_t*(*)(database_factory_t*, char *url))create;
110 this->public.add_database = (void(*)(database_factory_t*, database_constructor_t))add_database;
111 this->public.remove_database = (void(*)(database_factory_t*, database_constructor_t))remove_database;
112 this->public.destroy = (void(*)(database_factory_t*))destroy;
113
114 this->databases = linked_list_create();
115 this->mutex = mutex_create(MUTEX_DEFAULT);
116
117 return &this->public;
118 }
119