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 "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         delete c ;
00226         ret = true ;
00227     }
00228     return ret ;
00229 }
00230 
00238 bool
00239 BESContainerStorageFile::del_containers( )
00240 {
00241     while( _container_list.size() != 0 )
00242     {
00243         Container_iter ci = _container_list.begin() ;
00244         BESContainerStorageFile::container *c = (*ci).second;
00245         _container_list.erase( ci ) ;
00246         if( c )
00247         {
00248             delete c ;
00249         }
00250     }
00251     return true ;
00252 }
00253 
00270 void
00271 BESContainerStorageFile::show_containers( BESInfo &info )
00272 {
00273     BESContainerStorageFile::Container_citer i ;
00274     i = _container_list.begin() ;
00275     for( i = _container_list.begin(); i != _container_list.end(); i++ )
00276     {
00277         BESContainerStorageFile::container *c = (*i).second;
00278         string sym = c->_symbolic_name ;
00279         string real = c->_real_name ;
00280         string type = c->_container_type ;
00281         show_container( sym, real, type, info ) ;
00282     }
00283 }
00284 
00292 void
00293 BESContainerStorageFile::dump( ostream &strm ) const
00294 {
00295     strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
00296                              << (void *)this << ")" << endl ;
00297     BESIndent::Indent() ;
00298     strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00299     strm << BESIndent::LMarg << "file: " << _file << endl ;
00300     if( _container_list.size() )
00301     {
00302         strm << BESIndent::LMarg << "containers:" << endl ;
00303         BESIndent::Indent() ;
00304         BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00305         BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00306         for( i = _container_list.begin(); i != ie; i++ )
00307         {
00308             BESContainerStorageFile::container *c = (*i).second;
00309             strm << BESIndent::LMarg << c->_symbolic_name ;
00310             strm << ", " << c->_real_name ;
00311             strm << ", " << c->_container_type ;
00312             strm << endl ;
00313         }
00314         BESIndent::UnIndent() ;
00315     }
00316     else
00317     {
00318         strm << BESIndent::LMarg << "    containers: none" << endl ;
00319     }
00320     BESIndent::UnIndent() ;
00321 }
00322