BESContainerStorageFile.cc

Go to the documentation of this file.
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 "BESInfo.h"
00047 
00077 BESContainerStorageFile::BESContainerStorageFile( const string &n )
00078     : BESContainerStorage( n )
00079 {
00080     // TODO: Need to store the kind of container each line represents. Does
00081     // it represent a file? A database entry? What? For now, they all
00082     // represent a BESFileContainer.
00083 
00084     string key = "BES.Container.Persistence.File." + n ;
00085     bool found = false ;
00086     _file = TheBESKeys::TheKeys()->get_key( key, found ) ;
00087     if( _file == "" )
00088     {
00089         string s = key + " not defined in key file" ;
00090         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00091     }
00092 
00093     ifstream persistence_file( _file.c_str() ) ;
00094     int myerrno = errno ;
00095     if( !persistence_file )
00096     {
00097         char *err = strerror( myerrno ) ;
00098         string s = "Unable to open persistence file " + _file + ": " ;
00099         if( err )
00100             s += err ;
00101         else
00102             s += "Unknown error" ;
00103 
00104         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00105     }
00106 
00107     char cline[80] ;
00108 
00109     while( !persistence_file.eof() )
00110     {
00111         stringstream strm ;
00112         persistence_file.getline( cline, 80 ) ;
00113         if( !persistence_file.eof() )
00114         {
00115             strm << cline ;
00116             BESContainerStorageFile::container *c =
00117                 new BESContainerStorageFile::container ;
00118             strm >> c->_symbolic_name ;
00119             strm >> c->_real_name ;
00120             strm >> c->_container_type ;
00121             string dummy ;
00122             strm >> dummy ;
00123             if( c->_symbolic_name == "" ||
00124                 c->_real_name == "" ||
00125                 c->_container_type == "" )
00126             {
00127                 delete c ;
00128                 persistence_file.close() ;
00129                 string s = "Incomplete container persistence line in file "
00130                            + _file ;
00131                 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00132             }
00133             if( dummy != "" )
00134             {
00135                 persistence_file.close() ;
00136                 delete c ;
00137                 string s = "Too many fields in persistence file "
00138                            + _file ;
00139                 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00140             }
00141             _container_list[c->_symbolic_name] = c ;
00142         }
00143     }
00144     persistence_file.close() ;
00145 }
00146 
00147 BESContainerStorageFile::~BESContainerStorageFile()
00148 {
00149     BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00150     BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00151     for( ; i != ie; i++ )
00152     {
00153         BESContainerStorageFile::container *c = (*i).second ;
00154         delete c ;
00155     }
00156 }
00157 
00169 BESContainer *
00170 BESContainerStorageFile::look_for( const string &sym_name )
00171 {
00172     BESFileContainer *ret_container = 0 ;
00173     BESContainerStorageFile::Container_citer i ;
00174     i = _container_list.find( sym_name ) ;
00175     if( i != _container_list.end() )
00176     {
00177         BESContainerStorageFile::container *c = (*i).second;
00178         ret_container = new BESFileContainer( c->_symbolic_name,
00179                                               c->_real_name,
00180                                               c->_container_type ) ;
00181     }
00182 
00183     return ret_container ;
00184 }
00185 
00196 void
00197 BESContainerStorageFile::add_container( const string &sym_name,
00198                                         const string &real_name,
00199                                         const string &type )
00200 {
00201     string err = "Unable to add a container to a file, not yet implemented" ;
00202     throw BESInternalError( err, __FILE__, __LINE__ ) ;
00203 }
00204 
00214 bool
00215 BESContainerStorageFile::del_container( const string &s_name )
00216 {
00217     bool ret = false ;
00218     BESContainerStorageFile::Container_iter i ;
00219     i = _container_list.find( s_name ) ;
00220     if( i != _container_list.end() )
00221     {
00222         BESContainerStorageFile::container *c = (*i).second;
00223         _container_list.erase( i ) ;
00224         delete c ;
00225         ret = true ;
00226     }
00227     return ret ;
00228 }
00229 
00237 bool
00238 BESContainerStorageFile::del_containers( )
00239 {
00240     while( _container_list.size() != 0 )
00241     {
00242         Container_iter ci = _container_list.begin() ;
00243         BESContainerStorageFile::container *c = (*ci).second;
00244         _container_list.erase( ci ) ;
00245         if( c )
00246         {
00247             delete c ;
00248         }
00249     }
00250     return true ;
00251 }
00252 
00269 void
00270 BESContainerStorageFile::show_containers( BESInfo &info )
00271 {
00272     BESContainerStorageFile::Container_citer i ;
00273     i = _container_list.begin() ;
00274     for( i = _container_list.begin(); i != _container_list.end(); i++ )
00275     {
00276         BESContainerStorageFile::container *c = (*i).second;
00277         string sym = c->_symbolic_name ;
00278         string real = c->_real_name ;
00279         string type = c->_container_type ;
00280         show_container( sym, real, type, info ) ;
00281     }
00282 }
00283 
00291 void
00292 BESContainerStorageFile::dump( ostream &strm ) const
00293 {
00294     strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
00295                              << (void *)this << ")" << endl ;
00296     BESIndent::Indent() ;
00297     strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00298     strm << BESIndent::LMarg << "file: " << _file << endl ;
00299     if( _container_list.size() )
00300     {
00301         strm << BESIndent::LMarg << "containers:" << endl ;
00302         BESIndent::Indent() ;
00303         BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00304         BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00305         for( i = _container_list.begin(); i != ie; i++ )
00306         {
00307             BESContainerStorageFile::container *c = (*i).second;
00308             strm << BESIndent::LMarg << c->_symbolic_name ;
00309             strm << ", " << c->_real_name ;
00310             strm << ", " << c->_container_type ;
00311             strm << endl ;
00312         }
00313         BESIndent::UnIndent() ;
00314     }
00315     else
00316     {
00317         strm << BESIndent::LMarg << "    containers: none" << endl ;
00318     }
00319     BESIndent::UnIndent() ;
00320 }
00321 

Generated on Sat Aug 22 06:06:26 2009 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.6.0