BESContainerStorageList.cc

Go to the documentation of this file.
00001 // BESContainerStorageList.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 <iostream>
00034 
00035 using std::endl ;
00036 
00037 #include "BESContainerStorageList.h"
00038 #include "BESContainerStorage.h"
00039 #include "BESSyntaxUserError.h"
00040 #include "BESContainer.h"
00041 #include "TheBESKeys.h"
00042 #include "BESLog.h"
00043 #include "BESInfo.h"
00044 
00045 BESContainerStorageList *BESContainerStorageList::_instance = 0 ;
00046 
00047 BESContainerStorageList::BESContainerStorageList()
00048     : _first( 0 )
00049 {
00050 }
00051 
00052 BESContainerStorageList::~BESContainerStorageList()
00053 {
00054     BESContainerStorageList::persistence_list *pl = _first ;
00055     while( pl )
00056     {
00057         if( pl->_persistence_obj )
00058         {
00059             delete pl->_persistence_obj ;
00060         }
00061         BESContainerStorageList::persistence_list *next = pl->_next ;
00062         delete pl ;
00063         pl = next ;
00064     }
00065 }
00066 
00079 bool
00080 BESContainerStorageList::add_persistence( BESContainerStorage *cp )
00081 {
00082     bool ret = false ;
00083     if( !_first )
00084     {
00085         _first = new BESContainerStorageList::persistence_list ;
00086         _first->_persistence_obj = cp ;
00087         _first->_reference = 1 ;
00088         _first->_next = 0 ;
00089         ret = true ;
00090     }
00091     else
00092     {
00093         BESContainerStorageList::persistence_list *pl = _first ;
00094         bool done = false ;
00095         while( done == false )
00096         {
00097             if( pl->_persistence_obj->get_name() != cp->get_name() )
00098             {
00099                 if( pl->_next )
00100                 {
00101                     pl = pl->_next ;
00102                 }
00103                 else
00104                 {
00105                     pl->_next = new BESContainerStorageList::persistence_list ;
00106                     pl->_next->_reference = 1 ;
00107                     pl->_next->_persistence_obj = cp ;
00108                     pl->_next->_next = 0 ;
00109                     done = true ;
00110                     ret = true ;
00111                 }
00112             }
00113             else
00114             {
00115                 done = true ;
00116                 ret = false ;
00117             }
00118         }
00119     }
00120     return ret ;
00121 }
00122 
00133 bool
00134 BESContainerStorageList::ref_persistence( const string &persist_name )
00135 {
00136     bool ret = false ;
00137     BESContainerStorageList::persistence_list *pl = _first ;
00138 
00139     bool done = false ;
00140     while( done == false )
00141     {
00142         if( pl )
00143         {
00144             if( pl->_persistence_obj &&
00145                 pl->_persistence_obj->get_name() == persist_name )
00146             {
00147                 done = true ;
00148                 ret = true ;
00149                 pl->_reference++ ;
00150             }
00151             else
00152             {
00153                 pl = pl->_next ;
00154             }
00155         }
00156         else
00157         {
00158             done = true ;
00159         }
00160     }
00161     return ret ;
00162 }
00163 
00176 bool
00177 BESContainerStorageList::deref_persistence( const string &persist_name )
00178 {
00179     bool ret = false ;
00180     BESContainerStorageList::persistence_list *pl = _first ;
00181     BESContainerStorageList::persistence_list *last = 0 ;
00182 
00183     bool done = false ;
00184     while( done == false )
00185     {
00186         if( pl )
00187         {
00188             if( pl->_persistence_obj &&
00189                 pl->_persistence_obj->get_name() == persist_name )
00190             {
00191                 ret = true ;
00192                 done = true ;
00193                 pl->_reference-- ;
00194                 if( !pl->_reference )
00195                 {
00196                     if( pl == _first )
00197                     {
00198                         _first = _first->_next ;
00199                     }
00200                     else
00201                     {
00202                         if (!last)
00203                                 throw BESInternalError("ContainerStorageList last is null", __FILE__, __LINE__);
00204                         last->_next = pl->_next ;
00205                     }
00206                     delete pl->_persistence_obj ;
00207                     delete pl ;
00208                     pl = 0 ;
00209                 }
00210             }
00211             else
00212             {
00213                 last = pl ;
00214                 pl = pl->_next ;
00215             }
00216         }
00217         else
00218         {
00219             done = true ;
00220         }
00221     }
00222 
00223     return ret ;
00224 }
00225 
00234 BESContainerStorage *
00235 BESContainerStorageList::find_persistence( const string &persist_name )
00236 {
00237     BESContainerStorage *ret = NULL ;
00238     BESContainerStorageList::persistence_list *pl = _first ;
00239     bool done = false ;
00240     while( done == false )
00241     {
00242         if( pl )
00243         {
00244             if( persist_name == pl->_persistence_obj->get_name() )
00245             {
00246                 ret = pl->_persistence_obj ;
00247                 done = true ;
00248             }
00249             else
00250             {
00251                 pl = pl->_next ;
00252             }
00253         }
00254         else
00255         {
00256             done = true ;
00257         }
00258     }
00259     return ret ;
00260 }
00261 
00262 bool
00263 BESContainerStorageList::isnice()
00264 {
00265     bool ret = false ;
00266     string key = "BES.Container.Persistence" ;
00267     bool found = false ;
00268     string isnice = TheBESKeys::TheKeys()->get_key( key, found ) ;
00269     if( isnice == "Nice" || isnice == "nice" || isnice == "NICE" )
00270         ret = true ;
00271     else 
00272         ret = false ;
00273     return ret ;
00274 }
00275 
00299 BESContainer *
00300 BESContainerStorageList::look_for( const string &sym_name )
00301 {
00302     BESContainer *ret_container = 0 ;
00303     BESContainerStorageList::persistence_list *pl = _first ;
00304     bool done = false ;
00305     while( done == false )
00306     {
00307         if( pl )
00308         {
00309             ret_container = pl->_persistence_obj->look_for( sym_name ) ;
00310             if( ret_container )
00311             {
00312                 done = true ;
00313             }
00314             else
00315             {
00316                 pl = pl->_next ;
00317             }
00318         }
00319         else
00320         {
00321             done = true ;
00322         }
00323     }
00324     if( !ret_container )
00325     {
00326         if( isnice() )
00327         {
00328             (*BESLog::TheLog()) << "Could not find the symbolic name "
00329                                 << sym_name << endl ;
00330         }
00331         else
00332         {
00333             string s = (string)"Could not find the symbolic name "
00334                        + sym_name ;
00335             throw BESSyntaxUserError( s, __FILE__, __LINE__ ) ;
00336         }
00337     }
00338 
00339     return ret_container ;
00340 }
00341 
00354 void
00355 BESContainerStorageList::show_containers( BESInfo &info )
00356 {
00357     BESContainerStorageList::persistence_list *pl = _first ;
00358     while( pl )
00359     {
00360         map<string,string> props ;
00361         props["name"] = pl->_persistence_obj->get_name() ;
00362         info.begin_tag( "store", &props ) ;
00363         pl->_persistence_obj->show_containers( info ) ;
00364         info.end_tag( "store" ) ;
00365         pl = pl->_next ;
00366     }
00367 }
00368 
00376 void
00377 BESContainerStorageList::dump( ostream &strm ) const
00378 {
00379     strm << BESIndent::LMarg << "BESContainerStorageList::dump - ("
00380                              << (void *)this << ")" << endl ;
00381     BESIndent::Indent() ;
00382     BESContainerStorageList::persistence_list *pl = _first ;
00383     if( pl )
00384     {
00385         strm << BESIndent::LMarg << "container storage:" << endl ;
00386         BESIndent::Indent() ;
00387         while( pl )
00388         {
00389             pl->_persistence_obj->dump( strm ) ;
00390             pl = pl->_next ;
00391         }
00392         BESIndent::UnIndent() ;
00393     }
00394     else
00395     {
00396         strm << BESIndent::LMarg << "container storage: empty" << endl ;
00397     }
00398     BESIndent::UnIndent() ;
00399 }
00400 
00401 BESContainerStorageList *
00402 BESContainerStorageList::TheList()
00403 {
00404     if( _instance == 0 )
00405     {
00406         _instance = new BESContainerStorageList ;
00407     }
00408     return _instance ;
00409 }
00410 

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