bes  Updated for version 3.20.6
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 
42 using std::endl;
43 using std::string;
44 using std::ostream;
45 
46 BESContextManager *BESContextManager::_instance = 0;
47 
53 void BESContextManager::set_context(const string &name, const string &value)
54 {
55  _context_list[name] = value;
56 }
57 
63 void BESContextManager::unset_context(const string &name)
64 {
65  _context_list.erase(name);
66 }
67 
77 string BESContextManager::get_context(const string &name, bool &found)
78 {
79  string ret = "";
80  found = false;
81  BESContextManager::Context_iter i;
82  i = _context_list.find(name);
83  if (i != _context_list.end()) {
84  ret = (*i).second;
85  found = true;
86  }
87  return ret;
88 }
89 
99 int BESContextManager::get_context_int(const string &name, bool &found)
100 {
101  string value = BESContextManager::TheManager()->get_context(name, found);
102  if (!found || value.empty()) return 0;
103 
104  char *endptr;
105  errno = 0;
106  int val = strtol(value.c_str(), &endptr, /*int base*/10);
107  if (val == 0 && errno > 0) {
108  throw BESInternalError(string("Error reading an integer value for the context '") + name + "': " + strerror(errno),
109  __FILE__, __LINE__);
110  }
111 
112  return val;
113 }
114 
119 {
120  string name;
121  string value;
122  std::map<string, string> props;
123  BESContextManager::Context_citer i = _context_list.begin();
124  BESContextManager::Context_citer e = _context_list.end();
125  for (; i != e; i++) {
126  props.clear();
127  name = (*i).first;
128  value = (*i).second;
129  props["name"] = name;
130  info.add_tag("context", value, &props);
131  }
132 }
133 
141 void BESContextManager::dump(ostream &strm) const
142 {
143  strm << BESIndent::LMarg << "BESContextManager::dump - (" << (void *) this << ")" << endl;
144  BESIndent::Indent();
145  if (_context_list.size()) {
146  strm << BESIndent::LMarg << "current context:" << endl;
147  BESIndent::Indent();
148  BESContextManager::Context_citer i = _context_list.begin();
149  BESContextManager::Context_citer ie = _context_list.end();
150  for (; i != ie; i++) {
151  strm << BESIndent::LMarg << (*i).first << ": " << (*i).second << endl;
152  }
153  BESIndent::UnIndent();
154  }
155  else {
156  strm << BESIndent::LMarg << "no context" << endl;
157  }
158  BESIndent::UnIndent();
159 }
160 
162 BESContextManager::TheManager()
163 {
164  if (_instance == 0) {
165  _instance = new BESContextManager;
166  }
167  return _instance;
168 }
169 
BESInfo
informational response object
Definition: BESInfo.h:63
BESContextManager::list_context
virtual void list_context(BESInfo &info)
Adds all context and their values to the given informational object.
Definition: BESContextManager.cc:118
BESContextManager::set_context
virtual void set_context(const std::string &name, const std::string &value)
set context in the BES
Definition: BESContextManager.cc:53
BESContextManager
maintains the list of registered request handlers for this server
Definition: BESContextManager.h:50
BESContextManager::get_context_int
virtual int get_context_int(const std::string &name, bool &found)
Get the value of the given context and return it as an integer.
Definition: BESContextManager.cc:99
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
BESContextManager::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESContextManager.cc:141
BESContextManager::get_context
virtual std::string get_context(const std::string &name, bool &found)
retrieve the value of the specified context from the BES
Definition: BESContextManager.cc:77
BESContextManager::unset_context
virtual void unset_context(const std::string &name)
set context in the BES
Definition: BESContextManager.cc:63