1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #include <anna/core/util/Environment.hpp>
39 #include <anna/config/defines.hpp>
40 #include <anna/core/tracing/Logger.hpp>
41 #include <anna/core/tracing/TraceMethod.hpp>
43 #include <boost/program_options.hpp>
44 #include <boost/algorithm/string.hpp>
56 void Environment::initialize() throw() {
57 LOGMETHOD(TraceMethod tm("Environment", "initialize", ANNA_FILE_LOCATION));
60 a_managedVars.clear();
63 namespace po = boost::program_options;
64 po::options_description desc("Options");
70 /* Open the command for reading. */
71 fp = popen("env | cut -d'=' -f1", "r");
73 Logger::error("Failed to get environment variables list", ANNA_FILE_LOCATION);
77 /* Read the output a line at a time - output it. */
78 while (fgets(c_var, sizeof(c_var)-1, fp) != NULL) {
81 desc.add_options()(var.c_str(), var.c_str());
82 a_managedVars[var] = ""; // temporary
91 po::store(po::parse_environment(desc, [](const std::string& variable) { return variable; }), vm); // can throw
93 std::map<std::string, std::string>::const_iterator it;
95 for (it = a_managedVars.begin(); it != a_managedVars.end(); it++) {
97 if (vm.count(var.c_str())) { // protection
98 val = vm[var.c_str()].as<std::string>();
99 a_managedVars[var] = val;
105 } catch (po::error& e) {
106 Logger::error(e.what(), ANNA_FILE_LOCATION);
110 std::string Environment::getValue (const char* variableName, bool exceptionIfMissing) const throw(RuntimeException) {
111 std::string result = "";
114 throw RuntimeException("Invalid NULL variable name!", ANNA_FILE_LOCATION);
116 std::map<std::string, std::string>::const_iterator it = a_managedVars.find(variableName);
117 if (it == a_managedVars.end()) {
118 std::string msg = "The variable '";
120 msg += "' is not defined in the environment.";
121 LOGDEBUG(Logger::debug(msg, ANNA_FILE_LOCATION));
122 if (exceptionIfMissing)
123 throw RuntimeException(msg, ANNA_FILE_LOCATION);