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,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 "BESRequestHandlerList.h" 00034 #include "BESRequestHandler.h" 00035 #include "BESHandlerException.h" 00036 #include "BESDataNames.h" 00037 00038 BESRequestHandlerList *BESRequestHandlerList::_instance = 0 ; 00039 00049 bool 00050 BESRequestHandlerList::add_handler( 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( 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( 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->is_valid() ) 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( p ) 00265 { 00266 p( dhi ) ; 00267 if( dhi.container ) 00268 { 00269 string c_list = dhi.data[REAL_NAME_LIST] ; 00270 if( !c_list.empty() ) 00271 c_list += ", " ; 00272 c_list += dhi.container->get_real_name() ; 00273 dhi.data[REAL_NAME_LIST] = c_list ; 00274 } 00275 } else { 00276 string se = "Request handler \"" 00277 + dhi.container->get_container_type() 00278 + "\" does not handle the response type \"" 00279 + dhi.action + "\"" ; 00280 throw BESHandlerException( se, __FILE__, __LINE__ ) ; 00281 } 00282 } else { 00283 string se = "The data handler \"" 00284 + dhi.container->get_container_type() 00285 + "\" does not exist" ; 00286 throw BESHandlerException( se, __FILE__, __LINE__ ) ; 00287 } 00288 } 00289 } 00290 00298 void 00299 BESRequestHandlerList::dump( ostream &strm ) const 00300 { 00301 strm << BESIndent::LMarg << "BESRequestHandlerList::dump - (" 00302 << (void *)this << ")" << endl ; 00303 BESIndent::Indent() ; 00304 if( _handler_list.size() ) 00305 { 00306 strm << BESIndent::LMarg << "registered handlers:" << endl ; 00307 BESIndent::Indent() ; 00308 BESRequestHandlerList::Handler_citer i = _handler_list.begin() ; 00309 BESRequestHandlerList::Handler_citer ie = _handler_list.end() ; 00310 for( ; i != ie; i++ ) 00311 { 00312 BESRequestHandler *rh = (*i).second ; 00313 rh->dump( strm ) ; 00314 } 00315 BESIndent::UnIndent() ; 00316 } 00317 else 00318 { 00319 strm << BESIndent::LMarg << "registered handlers: none" << endl ; 00320 } 00321 BESIndent::UnIndent() ; 00322 } 00323 00324 BESRequestHandlerList * 00325 BESRequestHandlerList::TheList() 00326 { 00327 if( _instance == 0 ) 00328 { 00329 _instance = new BESRequestHandlerList ; 00330 } 00331 return _instance ; 00332 } 00333