bes  Updated for version 3.20.10
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 #include <mutex>
39 
40 #include "BESContextManager.h"
41 #include "BESInfo.h"
42 #include "BESDebug.h"
43 
44 using std::endl;
45 using std::string;
46 using std::ostream;
47 
48 BESContextManager *BESContextManager::d_instance = nullptr;
49 static std::once_flag d_euc_init_once;
50 
51 #define MODULE "context"
52 #define prolog std::string("BESContextManager::").append(__func__).append("() - ")
53 
54 BESContextManager::BESContextManager() {}
55 
56 BESContextManager::~BESContextManager() {}
57 
63 void BESContextManager::set_context(const string &name, const string &value)
64 {
65  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
66 
67  BESDEBUG(MODULE, prolog << "name=\"" << name << "\", value=\"" << value << "\"" << endl);
68  _context_list[name] = value;
69 }
70 
76 void BESContextManager::unset_context(const string &name)
77 {
78  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
79 
80  BESDEBUG(MODULE, prolog << "name=\"" << name << "\"" << endl);
81  _context_list.erase(name);
82 }
83 
93 string BESContextManager::get_context(const string &name, bool &found)
94 {
95  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
96 #if 1
97  string ret = "";
98  found = false;
99  BESContextManager::Context_iter i;
100  i = _context_list.find(name);
101  if (i != _context_list.end()) {
102  ret = (*i).second;
103  found = true;
104  }
105  BESDEBUG(MODULE, prolog << "name=\"" << name << "\", found=\"" << found << "\" value:\"" << ret << "\"" << endl);
106 #else
107 
108  string ret = _context_list[name];
109  BESDEBUG(MODULE, prolog << "name=\"" << name << "\" value: \"" << ret << "\"" << endl);
110 
111 #endif
112 
113  return ret;
114 }
115 
125 int BESContextManager::get_context_int(const string &name, bool &found)
126 {
127  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
128 
129  string value = BESContextManager::TheManager()->get_context(name, found);
130 
131  if (!found || value.empty()) return 0;
132 
133  char *endptr;
134  errno = 0;
135  int val = strtol(value.c_str(), &endptr, /*int base*/10);
136  if (val == 0 && errno > 0) {
137  throw BESInternalError(string("Error reading an integer value for the context '") + name + "': " + strerror(errno),
138  __FILE__, __LINE__);
139  }
140  BESDEBUG(MODULE, prolog << "name=\"" << name << "\", found=\"" << found << "\" value: \"" << val << "\"" << endl);
141 
142  return val;
143 }
144 
149 {
150  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
151 
152  string name;
153  string value;
154  std::map<string, string> props;
155  BESContextManager::Context_citer i = _context_list.begin();
156  BESContextManager::Context_citer e = _context_list.end();
157  for (; i != e; i++) {
158  props.clear();
159  name = (*i).first;
160  value = (*i).second;
161  props["name"] = name;
162  info.add_tag("context", value, &props);
163  }
164 }
165 
173 void BESContextManager::dump(ostream &strm) const
174 {
175  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
176 
177  strm << BESIndent::LMarg << prolog << "(this: " << (void *) this << ")" << endl;
178  BESIndent::Indent();
179  if (_context_list.size()) {
180  strm << BESIndent::LMarg << "current context:" << endl;
181  BESIndent::Indent();
182  BESContextManager::Context_citer i = _context_list.begin();
183  BESContextManager::Context_citer ie = _context_list.end();
184  for (; i != ie; i++) {
185  strm << BESIndent::LMarg << (*i).first << ": " << (*i).second << endl;
186  }
187  BESIndent::UnIndent();
188  }
189  else {
190  strm << BESIndent::LMarg << "no context" << endl;
191  }
192  BESIndent::UnIndent();
193 }
194 
196 BESContextManager::TheManager()
197 {
198  std::call_once(d_euc_init_once,BESContextManager::initialize_instance);
199  return d_instance;
200 }
201 
202 void BESContextManager::initialize_instance() {
203  d_instance = new BESContextManager;
204 #ifdef HAVE_ATEXIT
205  atexit(delete_instance);
206 #endif
207 }
208 
209 void BESContextManager::delete_instance() {
210  delete d_instance;
211  d_instance = 0;
212 }
213 
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