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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <sstream>
34 
35 using std::ostringstream ;
36 
37 #include "BESCatalogList.h"
38 #include "BESCatalog.h"
39 #include "BESCatalogEntry.h"
40 #include "BESInfo.h"
41 #include "BESSyntaxUserError.h"
42 #include "TheBESKeys.h"
43 #include "BESDapNames.h"
44 
45 BESCatalogList *BESCatalogList::_instance = 0 ;
46 
52 {
53  bool found = false ;
54  string key = "BES.Catalog.Default" ;
55  try
56  {
57  TheBESKeys::TheKeys()->get_value( key, _default_catalog, found ) ;
58  }
59  catch( BESError & )
60  {
61  found = false ;
62  }
63  if( !found || _default_catalog.empty() )
64  {
65  _default_catalog = BES_DEFAULT_CATALOG ;
66  }
67 }
68 
74 {
75  catalog_iter i = _catalogs.begin() ;
76  catalog_iter e = _catalogs.end() ;
77  for( ; i != e; i++ )
78  {
79  BESCatalog *catalog = (*i).second ;
80  if( catalog ) delete catalog ;
81  }
82 }
83 
91 bool
93 {
94  bool result = false;
95  if( catalog )
96  {
97  if (find_catalog(catalog->get_catalog_name()) == 0)
98  {
99 #if 0
100  _catalogs[catalog->get_catalog_name()] = catalog;
101 #endif
102  string name = catalog->get_catalog_name() ;
103  std::pair<const std::string, BESCatalog*> p =
104  std::make_pair( name, catalog ) ;
105  result = _catalogs.insert(p).second;
106 #if 0
107  result = true;
108 #endif
109  }
110  }
111  return result;
112 }
113 
124 bool
125 BESCatalogList::ref_catalog( const string &catalog_name )
126 {
127  bool ret = false ;
128  BESCatalog *cat = 0 ;
130  i = _catalogs.find( catalog_name ) ;
131  if( i != _catalogs.end() )
132  {
133  cat = (*i).second;
134  cat->reference_catalog() ;
135  ret = true ;
136  }
137  return ret ;
138 }
139 
151 bool
152 BESCatalogList::deref_catalog( const string &catalog_name )
153 {
154  bool ret = false ;
155  BESCatalog *cat = 0 ;
157  i = _catalogs.find( catalog_name ) ;
158  if( i != _catalogs.end() )
159  {
160  cat = (*i).second;
161  if( !cat->dereference_catalog() )
162  {
163  _catalogs.erase( i ) ;
164  delete cat ;
165  }
166  ret = true ;
167  }
168  return ret ;
169 }
170 
177 BESCatalog *
178 BESCatalogList::find_catalog( const string &catalog_name )
179 {
180  BESCatalog *ret = 0 ;
182  i = _catalogs.find( catalog_name ) ;
183  if( i != _catalogs.end() )
184  {
185  ret = (*i).second;
186  }
187  return ret ;
188 }
189 
204  BESCatalogEntry *entry,
205  bool show_default )
206 {
207  BESCatalogEntry *myentry = entry ;
208  if( !myentry )
209  {
210  myentry = new BESCatalogEntry( "/", "" ) ;
211  }
212  catalog_citer i = _catalogs.begin() ;
213  catalog_citer e = _catalogs.end() ;
214  for( ; i != e; i++ )
215  {
216  // if show_default is true then display all catalogs
217  // if !show_default but this current catalog is not the default
218  // then display
219  if( show_default || (*i).first != default_catalog() )
220  {
221  BESCatalog *catalog = (*i).second ;
222  catalog->show_catalog( "", SHOW_INFO_RESPONSE, myentry ) ;
223  }
224  }
225 
226  return myentry ;
227 }
228 
233 {
234  if( _instance == 0 )
235  {
236  _instance = new BESCatalogList ;
237  }
238  return _instance ;
239 }
240 
248 void
249 BESCatalogList::dump( ostream &strm ) const
250 {
251  strm << BESIndent::LMarg << "BESCatalogList::dump - ("
252  << (void *)this << ")" << endl ;
254  strm << BESIndent::LMarg << "default catalog: "
255  << _default_catalog << endl ;
256  if( _catalogs.size() )
257  {
258  strm << BESIndent::LMarg << "catalog list:" << endl ;
260  catalog_citer i = _catalogs.begin() ;
261  catalog_citer e = _catalogs.end() ;
262  for( ; i != e; i++ )
263  {
264  BESCatalog *catalog = (*i).second ;
265  strm << BESIndent::LMarg << (*i).first << catalog << endl ;
266  }
268  }
269  else
270  {
271  strm << BESIndent::LMarg << "catalog list: empty" << endl ;
272  }
274 }
275 
virtual BESCatalog * find_catalog(const string &catalog_name)
find the catalog in the list with the specified name
#define SHOW_INFO_RESPONSE
Definition: BESDapNames.h:77
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
virtual ~BESCatalogList()
list destructor deletes all registered catalogs
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