| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
Configuration.cpp |
| 29 |
|
| 30 |
Loads the configuration and supports retrieval of configuration information |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <string> |
| 34 |
#include <map> |
| 35 |
#include "Configuration.h" |
| 36 |
#include "log.h" |
| 37 |
|
| 38 |
// Initialise the one-and-only instance |
| 39 |
#ifdef HAVE_CXX11 |
| 40 |
std::unique_ptr<Configuration> Configuration::instance(nullptr); |
| 41 |
#else |
| 42 |
std::auto_ptr<Configuration> Configuration::instance(NULL); |
| 43 |
#endif |
| 44 |
|
| 45 |
// Add all valid configurations |
| 46 |
const struct config Configuration::valid_config[] = { |
| 47 |
{ "directories.tokendir", CONFIG_TYPE_STRING }, |
| 48 |
{ "objectstore.backend", CONFIG_TYPE_STRING }, |
| 49 |
{ "log.level", CONFIG_TYPE_STRING }, |
| 50 |
{ "slots.removable", CONFIG_TYPE_BOOL }, |
| 51 |
{ "slots.mechanisms", CONFIG_TYPE_STRING }, |
| 52 |
{ "library.reset_on_fork", CONFIG_TYPE_BOOL }, |
| 53 |
{ "", CONFIG_TYPE_UNSUPPORTED } |
| 54 |
}; |
| 55 |
|
| 56 |
// Return the one-and-only instance |
| 57 |
Configuration* Configuration::i() |
| 58 |
{ |
| 59 |
if (instance.get() == NULL) |
| 60 |
{ |
| 61 |
instance.reset(new Configuration()); |
| 62 |
} |
| 63 |
|
| 64 |
return instance.get(); |
| 65 |
} |
| 66 |
|
| 67 |
// Constructor |
| 68 |
Configuration::Configuration() |
| 69 |
{ |
| 70 |
configLoader = NULL; |
| 71 |
} |
| 72 |
|
| 73 |
// Get the type of the configuration value |
| 74 |
int Configuration::getType(std::string key) |
| 75 |
{ |
| 76 |
for (int i = 0; valid_config[i].key.compare("") != 0; i++) |
| 77 |
{ |
| 78 |
if (valid_config[i].key.compare(key) == 0) |
| 79 |
{ |
| 80 |
return valid_config[i].type; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return CONFIG_TYPE_UNSUPPORTED; |
| 85 |
} |
| 86 |
|
| 87 |
// Retrieve a string based configuration value |
| 88 |
std::string Configuration::getString(std::string key, std::string ifEmpty /* = "" */) |
| 89 |
{ |
| 90 |
if (stringConfiguration.find(key) != stringConfiguration.end()) |
| 91 |
{ |
| 92 |
return stringConfiguration[key]; |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
WARNING_MSG("Missing %s in configuration. Using default value: %s", key.c_str(), ifEmpty.c_str()); |
| 97 |
return ifEmpty; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Retrieve an integer configuration value |
| 102 |
int Configuration::getInt(std::string key, int ifEmpty /* = 0 */) |
| 103 |
{ |
| 104 |
if (integerConfiguration.find(key) != integerConfiguration.end()) |
| 105 |
{ |
| 106 |
return integerConfiguration[key]; |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
WARNING_MSG("Missing %s in configuration. Using default value: %i", key.c_str(), ifEmpty); |
| 111 |
return ifEmpty; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// Retrieve a boolean configuration value |
| 116 |
bool Configuration::getBool(std::string key, bool ifEmpty /* = false */) |
| 117 |
{ |
| 118 |
if (booleanConfiguration.find(key) != booleanConfiguration.end()) |
| 119 |
{ |
| 120 |
return booleanConfiguration[key]; |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
WARNING_MSG("Missing %s in configuration. Using default value: %s", key.c_str(), ifEmpty ? "true" : "false"); |
| 125 |
return ifEmpty; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// Set a string based configuration value |
| 130 |
void Configuration::setString(std::string key, std::string value) |
| 131 |
{ |
| 132 |
stringConfiguration[key] = value; |
| 133 |
} |
| 134 |
|
| 135 |
// Set an integer based configuration value |
| 136 |
void Configuration::setInt(std::string key, int value) |
| 137 |
{ |
| 138 |
integerConfiguration[key] = value; |
| 139 |
} |
| 140 |
|
| 141 |
// Set a boolean configuration value |
| 142 |
void Configuration::setBool(std::string key, bool value) |
| 143 |
{ |
| 144 |
booleanConfiguration[key] = value; |
| 145 |
} |
| 146 |
|
| 147 |
// Reload the configuration |
| 148 |
bool Configuration::reload() |
| 149 |
{ |
| 150 |
if (configLoader == NULL) |
| 151 |
{ |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
// Discard the current configuration |
| 156 |
stringConfiguration.clear(); |
| 157 |
integerConfiguration.clear(); |
| 158 |
booleanConfiguration.clear(); |
| 159 |
|
| 160 |
// Reload the configuration |
| 161 |
if (!configLoader->loadConfiguration()) |
| 162 |
{ |
| 163 |
ERROR_MSG("Failed to load the SoftHSM configuration"); |
| 164 |
|
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
// Reload the configuration using the specified configuration loader |
| 172 |
bool Configuration::reload(ConfigLoader* inConfigLoader) |
| 173 |
{ |
| 174 |
configLoader = inConfigLoader; |
| 175 |
|
| 176 |
return reload(); |
| 177 |
} |
| 178 |
|