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 "BESInfo.h"
40 #include "BESSyntaxUserError.h"
41 #include "TheBESKeys.h"
42 #include "BESDapNames.h"
43 
44 BESCatalogList *BESCatalogList::_instance = 0 ;
45 
51 {
52  bool found = false ;
53  string key = "BES.Catalog.Default" ;
54  try
55  {
56  TheBESKeys::TheKeys()->get_value( key, _default_catalog, found ) ;
57  }
58  catch( BESError & )
59  {
60  found = false ;
61  }
62  if( !found || _default_catalog.empty() )
63  {
64  _default_catalog = BES_DEFAULT_CATALOG ;
65  }
66 }
67 
73 {
74  catalog_iter i = _catalogs.begin() ;
75  catalog_iter e = _catalogs.end() ;
76  for( ; i != e; i++ )
77  {
78  BESCatalog *catalog = (*i).second ;
79  if( catalog ) delete catalog ;
80  }
81 }
82 
90 bool
92 {
93  bool result = false;
94  if (find_catalog(catalog->get_catalog_name()) == 0) {
95 #if 0
96  _catalogs[catalog->get_catalog_name()] = catalog;
97 #endif
98  string name = catalog->get_catalog_name() ;
99  std::pair<const std::string, BESCatalog*> p =
100  std::make_pair( name, catalog ) ;
101  result = _catalogs.insert(p).second;
102 #if 0
103  result = true;
104 #endif
105  }
106  return result;
107 }
108 
119 bool
120 BESCatalogList::ref_catalog( const string &catalog_name )
121 {
122  bool ret = false ;
123  BESCatalog *cat = 0 ;
125  i = _catalogs.find( catalog_name ) ;
126  if( i != _catalogs.end() )
127  {
128  cat = (*i).second;
129  cat->reference_catalog() ;
130  ret = true ;
131  }
132  return ret ;
133 }
134 
146 bool
147 BESCatalogList::deref_catalog( const string &catalog_name )
148 {
149  bool ret = false ;
150  BESCatalog *cat = 0 ;
152  i = _catalogs.find( catalog_name ) ;
153  if( i != _catalogs.end() )
154  {
155  cat = (*i).second;
156  if( !cat->dereference_catalog() )
157  {
158  _catalogs.erase( i ) ;
159  delete cat ;
160  }
161  ret = true ;
162  }
163  return ret ;
164 }
165 
172 BESCatalog *
173 BESCatalogList::find_catalog( const string &catalog_name )
174 {
175  BESCatalog *ret = 0 ;
177  i = _catalogs.find( catalog_name ) ;
178  if( i != _catalogs.end() )
179  {
180  ret = (*i).second;
181  }
182  return ret ;
183 }
184 
217 void
218 BESCatalogList::show_catalog( const string &container,
219  const string &coi,
220  BESInfo *info )
221 {
222  string cat_name ;
223  string cat_node ;
224  BESCatalog *catalog = 0 ;
225  if( container.empty() )
226  {
227  if( _catalogs.size() == 1 )
228  {
229  catalog_citer i = _catalogs.begin() ;
230  catalog = (*i).second ;
231  catalog->show_catalog( container, coi, info ) ;
232  }
233  else
234  {
235  map<string,string> props ;
236  props["name"] = "/" ;
237  props["catalog"] = "/" ;
238  ostringstream ssize ;
239  ssize << _catalogs.size() ;
240  props["count"] = ssize.str() ;
241  props["node"] = "true" ;
242  info->begin_tag( "dataset", &props ) ;
243 
244  catalog_citer i = _catalogs.begin() ;
245  catalog_citer e = _catalogs.end() ;
246  for( ; i != e; i++ )
247  {
248  BESCatalog *catalog = (*i).second ;
249  catalog->show_catalog( "", SHOW_INFO_RESPONSE, info ) ;
250  }
251 
252  info->end_tag( "dataset" ) ;
253  }
254  }
255  else
256  {
257  string::size_type colon = container.find( ":" ) ;
258  if( colon == string::npos )
259  {
260  // no colon, so if only one catalog then use it, otherwise use
261  // the default name
262  if( _catalogs.size() == 1 )
263  {
264  catalog_citer i = _catalogs.begin() ;
265  catalog = (*i).second ;
266  cat_name = catalog->get_catalog_name() ;
267  }
268  else
269  {
270  cat_name = _default_catalog ;
271  }
272  cat_node = container ;
273  }
274  else
275  {
276  // there is a colon. The name is the part before the colon.
277  cat_name = container.substr( 0, colon ) ;
278  cat_node = container.substr( colon+1, container.length() - colon ) ;
279  }
280 
281  catalog = _catalogs[ cat_name ] ;
282  if( catalog )
283  {
284  catalog->show_catalog( cat_node, coi, info ) ;
285  }
286  else
287  {
288  string serr = "The catalog " + cat_name + " does not exist." ;
289  throw BESSyntaxUserError( serr, __FILE__, __LINE__ ) ;
290  }
291  }
292 }
293 
298 {
299  if( _instance == 0 )
300  {
301  _instance = new BESCatalogList ;
302  }
303  return _instance ;
304 }
305 
313 void
314 BESCatalogList::dump( ostream &strm ) const
315 {
316  strm << BESIndent::LMarg << "BESCatalogList::dump - ("
317  << (void *)this << ")" << endl ;
319  if( _catalogs.size() )
320  {
321  strm << BESIndent::LMarg << "catalog list:" << endl ;
323  catalog_citer i = _catalogs.begin() ;
324  catalog_citer e = _catalogs.end() ;
325  for( ; i != e; i++ )
326  {
327  BESCatalog *catalog = (*i).second ;
328  strm << BESIndent::LMarg << (*i).first << catalog << endl ;
329  }
331  }
332  else
333  {
334  strm << BESIndent::LMarg << "catalog list: empty" << endl ;
335  }
337 }
338