BESRequestHandlerList.cc

Go to the documentation of this file.
00001 // BESRequestHandlerList.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 "BESRequestHandlerList.h"
00034 #include "BESRequestHandler.h"
00035 #include "BESInternalError.h"
00036 #include "BESDataNames.h"
00037 
00038 BESRequestHandlerList *BESRequestHandlerList::_instance = 0 ;
00039 
00049 bool
00050 BESRequestHandlerList::add_handler( const string &handler_name,
00051                                     BESRequestHandler *handler_object )
00052 {
00053     if( find_handler( handler_name ) == 0 )
00054     {
00055         _handler_list[handler_name] = handler_object ;
00056         return true ;
00057     }
00058     return false ;
00059 }
00060 
00073 BESRequestHandler *
00074 BESRequestHandlerList::remove_handler( const string &handler_name )
00075 {
00076     BESRequestHandler *ret = 0 ;
00077     BESRequestHandlerList::Handler_iter i ;
00078     i = _handler_list.find( handler_name ) ;
00079     if( i != _handler_list.end() )
00080     {
00081         ret = (*i).second;
00082         _handler_list.erase( i ) ;
00083     }
00084     return ret ;
00085 }
00086 
00093 BESRequestHandler *
00094 BESRequestHandlerList::find_handler( const string &handler_name )
00095 {
00096     BESRequestHandlerList::Handler_citer i ;
00097     i = _handler_list.find( handler_name ) ;
00098     if( i != _handler_list.end() )
00099     {
00100         return (*i).second;
00101     }
00102     return 0 ;
00103 }
00104 
00112 BESRequestHandlerList::Handler_citer
00113 BESRequestHandlerList::get_first_handler()
00114 {
00115     return _handler_list.begin() ;
00116 }
00117 
00123 BESRequestHandlerList::Handler_citer
00124 BESRequestHandlerList::get_last_handler()
00125 {
00126     return _handler_list.end() ;
00127 }
00128 
00136 string
00137 BESRequestHandlerList::get_handler_names()
00138 {
00139     string ret ;
00140     bool first_name = true ;
00141     BESRequestHandlerList::Handler_citer i = _handler_list.begin() ;
00142     for( ; i != _handler_list.end(); i++ )
00143     {
00144         if( !first_name )
00145             ret += ", " ;
00146         ret += (*i).first ;
00147         first_name = false ;
00148     }
00149     return ret ;
00150 }
00151 
00172 void
00173 BESRequestHandlerList::execute_each( BESDataHandlerInterface &dhi )
00174 {
00175     dhi.first_container() ;
00176     while( dhi.container )
00177     {
00178         execute_current( dhi ) ;
00179         dhi.next_container() ;
00180     }
00181 }
00182 
00198 void
00199 BESRequestHandlerList::execute_all( BESDataHandlerInterface &dhi )
00200 {
00201     BESRequestHandlerList::Handler_citer i = get_first_handler() ;
00202     BESRequestHandlerList::Handler_citer ie = get_last_handler() ;
00203     for( ; i != ie; i++ ) 
00204     {
00205         BESRequestHandler *rh = (*i).second ;
00206         p_request_handler p = rh->find_handler( dhi.action ) ;
00207         if( p )
00208         {
00209             p( dhi ) ;
00210         }
00211     }
00212 }
00213 
00232 void
00233 BESRequestHandlerList::execute_once( BESDataHandlerInterface &dhi )
00234 {
00235     dhi.first_container() ;
00236     execute_current( dhi ) ;
00237 }
00238 
00255 void
00256 BESRequestHandlerList::execute_current( BESDataHandlerInterface &dhi )
00257 {
00258     if( dhi.container )
00259     {
00260         BESRequestHandler *rh = find_handler( (dhi.container->get_container_type()).c_str() ) ;
00261         if( rh )
00262         {
00263             p_request_handler p = rh->find_handler( dhi.action ) ;
00264             // if we can't find the function, see if there is a catch all
00265             // function that handles or redirects the request.
00266             if( !p )
00267             {
00268                 p = rh->find_handler( BES_REQUEST_HANDLER_CATCH_ALL ) ;
00269             }
00270 
00271             if( p )
00272             {
00273                 p( dhi ) ;
00274                 if( dhi.container )
00275                 {
00276                     string c_list = dhi.data[REAL_NAME_LIST] ;
00277                     if( !c_list.empty() )
00278                        c_list += ", " ;
00279                     c_list += dhi.container->get_real_name() ;
00280                     dhi.data[REAL_NAME_LIST] = c_list ;
00281                 }
00282             } else {
00283                 string se = "Request handler \""
00284                             + dhi.container->get_container_type()
00285                             + "\" does not handle the response type \""
00286                             + dhi.action + "\"" ;
00287                 throw BESInternalError( se, __FILE__, __LINE__ ) ;
00288             }
00289         } else {
00290             string se = "The data handler \""
00291                         + dhi.container->get_container_type()
00292                         + "\" does not exist" ;
00293             throw BESInternalError( se, __FILE__, __LINE__ ) ;
00294         }
00295     }
00296 }
00297 
00305 void
00306 BESRequestHandlerList::dump( ostream &strm ) const
00307 {
00308     strm << BESIndent::LMarg << "BESRequestHandlerList::dump - ("
00309                              << (void *)this << ")" << endl ;
00310     BESIndent::Indent() ;
00311     if( _handler_list.size() )
00312     {
00313         strm << BESIndent::LMarg << "registered handlers:" << endl ;
00314         BESIndent::Indent() ;
00315         BESRequestHandlerList::Handler_citer i = _handler_list.begin() ;
00316         BESRequestHandlerList::Handler_citer ie = _handler_list.end() ;
00317         for( ; i != ie; i++ ) 
00318         {
00319             BESRequestHandler *rh = (*i).second ;
00320             rh->dump( strm ) ;
00321         }
00322         BESIndent::UnIndent() ;
00323     }
00324     else
00325     {
00326         strm << BESIndent::LMarg << "registered handlers: none" << endl ;
00327     }
00328     BESIndent::UnIndent() ;
00329 }
00330 
00331 BESRequestHandlerList *
00332 BESRequestHandlerList::TheList()
00333 {
00334     if( _instance == 0 )
00335     {
00336         _instance = new BESRequestHandlerList ;
00337     }
00338     return _instance ;
00339 }
00340 

Generated on Wed May 12 09:53:07 2010 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.4.7