BESInfo.cc

Go to the documentation of this file.
00001 // BESInfo.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 <iostream>
00036 #include <fstream>
00037 #include <cstring>
00038 
00039 using std::ostringstream ;
00040 using std::ifstream ;
00041 
00042 #include "BESInfo.h"
00043 #include "TheBESKeys.h"
00044 #include "BESInternalError.h"
00045 
00046 #define BES_INFO_FILE_BUFFER_SIZE 4096
00047 
00053 BESInfo::BESInfo( )
00054     : _strm( 0 ),
00055       _strm_owned( false ),
00056       _buffered( true )
00057 {
00058     _strm = new ostringstream ;
00059     _strm_owned = true ;
00060 }
00061 
00075 BESInfo::BESInfo( const string &key, ostream *strm, bool strm_owned )
00076     : _strm( 0 ),
00077       _strm_owned( false ),
00078       _buffered( true )
00079 {
00080     bool found = false ;
00081     string b = TheBESKeys::TheKeys()->get_key( key, found ) ;
00082     if( b == "true" || b == "True" || b == "TRUE" ||
00083         b == "yes" || b == "Yes" || b == "YES" )
00084     {
00085         _strm = new ostringstream ;
00086         _strm_owned = true ;
00087         _buffered = true ;
00088         if( strm && strm_owned )
00089             delete strm ;
00090     }
00091     else
00092     {
00093         if( !strm )
00094         {
00095             string s = "Informational response not buffered but no stream passed" ;
00096             throw BESInternalError( s, __FILE__, __LINE__ ) ;
00097         }
00098         _strm = strm ;
00099         _strm_owned = strm_owned ;
00100         _buffered = false ;
00101     }
00102 }
00103 
00104 BESInfo::~BESInfo()
00105 {
00106     if( _strm && _strm_owned )
00107     {
00108         delete _strm ;
00109         _strm = 0 ;
00110     }
00111 }
00112 
00120 void
00121 BESInfo::begin_response( const string &response_name,
00122                          BESDataHandlerInterface &dhi )
00123 {
00124     _response_started = true ;
00125     _response_name = response_name ;
00126 }
00127 
00128 void
00129 BESInfo::end_response( )
00130 {
00131     _response_started = false ;
00132     if( _tags.size() )
00133     {
00134         string s = "Not all tags were ended in info response" ;
00135         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00136     }
00137 }
00138 
00139 void
00140 BESInfo::begin_tag( const string &tag_name,
00141                     map<string,string> *attrs )
00142 {
00143     _tags.push( tag_name ) ;
00144 }
00145 
00146 void
00147 BESInfo::end_tag( const string &tag_name )
00148 {
00149     if( _tags.size() == 0 || _tags.top() != tag_name )
00150     {
00151         string s = (string)"tag " + tag_name
00152                    + " already ended or not started" ;
00153         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00154     }
00155     else
00156     {
00157         _tags.pop() ;
00158     }
00159 }
00160 
00166 void
00167 BESInfo::add_data( const string &s )
00168 {
00169     // If not buffered then we've been passed a stream to use.
00170     // If it is buffered then we created the stream.
00171     (*_strm) << s ;
00172 }
00173 
00189 void
00190 BESInfo::add_data_from_file( const string &key, const string &name )
00191 {
00192     bool found = false ;
00193     string file = TheBESKeys::TheKeys()->get_key( key, found ) ;
00194     if( found == false )
00195     {
00196         add_data( name + " file key " + key + " not found, information not available\n" ) ;
00197     }
00198     else
00199     {
00200         ifstream ifs( file.c_str() ) ;
00201         int myerrno = errno ;
00202         if( !ifs )
00203         {
00204             string serr = name + " file " + file
00205                           + " not found, information not available: " ;
00206             char *err = strerror( myerrno ) ;
00207             if( err )
00208                 serr += err ;
00209             else
00210                 serr += "Unknown error" ;
00211 
00212             serr += "\n" ;
00213 
00214             add_data( serr ) ;
00215         }
00216         else
00217         {
00218             char line[BES_INFO_FILE_BUFFER_SIZE] ;
00219             while( !ifs.eof() )
00220             {
00221                 ifs.getline( line, BES_INFO_FILE_BUFFER_SIZE ) ;
00222                 if( !ifs.eof() )
00223                 {
00224                     add_data( line ) ;
00225                     add_data( "\n" ) ;
00226                 }
00227             }
00228             ifs.close() ;
00229         }
00230     }
00231 }
00232 
00241 void
00242 BESInfo::add_exception( BESError &e, const string &administrator )
00243 {
00244     begin_tag( "BESError" ) ;
00245     ostringstream stype ;
00246     stype << e.get_error_type() ;
00247     add_tag( "Type", stype.str() ) ;
00248     add_tag( "Message", e.get_message() ) ;
00249     add_tag( "Administrator", administrator ) ;
00250 #ifdef BES_DEVELOPER
00251     begin_tag( "Location" ) ;
00252     add_tag( "File", e.get_file() ) ;
00253     ostringstream sline ;
00254     sline << e.get_line() ;
00255     add_tag( "Line", sline.str() ) ;
00256     end_tag( "Location" ) ;
00257 #endif
00258     end_tag( "BESError" ) ;
00259 }
00260 
00269 void
00270 BESInfo::print( ostream &strm )
00271 {
00272     if( _buffered )
00273     {
00274         strm << ((ostringstream *)_strm)->str() ;
00275     }
00276 }
00277 
00285 void
00286 BESInfo::dump( ostream &strm ) const
00287 {
00288     strm << BESIndent::LMarg << "BESInfo::dump - ("
00289                              << (void *)this << ")" << endl ;
00290     BESIndent::Indent() ;
00291     strm << BESIndent::LMarg << "response name: " << _response_name << endl ;
00292     strm << BESIndent::LMarg << "is it buffered? " << _buffered << endl ;
00293     strm << BESIndent::LMarg << "has response been started? " << _response_started << endl ;
00294     if( _tags.size() )
00295     {
00296         strm << BESIndent::LMarg << "tags:" << endl ;
00297         BESIndent::Indent() ;
00298         stack<string> temp_tags = _tags ;
00299         while( !temp_tags.empty() )
00300         {
00301             string tag = temp_tags.top() ;
00302             strm << BESIndent::LMarg << tag << endl ;
00303             temp_tags.pop() ;
00304         }
00305         BESIndent::UnIndent() ;
00306     }
00307     else
00308     {
00309         strm << BESIndent::LMarg << "tags: empty" << endl ;
00310     }
00311     BESIndent::UnIndent() ;
00312 }
00313 

Generated on 19 Feb 2010 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.6.1