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 Atmostpheric 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 "BESContainer.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     string key = "BES.Container.Persistence.File." + n ;
00079     bool found = false ;
00080     _file = TheBESKeys::TheKeys()->get_key( key, found ) ;
00081     if( _file == "" )
00082     {
00083         string s = key + " not defined in key file" ;
00084         throw BESContainerStorageException( s, __FILE__, __LINE__ ) ;
00085     }
00086 
00087     ifstream persistence_file( _file.c_str() ) ;
00088     if( !persistence_file )
00089     {
00090         string s = "Unable to open persistence file " + _file ;
00091         throw BESContainerStorageException( s, __FILE__, __LINE__ ) ;
00092     }
00093 
00094     char cline[80] ;
00095 
00096     while( !persistence_file.eof() )
00097     {
00098         stringstream strm ;
00099         persistence_file.getline( cline, 80 ) ;
00100         if( !persistence_file.eof() )
00101         {
00102             strm << cline ;
00103             BESContainerStorageFile::container *c =
00104                 new BESContainerStorageFile::container ;
00105             strm >> c->_symbolic_name ;
00106             strm >> c->_real_name ;
00107             strm >> c->_container_type ;
00108             string dummy ;
00109             strm >> dummy ;
00110             if( c->_symbolic_name == "" ||
00111                 c->_real_name == "" ||
00112                 c->_container_type == "" )
00113             {
00114                 delete c ;
00115                 string s = "Incomplete container persistence line in file "
00116                            + _file ;
00117                 throw BESContainerStorageException( s, __FILE__, __LINE__ ) ;
00118             }
00119             if( dummy != "" )
00120             {
00121                 delete c ;
00122                 string s = "Too many fields in persistence file "
00123                            + _file ;
00124                 throw BESContainerStorageException( s, __FILE__, __LINE__ ) ;
00125             }
00126             _container_list[c->_symbolic_name] = c ;
00127         }
00128     }
00129     persistence_file.close() ;
00130 }
00131 
00132 BESContainerStorageFile::~BESContainerStorageFile()
00133 {
00134     BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00135     BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00136     for( ; i != ie; i++ )
00137     {
00138         BESContainerStorageFile::container *c = (*i).second ;
00139         delete c ;
00140     }
00141 }
00142 
00153 void
00154 BESContainerStorageFile::look_for( BESContainer &d )
00155 {
00156     d.set_valid_flag( false ) ;
00157     BESContainerStorageFile::Container_citer i ;
00158     i = _container_list.find( d.get_symbolic_name() ) ;
00159     if( i != _container_list.end() )
00160     {
00161         BESContainerStorageFile::container *c = (*i).second;
00162         d.set_real_name( c->_real_name ) ;
00163         d.set_container_type( c->_container_type ) ;
00164         d.set_valid_flag( true ) ;
00165     }
00166 }
00167 
00178 void
00179 BESContainerStorageFile::add_container( const string &,
00180                                      const string &,
00181                                      const string & )
00182 {
00183     throw BESContainerStorageException( "Unable to add a container to a file, not yet implemented", __FILE__, __LINE__ ) ;
00184 }
00185 
00195 bool
00196 BESContainerStorageFile::del_container( const string &s_name )
00197 {
00198     bool ret = false ;
00199     BESContainerStorageFile::Container_iter i ;
00200     i = _container_list.find( s_name ) ;
00201     if( i != _container_list.end() )
00202     {
00203         BESContainerStorageFile::container *c = (*i).second;
00204         _container_list.erase( i ) ;
00205         delete c ;
00206         ret = true ;
00207     }
00208     return ret ;
00209 }
00210 
00218 bool
00219 BESContainerStorageFile::del_containers( )
00220 {
00221     while( _container_list.size() != 0 )
00222     {
00223         Container_iter ci = _container_list.begin() ;
00224         BESContainerStorageFile::container *c = (*ci).second;
00225         _container_list.erase( ci ) ;
00226         if( c )
00227         {
00228             delete c ;
00229         }
00230     }
00231     return true ;
00232 }
00233 
00250 void
00251 BESContainerStorageFile::show_containers( BESInfo &info )
00252 {
00253     info.add_tag( "name", get_name() ) ;
00254     BESContainerStorageFile::Container_citer i ;
00255     i = _container_list.begin() ;
00256     for( i = _container_list.begin(); i != _container_list.end(); i++ )
00257     {
00258         info.begin_tag( "container" ) ;
00259         BESContainerStorageFile::container *c = (*i).second;
00260         string sym = c->_symbolic_name ;
00261         info.add_tag( "symbolicName", sym ) ;
00262         string real = c->_real_name ;
00263         info.add_tag( "realName", real ) ;
00264         string type = c->_container_type ;
00265         info.add_tag( "dataType", type ) ;
00266         info.end_tag( "container" ) ;
00267     }
00268 }
00269 
00277 void
00278 BESContainerStorageFile::dump( ostream &strm ) const
00279 {
00280     strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
00281                              << (void *)this << ")" << endl ;
00282     BESIndent::Indent() ;
00283     strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00284     strm << BESIndent::LMarg << "file: " << _file << endl ;
00285     if( _container_list.size() )
00286     {
00287         strm << BESIndent::LMarg << "containers:" << endl ;
00288         BESIndent::Indent() ;
00289         BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00290         BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00291         for( i = _container_list.begin(); i != ie; i++ )
00292         {
00293             BESContainerStorageFile::container *c = (*i).second;
00294             strm << BESIndent::LMarg << c->_symbolic_name ;
00295             strm << ", " << c->_real_name ;
00296             strm << ", " << c->_container_type ;
00297             strm << endl ;
00298         }
00299         BESIndent::UnIndent() ;
00300     }
00301     else
00302     {
00303         strm << BESIndent::LMarg << "    containers: none" << endl ;
00304     }
00305     BESIndent::UnIndent() ;
00306 }
00307 

Generated on Wed Aug 29 02:59:01 2007 for OPeNDAP Back End Server (BES) by  doxygen 1.5.2