OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESContainerStorageFile.cc
Go to the documentation of this file.
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 
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 
80  : BESContainerStorage( n )
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  {
91  string s = key + " not defined in BES configuration file" ;
92  throw BESSyntaxUserError( s, __FILE__, __LINE__ ) ;
93  }
94 
95  ifstream persistence_file( _file.c_str() ) ;
96  int myerrno = errno ;
97  if( !persistence_file )
98  {
99  char *err = strerror( myerrno ) ;
100  string s = "Unable to open persistence file " + _file + ": " ;
101  if( err )
102  s += err ;
103  else
104  s += "Unknown error" ;
105 
106  throw BESInternalError( s, __FILE__, __LINE__ ) ;
107  }
108 
109  char cline[80] ;
110 
111  while( !persistence_file.eof() )
112  {
113  stringstream strm ;
114  persistence_file.getline( cline, 80 ) ;
115  if( !persistence_file.eof() )
116  {
117  strm << cline ;
118  BESContainerStorageFile::container *c =
119  new BESContainerStorageFile::container ;
120  strm >> c->_symbolic_name ;
121  strm >> c->_real_name ;
122  strm >> c->_container_type ;
123  string dummy ;
124  strm >> dummy ;
125  if( c->_symbolic_name == "" ||
126  c->_real_name == "" ||
127  c->_container_type == "" )
128  {
129  delete c ;
130  persistence_file.close() ;
131  string s = "Incomplete container persistence line in file "
132  + _file ;
133  throw BESInternalError( s, __FILE__, __LINE__ ) ;
134  }
135  if( dummy != "" )
136  {
137  persistence_file.close() ;
138  delete c ;
139  string s = "Too many fields in persistence file "
140  + _file ;
141  throw BESInternalError( s, __FILE__, __LINE__ ) ;
142  }
143  _container_list[c->_symbolic_name] = c ;
144  }
145  }
146  persistence_file.close() ;
147 }
148 
150 {
151  BESContainerStorageFile::Container_citer i = _container_list.begin() ;
152  BESContainerStorageFile::Container_citer ie = _container_list.end() ;
153  for( ; i != ie; i++ )
154  {
155  BESContainerStorageFile::container *c = (*i).second ;
156  delete c ;
157  }
158 }
159 
171 BESContainer *
172 BESContainerStorageFile::look_for( const string &sym_name )
173 {
174  BESFileContainer *ret_container = 0 ;
175  BESContainerStorageFile::Container_citer i ;
176  i = _container_list.find( sym_name ) ;
177  if( i != _container_list.end() )
178  {
179  BESContainerStorageFile::container *c = (*i).second;
180  ret_container = new BESFileContainer( c->_symbolic_name,
181  c->_real_name,
182  c->_container_type ) ;
183  }
184 
185  return ret_container ;
186 }
187 
198 void
200  const string &,
201  const string & )
202 {
203  string err = "Unable to add a container to a file, not yet implemented" ;
204  throw BESInternalError( err, __FILE__, __LINE__ ) ;
205 }
206 
216 bool
218 {
219  bool ret = false ;
220  BESContainerStorageFile::Container_iter i ;
221  i = _container_list.find( s_name ) ;
222  if( i != _container_list.end() )
223  {
224  BESContainerStorageFile::container *c = (*i).second;
225  _container_list.erase( i ) ;
226  if( c )
227  {
228  delete c ;
229  }
230  ret = true ;
231  }
232  return ret ;
233 }
234 
242 bool
244 {
245  while( _container_list.size() != 0 )
246  {
247  Container_iter ci = _container_list.begin() ;
248  BESContainerStorageFile::container *c = (*ci).second;
249  _container_list.erase( ci ) ;
250  if( c )
251  {
252  delete c ;
253  }
254  }
255  return true ;
256 }
257 
265 bool
266 BESContainerStorageFile::isData( const string &inQuestion,
267  list<string> &provides )
268 {
269  bool isit = false ;
270  BESContainer *c = look_for( inQuestion ) ;
271  if( c )
272  {
273  isit = true ;
274  string node_type = c->get_container_type() ;
276  provides ) ;
277  delete c; c = 0; // added jhrg 1.4.12
278  }
279  return isit ;
280 }
281 
298 void
300 {
301  BESContainerStorageFile::Container_citer i ;
302  i = _container_list.begin() ;
303  for( i = _container_list.begin(); i != _container_list.end(); i++ )
304  {
305  BESContainerStorageFile::container *c = (*i).second;
306  string sym = c->_symbolic_name ;
307  string real = c->_real_name ;
308  string type = c->_container_type ;
309  show_container( sym, real, type, info ) ;
310  }
311 }
312 
320 void
321 BESContainerStorageFile::dump( ostream &strm ) const
322 {
323  strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
324  << (void *)this << ")" << endl ;
326  strm << BESIndent::LMarg << "name: " << get_name() << endl ;
327  strm << BESIndent::LMarg << "file: " << _file << endl ;
328  if( _container_list.size() )
329  {
330  strm << BESIndent::LMarg << "containers:" << endl ;
332  BESContainerStorageFile::Container_citer i = _container_list.begin() ;
333  BESContainerStorageFile::Container_citer ie = _container_list.end() ;
334  for( i = _container_list.begin(); i != ie; i++ )
335  {
336  BESContainerStorageFile::container *c = (*i).second;
337  strm << BESIndent::LMarg << c->_symbolic_name ;
338  strm << ", " << c->_real_name ;
339  strm << ", " << c->_container_type ;
340  strm << endl ;
341  }
343  }
344  else
345  {
346  strm << BESIndent::LMarg << " containers: none" << endl ;
347  }
349 }
350 
provides persistent storage for data storage information represented by a container.
virtual void show_container(const string &sym_name, const string &real_name, const string &type, BESInfo &info)
add information for a container to the informational response object
exception thrown if inernal error encountered
virtual bool del_container(const string &s_name)
removes a container with the given symbolic name
virtual void show_containers(BESInfo &info)
show information for each container in this persistent store
Holds real data, container type and constraint for symbolic name read from persistence.
string get_container_type() const
retrieve the type of data this container holds, such as cedar or netcdf.
Definition: BESContainer.h:170
static void Indent()
Definition: BESIndent.cc:38
error thrown if there is a user syntax error in the request or any other user error ...
informational response object
Definition: BESInfo.h:68
static BESServiceRegistry * TheRegistry()
virtual void add_container(const string &sym_name, const string &real_name, const string &type)
adds a container with the provided information
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
virtual bool isData(const string &inQuestion, list< string > &provides)
determine if the given container is data and what servies are available for it
virtual BESContainer * look_for(const string &sym_name)
looks for the specified container in the list of containers loaded from the file. ...
virtual void dump(ostream &strm) const
dumps information about this object
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:453
virtual bool del_containers()
removes all containers
A container is something that holds data.
Definition: BESContainer.h:60
BESContainerStorageFile(const string &n)
pull container information from the specified file
static void UnIndent()
Definition: BESIndent.cc:44
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:48
virtual const string & get_name() const
retrieve the name of this persistent store
virtual void services_handled(const string &handler, list< string > &services)
returns the list of servies provided by the handler in question