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,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 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 "BESCatalogList.h"
00034 #include "BESCatalog.h"
00035 #include "BESResponseException.h"
00036 #include "BESInfo.h"
00037 #include "BESHandlerException.h"
00038 
00039 BESCatalogList *BESCatalogList::_instance = 0 ;
00040 
00045 BESCatalogList::~BESCatalogList()
00046 {
00047     catalog_iter i = _catalogs.begin() ;
00048     catalog_iter e = _catalogs.end() ;
00049     for( ; i != e; i++ )
00050     {
00051         BESCatalog *catalog = (*i).second ;
00052         if( catalog ) delete catalog ;
00053     }
00054 }
00055 
00063 bool
00064 BESCatalogList::add_catalog(BESCatalog * catalog)
00065 {
00066     bool stat = false;
00067     if (find_catalog(catalog->get_catalog_name()) == 0) {
00068 #if 0
00069         _catalogs[catalog->get_catalog_name()] = catalog;
00070 #endif
00071         string name = catalog->get_catalog_name() ;
00072         std::pair<const std::string, BESCatalog*> p =
00073             std::make_pair( name, catalog ) ;
00074         stat = _catalogs.insert(p).second;
00075 #if 0
00076         stat = true;
00077 #endif
00078     }
00079     return stat;
00080 }
00081 
00091 bool
00092 BESCatalogList::del_catalog( const string &catalog_name )
00093 {
00094     bool ret = false ;
00095     BESCatalog *cat = 0 ;
00096     BESCatalogList::catalog_iter i ;
00097     i = _catalogs.find( catalog_name ) ;
00098     if( i != _catalogs.end() )
00099     {
00100         cat = (*i).second;
00101         _catalogs.erase( i ) ;
00102         delete cat ;
00103         ret = true ;
00104     }
00105     return ret ;
00106 }
00107 
00114 BESCatalog *
00115 BESCatalogList::find_catalog( const string &catalog_name )
00116 {
00117     BESCatalog *ret = 0 ;
00118     BESCatalogList::catalog_citer i ;
00119     i = _catalogs.find( catalog_name ) ;
00120     if( i != _catalogs.end() )
00121     {
00122         ret = (*i).second;
00123     }
00124     return ret ;
00125 }
00126 
00155 void
00156 BESCatalogList::show_catalog( const string &container,
00157                               const string &coi,
00158                               BESInfo *info )
00159 {
00160     bool done = false ;
00161     // if there is only one catalog then go to it to show the catalog
00162     if( _catalogs.size() == 1 )
00163     {
00164         catalog_citer i = _catalogs.begin() ;
00165         BESCatalog *catalog = (*i).second ;
00166         done = catalog->show_catalog( container, coi, info ) ;
00167     }
00168     else if( _catalogs.size() != 0 )
00169     {
00170         // This means there are more than one catalog. If the specified container
00171         // is empty then display the names of the catalogs
00172         if( container.empty() )
00173         {
00174             map<string,string> a1 ;
00175             a1["thredds_collection"] = "\"true\"" ;
00176             a1["isData"] = "\"false\"" ;
00177             info->begin_tag( "dataset", &a1 ) ;
00178             info->add_tag( "name", "/" ) ;
00179 
00180             a1["catalogRoot"] = "\"true\"" ;
00181             catalog_citer i = _catalogs.begin() ;
00182             catalog_citer e = _catalogs.end() ;
00183             for( ; i != e; i++ )
00184             {
00185                 string name = (*i).first ;
00186                 BESCatalog *catalog = (*i).second ;
00187                 info->begin_tag( "dataset", &a1 ) ;
00188                 info->add_tag( "name", name ) ;
00189                 info->end_tag( "dataset" ) ;
00190             }
00191 
00192             info->end_tag( "dataset" ) ;
00193 
00194             done = true ;
00195         }
00196         else
00197         {
00198             // if a container is specified then the name of the catalog
00199             // comes first, followed by a colon. If no colon, then no
00200             // catalog specified, which means error
00201             string::size_type colon = container.find( ":" ) ;
00202             if( colon == string::npos )
00203             {
00204                 string serr = "Multiple catalogs present but none specified in request" ;
00205                 throw BESHandlerException( serr, __FILE__, __LINE__ ) ;
00206             }
00207             else
00208             {
00209                 // there is a colon. The name is the part before the colon.
00210                 string name = container.substr( 0, colon ) ;
00211                 string rest = container.substr( colon+1, container.length() - colon ) ;
00212                 BESCatalog *catalog = _catalogs[ name ] ;
00213                 if( catalog )
00214                 {
00215                     done = catalog->show_catalog( rest, coi, info ) ;
00216                 }
00217                 else
00218                 {
00219                     string serr = "The catalog " + name + " does not exist." ;
00220                     throw BESHandlerException( serr, __FILE__, __LINE__ ) ;
00221                 }
00222             }
00223         }
00224     }
00225     if( done == false )
00226     {
00227         string serr ;
00228         if( container != "" )
00229         {
00230             serr = (string)"Unable to find catalog information for container "
00231                    + container ;
00232         }
00233         else
00234         {
00235             serr = "Unable to find catalog information for root" ;
00236         }
00237         throw BESHandlerException( serr, __FILE__, __LINE__ ) ;
00238     }
00239 }
00240 
00243 BESCatalogList *
00244 BESCatalogList::TheCatalogList()
00245 {
00246     if( _instance == 0 )
00247     {
00248         _instance = new BESCatalogList ;
00249     }
00250     return _instance ;
00251 }
00252 
00260 void
00261 BESCatalogList::dump( ostream &strm ) const
00262 {
00263     strm << BESIndent::LMarg << "BESCatalogList::dump - ("
00264                              << (void *)this << ")" << endl ;
00265     BESIndent::Indent() ;
00266     if( _catalogs.size() )
00267     {
00268         strm << BESIndent::LMarg << "catalog list:" << endl ;
00269         BESIndent::Indent() ;
00270         catalog_citer i = _catalogs.begin() ;
00271         catalog_citer e = _catalogs.end() ;
00272         for( ; i != e; i++ )
00273         {
00274             BESCatalog *catalog = (*i).second ;
00275             strm << BESIndent::LMarg << (*i).first << catalog << endl ;
00276         }
00277         BESIndent::UnIndent() ;
00278     }
00279     else
00280     {
00281         strm << BESIndent::LMarg << "catalog list: empty" << endl ;
00282     }
00283     BESIndent::UnIndent() ;
00284 }
00285 

Generated on Wed Jan 2 06:01:18 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.4