OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESCatalogList.cc
Go to the documentation of this file.
1 // BESCatalogList.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "config.h"
34 
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 
39 #include <pthread.h>
40 
41 #include <sstream>
42 
43 using std::ostringstream ;
44 
45 #include "BESCatalogList.h"
46 #include "BESCatalog.h"
47 #include "BESCatalogEntry.h"
48 #include "BESInfo.h"
49 #include "BESSyntaxUserError.h"
50 #include "TheBESKeys.h"
51 #include "BESNames.h"
52 
53 
54 
55 
56 
57 static pthread_once_t BESCatalogList_instance_control = PTHREAD_ONCE_INIT;
58 
59 
60 BESCatalogList *BESCatalogList::_instance = 0 ;
61 
67 {
68  pthread_once(&BESCatalogList_instance_control, initialize_instance);
69  return _instance;
70 }
71 
75 void BESCatalogList::initialize_instance() {
76  if (_instance == 0) {
77  _instance = new BESCatalogList;
78  #if HAVE_ATEXIT
79  atexit(delete_instance);
80  #endif
81  }
82 }
83 
87 void BESCatalogList::delete_instance() {
88  delete _instance;
89  _instance = 0;
90 }
91 
92 
93 
99  bool found = false;
100  string key = "BES.Catalog.Default";
101  try {
102  TheBESKeys::TheKeys()->get_value(key, _default_catalog, found);
103  } catch (BESError &) {
104  found = false;
105  }
106  if (!found || _default_catalog.empty()) {
107  _default_catalog = BES_DEFAULT_CATALOG;
108  }
109 }
110 
111 
112 
113 
118 BESCatalogList::~BESCatalogList()
119 {
120  catalog_iter i = _catalogs.begin() ;
121  catalog_iter e = _catalogs.end() ;
122  for( ; i != e; i++ )
123  {
124  BESCatalog *catalog = (*i).second ;
125  if( catalog ) delete catalog ;
126  }
127 }
128 
129 
130 
131 
132 
140 bool
142 {
143  bool result = false;
144  if( catalog )
145  {
146  if (find_catalog(catalog->get_catalog_name()) == 0)
147  {
148 #if 0
149  _catalogs[catalog->get_catalog_name()] = catalog;
150 #endif
151  string name = catalog->get_catalog_name() ;
152  std::pair<const std::string, BESCatalog*> p =
153  std::make_pair( name, catalog ) ;
154  result = _catalogs.insert(p).second;
155 #if 0
156  result = true;
157 #endif
158  }
159  }
160  return result;
161 }
162 
173 bool
174 BESCatalogList::ref_catalog( const string &catalog_name )
175 {
176  bool ret = false ;
177  BESCatalog *cat = 0 ;
179  i = _catalogs.find( catalog_name ) ;
180  if( i != _catalogs.end() )
181  {
182  cat = (*i).second;
183  cat->reference_catalog() ;
184  ret = true ;
185  }
186  return ret ;
187 }
188 
200 bool
201 BESCatalogList::deref_catalog( const string &catalog_name )
202 {
203  bool ret = false ;
204  BESCatalog *cat = 0 ;
206  i = _catalogs.find( catalog_name ) ;
207  if( i != _catalogs.end() )
208  {
209  cat = (*i).second;
210  if( !cat->dereference_catalog() )
211  {
212  _catalogs.erase( i ) ;
213  delete cat ;
214  }
215  ret = true ;
216  }
217  return ret ;
218 }
219 
226 BESCatalog *
227 BESCatalogList::find_catalog( const string &catalog_name )
228 {
229  BESCatalog *ret = 0 ;
231  i = _catalogs.find( catalog_name ) ;
232  if( i != _catalogs.end() )
233  {
234  ret = (*i).second;
235  }
236  return ret ;
237 }
238 
253  BESCatalogEntry *entry,
254  bool show_default )
255 {
256  BESCatalogEntry *myentry = entry ;
257  if( !myentry )
258  {
259  myentry = new BESCatalogEntry( "/", "" ) ;
260  }
261  catalog_citer i = _catalogs.begin() ;
262  catalog_citer e = _catalogs.end() ;
263  for( ; i != e; i++ )
264  {
265  // if show_default is true then display all catalogs
266  // if !show_default but this current catalog is not the default
267  // then display
268  if( show_default || (*i).first != default_catalog() )
269  {
270  BESCatalog *catalog = (*i).second ;
271  catalog->show_catalog( "", SHOW_INFO_RESPONSE, myentry ) ;
272  }
273  }
274 
275  return myentry ;
276 }
277 
278 
286 void
287 BESCatalogList::dump( ostream &strm ) const
288 {
289  strm << BESIndent::LMarg << "BESCatalogList::dump - ("
290  << (void *)this << ")" << endl ;
292  strm << BESIndent::LMarg << "default catalog: "
293  << _default_catalog << endl ;
294  if( _catalogs.size() )
295  {
296  strm << BESIndent::LMarg << "catalog list:" << endl ;
298  catalog_citer i = _catalogs.begin() ;
299  catalog_citer e = _catalogs.end() ;
300  for( ; i != e; i++ )
301  {
302  BESCatalog *catalog = (*i).second ;
303  strm << BESIndent::LMarg << (*i).first << catalog << endl ;
304  }
306  }
307  else
308  {
309  strm << BESIndent::LMarg << "catalog list: empty" << endl ;
310  }
312 }
313 
#define SHOW_INFO_RESPONSE
Definition: BESNames.h:40
virtual BESCatalog * find_catalog(const string &catalog_name)
find the catalog in the list with the specified name
virtual BESCatalogEntry * show_catalog(const string &container, const string &coi, BESCatalogEntry *entry)=0
virtual bool add_catalog(BESCatalog *catalog)
adds the speciifed catalog to the list
virtual unsigned int dereference_catalog()
Definition: BESCatalog.h:72
virtual BESCatalogEntry * show_catalogs(BESDataHandlerInterface &dhi, BESCatalogEntry *entry, bool show_default=true)
show the list of catalogs
static void Indent()
Definition: BESIndent.cc:38
virtual bool ref_catalog(const string &catalog_name)
reference the specified catalog
List of all registered catalogs.
virtual void dump(ostream &strm) const
dumps information about this object
Abstract exception class for the BES with basic string message.
Definition: BESError.h:51
virtual string default_catalog()
virtual bool deref_catalog(const string &catalog_name)
de-reference the specified catalog and remove from list if no longer referenced
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
#define BES_DEFAULT_CATALOG
BESCatalogList()
construct a catalog list
abstract base class catalog object.
Definition: BESCatalog.h:47
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:453
Structure storing information used by the BES to handle the request.
virtual void reference_catalog()
Definition: BESCatalog.h:67
virtual string get_catalog_name()
Definition: BESCatalog.h:79
static void UnIndent()
Definition: BESIndent.cc:44
static BESCatalogList * TheCatalogList()
returns the singleton BESCatalogList instance.
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:48
map< string, BESCatalog * >::iterator catalog_iter
map< string, BESCatalog * >::const_iterator catalog_citer