BESContainerStorageVolatile.cc

Go to the documentation of this file.
00001 // BESContainerStorageVolatile.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00032 
00033 #include "BESContainerStorageVolatile.h"
00034 #include "BESFileContainer.h"
00035 #include "BESInternalError.h"
00036 #include "BESInfo.h"
00037 #include "TheBESKeys.h"
00038 #include "BESUtil.h"
00039 
00047 BESContainerStorageVolatile::BESContainerStorageVolatile( const string &n )
00048     : BESContainerStorage( n )
00049 {
00050     string key = "BES.Data.RootDirectory" ;
00051     bool found = false ;
00052     _root_dir = TheBESKeys::TheKeys()->get_key( key, found ) ;
00053     if( _root_dir == "" )
00054     {
00055         string s = key + " not defined in bes configuration file" ;
00056         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00057     }
00058 
00059     key = (string)"BES.FollowSymLinks" ;
00060     string s_str =
00061         BESUtil::lowercase( TheBESKeys::TheKeys()->get_key( key, found ) ) ;
00062     if( found && ( s_str == "yes" || s_str == "on" || s_str == "true" ) )
00063     {
00064         _follow_sym_links = true ;
00065     }
00066 }
00067 
00068 BESContainerStorageVolatile::~BESContainerStorageVolatile()
00069 { 
00070     del_containers() ;
00071 }
00072 
00082 BESContainer *
00083 BESContainerStorageVolatile::look_for( const string &sym_name )
00084 {
00085     BESContainer *ret_container = 0 ;
00086 
00087     BESContainerStorageVolatile::Container_citer i ;
00088     i = _container_list.find( sym_name ) ;
00089     if( i != _container_list.end() )
00090     {
00091         BESContainer *c = (*i).second ;
00092         ret_container = c->ptr_duplicate() ;
00093     }
00094 
00095     return ret_container ;
00096 }
00097 
00113 void
00114 BESContainerStorageVolatile::add_container( const string &sym_name,
00115                                             const string &real_name,
00116                                             const string &type )
00117 {
00118     // The type must be specified so that we can find the request handler
00119     // that knows how to handle the container.
00120     if( type == "" )
00121     {
00122         string s = "Unable to add container, type of data must be specified"  ;
00123         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00124     }
00125 
00126     // if the container already exists then throw an error
00127     BESContainerStorageVolatile::Container_citer i ;
00128     i = _container_list.find( sym_name ) ;
00129     if( i != _container_list.end() )
00130     {
00131         string s = (string)"A container with the name "
00132                    + sym_name
00133                    + " already exists" ;
00134         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00135     }
00136 
00137     // make sure that the path to the container exists. If follow_sym_links
00138     // is false and there is a symbolic link in the path then an error will
00139     // be thrown. If the path does not exist, an error will be thrown.
00140     BESUtil::check_path( real_name, _root_dir, _follow_sym_links ) ;
00141 
00142     // add the root directory to the real_name passed
00143     string new_r_name = _root_dir + "/" + real_name ;
00144 
00145     // Create the file container with the new information
00146     BESContainer *c = new BESFileContainer( sym_name, new_r_name, type ) ;
00147 
00148     // add it to the container list
00149     _container_list[sym_name] = c ;
00150 }
00151 
00169 void
00170 BESContainerStorageVolatile::add_container( BESContainer *c )
00171 {
00172     if( !c )
00173     {
00174         string s = "Unable to add container, container passed is null"  ;
00175         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00176     }
00177     if( c->get_container_type() == "" )
00178     {
00179         string s = "Unable to add container, type of data must be specified"  ;
00180         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00181     }
00182     string sym_name = c->get_symbolic_name() ;
00183     BESContainerStorageVolatile::Container_citer i ;
00184     i = _container_list.find( sym_name ) ;
00185     if( i != _container_list.end() )
00186     {
00187         string s = (string)"A container with the name "
00188                    + sym_name
00189                    + " already exists" ;
00190         throw BESInternalError( s, __FILE__, __LINE__ ) ;
00191     }
00192     _container_list[sym_name] = c ;
00193 }
00194 
00201 bool
00202 BESContainerStorageVolatile::del_container( const string &s_name )
00203 {
00204     bool ret = false ;
00205     BESContainerStorageVolatile::Container_iter i ;
00206     i = _container_list.find( s_name ) ;
00207     if( i != _container_list.end() )
00208     {
00209         BESContainer *c = (*i).second;
00210         _container_list.erase( i ) ;
00211         delete c ;
00212         ret = true ;
00213     }
00214     return ret ;
00215 }
00216 
00224 bool
00225 BESContainerStorageVolatile::del_containers( )
00226 {
00227     while( _container_list.size() != 0 )
00228     {
00229         Container_iter ci = _container_list.begin() ;
00230         BESContainer *c = (*ci).second ;
00231         _container_list.erase( ci ) ;
00232         if( c )
00233         {
00234             delete c ;
00235         }
00236     }
00237     return true ;
00238 }
00239 
00254 void
00255 BESContainerStorageVolatile::show_containers( BESInfo &info )
00256 {
00257     info.add_tag( "name", get_name() ) ;
00258     string::size_type root_len = _root_dir.length() ;
00259     BESContainerStorageVolatile::Container_iter i = _container_list.begin() ;
00260     BESContainerStorageVolatile::Container_iter e = _container_list.end() ;
00261     for( ; i != e; i++ )
00262     {
00263         BESContainer *c = (*i).second;
00264         string sym = c->get_symbolic_name() ;
00265         string real = c->get_real_name() ;
00266         if( real.length() > root_len )
00267         {
00268             if( real.compare( 0, root_len, _root_dir ) == 0 )
00269             {
00270                 real = real.substr( root_len, real.length() - root_len ) ;
00271             }
00272         }
00273         string type = c->get_container_type() ;
00274         show_container( sym, real, type, info ) ;
00275     }
00276 }
00277 
00285 void
00286 BESContainerStorageVolatile::dump( ostream &strm ) const
00287 {
00288     strm << BESIndent::LMarg << "BESContainerStorageVolatile::dump - ("
00289                              << (void *)this << ")" << endl ;
00290     BESIndent::Indent() ;
00291     strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00292     if( _container_list.size() )
00293     {
00294         strm << BESIndent::LMarg << "containers:" << endl ;
00295         BESIndent::Indent() ;
00296         BESContainerStorageVolatile::Container_citer i
00297             = _container_list.begin() ;
00298         BESContainerStorageVolatile::Container_citer ie
00299             = _container_list.end() ;
00300         for( ; i != ie; i++ )
00301         {
00302             BESContainer *c = (*i).second;
00303             c->dump( strm ) ;
00304         }
00305         BESIndent::UnIndent() ;
00306     }
00307     else
00308     {
00309         strm << BESIndent::LMarg << "containers: none" << endl ;
00310     }
00311     BESIndent::UnIndent() ;
00312 }
00313 

Generated on 18 Feb 2010 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.6.1