OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESContainerStorageList.cc
Go to the documentation of this file.
1 // BESContainerStorageList.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 <iostream>
34 
35 using std::endl ;
36 
38 #include "BESContainerStorage.h"
39 #include "BESSyntaxUserError.h"
40 #include "BESContainer.h"
41 #include "TheBESKeys.h"
42 #include "BESLog.h"
43 #include "BESInfo.h"
44 
45 BESContainerStorageList *BESContainerStorageList::_instance = 0 ;
46 
48  : _first( 0 )
49 {
50 }
51 
53 {
54  BESContainerStorageList::persistence_list *pl = _first ;
55  while( pl )
56  {
57  if( pl->_persistence_obj )
58  {
59  delete pl->_persistence_obj ;
60  }
61  BESContainerStorageList::persistence_list *next = pl->_next ;
62  delete pl ;
63  pl = next ;
64  }
65 }
66 
79 bool
81 {
82  bool ret = false ;
83  if( !_first )
84  {
85  _first = new BESContainerStorageList::persistence_list ;
86  _first->_persistence_obj = cp ;
87  _first->_reference = 1 ;
88  _first->_next = 0 ;
89  ret = true ;
90  }
91  else
92  {
93  BESContainerStorageList::persistence_list *pl = _first ;
94  bool done = false ;
95  while( done == false )
96  {
97  if( pl->_persistence_obj->get_name() != cp->get_name() )
98  {
99  if( pl->_next )
100  {
101  pl = pl->_next ;
102  }
103  else
104  {
105  pl->_next = new BESContainerStorageList::persistence_list ;
106  pl->_next->_reference = 1 ;
107  pl->_next->_persistence_obj = cp ;
108  pl->_next->_next = 0 ;
109  done = true ;
110  ret = true ;
111  }
112  }
113  else
114  {
115  done = true ;
116  ret = false ;
117  }
118  }
119  }
120  return ret ;
121 }
122 
133 bool
134 BESContainerStorageList::ref_persistence( const string &persist_name )
135 {
136  bool ret = false ;
137  BESContainerStorageList::persistence_list *pl = _first ;
138 
139  bool done = false ;
140  while( done == false )
141  {
142  if( pl )
143  {
144  if( pl->_persistence_obj &&
145  pl->_persistence_obj->get_name() == persist_name )
146  {
147  done = true ;
148  ret = true ;
149  pl->_reference++ ;
150  }
151  else
152  {
153  pl = pl->_next ;
154  }
155  }
156  else
157  {
158  done = true ;
159  }
160  }
161  return ret ;
162 }
163 
176 bool
177 BESContainerStorageList::deref_persistence( const string &persist_name )
178 {
179  bool ret = false ;
180  BESContainerStorageList::persistence_list *pl = _first ;
181  BESContainerStorageList::persistence_list *last = 0 ;
182 
183  bool done = false ;
184  while( done == false )
185  {
186  if( pl )
187  {
188  if( pl->_persistence_obj &&
189  pl->_persistence_obj->get_name() == persist_name )
190  {
191  ret = true ;
192  done = true ;
193  pl->_reference-- ;
194  if( !pl->_reference )
195  {
196  if( pl == _first )
197  {
198  _first = _first->_next ;
199  }
200  else
201  {
202  if (!last)
203  throw BESInternalError("ContainerStorageList last is null", __FILE__, __LINE__);
204  last->_next = pl->_next ;
205  }
206  delete pl->_persistence_obj ;
207  delete pl ;
208  pl = 0 ;
209  }
210  }
211  else
212  {
213  last = pl ;
214  pl = pl->_next ;
215  }
216  }
217  else
218  {
219  done = true ;
220  }
221  }
222 
223  return ret ;
224 }
225 
235 BESContainerStorageList::find_persistence( const string &persist_name )
236 {
237  BESContainerStorage *ret = NULL ;
238  BESContainerStorageList::persistence_list *pl = _first ;
239  bool done = false ;
240  while( done == false )
241  {
242  if( pl )
243  {
244  if( persist_name == pl->_persistence_obj->get_name() )
245  {
246  ret = pl->_persistence_obj ;
247  done = true ;
248  }
249  else
250  {
251  pl = pl->_next ;
252  }
253  }
254  else
255  {
256  done = true ;
257  }
258  }
259  return ret ;
260 }
261 
262 bool
263 BESContainerStorageList::isnice()
264 {
265  bool ret = false ;
266  string key = "BES.Container.Persistence" ;
267  bool found = false ;
268  string isnice ;
269  TheBESKeys::TheKeys()->get_value( key, isnice, found ) ;
270  if( isnice == "Nice" || isnice == "nice" || isnice == "NICE" )
271  ret = true ;
272  else
273  ret = false ;
274  return ret ;
275 }
276 
300 BESContainer *
301 BESContainerStorageList::look_for( const string &sym_name )
302 {
303  BESContainer *ret_container = 0 ;
304  BESContainerStorageList::persistence_list *pl = _first ;
305  bool done = false ;
306  while( done == false )
307  {
308  if( pl )
309  {
310  ret_container = pl->_persistence_obj->look_for( sym_name ) ;
311  if( ret_container )
312  {
313  done = true ;
314  }
315  else
316  {
317  pl = pl->_next ;
318  }
319  }
320  else
321  {
322  done = true ;
323  }
324  }
325  if( !ret_container )
326  {
327  if( isnice() )
328  {
329  (*BESLog::TheLog()) << "Could not find the symbolic name "
330  << sym_name << endl ;
331  }
332  else
333  {
334  string s = (string)"Could not find the symbolic name "
335  + sym_name ;
336  throw BESSyntaxUserError( s, __FILE__, __LINE__ ) ;
337  }
338  }
339 
340  return ret_container ;
341 }
342 
355 void
357 {
358  BESContainerStorageList::persistence_list *pl = _first ;
359  while( pl )
360  {
361  map<string,string> props ;
362  props["name"] = pl->_persistence_obj->get_name() ;
363  info.begin_tag( "store", &props ) ;
364  pl->_persistence_obj->show_containers( info ) ;
365  info.end_tag( "store" ) ;
366  pl = pl->_next ;
367  }
368 }
369 
377 void
378 BESContainerStorageList::dump( ostream &strm ) const
379 {
380  strm << BESIndent::LMarg << "BESContainerStorageList::dump - ("
381  << (void *)this << ")" << endl ;
383  BESContainerStorageList::persistence_list *pl = _first ;
384  if( pl )
385  {
386  strm << BESIndent::LMarg << "container storage:" << endl ;
387  BESIndent::Indent() ;
388  while( pl )
389  {
390  pl->_persistence_obj->dump( strm ) ;
391  pl = pl->_next ;
392  }
394  }
395  else
396  {
397  strm << BESIndent::LMarg << "container storage: empty" << endl ;
398  }
400 }
401 
404 {
405  if( _instance == 0 )
406  {
407  _instance = new BESContainerStorageList ;
408  }
409  return _instance ;
410 }
411