OPeNDAP Hyrax Back End Server (BES) Updated for version 3.8.3
|
00001 // BESContainerStorageFile.cc 00002 00003 // This file is part of bes, A C++ back-end server implementation framework 00004 // for the OPeNDAP Data Access Protocol. 00005 00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research 00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu> 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Lesser General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2.1 of the License, or (at your option) any later version. 00013 // 00014 // This library is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 // 00023 // You can contact University Corporation for Atmospheric Research at 00024 // 3080 Center Green Drive, Boulder, CO 80301 00025 00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005 00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR. 00028 // 00029 // Authors: 00030 // pwest Patrick West <pwest@ucar.edu> 00031 // jgarcia Jose Garcia <jgarcia@ucar.edu> 00032 00033 #include <cerrno> 00034 #include <sstream> 00035 #include <fstream> 00036 #include <iostream> 00037 #include <cstring> 00038 00039 using std::stringstream ; 00040 using std::ifstream ; 00041 00042 #include "BESContainerStorageFile.h" 00043 #include "BESFileContainer.h" 00044 #include "TheBESKeys.h" 00045 #include "BESInternalError.h" 00046 #include "BESSyntaxUserError.h" 00047 #include "BESInfo.h" 00048 00078 BESContainerStorageFile::BESContainerStorageFile( const string &n ) 00079 : BESContainerStorage( n ) 00080 { 00081 // TODO: Need to store the kind of container each line represents. Does 00082 // it represent a file? A database entry? What? For now, they all 00083 // represent a BESFileContainer. 00084 00085 string key = "BES.Container.Persistence.File." + n ; 00086 bool found = false ; 00087 TheBESKeys::TheKeys()->get_value( key, _file, found ) ; 00088 if( _file == "" ) 00089 { 00090 string s = key + " not defined in BES configuration file" ; 00091 throw BESSyntaxUserError( s, __FILE__, __LINE__ ) ; 00092 } 00093 00094 ifstream persistence_file( _file.c_str() ) ; 00095 int myerrno = errno ; 00096 if( !persistence_file ) 00097 { 00098 char *err = strerror( myerrno ) ; 00099 string s = "Unable to open persistence file " + _file + ": " ; 00100 if( err ) 00101 s += err ; 00102 else 00103 s += "Unknown error" ; 00104 00105 throw BESInternalError( s, __FILE__, __LINE__ ) ; 00106 } 00107 00108 char cline[80] ; 00109 00110 while( !persistence_file.eof() ) 00111 { 00112 stringstream strm ; 00113 persistence_file.getline( cline, 80 ) ; 00114 if( !persistence_file.eof() ) 00115 { 00116 strm << cline ; 00117 BESContainerStorageFile::container *c = 00118 new BESContainerStorageFile::container ; 00119 strm >> c->_symbolic_name ; 00120 strm >> c->_real_name ; 00121 strm >> c->_container_type ; 00122 string dummy ; 00123 strm >> dummy ; 00124 if( c->_symbolic_name == "" || 00125 c->_real_name == "" || 00126 c->_container_type == "" ) 00127 { 00128 delete c ; 00129 persistence_file.close() ; 00130 string s = "Incomplete container persistence line in file " 00131 + _file ; 00132 throw BESInternalError( s, __FILE__, __LINE__ ) ; 00133 } 00134 if( dummy != "" ) 00135 { 00136 persistence_file.close() ; 00137 delete c ; 00138 string s = "Too many fields in persistence file " 00139 + _file ; 00140 throw BESInternalError( s, __FILE__, __LINE__ ) ; 00141 } 00142 _container_list[c->_symbolic_name] = c ; 00143 } 00144 } 00145 persistence_file.close() ; 00146 } 00147 00148 BESContainerStorageFile::~BESContainerStorageFile() 00149 { 00150 BESContainerStorageFile::Container_citer i = _container_list.begin() ; 00151 BESContainerStorageFile::Container_citer ie = _container_list.end() ; 00152 for( ; i != ie; i++ ) 00153 { 00154 BESContainerStorageFile::container *c = (*i).second ; 00155 delete c ; 00156 } 00157 } 00158 00170 BESContainer * 00171 BESContainerStorageFile::look_for( const string &sym_name ) 00172 { 00173 BESFileContainer *ret_container = 0 ; 00174 BESContainerStorageFile::Container_citer i ; 00175 i = _container_list.find( sym_name ) ; 00176 if( i != _container_list.end() ) 00177 { 00178 BESContainerStorageFile::container *c = (*i).second; 00179 ret_container = new BESFileContainer( c->_symbolic_name, 00180 c->_real_name, 00181 c->_container_type ) ; 00182 } 00183 00184 return ret_container ; 00185 } 00186 00197 void 00198 BESContainerStorageFile::add_container( const string &sym_name, 00199 const string &real_name, 00200 const string &type ) 00201 { 00202 string err = "Unable to add a container to a file, not yet implemented" ; 00203 throw BESInternalError( err, __FILE__, __LINE__ ) ; 00204 } 00205 00215 bool 00216 BESContainerStorageFile::del_container( const string &s_name ) 00217 { 00218 bool ret = false ; 00219 BESContainerStorageFile::Container_iter i ; 00220 i = _container_list.find( s_name ) ; 00221 if( i != _container_list.end() ) 00222 { 00223 BESContainerStorageFile::container *c = (*i).second; 00224 _container_list.erase( i ) ; 00225 if( c ) 00226 { 00227 delete c ; 00228 } 00229 ret = true ; 00230 } 00231 return ret ; 00232 } 00233 00241 bool 00242 BESContainerStorageFile::del_containers( ) 00243 { 00244 while( _container_list.size() != 0 ) 00245 { 00246 Container_iter ci = _container_list.begin() ; 00247 BESContainerStorageFile::container *c = (*ci).second; 00248 _container_list.erase( ci ) ; 00249 if( c ) 00250 { 00251 delete c ; 00252 } 00253 } 00254 return true ; 00255 } 00256 00273 void 00274 BESContainerStorageFile::show_containers( BESInfo &info ) 00275 { 00276 BESContainerStorageFile::Container_citer i ; 00277 i = _container_list.begin() ; 00278 for( i = _container_list.begin(); i != _container_list.end(); i++ ) 00279 { 00280 BESContainerStorageFile::container *c = (*i).second; 00281 string sym = c->_symbolic_name ; 00282 string real = c->_real_name ; 00283 string type = c->_container_type ; 00284 show_container( sym, real, type, info ) ; 00285 } 00286 } 00287 00295 void 00296 BESContainerStorageFile::dump( ostream &strm ) const 00297 { 00298 strm << BESIndent::LMarg << "BESContainerStorageFile::dump - (" 00299 << (void *)this << ")" << endl ; 00300 BESIndent::Indent() ; 00301 strm << BESIndent::LMarg << "name: " << get_name() << endl ; 00302 strm << BESIndent::LMarg << "file: " << _file << endl ; 00303 if( _container_list.size() ) 00304 { 00305 strm << BESIndent::LMarg << "containers:" << endl ; 00306 BESIndent::Indent() ; 00307 BESContainerStorageFile::Container_citer i = _container_list.begin() ; 00308 BESContainerStorageFile::Container_citer ie = _container_list.end() ; 00309 for( i = _container_list.begin(); i != ie; i++ ) 00310 { 00311 BESContainerStorageFile::container *c = (*i).second; 00312 strm << BESIndent::LMarg << c->_symbolic_name ; 00313 strm << ", " << c->_real_name ; 00314 strm << ", " << c->_container_type ; 00315 strm << endl ; 00316 } 00317 BESIndent::UnIndent() ; 00318 } 00319 else 00320 { 00321 strm << BESIndent::LMarg << " containers: none" << endl ; 00322 } 00323 BESIndent::UnIndent() ; 00324 } 00325