bes  Updated for version 3.20.6
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 #include "BESCatalogList.h"
44 #include "BESCatalog.h"
45 #include "BESCatalogDirectory.h"
46 #include "BESCatalogEntry.h"
47 #include "BESInfo.h"
48 
49 #include "BESSyntaxUserError.h"
50 #include "TheBESKeys.h"
51 #include "BESNames.h"
52 
53 using namespace std;
54 
55 #if 0
56 static pthread_once_t BESCatalogList_instance_control = PTHREAD_ONCE_INIT;
57 #endif
58 
59 
60 BESCatalogList *BESCatalogList::d_instance = 0;
61 
82 {
83  if (d_instance == 0) initialize_instance();
84 
85  return d_instance;
86 }
87 
92 void BESCatalogList::initialize_instance()
93 {
94  d_instance = new BESCatalogList;
95 #ifdef HAVE_ATEXIT
96  atexit(delete_instance);
97 #endif
98 
99 }
100 
104 void BESCatalogList::delete_instance()
105 {
106  delete d_instance;
107  d_instance = 0;
108 }
109 
115 {
116  bool found = false;
117  string key = "BES.Catalog.Default";
118 
119  // The only way get_value() throws is when a single key has multiple values.
120  // However, TheKeys() throws if the bes.conf file cannot be found.
121  // This code should probably allow that to be logged and the server to fail
122  // to start, not hide the error. jhrg 7/22/18
123  TheBESKeys::TheKeys()->get_value(key, d_default_catalog_name, found);
124 
125  if (!found || d_default_catalog_name.empty()) {
126  d_default_catalog_name = BES_DEFAULT_CATALOG;
127  }
128 
129  // Build the default catalog and add it to the map of catalogs. jhrg 7/21/18
130  d_default_catalog = new BESCatalogDirectory(d_default_catalog_name);
131  add_catalog(d_default_catalog);
132 }
133 
139 {
140  catalog_iter i = d_catalogs.begin();
141  catalog_iter e = d_catalogs.end();
142  for (; i != e; i++) {
143  BESCatalog *catalog = (*i).second;
144  delete catalog;
145  }
146 
147  d_catalogs.clear();
148 }
149 
163 {
164  bool result = false;
165  if (catalog) {
166  if (find_catalog(catalog->get_catalog_name()) == 0) {
167  // TODO I have no idea why this code was re-written. jhrg 2.25.18
168 #if 0
169  d_catalogs[catalog->get_catalog_name()] = catalog;
170 #endif
171  string name = catalog->get_catalog_name();
172  pair<const string, BESCatalog*> p = make_pair(name, catalog);
173  result = d_catalogs.insert(p).second;
174 #if 0
175  result = true;
176 #endif
177  }
178  }
179 
180  return result;
181 }
182 
183 // Modules that call ref_catalog: csv, dap, dmrpp, ff, fits, gdal, hdf4,
184 // hdf5, ncml, nc, sql. jhrg 2.25.18
185 
219 bool BESCatalogList::ref_catalog(const string &catalog_name)
220 {
221  bool ret = false;
222  BESCatalog *cat = 0;
223  BESCatalogList::catalog_iter i;
224  i = d_catalogs.find(catalog_name);
225  if (i != d_catalogs.end()) {
226  cat = (*i).second;
227  cat->reference_catalog();
228  ret = true;
229  }
230  return ret;
231 }
232 
233 // Modules that call deref_catalog: csv, dap, dmrpp, ff, fits, gdal, hdf4,
234 // hdf5, ncml, nc, sql. jhrg 2.25.18
235 
250 bool BESCatalogList::deref_catalog(const string &catalog_name)
251 {
252  bool ret = false;
253  BESCatalog *cat = 0;
254  BESCatalogList::catalog_iter i;
255  i = d_catalogs.find(catalog_name);
256  if (i != d_catalogs.end()) {
257  cat = (*i).second;
258  if (!cat->dereference_catalog()) {
259  d_catalogs.erase(i);
260  delete cat;
261  }
262  ret = true;
263  }
264  return ret;
265 }
266 
273 BESCatalog *
274 BESCatalogList::find_catalog(const string &catalog_name) const
275 {
276  BESCatalogList::catalog_citer i = d_catalogs.find(catalog_name);
277  if (i != d_catalogs.end()) {
278  return (*i).second;
279  }
280  return 0;
281 }
282 
312 BESCatalogList::show_catalogs(BESCatalogEntry *entry, bool show_default)
313 {
314  BESCatalogEntry *myentry = entry;
315  if (!myentry) {
316  myentry = new BESCatalogEntry("/", "");
317  }
318  catalog_citer i = d_catalogs.begin();
319  catalog_citer e = d_catalogs.end();
320  for (; i != e; i++) {
321  // if show_default is true then display all catalogs
322  // if !show_default but this current catalog is not the default
323  // then display
324  if (show_default || (*i).first != default_catalog_name()) {
325  BESCatalog *catalog = (*i).second;
326  catalog->show_catalog("", myentry);
327  }
328  }
329 
330  return myentry;
331 }
332 
340 void BESCatalogList::dump(ostream &strm) const
341 {
342  strm << BESIndent::LMarg << "BESCatalogList::dump - (" << (void *) this << ")" << endl;
343  BESIndent::Indent();
344  strm << BESIndent::LMarg << "default catalog: " << d_default_catalog_name << endl;
345  if (d_catalogs.size()) {
346  strm << BESIndent::LMarg << "catalog list:" << endl;
347  BESIndent::Indent();
348  catalog_citer i = d_catalogs.begin();
349  catalog_citer e = d_catalogs.end();
350  for (; i != e; i++) {
351  BESCatalog *catalog = (*i).second;
352  strm << BESIndent::LMarg << (*i).first << catalog << endl;
353  }
354  BESIndent::UnIndent();
355  }
356  else {
357  strm << BESIndent::LMarg << "catalog list: empty" << endl;
358  }
359  BESIndent::UnIndent();
360 }
361 
BESCatalogList::~BESCatalogList
virtual ~BESCatalogList()
list destructor deletes all registered catalogs
Definition: BESCatalogList.cc:138
BESCatalog::show_catalog
virtual BESCatalogEntry * show_catalog(const std::string &container, BESCatalogEntry *entry)=0
BESCatalog::dereference_catalog
virtual unsigned int dereference_catalog()
Decrement the count of clients that reference this catalog.
Definition: BESCatalog.h:92
BESCatalogDirectory
Catalogs from a directory structure.
Definition: BESCatalogDirectory.h:52
BESCatalogList::BESCatalogList
BESCatalogList()
construct a catalog list
Definition: BESCatalogList.cc:114
BESCatalog::get_catalog_name
virtual std::string get_catalog_name() const
Get the name for this catalog.
Definition: BESCatalog.h:103
BESCatalogList::TheCatalogList
static BESCatalogList * TheCatalogList()
Get the singleton BESCatalogList instance.
Definition: BESCatalogList.cc:81
TheBESKeys::TheKeys
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:62
BESCatalogList
List of all registered catalogs.
Definition: BESCatalogList.h:84
BESCatalogList::add_catalog
virtual bool add_catalog(BESCatalog *catalog)
adds the specified catalog to the list
Definition: BESCatalogList.cc:162
TheBESKeys::get_value
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: TheBESKeys.cc:272
BESCatalogEntry
Definition: BESCatalogEntry.h:46
BESCatalog
Catalogs provide a hierarchical organization for data.
Definition: BESCatalog.h:51
BESCatalog::reference_catalog
virtual void reference_catalog()
Increase the count of clients that reference this catalog.
Definition: BESCatalog.h:81
BESCatalogList::dump
virtual void dump(std::ostream &strm) const
dump the contents of this object to the specified ostream