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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 
79  : BESContainerStorage( n )
80 {
81  // TODO: Need to store the kind of container each line represents. Does
82  // it represent a file? A database entry? What? For now, they all
83  // represent a BESFileContainer.
84 
85  string key = "BES.Container.Persistence.File." + n ;
86  bool found = false ;
87  TheBESKeys::TheKeys()->get_value( key, _file, found ) ;
88  if( _file == "" )
89  {
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  {
98  char *err = strerror( myerrno ) ;
99  string s = "Unable to open persistence file " + _file + ": " ;
100  if( err )
101  s += err ;
102  else
103  s += "Unknown error" ;
104 
105  throw BESInternalError( s, __FILE__, __LINE__ ) ;
106  }
107 
108  char cline[80] ;
109 
110  while( !persistence_file.eof() )
111  {
112  stringstream strm ;
113  persistence_file.getline( cline, 80 ) ;
114  if( !persistence_file.eof() )
115  {
116  strm << cline ;
117  BESContainerStorageFile::container *c =
118  new BESContainerStorageFile::container ;
119  strm >> c->_symbolic_name ;
120  strm >> c->_real_name ;
121  strm >> c->_container_type ;
122  string dummy ;
123  strm >> dummy ;
124  if( c->_symbolic_name == "" ||
125  c->_real_name == "" ||
126  c->_container_type == "" )
127  {
128  delete c ;
129  persistence_file.close() ;
130  string s = "Incomplete container persistence line in file "
131  + _file ;
132  throw BESInternalError( s, __FILE__, __LINE__ ) ;
133  }
134  if( dummy != "" )
135  {
136  persistence_file.close() ;
137  delete c ;
138  string s = "Too many fields in persistence file "
139  + _file ;
140  throw BESInternalError( s, __FILE__, __LINE__ ) ;
141  }
142  _container_list[c->_symbolic_name] = c ;
143  }
144  }
145  persistence_file.close() ;
146 }
147 
149 {
150  BESContainerStorageFile::Container_citer i = _container_list.begin() ;
151  BESContainerStorageFile::Container_citer ie = _container_list.end() ;
152  for( ; i != ie; i++ )
153  {
154  BESContainerStorageFile::container *c = (*i).second ;
155  delete c ;
156  }
157 }
158 
170 BESContainer *
171 BESContainerStorageFile::look_for( const string &sym_name )
172 {
173  BESFileContainer *ret_container = 0 ;
174  BESContainerStorageFile::Container_citer i ;
175  i = _container_list.find( sym_name ) ;
176  if( i != _container_list.end() )
177  {
178  BESContainerStorageFile::container *c = (*i).second;
179  ret_container = new BESFileContainer( c->_symbolic_name,
180  c->_real_name,
181  c->_container_type ) ;
182  }
183 
184  return ret_container ;
185 }
186 
197 void
199  const string &real_name,
200  const string &type )
201 {
202  string err = "Unable to add a container to a file, not yet implemented" ;
203  throw BESInternalError( err, __FILE__, __LINE__ ) ;
204 }
205 
215 bool
217 {
218  bool ret = false ;
219  BESContainerStorageFile::Container_iter i ;
220  i = _container_list.find( s_name ) ;
221  if( i != _container_list.end() )
222  {
223  BESContainerStorageFile::container *c = (*i).second;
224  _container_list.erase( i ) ;
225  if( c )
226  {
227  delete c ;
228  }
229  ret = true ;
230  }
231  return ret ;
232 }
233 
241 bool
243 {
244  while( _container_list.size() != 0 )
245  {
246  Container_iter ci = _container_list.begin() ;
247  BESContainerStorageFile::container *c = (*ci).second;
248  _container_list.erase( ci ) ;
249  if( c )
250  {
251  delete c ;
252  }
253  }
254  return true ;
255 }
256 
273 void
275 {
276  BESContainerStorageFile::Container_citer i ;
277  i = _container_list.begin() ;
278  for( i = _container_list.begin(); i != _container_list.end(); i++ )
279  {
280  BESContainerStorageFile::container *c = (*i).second;
281  string sym = c->_symbolic_name ;
282  string real = c->_real_name ;
283  string type = c->_container_type ;
284  show_container( sym, real, type, info ) ;
285  }
286 }
287 
295 void
296 BESContainerStorageFile::dump( ostream &strm ) const
297 {
298  strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
299  << (void *)this << ")" << endl ;
301  strm << BESIndent::LMarg << "name: " << get_name() << endl ;
302  strm << BESIndent::LMarg << "file: " << _file << endl ;
303  if( _container_list.size() )
304  {
305  strm << BESIndent::LMarg << "containers:" << endl ;
306  BESIndent::Indent() ;
307  BESContainerStorageFile::Container_citer i = _container_list.begin() ;
308  BESContainerStorageFile::Container_citer ie = _container_list.end() ;
309  for( i = _container_list.begin(); i != ie; i++ )
310  {
311  BESContainerStorageFile::container *c = (*i).second;
312  strm << BESIndent::LMarg << c->_symbolic_name ;
313  strm << ", " << c->_real_name ;
314  strm << ", " << c->_container_type ;
315  strm << endl ;
316  }
318  }
319  else
320  {
321  strm << BESIndent::LMarg << " containers: none" << endl ;
322  }
324 }
325