BESCatalogList.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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 "BESResponseNames.h"
00043
00044 BESCatalogList *BESCatalogList::_instance = 0 ;
00045
00050 BESCatalogList::BESCatalogList()
00051 {
00052 bool found = false ;
00053 string key = "BES.Catalog.Default" ;
00054 _default_catalog = TheBESKeys::TheKeys()->get_key( key, found ) ;
00055 if( !found || _default_catalog.empty() )
00056 {
00057 _default_catalog = BES_DEFAULT_CATALOG ;
00058 }
00059 }
00060
00065 BESCatalogList::~BESCatalogList()
00066 {
00067 catalog_iter i = _catalogs.begin() ;
00068 catalog_iter e = _catalogs.end() ;
00069 for( ; i != e; i++ )
00070 {
00071 BESCatalog *catalog = (*i).second ;
00072 if( catalog ) delete catalog ;
00073 }
00074 }
00075
00083 bool
00084 BESCatalogList::add_catalog(BESCatalog * catalog)
00085 {
00086 bool result = false;
00087 if (find_catalog(catalog->get_catalog_name()) == 0) {
00088 #if 0
00089 _catalogs[catalog->get_catalog_name()] = catalog;
00090 #endif
00091 string name = catalog->get_catalog_name() ;
00092 std::pair<const std::string, BESCatalog*> p =
00093 std::make_pair( name, catalog ) ;
00094 result = _catalogs.insert(p).second;
00095 #if 0
00096 result = true;
00097 #endif
00098 }
00099 return result;
00100 }
00101
00112 bool
00113 BESCatalogList::ref_catalog( const string &catalog_name )
00114 {
00115 bool ret = false ;
00116 BESCatalog *cat = 0 ;
00117 BESCatalogList::catalog_iter i ;
00118 i = _catalogs.find( catalog_name ) ;
00119 if( i != _catalogs.end() )
00120 {
00121 cat = (*i).second;
00122 cat->reference_catalog() ;
00123 ret = true ;
00124 }
00125 return ret ;
00126 }
00127
00139 bool
00140 BESCatalogList::deref_catalog( const string &catalog_name )
00141 {
00142 bool ret = false ;
00143 BESCatalog *cat = 0 ;
00144 BESCatalogList::catalog_iter i ;
00145 i = _catalogs.find( catalog_name ) ;
00146 if( i != _catalogs.end() )
00147 {
00148 cat = (*i).second;
00149 if( !cat->dereference_catalog() )
00150 {
00151 _catalogs.erase( i ) ;
00152 delete cat ;
00153 }
00154 ret = true ;
00155 }
00156 return ret ;
00157 }
00158
00165 BESCatalog *
00166 BESCatalogList::find_catalog( const string &catalog_name )
00167 {
00168 BESCatalog *ret = 0 ;
00169 BESCatalogList::catalog_citer i ;
00170 i = _catalogs.find( catalog_name ) ;
00171 if( i != _catalogs.end() )
00172 {
00173 ret = (*i).second;
00174 }
00175 return ret ;
00176 }
00177
00210 void
00211 BESCatalogList::show_catalog( const string &container,
00212 const string &coi,
00213 BESInfo *info )
00214 {
00215 string cat_name ;
00216 string cat_node ;
00217 BESCatalog *catalog = 0 ;
00218 if( container.empty() )
00219 {
00220 if( _catalogs.size() == 1 )
00221 {
00222 catalog_citer i = _catalogs.begin() ;
00223 catalog = (*i).second ;
00224 catalog->show_catalog( container, coi, info ) ;
00225 }
00226 else
00227 {
00228 map<string,string> props ;
00229 props["name"] = "/" ;
00230 props["catalog"] = "/" ;
00231 ostringstream ssize ;
00232 ssize << _catalogs.size() ;
00233 props["count"] = ssize.str() ;
00234 props["node"] = "true" ;
00235 info->begin_tag( "dataset", &props ) ;
00236
00237 catalog_citer i = _catalogs.begin() ;
00238 catalog_citer e = _catalogs.end() ;
00239 for( ; i != e; i++ )
00240 {
00241 BESCatalog *catalog = (*i).second ;
00242 catalog->show_catalog( "", SHOW_INFO_RESPONSE, info ) ;
00243 }
00244
00245 info->end_tag( "dataset" ) ;
00246 }
00247 }
00248 else
00249 {
00250 string::size_type colon = container.find( ":" ) ;
00251 if( colon == string::npos )
00252 {
00253
00254
00255 if( _catalogs.size() == 1 )
00256 {
00257 catalog_citer i = _catalogs.begin() ;
00258 catalog = (*i).second ;
00259 cat_name = catalog->get_catalog_name() ;
00260 }
00261 else
00262 {
00263 cat_name = _default_catalog ;
00264 }
00265 cat_node = container ;
00266 }
00267 else
00268 {
00269
00270 cat_name = container.substr( 0, colon ) ;
00271 cat_node = container.substr( colon+1, container.length() - colon ) ;
00272 }
00273
00274 catalog = _catalogs[ cat_name ] ;
00275 if( catalog )
00276 {
00277 catalog->show_catalog( cat_node, coi, info ) ;
00278 }
00279 else
00280 {
00281 string serr = "The catalog " + cat_name + " does not exist." ;
00282 throw BESSyntaxUserError( serr, __FILE__, __LINE__ ) ;
00283 }
00284 }
00285 }
00286
00289 BESCatalogList *
00290 BESCatalogList::TheCatalogList()
00291 {
00292 if( _instance == 0 )
00293 {
00294 _instance = new BESCatalogList ;
00295 }
00296 return _instance ;
00297 }
00298
00306 void
00307 BESCatalogList::dump( ostream &strm ) const
00308 {
00309 strm << BESIndent::LMarg << "BESCatalogList::dump - ("
00310 << (void *)this << ")" << endl ;
00311 BESIndent::Indent() ;
00312 if( _catalogs.size() )
00313 {
00314 strm << BESIndent::LMarg << "catalog list:" << endl ;
00315 BESIndent::Indent() ;
00316 catalog_citer i = _catalogs.begin() ;
00317 catalog_citer e = _catalogs.end() ;
00318 for( ; i != e; i++ )
00319 {
00320 BESCatalog *catalog = (*i).second ;
00321 strm << BESIndent::LMarg << (*i).first << catalog << endl ;
00322 }
00323 BESIndent::UnIndent() ;
00324 }
00325 else
00326 {
00327 strm << BESIndent::LMarg << "catalog list: empty" << endl ;
00328 }
00329 BESIndent::UnIndent() ;
00330 }
00331