bes  Updated for version 3.20.5
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 BESContextManager *BESContextManager::_instance = 0;
43 
49 void BESContextManager::set_context(const string &name, const string &value)
50 {
51  _context_list[name] = value;
52 }
53 
59 void BESContextManager::unset_context(const string &name)
60 {
61  _context_list.erase(name);
62 }
63 
73 string BESContextManager::get_context(const string &name, bool &found)
74 {
75  string ret = "";
76  found = false;
77  BESContextManager::Context_iter i;
78  i = _context_list.find(name);
79  if (i != _context_list.end()) {
80  ret = (*i).second;
81  found = true;
82  }
83  return ret;
84 }
85 
95 int BESContextManager::get_context_int(const string &name, bool &found)
96 {
97  string value = BESContextManager::TheManager()->get_context(name, found);
98  if (!found || value.empty()) return 0;
99 
100  char *endptr;
101  errno = 0;
102  int val = strtol(value.c_str(), &endptr, /*int base*/10);
103  if (val == 0 && errno > 0) {
104  throw BESInternalError(string("Error reading an integer value for the context '") + name + "': " + strerror(errno),
105  __FILE__, __LINE__);
106  }
107 
108  return val;
109 }
110 
115 {
116  string name;
117  string value;
118  map<string, string> props;
119  BESContextManager::Context_citer i = _context_list.begin();
120  BESContextManager::Context_citer e = _context_list.end();
121  for (; i != e; i++) {
122  props.clear();
123  name = (*i).first;
124  value = (*i).second;
125  props["name"] = name;
126  info.add_tag("context", value, &props);
127  }
128 }
129 
137 void BESContextManager::dump(ostream &strm) const
138 {
139  strm << BESIndent::LMarg << "BESContextManager::dump - (" << (void *) this << ")" << endl;
140  BESIndent::Indent();
141  if (_context_list.size()) {
142  strm << BESIndent::LMarg << "current context:" << endl;
143  BESIndent::Indent();
144  BESContextManager::Context_citer i = _context_list.begin();
145  BESContextManager::Context_citer ie = _context_list.end();
146  for (; i != ie; i++) {
147  strm << BESIndent::LMarg << (*i).first << ": " << (*i).second << endl;
148  }
149  BESIndent::UnIndent();
150  }
151  else {
152  strm << BESIndent::LMarg << "no context" << endl;
153  }
154  BESIndent::UnIndent();
155 }
156 
158 BESContextManager::TheManager()
159 {
160  if (_instance == 0) {
161  _instance = new BESContextManager;
162  }
163  return _instance;
164 }
165 
BESContextManager::set_context
virtual void set_context(const string &name, const string &value)
set context in the BES
Definition: BESContextManager.cc:49
BESContextManager::get_context
virtual string get_context(const string &name, bool &found)
retrieve the value of the specified context from the BES
Definition: BESContextManager.cc:73
BESInfo
informational response object
Definition: BESInfo.h:68
BESContextManager::get_context_int
virtual int get_context_int(const string &name, bool &found)
Get the value of the given context and return it as an integer.
Definition: BESContextManager.cc:95
BESContextManager::list_context
virtual void list_context(BESInfo &info)
Adds all context and their values to the given informational object.
Definition: BESContextManager.cc:114
BESContextManager
maintains the list of registered request handlers for this server
Definition: BESContextManager.h:53
BESInternalError
exception thrown if inernal error encountered
Definition: BESInternalError.h:43
BESContextManager::unset_context
virtual void unset_context(const string &name)
set context in the BES
Definition: BESContextManager.cc:59
BESContextManager::dump
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESContextManager.cc:137