BESCatalogList.cc

Go to the documentation of this file.
00001 // BESCatalogList.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 <sstream>
00034 
00035 using std::ostringstream ;
00036 
00037 #include "BESCatalogList.h"
00038 #include "BESCatalog.h"
00039 #include "BESInfo.h"
00040 #include "BESSyntaxUserError.h"
00041 #include "TheBESKeys.h"
00042 #include "BESDapNames.h"
00043 
00044 BESCatalogList *BESCatalogList::_instance = 0 ;
00045 
00050 BESCatalogList::BESCatalogList()
00051 {
00052     bool found = false ;
00053     string key = "BES.Catalog.Default" ;
00054     try
00055     {
00056         TheBESKeys::TheKeys()->get_value( key, _default_catalog, found ) ;
00057     }
00058     catch( BESError & )
00059     {
00060         found = false ;
00061     }
00062     if( !found || _default_catalog.empty() )
00063     {
00064         _default_catalog = BES_DEFAULT_CATALOG ;
00065     }
00066 }
00067 
00072 BESCatalogList::~BESCatalogList()
00073 {
00074     catalog_iter i = _catalogs.begin() ;
00075     catalog_iter e = _catalogs.end() ;
00076     for( ; i != e; i++ )
00077     {
00078         BESCatalog *catalog = (*i).second ;
00079         if( catalog ) delete catalog ;
00080     }
00081 }
00082 
00090 bool
00091 BESCatalogList::add_catalog(BESCatalog * catalog)
00092 {
00093     bool result = false;
00094     if (find_catalog(catalog->get_catalog_name()) == 0) {
00095 #if 0
00096         _catalogs[catalog->get_catalog_name()] = catalog;
00097 #endif
00098         string name = catalog->get_catalog_name() ;
00099         std::pair<const std::string, BESCatalog*> p =
00100             std::make_pair( name, catalog ) ;
00101         result = _catalogs.insert(p).second;
00102 #if 0
00103         result = true;
00104 #endif
00105     }
00106     return result;
00107 }
00108 
00119 bool
00120 BESCatalogList::ref_catalog( const string &catalog_name )
00121 {
00122     bool ret = false ;
00123     BESCatalog *cat = 0 ;
00124     BESCatalogList::catalog_iter i ;
00125     i = _catalogs.find( catalog_name ) ;
00126     if( i != _catalogs.end() )
00127     {
00128         cat = (*i).second;
00129         cat->reference_catalog() ;
00130         ret = true ;
00131     }
00132     return ret ;
00133 }
00134 
00146 bool
00147 BESCatalogList::deref_catalog( const string &catalog_name )
00148 {
00149     bool ret = false ;
00150     BESCatalog *cat = 0 ;
00151     BESCatalogList::catalog_iter i ;
00152     i = _catalogs.find( catalog_name ) ;
00153     if( i != _catalogs.end() )
00154     {
00155         cat = (*i).second;
00156         if( !cat->dereference_catalog() )
00157         {
00158             _catalogs.erase( i ) ;
00159             delete cat ;
00160         }
00161         ret = true ;
00162     }
00163     return ret ;
00164 }
00165 
00172 BESCatalog *
00173 BESCatalogList::find_catalog( const string &catalog_name )
00174 {
00175     BESCatalog *ret = 0 ;
00176     BESCatalogList::catalog_citer i ;
00177     i = _catalogs.find( catalog_name ) ;
00178     if( i != _catalogs.end() )
00179     {
00180         ret = (*i).second;
00181     }
00182     return ret ;
00183 }
00184 
00217 void
00218 BESCatalogList::show_catalog( const string &container,
00219                               const string &coi,
00220                               BESInfo *info )
00221 {
00222     string cat_name ;
00223     string cat_node ;
00224     BESCatalog *catalog = 0 ;
00225     if( container.empty() )
00226     {
00227         if( _catalogs.size() == 1 )
00228         {
00229             catalog_citer i = _catalogs.begin() ;
00230             catalog = (*i).second ;
00231             catalog->show_catalog( container, coi, info ) ;
00232         }
00233         else
00234         {
00235             map<string,string> props ;
00236             props["name"] = "/" ;
00237             props["catalog"] = "/" ;
00238             ostringstream ssize ;
00239             ssize << _catalogs.size() ;
00240             props["count"] = ssize.str() ;
00241             props["node"] = "true" ;
00242             info->begin_tag( "dataset", &props ) ;
00243 
00244             catalog_citer i = _catalogs.begin() ;
00245             catalog_citer e = _catalogs.end() ;
00246             for( ; i != e; i++ )
00247             {
00248                 BESCatalog *catalog = (*i).second ;
00249                 catalog->show_catalog( "", SHOW_INFO_RESPONSE, info ) ;
00250             }
00251 
00252             info->end_tag( "dataset" ) ;
00253         }
00254     }
00255     else
00256     {
00257         string::size_type colon = container.find( ":" ) ;
00258         if( colon == string::npos )
00259         {
00260             // no colon, so if only one catalog then use it, otherwise use
00261             // the default name
00262             if( _catalogs.size() == 1 )
00263             {
00264                 catalog_citer i = _catalogs.begin() ;
00265                 catalog = (*i).second ;
00266                 cat_name = catalog->get_catalog_name() ;
00267             }
00268             else
00269             {
00270                 cat_name = _default_catalog ;
00271             }
00272             cat_node = container ;
00273         }
00274         else
00275         {
00276             // there is a colon. The name is the part before the colon.
00277             cat_name = container.substr( 0, colon ) ;
00278             cat_node = container.substr( colon+1, container.length() - colon ) ;
00279         }
00280 
00281         catalog = _catalogs[ cat_name ] ;
00282         if( catalog )
00283         {
00284             catalog->show_catalog( cat_node, coi, info ) ;
00285         }
00286         else
00287         {
00288             string serr = "The catalog " + cat_name + " does not exist." ;
00289             throw BESSyntaxUserError( serr, __FILE__, __LINE__ ) ;
00290         }
00291     }
00292 }
00293 
00296 BESCatalogList *
00297 BESCatalogList::TheCatalogList()
00298 {
00299     if( _instance == 0 )
00300     {
00301         _instance = new BESCatalogList ;
00302     }
00303     return _instance ;
00304 }
00305 
00313 void
00314 BESCatalogList::dump( ostream &strm ) const
00315 {
00316     strm << BESIndent::LMarg << "BESCatalogList::dump - ("
00317                              << (void *)this << ")" << endl ;
00318     BESIndent::Indent() ;
00319     if( _catalogs.size() )
00320     {
00321         strm << BESIndent::LMarg << "catalog list:" << endl ;
00322         BESIndent::Indent() ;
00323         catalog_citer i = _catalogs.begin() ;
00324         catalog_citer e = _catalogs.end() ;
00325         for( ; i != e; i++ )
00326         {
00327             BESCatalog *catalog = (*i).second ;
00328             strm << BESIndent::LMarg << (*i).first << catalog << endl ;
00329         }
00330         BESIndent::UnIndent() ;
00331     }
00332     else
00333     {
00334         strm << BESIndent::LMarg << "catalog list: empty" << endl ;
00335     }
00336     BESIndent::UnIndent() ;
00337 }
00338 

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