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 "BESContainerStorageException.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 BESContainerStorageException( 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 BESContainerStorageException( 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                 string s = "Incomplete container persistence line in file "
00120                            + _file ;
00121                 throw BESContainerStorageException( s, __FILE__, __LINE__ ) ;
00122             }
00123             if( dummy != "" )
00124             {
00125                 delete c ;
00126                 string s = "Too many fields in persistence file "
00127                            + _file ;
00128                 throw BESContainerStorageException( s, __FILE__, __LINE__ ) ;
00129             }
00130             _container_list[c->_symbolic_name] = c ;
00131         }
00132     }
00133     persistence_file.close() ;
00134 }
00135 
00136 BESContainerStorageFile::~BESContainerStorageFile()
00137 {
00138     BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00139     BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00140     for( ; i != ie; i++ )
00141     {
00142         BESContainerStorageFile::container *c = (*i).second ;
00143         delete c ;
00144     }
00145 }
00146 
00158 BESContainer *
00159 BESContainerStorageFile::look_for( const string &sym_name )
00160 {
00161     BESFileContainer *ret_container = 0 ;
00162     BESContainerStorageFile::Container_citer i ;
00163     i = _container_list.find( sym_name ) ;
00164     if( i != _container_list.end() )
00165     {
00166         BESContainerStorageFile::container *c = (*i).second;
00167         ret_container = new BESFileContainer( c->_symbolic_name,
00168                                               c->_real_name,
00169                                               c->_container_type ) ;
00170     }
00171 
00172     return ret_container ;
00173 }
00174 
00185 void
00186 BESContainerStorageFile::add_container( const string &sym_name,
00187                                         const string &real_name,
00188                                         const string &type )
00189 {
00190     string err = "Unable to add a container to a file, not yet implemented" ;
00191     throw BESContainerStorageException( err, __FILE__, __LINE__ ) ;
00192 }
00193 
00203 bool
00204 BESContainerStorageFile::del_container( const string &s_name )
00205 {
00206     bool ret = false ;
00207     BESContainerStorageFile::Container_iter i ;
00208     i = _container_list.find( s_name ) ;
00209     if( i != _container_list.end() )
00210     {
00211         BESContainerStorageFile::container *c = (*i).second;
00212         _container_list.erase( i ) ;
00213         delete c ;
00214         ret = true ;
00215     }
00216     return ret ;
00217 }
00218 
00226 bool
00227 BESContainerStorageFile::del_containers( )
00228 {
00229     while( _container_list.size() != 0 )
00230     {
00231         Container_iter ci = _container_list.begin() ;
00232         BESContainerStorageFile::container *c = (*ci).second;
00233         _container_list.erase( ci ) ;
00234         if( c )
00235         {
00236             delete c ;
00237         }
00238     }
00239     return true ;
00240 }
00241 
00258 void
00259 BESContainerStorageFile::show_containers( BESInfo &info )
00260 {
00261     info.add_tag( "name", get_name() ) ;
00262     BESContainerStorageFile::Container_citer i ;
00263     i = _container_list.begin() ;
00264     for( i = _container_list.begin(); i != _container_list.end(); i++ )
00265     {
00266         info.begin_tag( "container" ) ;
00267         BESContainerStorageFile::container *c = (*i).second;
00268         string sym = c->_symbolic_name ;
00269         info.add_tag( "symbolicName", sym ) ;
00270         string real = c->_real_name ;
00271         info.add_tag( "realName", real ) ;
00272         string type = c->_container_type ;
00273         info.add_tag( "dataType", type ) ;
00274         info.end_tag( "container" ) ;
00275     }
00276 }
00277 
00285 void
00286 BESContainerStorageFile::dump( ostream &strm ) const
00287 {
00288     strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
00289                              << (void *)this << ")" << endl ;
00290     BESIndent::Indent() ;
00291     strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00292     strm << BESIndent::LMarg << "file: " << _file << endl ;
00293     if( _container_list.size() )
00294     {
00295         strm << BESIndent::LMarg << "containers:" << endl ;
00296         BESIndent::Indent() ;
00297         BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00298         BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00299         for( i = _container_list.begin(); i != ie; i++ )
00300         {
00301             BESContainerStorageFile::container *c = (*i).second;
00302             strm << BESIndent::LMarg << c->_symbolic_name ;
00303             strm << ", " << c->_real_name ;
00304             strm << ", " << c->_container_type ;
00305             strm << endl ;
00306         }
00307         BESIndent::UnIndent() ;
00308     }
00309     else
00310     {
00311         strm << BESIndent::LMarg << "    containers: none" << endl ;
00312     }
00313     BESIndent::UnIndent() ;
00314 }
00315 

Generated on Sat Jan 19 04:05:36 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.4