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     vector<string> vals ;
00082     string b ;
00083     TheBESKeys::TheKeys()->get_value( key, b, found ) ;
00084     if( b == "true" || b == "True" || b == "TRUE" ||
00085         b == "yes" || b == "Yes" || b == "YES" )
00086     {
00087         _strm = new ostringstream ;
00088         _strm_owned = true ;
00089         _buffered = true ;
00090         if( strm && strm_owned )
00091             delete strm ;
00092     }
00093     else
00094     {
00095         if( !strm )
00096         {
00097             string s = "Informational response not buffered but no stream passed" ;
00098             throw BESInternalError( s, __FILE__, __LINE__ ) ;
00099         }
00100         _strm = strm ;
00101         _strm_owned = strm_owned ;
00102         _buffered = false ;
00103     }
00104 }
00105 
00106 BESInfo::~BESInfo()
00107 {
00108     if( _strm && _strm_owned )
00109     {
00110         delete _strm ;
00111         _strm = 0 ;
00112     }
00113 }
00114 
00122 void
00123 BESInfo::begin_response( const string &response_name,
00124                          BESDataHandlerInterface &dhi )
00125 {
00126     _response_started = true ;
00127     _response_name = response_name ;
00128 }
00129 
00130 void
00131 BESInfo::end_response( )
00132 {
00133     _response_started = false ;
00134     if( _tags.size() )
00135     {
00136         string s = "Not all tags were ended in info response" ;
00137         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00138     }
00139 }
00140 
00141 void
00142 BESInfo::begin_tag( const string &tag_name,
00143                     map<string,string> *attrs )
00144 {
00145     _tags.push( tag_name ) ;
00146 }
00147 
00148 void
00149 BESInfo::end_tag( const string &tag_name )
00150 {
00151     if( _tags.size() == 0 || _tags.top() != tag_name )
00152     {
00153         string s = (string)"tag " + tag_name
00154                    + " already ended or not started" ;
00155         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00156     }
00157     else
00158     {
00159         _tags.pop() ;
00160     }
00161 }
00162 
00168 void
00169 BESInfo::add_data( const string &s )
00170 {
00171     // If not buffered then we've been passed a stream to use.
00172     // If it is buffered then we created the stream.
00173     (*_strm) << s ;
00174 }
00175 
00191 void
00192 BESInfo::add_data_from_file( const string &key, const string &name )
00193 {
00194     bool found = false ;
00195     string file ;
00196     try
00197     {
00198         TheBESKeys::TheKeys()->get_value( key, file, found ) ;
00199     }
00200     catch( ... )
00201     {
00202         found = false ;
00203     }
00204     if( found == false )
00205     {
00206         add_data( name + " file key " + key
00207                   + " not found, information not available\n" ) ;
00208     }
00209     else
00210     {
00211         ifstream ifs( file.c_str() ) ;
00212         int myerrno = errno ;
00213         if( !ifs )
00214         {
00215             string serr = name + " file " + file
00216                           + " not found, information not available: " ;
00217             char *err = strerror( myerrno ) ;
00218             if( err )
00219                 serr += err ;
00220             else
00221                 serr += "Unknown error" ;
00222 
00223             serr += "\n" ;
00224 
00225             add_data( serr ) ;
00226         }
00227         else
00228         {
00229             char line[BES_INFO_FILE_BUFFER_SIZE] ;
00230             while( !ifs.eof() )
00231             {
00232                 ifs.getline( line, BES_INFO_FILE_BUFFER_SIZE ) ;
00233                 if( !ifs.eof() )
00234                 {
00235                     add_data( line ) ;
00236                     add_data( "\n" ) ;
00237                 }
00238             }
00239             ifs.close() ;
00240         }
00241     }
00242 }
00243 
00254 void
00255 BESInfo::add_exception( BESError &e, const string &admin )
00256 {
00257     begin_tag( "BESError" ) ;
00258     ostringstream stype ;
00259     stype << e.get_error_type() ;
00260     add_tag( "Type", stype.str() ) ;
00261     add_tag( "Message", e.get_message() ) ;
00262     add_tag( "Administrator", admin ) ;
00263 #ifdef BES_DEVELOPER
00264     begin_tag( "Location" ) ;
00265     add_tag( "File", e.get_file() ) ;
00266     ostringstream sline ;
00267     sline << e.get_line() ;
00268     add_tag( "Line", sline.str() ) ;
00269     end_tag( "Location" ) ;
00270 #endif
00271     end_tag( "BESError" ) ;
00272 }
00273 
00282 void
00283 BESInfo::print( ostream &strm )
00284 {
00285     if( _buffered )
00286     {
00287         strm << ((ostringstream *)_strm)->str() ;
00288     }
00289 }
00290 
00298 void
00299 BESInfo::dump( ostream &strm ) const
00300 {
00301     strm << BESIndent::LMarg << "BESInfo::dump - ("
00302                              << (void *)this << ")" << endl ;
00303     BESIndent::Indent() ;
00304     strm << BESIndent::LMarg << "response name: " << _response_name << endl ;
00305     strm << BESIndent::LMarg << "is it buffered? " << _buffered << endl ;
00306     strm << BESIndent::LMarg << "has response been started? " << _response_started << endl ;
00307     if( _tags.size() )
00308     {
00309         strm << BESIndent::LMarg << "tags:" << endl ;
00310         BESIndent::Indent() ;
00311         stack<string> temp_tags = _tags ;
00312         while( !temp_tags.empty() )
00313         {
00314             string tag = temp_tags.top() ;
00315             strm << BESIndent::LMarg << tag << endl ;
00316             temp_tags.pop() ;
00317         }
00318         BESIndent::UnIndent() ;
00319     }
00320     else
00321     {
00322         strm << BESIndent::LMarg << "tags: empty" << endl ;
00323     }
00324     BESIndent::UnIndent() ;
00325 }
00326 

Generated on Tue May 11 20:02:05 2010 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.4.7