bes  Updated for version 3.20.5
BESContainerStorageFile.cc
1 // BESContainerStorageFile.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 <cerrno>
34 #include <sstream>
35 #include <fstream>
36 #include <iostream>
37 #include <cstring>
38 
39 using std::stringstream;
40 using std::ifstream;
41 
42 #include "BESContainerStorageFile.h"
43 #include "BESFileContainer.h"
44 #include "TheBESKeys.h"
45 #include "BESInternalError.h"
46 #include "BESSyntaxUserError.h"
47 #include "BESInfo.h"
48 #include "BESServiceRegistry.h"
49 
81 {
82  // TODO: Need to store the kind of container each line represents. Does
83  // it represent a file? A database entry? What? For now, they all
84  // represent a BESFileContainer.
85 
86  string key = "BES.Container.Persistence.File." + n;
87  bool found = false;
88  TheBESKeys::TheKeys()->get_value(key, _file, found);
89  if (_file == "") {
90  string s = key + " not defined in BES configuration file";
91  throw BESSyntaxUserError(s, __FILE__, __LINE__);
92  }
93 
94  ifstream persistence_file(_file.c_str());
95  int myerrno = errno;
96  if (!persistence_file) {
97  char *err = strerror(myerrno);
98  string s = "Unable to open persistence file " + _file + ": ";
99  if (err)
100  s += err;
101  else
102  s += "Unknown error";
103 
104  throw BESInternalError(s, __FILE__, __LINE__);
105  }
106 
107  try {
108  char cline[80];
109 
110  while (!persistence_file.eof()) {
111  stringstream strm;
112  persistence_file.getline(cline, 80);
113  if (!persistence_file.eof()) {
114  strm << cline;
115  BESContainerStorageFile::container *c = new BESContainerStorageFile::container;
116  strm >> c->_symbolic_name;
117  strm >> c->_real_name;
118  strm >> c->_container_type;
119  string dummy;
120  strm >> dummy;
121  if (c->_symbolic_name == "" || c->_real_name == "" || c->_container_type == "") {
122  delete c;
123  persistence_file.close();
124  string s = "Incomplete container persistence line in file " + _file;
125  throw BESInternalError(s, __FILE__, __LINE__);
126  }
127  if (dummy != "") {
128  persistence_file.close();
129  delete c;
130  string s = "Too many fields in persistence file " + _file;
131  throw BESInternalError(s, __FILE__, __LINE__);
132  }
133  _container_list[c->_symbolic_name] = c;
134  }
135  }
136  persistence_file.close();
137  }
138  catch (...) {
139  persistence_file.close();
140  throw;
141  }
142 }
143 
144 BESContainerStorageFile::~BESContainerStorageFile()
145 {
146  BESContainerStorageFile::Container_citer i = _container_list.begin();
147  BESContainerStorageFile::Container_citer ie = _container_list.end();
148  for (; i != ie; i++) {
149  BESContainerStorageFile::container *c = (*i).second;
150  delete c;
151  }
152 }
153 
165 BESContainer *
166 BESContainerStorageFile::look_for(const string &sym_name)
167 {
168  BESFileContainer *ret_container = 0;
169  BESContainerStorageFile::Container_citer i;
170  i = _container_list.find(sym_name);
171  if (i != _container_list.end()) {
172  BESContainerStorageFile::container *c = (*i).second;
173  ret_container = new BESFileContainer(c->_symbolic_name, c->_real_name, c->_container_type);
174  }
175 
176  return ret_container;
177 }
178 
189 void BESContainerStorageFile::add_container(const string &, const string &, const string &)
190 {
191  string err = "Unable to add a container to a file, not yet implemented";
192  throw BESInternalError(err, __FILE__, __LINE__);
193 }
194 
196 {
197  string err = "Unable to add a container to a file, not yet implemented";
198  throw BESInternalError(err, __FILE__, __LINE__);
199 }
200 
210 bool BESContainerStorageFile::del_container(const string &s_name)
211 {
212  bool ret = false;
213  BESContainerStorageFile::Container_iter i;
214  i = _container_list.find(s_name);
215  if (i != _container_list.end()) {
216  BESContainerStorageFile::container *c = (*i).second;
217  _container_list.erase(i);
218  if (c) {
219  delete c;
220  }
221  ret = true;
222  }
223  return ret;
224 }
225 
234 {
235  while (_container_list.size() != 0) {
236  Container_iter ci = _container_list.begin();
237  BESContainerStorageFile::container *c = (*ci).second;
238  _container_list.erase(ci);
239  if (c) {
240  delete c;
241  }
242  }
243  return true;
244 }
245 
253 bool BESContainerStorageFile::isData(const string &inQuestion, list<string> &provides)
254 {
255  bool isit = false;
256  BESContainer *c = look_for(inQuestion);
257  if (c) {
258  isit = true;
259  string node_type = c->get_container_type();
260  BESServiceRegistry::TheRegistry()->services_handled(node_type, provides);
261  delete c;
262  c = 0; // added jhrg 1.4.12
263  }
264  return isit;
265 }
266 
284 {
285  BESContainerStorageFile::Container_citer i;
286  i = _container_list.begin();
287  for (i = _container_list.begin(); i != _container_list.end(); i++) {
288  BESContainerStorageFile::container *c = (*i).second;
289  string sym = c->_symbolic_name;
290  string real = c->_real_name;
291  string type = c->_container_type;
292  show_container(sym, real, type, info);
293  }
294 }
295 
303 void BESContainerStorageFile::dump(ostream &strm) const
304 {
305  strm << BESIndent::LMarg << "BESContainerStorageFile::dump - (" << (void *) this << ")" << endl;
306  BESIndent::Indent();
307  strm << BESIndent::LMarg << "name: " << get_name() << endl;
308  strm << BESIndent::LMarg << "file: " << _file << endl;
309  if (_container_list.size()) {
310  strm << BESIndent::LMarg << "containers:" << endl;
311  BESIndent::Indent();
312  BESContainerStorageFile::Container_citer i = _container_list.begin();
313  BESContainerStorageFile::Container_citer ie = _container_list.end();
314  for (i = _container_list.begin(); i != ie; i++) {
315  BESContainerStorageFile::container *c = (*i).second;
316  strm << BESIndent::LMarg << c->_symbolic_name;
317  strm << ", " << c->_real_name;
318  strm << ", " << c->_container_type;
319  strm << endl;
320  }
321  BESIndent::UnIndent();
322  }
323  else {
324  strm << BESIndent::LMarg << " containers: none" << endl;
325  }
326  BESIndent::UnIndent();
327 }
328 
BESContainerStorage
provides persistent storage for data storage information represented by a container.
Definition: BESContainerStorage.h:72
BESFileContainer
Holds real data, container type and constraint for symbolic name read from persistence.
Definition: BESFileContainer.h:59
BESContainerStorageFile::dump
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESContainerStorageFile.cc:303
BESInfo
informational response object
Definition: BESInfo.h:68
BESContainerStorageFile::add_container
virtual void add_container(const string &sym_name, const string &real_name, const string &type)
adds a container with the provided information
Definition: BESContainerStorageFile.cc:189
BESContainerStorageFile::del_container
virtual bool del_container(const string &s_name)
removes a container with the given symbolic name
Definition: BESContainerStorageFile.cc:210
BESContainerStorageFile::show_containers
virtual void show_containers(BESInfo &info)
show information for each container in this persistent store
Definition: BESContainerStorageFile.cc:283
BESContainerStorage::get_name
virtual const std::string & get_name() const
retrieve the name of this persistent store
Definition: BESContainerStorage.h:96
TheBESKeys::TheKeys
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:61
BESSyntaxUserError
error thrown if there is a user syntax error in the request or any other user error
Definition: BESSyntaxUserError.h:41
BESContainer::get_container_type
string get_container_type() const
retrieve the type of data this container holds, such as cedar or netcdf.
Definition: BESContainer.h:235
BESInternalError
exception thrown if inernal error encountered
Definition: BESInternalError.h:43
BESContainerStorage::show_container
virtual void show_container(const std::string &sym_name, const std::string &real_name, const std::string &type, BESInfo &info)
add information for a container to the informational response object
Definition: BESContainerStorage.cc:45
TheBESKeys::get_value
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: TheBESKeys.cc:420
BESContainerStorageFile::look_for
virtual BESContainer * look_for(const string &sym_name)
looks for the specified container in the list of containers loaded from the file.
Definition: BESContainerStorageFile.cc:166
BESContainerStorageFile::BESContainerStorageFile
BESContainerStorageFile(const string &n)
pull container information from the specified file
Definition: BESContainerStorageFile.cc:79
BESContainer
A container is something that holds data. E.G., a netcdf file or a database entry.
Definition: BESContainer.h:68
BESContainerStorageFile::del_containers
virtual bool del_containers()
removes all containers
Definition: BESContainerStorageFile.cc:233
BESServiceRegistry::services_handled
virtual void services_handled(const string &handler, list< string > &services)
returns the list of servies provided by the handler in question
Definition: BESServiceRegistry.cc:328
BESContainerStorageFile::isData
virtual bool isData(const string &inQuestion, std::list< string > &provides)
determine if the given container is data and what servies are available for it
Definition: BESContainerStorageFile.cc:253