bes  Updated for version 3.20.8
BESContextManager.cc
1 // BESContextManager.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "config.h"
34 
35 #include <cstdlib>
36 #include <cerrno>
37 #include <cstring>
38 
39 #include "BESContextManager.h"
40 #include "BESInfo.h"
41 #include "BESDebug.h"
42 
43 using std::endl;
44 using std::string;
45 using std::ostream;
46 
47 BESContextManager *BESContextManager::_instance = 0;
48 
49 #define MODULE "context"
50 #define prolog std::string("BESContextManager::").append(__func__).append("() - ")
51 
57 void BESContextManager::set_context(const string &name, const string &value)
58 {
59  BESDEBUG(MODULE, prolog << "name=\"" << name << "\", value=\"" << value << "\"" << endl);
60  _context_list[name] = value;
61 }
62 
68 void BESContextManager::unset_context(const string &name)
69 {
70  BESDEBUG(MODULE, prolog << "name=\"" << name << "\"" << endl);
71  _context_list.erase(name);
72 }
73 
83 string BESContextManager::get_context(const string &name, bool &found)
84 {
85 #if 1
86  string ret = "";
87  found = false;
88  BESContextManager::Context_iter i;
89  i = _context_list.find(name);
90  if (i != _context_list.end()) {
91  ret = (*i).second;
92  found = true;
93  }
94  BESDEBUG(MODULE, prolog << "name=\"" << name << "\", found=\"" << found << "\" value:\"" << ret << "\"" << endl);
95 #else
96 
97  string ret = _context_list[name];
98  BESDEBUG(MODULE, prolog << "name=\"" << name << "\" value: \"" << ret << "\"" << endl);
99 
100 #endif
101 
102  return ret;
103 }
104 
114 int BESContextManager::get_context_int(const string &name, bool &found)
115 {
116  string value = BESContextManager::TheManager()->get_context(name, found);
117  if (!found || value.empty()) return 0;
118 
119  char *endptr;
120  errno = 0;
121  int val = strtol(value.c_str(), &endptr, /*int base*/10);
122  if (val == 0 && errno > 0) {
123  throw BESInternalError(string("Error reading an integer value for the context '") + name + "': " + strerror(errno),
124  __FILE__, __LINE__);
125  }
126  BESDEBUG(MODULE, prolog << "name=\"" << name << "\", found=\"" << found << "\" value: \"" << val << "\"" << endl);
127 
128  return val;
129 }
130 
135 {
136  string name;
137  string value;
138  std::map<string, string> props;
139  BESContextManager::Context_citer i = _context_list.begin();
140  BESContextManager::Context_citer e = _context_list.end();
141  for (; i != e; i++) {
142  props.clear();
143  name = (*i).first;
144  value = (*i).second;
145  props["name"] = name;
146  info.add_tag("context", value, &props);
147  }
148 }
149 
157 void BESContextManager::dump(ostream &strm) const
158 {
159  strm << BESIndent::LMarg << prolog << "(this: " << (void *) this << ")" << endl;
160  BESIndent::Indent();
161  if (_context_list.size()) {
162  strm << BESIndent::LMarg << "current context:" << endl;
163  BESIndent::Indent();
164  BESContextManager::Context_citer i = _context_list.begin();
165  BESContextManager::Context_citer ie = _context_list.end();
166  for (; i != ie; i++) {
167  strm << BESIndent::LMarg << (*i).first << ": " << (*i).second << endl;
168  }
169  BESIndent::UnIndent();
170  }
171  else {
172  strm << BESIndent::LMarg << "no context" << endl;
173  }
174  BESIndent::UnIndent();
175 }
176 
178 BESContextManager::TheManager()
179 {
180  if (_instance == 0) {
181  _instance = new BESContextManager;
182  }
183  return _instance;
184 }
185 
maintains the list of registered request handlers for this server
virtual void list_context(BESInfo &info)
Adds all context and their values to the given informational object.
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual int get_context_int(const std::string &name, bool &found)
Get the value of the given context and return it as an integer.
virtual void set_context(const std::string &name, const std::string &value)
set context in the BES
virtual std::string get_context(const std::string &name, bool &found)
retrieve the value of the specified context from the BES
virtual void unset_context(const std::string &name)
set context in the BES
informational response object
Definition: BESInfo.h:63
exception thrown if internal error encountered