Gaudi Framework, version v21r8

Home   Generated: 17 Mar 2010

JobOptionsSvc.cpp

Go to the documentation of this file.
00001 //$Id: JobOptionsSvc.cpp,v 1.32 2007/09/25 09:49:26 marcocle Exp $
00002 // ============================================================================
00003 // CVS tag $Name:  $ , version $Revision: 1.32 $
00004 // ============================================================================
00005 //  Include files
00006 // ============================================================================
00007 // STD & STL
00008 // ============================================================================
00009 #include <fstream>
00010 #include <string>
00011 #include <algorithm>
00012 // ============================================================================
00013 // GaudiKernel
00014 // ============================================================================
00015 #include "GaudiKernel/SvcFactory.h"
00016 #include "GaudiKernel/MsgStream.h"
00017 #include "GaudiKernel/System.h"
00018 #include "GaudiKernel/Parsers.h"
00019 // ============================================================================
00020 // Local
00021 // ============================================================================
00022 #include "ParserUtils.h"
00023 #include "JobOptionsSvc.h"
00024 // ============================================================================
00028 // ============================================================================
00029 // Instantiation of a static factory class used by clients to create
00030 // instances of this service
00031 DECLARE_SERVICE_FACTORY(JobOptionsSvc)
00032 // ============================================================================
00033 JobOptionsSvc::JobOptionsSvc( const std::string& name,
00034                               ISvcLocator* svc)
00035   : base_class(name,svc)
00036   , m_source_path     ()
00037   , m_source_type     ()
00038   , m_dirSearchPath   ()
00039   , m_dump            ()
00040   , m_dumped ( false )
00041 {
00042   std::string tmp ;
00043   tmp = System::getEnv ( "JOBOPTSEARCHPATH" ) ;
00044   if ( !tmp.empty() && ("UNKNOWN" != tmp) ) { m_dirSearchPath = tmp ; }
00045   tmp = System::getEnv ( "JOBOPTSDUMPFILE"  ) ;
00046   if ( !tmp.empty() && ("UNKNOWN" != tmp) ) { m_dump          = tmp ; }
00047 
00048   m_pmgr.declareProperty( "TYPE"       , m_source_type   ) ;
00049   m_pmgr.declareProperty( "PATH"       , m_source_path   ) ;
00050   m_pmgr.declareProperty( "SEARCHPATH" , m_dirSearchPath ) ;
00051   m_pmgr.declareProperty( "DUMPFILE"   , m_dump          ) ;
00052 }
00053 // ============================================================================
00054 StatusCode JobOptionsSvc::setProperty( const Property &p )
00055 {
00056         return m_pmgr.setProperty( p );
00057 }
00058 // ============================================================================
00059 StatusCode JobOptionsSvc::getProperty( Property *p ) const
00060 {
00061         return m_pmgr.getProperty( p );
00062 }
00063 // ============================================================================
00064 StatusCode JobOptionsSvc::setMyProperties( const std::string& client,
00065                                            IProperty* myInt )
00066 {
00067   const JobOptionsCatalogue::PropertiesT* props =
00068     m_catalogue.getProperties(client);
00069 
00070   if ( NULL == props ){ return StatusCode::SUCCESS; }
00071 
00072   bool fail = false;
00073   for ( std::vector<const Property*>::const_iterator cur = props->begin();
00074     cur != props->end(); cur++)
00075   {
00076     StatusCode sc = myInt->setProperty (**cur ) ;
00077     if ( sc.isFailure() )
00078     {
00079       MsgStream my_log( this->msgSvc(), this->name() );
00080       my_log
00081         << MSG::ERROR
00082         << "Unable to set the property '" << (*cur)->name() << "'"
00083         <<                        " of '" << client         << "'. "
00084         << "Check option and algorithm names, type and bounds."
00085         << endmsg;
00086       fail = true;
00087     }
00088   }
00089   return fail ? StatusCode::FAILURE : StatusCode::SUCCESS ;
00090 }
00091 // ============================================================================
00092 StatusCode JobOptionsSvc::initialize()
00093 {
00094   // Call base class initializer
00095   StatusCode sc = Service::initialize();
00096   if( !sc.isSuccess() ) return sc;
00097   // Read the job options if needed
00098   if ( this->m_source_type == "NONE" ) {
00099     sc =  StatusCode::SUCCESS;
00100   }
00101   else {
00102     sc = this->readOptions( m_source_path , m_dirSearchPath);
00103   }
00104   return sc;
00105 }
00106 // ============================================================================
00107 StatusCode JobOptionsSvc::readOptions( const std::string& file ,
00108                                        const std::string& path )
00109 {
00110   // use the default non-empty path (if any) if no path specified
00111   if ( path.empty() && !m_dirSearchPath.empty() )
00112   { return readOptions ( file , m_dirSearchPath ) ; }
00113   //
00114   MsgStream my_log( this->msgSvc(), this->name() );
00115   // --------------------------------------------------------------------------
00116   std::vector<Gaudi::Parsers::Message> messages;
00117   // --------------------------------------------------------------------------
00118   // fillParserCatalogue();
00119   // --------------------------------------------------------------------------
00120   my_log << MSG::DEBUG                             // debug
00121          << "Job-options read from the file "
00122          << "'" << file << "'"
00123          << MSG::INFO                              // switch
00124          << std::endl ;
00125   // create the parser & parse the file
00126   Gaudi::Parsers::Parser parser (m_pcatalogue, m_included, path, my_log.stream() ) ;
00127   StatusCode sc = Gaudi::Parsers::parse
00128     ( parser , file , messages ) ;
00129   my_log << endmsg ;
00130 
00131   // --------------------------------------------------------------------------
00132   if ( sc.isSuccess() )
00133   {
00134     my_log << MSG::INFO
00135            << "Job options successfully read in from " << file << endmsg;
00136     fillServiceCatalogue();
00137   }
00138   else
00139   {
00140     my_log << MSG::FATAL << "Job options errors."<< endmsg;
00141   }
00142   // ----------------------------------------------------------------------------
00143   for ( std::vector<Gaudi::Parsers::Message>::const_iterator
00144           cur=messages.begin(); cur!=messages.end();cur++)
00145   {
00146     my_log << convertSeverity(cur->severity()) << cur->message() << endmsg;
00147   }
00148   // ----------------------------------------------------------------------------
00149   return sc;
00150 }
00151 // ============================================================================
00152 StatusCode JobOptionsSvc::addPropertyToCatalogue
00153 ( const std::string& client,
00154   const Property& property )
00155 {
00156   Property* p = new StringProperty ( property.name(), "" ) ;
00157   if ( !property.load( *p ) ) { delete p ; return StatusCode::FAILURE ; }
00158   return m_catalogue.addProperty( client , p );
00159 }
00160 // ============================================================================
00161 StatusCode
00162 JobOptionsSvc::removePropertyFromCatalogue
00163 ( const std::string& client,
00164   const std::string& name )
00165 {
00166   return m_catalogue.removeProperty(client,name);
00167 }
00168 // ============================================================================
00169 const JobOptionsSvc::PropertiesT*
00170 JobOptionsSvc::getProperties( const std::string& client) const
00171 {
00172   return m_catalogue.getProperties(client);
00173 }
00174 // ============================================================================
00175 std::vector<std::string>  JobOptionsSvc::getClients() const
00176 {
00177   return m_catalogue.getClients();
00178 }
00179 // ============================================================================
00181 void JobOptionsSvc::fillParserCatalogue(){
00182   std::vector<std::string> clients = getClients();
00183   for(std::vector<std::string>::const_iterator client = clients.begin();
00184     client != clients.end(); client++){
00185        const PropertiesT* props = getProperties(*client);
00186        for(PropertiesT::const_iterator prop = props->begin();
00187         prop != props->end(); prop++){
00188          m_pcatalogue.addProperty(*client,(*prop)->name(),
00189            (*prop)->toString());
00190         }
00191    }
00192 }
00193 // ============================================================================
00195 void JobOptionsSvc::fillServiceCatalogue()
00196 {
00197   typedef Gaudi::Parsers::Catalogue::CatalogueT      _Catalogue ;
00198   typedef std::vector<Gaudi::Parsers::PropertyEntry> _Entries   ;
00199   _Catalogue catalogue = m_pcatalogue.catalogue();
00200   for  ( _Catalogue::const_iterator curObj = catalogue.begin() ;
00201          curObj!=catalogue.end(); ++curObj)
00202   {
00203     for ( _Entries::const_iterator curProp = curObj->second.begin() ;
00204           curProp!=curObj->second.end(); ++curProp)
00205     {
00206       StringProperty tmp (curProp->name(), curProp->value() ) ;
00207       addPropertyToCatalogue ( curObj->first , tmp ) ;
00208     }
00209   }
00210 }
00211 // ============================================================================
00212 MSG::Level
00213 JobOptionsSvc::convertSeverity
00214 ( const Gaudi::Parsers::Message::Severity& severity )
00215 {
00216   switch ( severity )
00217   {
00218   case Gaudi::Parsers::Message::E_ERROR   :
00219     return MSG::FATAL   ;
00220   case Gaudi::Parsers::Message::E_WARNING :
00221     return MSG::WARNING ;
00222   case Gaudi::Parsers::Message::E_NOTICE  :
00223     return MSG::INFO    ;
00224   default:
00225     return MSG::DEBUG   ;
00226   }
00227 }
00228 // ============================================================================
00229 StatusCode JobOptionsSvc::finalize()
00230 {
00231   // dump the properties catalogue if needed
00232   if ( !m_dump.empty() && !m_dumped ) { dump() ;}
00233    // finalize the base class
00234   return Service::finalize() ;
00235 }
00236 // ============================================================================
00237 void JobOptionsSvc::dump ()
00238 {
00239   // avoid double dump
00240   if ( m_dump.empty() || m_dumped ) { return ; }               // RETURN
00241   dump ( m_dump ) ;
00242   // avoid double dump
00243   m_dumped = true ; 
00244 }
00245 // ============================================================================
00246 void JobOptionsSvc::dump( const std::string& file ) const
00247 {
00248   MsgStream log ( msgSvc() , name() ) ;
00249   std::ofstream out
00250     ( file.c_str() , std::ios_base::out | std::ios_base::trunc ) ;
00251   // perform the actual dumping
00252   if ( !out )
00253   {
00254     log << MSG::ERROR << "Unable to open dump-file \""+file+"\"" << endmsg ;
00255     return ;                                                   // RETURN
00256   }
00257   else
00258   { log << MSG::INFO << "Properties are dumped into \""+file+"\"" << endmsg ; }
00259   // perform the actual dump:
00260   fillStream ( out ) ;
00261 }
00262 // ============================================================================
00263 std::ostream& JobOptionsSvc::fillStream ( std::ostream& o ) const
00264 { return  o << m_pcatalogue ; }
00265 // ============================================================================
00266 JobOptionsSvc::~JobOptionsSvc()
00267 {
00268   // dump needed? @attention JobOptionsSvc::finalize is never invoked!
00269   if ( !m_dump.empty() && !m_dumped ) { dump() ;}
00270 }
00271 // ============================================================================
00272 
00273 
00274 // ============================================================================
00275 // The END
00276 // ============================================================================
00277 
00278 
00279 

Generated at Wed Mar 17 18:06:46 2010 for Gaudi Framework, version v21r8 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004