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,2005 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 <sstream>
00034 #include <fstream>
00035 #include <iostream>
00036 
00037 using std::stringstream ;
00038 using std::ifstream ;
00039 
00040 #include "BESContainerStorageFile.h"
00041 #include "BESFileContainer.h"
00042 #include "TheBESKeys.h"
00043 #include "BESInternalError.h"
00044 #include "BESInfo.h"
00045 
00075 BESContainerStorageFile::BESContainerStorageFile( const string &n )
00076     : BESContainerStorage( n )
00077 {
00078     // TODO: Need to store the kind of container each line represents. Does
00079     // it represent a file? A database entry? What? For now, they all
00080     // represent a BESFileContainer.
00081 
00082     string key = "BES.Container.Persistence.File." + n ;
00083     bool found = false ;
00084     _file = TheBESKeys::TheKeys()->get_key( key, found ) ;
00085     if( _file == "" )
00086     {
00087         string s = key + " not defined in key file" ;
00088         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00089     }
00090 
00091     ifstream persistence_file( _file.c_str() ) ;
00092     if( !persistence_file )
00093     {
00094         string s = "Unable to open persistence file " + _file ;
00095         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00096     }
00097 
00098     char cline[80] ;
00099 
00100     while( !persistence_file.eof() )
00101     {
00102         stringstream strm ;
00103         persistence_file.getline( cline, 80 ) ;
00104         if( !persistence_file.eof() )
00105         {
00106             strm << cline ;
00107             BESContainerStorageFile::container *c =
00108                 new BESContainerStorageFile::container ;
00109             strm >> c->_symbolic_name ;
00110             strm >> c->_real_name ;
00111             strm >> c->_container_type ;
00112             string dummy ;
00113             strm >> dummy ;
00114             if( c->_symbolic_name == "" ||
00115                 c->_real_name == "" ||
00116                 c->_container_type == "" )
00117             {
00118                 delete c ;
00119                 persistence_file.close() ;
00120                 string s = "Incomplete container persistence line in file "
00121                            + _file ;
00122                 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00123             }
00124             if( dummy != "" )
00125             {
00126                 persistence_file.close() ;
00127                 delete c ;
00128                 string s = "Too many fields in persistence file "
00129                            + _file ;
00130                 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00131             }
00132             _container_list[c->_symbolic_name] = c ;
00133         }
00134     }
00135     persistence_file.close() ;
00136 }
00137 
00138 BESContainerStorageFile::~BESContainerStorageFile()
00139 {
00140     BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00141     BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00142     for( ; i != ie; i++ )
00143     {
00144         BESContainerStorageFile::container *c = (*i).second ;
00145         delete c ;
00146     }
00147 }
00148 
00160 BESContainer *
00161 BESContainerStorageFile::look_for( const string &sym_name )
00162 {
00163     BESFileContainer *ret_container = 0 ;
00164     BESContainerStorageFile::Container_citer i ;
00165     i = _container_list.find( sym_name ) ;
00166     if( i != _container_list.end() )
00167     {
00168         BESContainerStorageFile::container *c = (*i).second;
00169         ret_container = new BESFileContainer( c->_symbolic_name,
00170                                               c->_real_name,
00171                                               c->_container_type ) ;
00172     }
00173 
00174     return ret_container ;
00175 }
00176 
00187 void
00188 BESContainerStorageFile::add_container( const string &sym_name,
00189                                         const string &real_name,
00190                                         const string &type )
00191 {
00192     string err = "Unable to add a container to a file, not yet implemented" ;
00193     throw BESInternalError( err, __FILE__, __LINE__ ) ;
00194 }
00195 
00205 bool
00206 BESContainerStorageFile::del_container( const string &s_name )
00207 {
00208     bool ret = false ;
00209     BESContainerStorageFile::Container_iter i ;
00210     i = _container_list.find( s_name ) ;
00211     if( i != _container_list.end() )
00212     {
00213         BESContainerStorageFile::container *c = (*i).second;
00214         _container_list.erase( i ) ;
00215         delete c ;
00216         ret = true ;
00217     }
00218     return ret ;
00219 }
00220 
00228 bool
00229 BESContainerStorageFile::del_containers( )
00230 {
00231     while( _container_list.size() != 0 )
00232     {
00233         Container_iter ci = _container_list.begin() ;
00234         BESContainerStorageFile::container *c = (*ci).second;
00235         _container_list.erase( ci ) ;
00236         if( c )
00237         {
00238             delete c ;
00239         }
00240     }
00241     return true ;
00242 }
00243 
00260 void
00261 BESContainerStorageFile::show_containers( BESInfo &info )
00262 {
00263     info.add_tag( "name", get_name() ) ;
00264     BESContainerStorageFile::Container_citer i ;
00265     i = _container_list.begin() ;
00266     for( i = _container_list.begin(); i != _container_list.end(); i++ )
00267     {
00268         info.begin_tag( "container" ) ;
00269         BESContainerStorageFile::container *c = (*i).second;
00270         string sym = c->_symbolic_name ;
00271         info.add_tag( "symbolicName", sym ) ;
00272         string real = c->_real_name ;
00273         info.add_tag( "realName", real ) ;
00274         string type = c->_container_type ;
00275         info.add_tag( "dataType", type ) ;
00276         info.end_tag( "container" ) ;
00277     }
00278 }
00279 
00287 void
00288 BESContainerStorageFile::dump( ostream &strm ) const
00289 {
00290     strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
00291                              << (void *)this << ")" << endl ;
00292     BESIndent::Indent() ;
00293     strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00294     strm << BESIndent::LMarg << "file: " << _file << endl ;
00295     if( _container_list.size() )
00296     {
00297         strm << BESIndent::LMarg << "containers:" << endl ;
00298         BESIndent::Indent() ;
00299         BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00300         BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00301         for( i = _container_list.begin(); i != ie; i++ )
00302         {
00303             BESContainerStorageFile::container *c = (*i).second;
00304             strm << BESIndent::LMarg << c->_symbolic_name ;
00305             strm << ", " << c->_real_name ;
00306             strm << ", " << c->_container_type ;
00307             strm << endl ;
00308         }
00309         BESIndent::UnIndent() ;
00310     }
00311     else
00312     {
00313         strm << BESIndent::LMarg << "    containers: none" << endl ;
00314     }
00315     BESIndent::UnIndent() ;
00316 }
00317 

Generated on Tue Mar 4 23:13:33 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.1