OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESContainerStorageVolatile.cc
Go to the documentation of this file.
1 // BESContainerStorageVolatile.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 
34 #include "BESFileContainer.h"
35 #include "BESInternalError.h"
36 #include "BESSyntaxUserError.h"
37 #include "BESInfo.h"
38 #include "TheBESKeys.h"
39 #include "BESUtil.h"
40 
49  : BESContainerStorage( n )
50 {
51  string key = "BES.Data.RootDirectory" ;
52  bool found = false ;
53  TheBESKeys::TheKeys()->get_value( key, _root_dir, found ) ;
54  if( _root_dir == "" )
55  {
56  string s = key + " not defined in BES configuration file" ;
57  throw BESSyntaxUserError( s, __FILE__, __LINE__ ) ;
58  }
59 
60  found = false ;
61  key = (string)"BES.FollowSymLinks" ;
62  string s_str ;
63  TheBESKeys::TheKeys()->get_value( key, s_str, found ) ;
64  s_str = BESUtil::lowercase( s_str ) ;
65  if( found && ( s_str == "yes" || s_str == "on" || s_str == "true" ) )
66  {
67  _follow_sym_links = true ;
68  }
69 }
70 
72 {
73  del_containers() ;
74 }
75 
86 BESContainerStorageVolatile::look_for( const string &sym_name )
87 {
88  BESContainer *ret_container = 0 ;
89 
91  i = _container_list.find( sym_name ) ;
92  if( i != _container_list.end() )
93  {
94  BESContainer *c = (*i).second ;
95  ret_container = c->ptr_duplicate() ;
96  }
97 
98  return ret_container ;
99 }
100 
116 void
118  const string &real_name,
119  const string &type )
120 {
121  // The type must be specified so that we can find the request handler
122  // that knows how to handle the container.
123  if( type == "" )
124  {
125  string s = "Unable to add container, type of data must be specified" ;
126  throw BESInternalError( s, __FILE__, __LINE__ ) ;
127  }
128 
129  // if the container already exists then throw an error
131  i = _container_list.find( sym_name ) ;
132  if( i != _container_list.end() )
133  {
134  string s = (string)"A container with the name "
135  + sym_name
136  + " already exists" ;
137  throw BESInternalError( s, __FILE__, __LINE__ ) ;
138  }
139 
140  // make sure that the path to the container exists. If follow_sym_links
141  // is false and there is a symbolic link in the path then an error will
142  // be thrown. If the path does not exist, an error will be thrown.
144 
145  // add the root directory to the real_name passed
146  string new_r_name = _root_dir + "/" + real_name ;
147 
148  // Create the file container with the new information
149  BESContainer *c = new BESFileContainer( sym_name, new_r_name, type ) ;
150 
151  // add it to the container list
152  _container_list[sym_name] = c ;
153 }
154 
172 void
174 {
175  if( !c )
176  {
177  string s = "Unable to add container, container passed is null" ;
178  throw BESInternalError( s, __FILE__, __LINE__ ) ;
179  }
180  if( c->get_container_type() == "" )
181  {
182  string s = "Unable to add container, type of data must be specified" ;
183  throw BESInternalError( s, __FILE__, __LINE__ ) ;
184  }
185  string sym_name = c->get_symbolic_name() ;
187  i = _container_list.find( sym_name ) ;
188  if( i != _container_list.end() )
189  {
190  string s = (string)"A container with the name "
191  + sym_name
192  + " already exists" ;
193  throw BESInternalError( s, __FILE__, __LINE__ ) ;
194  }
195  _container_list[sym_name] = c ;
196 }
197 
204 bool
206 {
207  bool ret = false ;
209  i = _container_list.find( s_name ) ;
210  if( i != _container_list.end() )
211  {
212  BESContainer *c = (*i).second;
213  _container_list.erase( i ) ;
214  if( c )
215  {
216  delete c ;
217  }
218  ret = true ;
219  }
220  return ret ;
221 }
222 
230 bool
232 {
233  while( _container_list.size() != 0 )
234  {
235  Container_iter ci = _container_list.begin() ;
236  BESContainer *c = (*ci).second ;
237  _container_list.erase( ci ) ;
238  if( c )
239  {
240  delete c ;
241  }
242  }
243  return true ;
244 }
245 
260 void
262 {
263  info.add_tag( "name", get_name() ) ;
264  string::size_type root_len = _root_dir.length() ;
265  BESContainerStorageVolatile::Container_iter i = _container_list.begin() ;
266  BESContainerStorageVolatile::Container_iter e = _container_list.end() ;
267  for( ; i != e; i++ )
268  {
269  BESContainer *c = (*i).second;
270  string sym = c->get_symbolic_name() ;
271  string real = c->get_real_name() ;
272  if( real.length() > root_len )
273  {
274  if( real.compare( 0, root_len, _root_dir ) == 0 )
275  {
276  real = real.substr( root_len, real.length() - root_len ) ;
277  }
278  }
279  string type = c->get_container_type() ;
280  show_container( sym, real, type, info ) ;
281  }
282 }
283 
291 void
292 BESContainerStorageVolatile::dump( ostream &strm ) const
293 {
294  strm << BESIndent::LMarg << "BESContainerStorageVolatile::dump - ("
295  << (void *)this << ")" << endl ;
297  strm << BESIndent::LMarg << "name: " << get_name() << endl ;
298  if( _container_list.size() )
299  {
300  strm << BESIndent::LMarg << "containers:" << endl ;
303  = _container_list.begin() ;
305  = _container_list.end() ;
306  for( ; i != ie; i++ )
307  {
308  BESContainer *c = (*i).second;
309  c->dump( strm ) ;
310  }
312  }
313  else
314  {
315  strm << BESIndent::LMarg << "containers: none" << endl ;
316  }
318 }
319 
provides persistent storage for data storage information represented by a container.
virtual void show_container(const string &sym_name, const string &real_name, const string &type, BESInfo &info)
add information for a container to the informational response object
virtual BESContainer * look_for(const string &sym_name)
looks for the specified container using the symbolic name passed
exception thrown if inernal error encountered
static string lowercase(const string &s)
Convert a string to all lower case.
Definition: BESUtil.cc:190
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESContainer.cc:71
virtual void add_tag(const string &tag_name, const string &tag_data, map< string, string > *attrs=0)=0
Holds real data, container type and constraint for symbolic name read from persistence.
virtual void dump(ostream &strm) const
dumps information about this object
static void Indent()
Definition: BESIndent.cc:38
virtual void add_container(BESContainer *c)
add the passed container to the list of containers in volatile storage
error thrown if there is a user syntax error in the request or any other user error ...
map< string, BESContainer *>::const_iterator Container_citer
virtual void show_containers(BESInfo &info)
show information for each container in this persistent store
informational response object
Definition: BESInfo.h:68
virtual bool del_container(const string &s_name)
removes a container with the given symbolic name from the list and deletes it.
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:466
BESContainerStorageVolatile(const string &n)
create an instance of this persistent store with the given name.
string get_real_name() const
retreive the real name for this container, such as a file name.
Definition: BESContainer.h:141
string get_container_type() const
retrieve the type of data this container holds, such as cedar or netcdf.
Definition: BESContainer.h:170
map< string, BESContainer *>::iterator Container_iter
virtual bool del_containers()
removes all container
A container is something that holds data.
Definition: BESContainer.h:60
static void UnIndent()
Definition: BESIndent.cc:44
static void check_path(const string &path, const string &root, bool follow_sym_links)
Check if the specified path is valid.
Definition: BESUtil.cc:249
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:46
virtual const string & get_name() const
retrieve the name of this persistent store
virtual BESContainer * ptr_duplicate()=0
pure abstract method to duplicate this instances of BESContainer
string get_symbolic_name() const
retrieve the symbolic name for this container
Definition: BESContainer.h:159