bes  Updated for version 3.17.4
BESCatalogList.cc
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 static pthread_once_t BESCatalogList_instance_control = PTHREAD_ONCE_INIT;
54 
55 BESCatalogList *BESCatalogList::_instance = 0;
56 
62  pthread_once(&BESCatalogList_instance_control, initialize_instance);
63  return _instance;
64 }
65 
69 void BESCatalogList::initialize_instance() {
70  if (_instance == 0) {
71  _instance = new BESCatalogList;
72 #ifdef HAVE_ATEXIT
73  atexit(delete_instance);
74 #endif
75  }
76 }
77 
81 void BESCatalogList::delete_instance() {
82  delete _instance;
83  _instance = 0;
84 }
85 
91  bool found = false;
92  string key = "BES.Catalog.Default";
93  try {
94  TheBESKeys::TheKeys()->get_value(key, _default_catalog, found);
95  }
96  catch (BESError &) {
97  found = false;
98  }
99  if (!found || _default_catalog.empty()) {
100  _default_catalog = BES_DEFAULT_CATALOG;
101  }
102 }
103 
108 BESCatalogList::~BESCatalogList() {
109  catalog_iter i = _catalogs.begin();
110  catalog_iter e = _catalogs.end();
111  for (; i != e; i++) {
112  BESCatalog *catalog = (*i).second;
113  if (catalog) delete catalog;
114  }
115 }
116 
125  bool result = false;
126  if (catalog) {
127  if (find_catalog(catalog->get_catalog_name()) == 0) {
128 #if 0
129  _catalogs[catalog->get_catalog_name()] = catalog;
130 #endif
131  string name = catalog->get_catalog_name();
132  std::pair<const std::string, BESCatalog*> p = std::make_pair(name, catalog);
133  result = _catalogs.insert(p).second;
134 #if 0
135  result = true;
136 #endif
137  }
138  }
139  return result;
140 }
141 
152 bool BESCatalogList::ref_catalog(const string &catalog_name) {
153  bool ret = false;
154  BESCatalog *cat = 0;
155  BESCatalogList::catalog_iter i;
156  i = _catalogs.find(catalog_name);
157  if (i != _catalogs.end()) {
158  cat = (*i).second;
159  cat->reference_catalog();
160  ret = true;
161  }
162  return ret;
163 }
164 
176 bool BESCatalogList::deref_catalog(const string &catalog_name) {
177  bool ret = false;
178  BESCatalog *cat = 0;
179  BESCatalogList::catalog_iter i;
180  i = _catalogs.find(catalog_name);
181  if (i != _catalogs.end()) {
182  cat = (*i).second;
183  if (!cat->dereference_catalog()) {
184  _catalogs.erase(i);
185  delete cat;
186  }
187  ret = true;
188  }
189  return ret;
190 }
191 
198 BESCatalog *
199 BESCatalogList::find_catalog(const string &catalog_name) {
200  BESCatalog *ret = 0;
201  BESCatalogList::catalog_citer i;
202  i = _catalogs.find(catalog_name);
203  if (i != _catalogs.end()) {
204  ret = (*i).second;
205  }
206  return ret;
207 }
208 
223  BESCatalogEntry *myentry = entry;
224  if (!myentry) {
225  myentry = new BESCatalogEntry("/", "");
226  }
227  catalog_citer i = _catalogs.begin();
228  catalog_citer e = _catalogs.end();
229  for (; i != e; i++) {
230  // if show_default is true then display all catalogs
231  // if !show_default but this current catalog is not the default
232  // then display
233  if (show_default || (*i).first != default_catalog()) {
234  BESCatalog *catalog = (*i).second;
235  catalog->show_catalog("", SHOW_INFO_RESPONSE, myentry);
236  }
237  }
238 
239  return myentry;
240 }
241 
249 void BESCatalogList::dump(ostream &strm) const {
250  strm << BESIndent::LMarg << "BESCatalogList::dump - (" << (void *) this << ")" << endl;
251  BESIndent::Indent();
252  strm << BESIndent::LMarg << "default catalog: " << _default_catalog << endl;
253  if (_catalogs.size()) {
254  strm << BESIndent::LMarg << "catalog list:" << endl;
255  BESIndent::Indent();
256  catalog_citer i = _catalogs.begin();
257  catalog_citer e = _catalogs.end();
258  for (; i != e; i++) {
259  BESCatalog *catalog = (*i).second;
260  strm << BESIndent::LMarg << (*i).first << catalog << endl;
261  }
262  BESIndent::UnIndent();
263  }
264  else {
265  strm << BESIndent::LMarg << "catalog list: empty" << endl;
266  }
267  BESIndent::UnIndent();
268 }
269 
virtual BESCatalog * find_catalog(const string &catalog_name)
find the catalog in the list with the specified name
virtual bool add_catalog(BESCatalog *catalog)
adds the speciifed catalog to the list
virtual BESCatalogEntry * show_catalogs(BESDataHandlerInterface &dhi, BESCatalogEntry *entry, bool show_default=true)
show the list of catalogs
virtual void dump(ostream &strm) const
dumps information about this object
virtual bool ref_catalog(const string &catalog_name)
reference the specified catalog
List of all registered catalogs.
Abstract exception class for the BES with basic string message.
Definition: BESError.h:56
virtual bool deref_catalog(const string &catalog_name)
de-reference the specified catalog and remove from list if no longer referenced
BESCatalogList()
construct a catalog list
abstract base class catalog object. Derived classes know how to show nodes and leaves in a catalog...
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:483
Structure storing information used by the BES to handle the request.
static BESCatalogList * TheCatalogList()
returns the singleton BESCatalogList instance. The pthreads library insures that only one instance ca...
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:43