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));
59 a_managedVars.clear();
61 namespace po = boost::program_options;
62 po::options_description desc("Options");
66 /* Open the command for reading. */
67 fp = popen("env | cut -d'=' -f1", "r");
70 Logger::error("Failed to get environment variables list", ANNA_FILE_LOCATION);
74 /* Read the output a line at a time - output it. */
75 while(fgets(c_var, sizeof(c_var) - 1, fp) != NULL) {
78 desc.add_options()(var.c_str(), var.c_str());
79 a_managedVars[var] = ""; // temporary
88 po::store(po::parse_environment(desc, [](const std::string & variable) { return variable; }), vm); // can throw
89 std::map<std::string, std::string>::const_iterator it;
92 for(it = a_managedVars.begin(); it != a_managedVars.end(); it++) {
95 if(vm.count(var.c_str())) { // protection
96 val = vm[var.c_str()].as<std::string>();
97 a_managedVars[var] = val;
102 } catch(po::error& e) {
103 Logger::error(e.what(), ANNA_FILE_LOCATION);
107 std::string Environment::getValue(const char* variableName, bool exceptionIfMissing) const throw(RuntimeException) {
108 std::string result = "";
111 throw RuntimeException("Invalid NULL variable name!", ANNA_FILE_LOCATION);
113 std::map<std::string, std::string>::const_iterator it = a_managedVars.find(variableName);
115 if(it == a_managedVars.end()) {
116 std::string msg = "The variable '";
118 msg += "' is not defined in the environment.";
119 LOGDEBUG(Logger::debug(msg, ANNA_FILE_LOCATION));
121 if(exceptionIfMissing) throw RuntimeException(msg, ANNA_FILE_LOCATION);