![]() |
|
|
Generated: 8 Jan 2009 |
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 : Service(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 << endreq; 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::queryInterface(const InterfaceID& riid, 00108 void** ppvInterface) 00109 { 00110 if ( IID_IJobOptionsSvc == riid ) { 00111 *ppvInterface = static_cast<IJobOptionsSvc*>( this ) ; 00112 } 00113 else if ( IID_IProperty == riid ) { 00114 *ppvInterface = static_cast<IProperty*>( this ) ; 00115 } 00116 else { 00117 return Service::queryInterface(riid, ppvInterface); 00118 } 00119 addRef(); 00120 return StatusCode::SUCCESS; 00121 } 00122 // ============================================================================ 00123 StatusCode JobOptionsSvc::readOptions( const std::string& file , 00124 const std::string& path ) 00125 { 00126 // use the default non-empty path (if any) if no path specified 00127 if ( path.empty() && !m_dirSearchPath.empty() ) 00128 { return readOptions ( file , m_dirSearchPath ) ; } 00129 // 00130 MsgStream my_log( this->msgSvc(), this->name() ); 00131 // -------------------------------------------------------------------------- 00132 std::vector<Gaudi::Parsers::Message> messages; 00133 // -------------------------------------------------------------------------- 00134 // fillParserCatalogue(); 00135 // -------------------------------------------------------------------------- 00136 my_log << MSG::DEBUG // debug 00137 << "Job-options read from the file " 00138 << "'" << file << "'" 00139 << MSG::INFO // switch 00140 << std::endl ; 00141 // create the parser & parse the file 00142 Gaudi::Parsers::Parser parser (m_pcatalogue, m_included, path, my_log.stream() ) ; 00143 StatusCode sc = Gaudi::Parsers::parse 00144 ( parser , file , messages ) ; 00145 my_log << endreq ; 00146 00147 // -------------------------------------------------------------------------- 00148 if ( sc.isSuccess() ) 00149 { 00150 my_log << MSG::INFO 00151 << "Job options successfully read in from " << file << endreq; 00152 fillServiceCatalogue(); 00153 } 00154 else 00155 { 00156 my_log << MSG::FATAL << "Job options errors."<< endreq; 00157 } 00158 // ---------------------------------------------------------------------------- 00159 for ( std::vector<Gaudi::Parsers::Message>::const_iterator 00160 cur=messages.begin(); cur!=messages.end();cur++) 00161 { 00162 my_log << convertSeverity(cur->severity()) << cur->message() << endreq; 00163 } 00164 // ---------------------------------------------------------------------------- 00165 return sc; 00166 } 00167 // ============================================================================ 00168 StatusCode JobOptionsSvc::addPropertyToCatalogue 00169 ( const std::string& client, 00170 const Property& property ) 00171 { 00172 Property* p = new StringProperty ( property.name(), "" ) ; 00173 if ( !property.load( *p ) ) { delete p ; return StatusCode::FAILURE ; } 00174 return m_catalogue.addProperty( client , p ); 00175 } 00176 // ============================================================================ 00177 StatusCode 00178 JobOptionsSvc::removePropertyFromCatalogue 00179 ( const std::string& client, 00180 const std::string& name ) 00181 { 00182 return m_catalogue.removeProperty(client,name); 00183 } 00184 // ============================================================================ 00185 const JobOptionsSvc::PropertiesT* 00186 JobOptionsSvc::getProperties( const std::string& client) const 00187 { 00188 return m_catalogue.getProperties(client); 00189 } 00190 // ============================================================================ 00191 std::vector<std::string> JobOptionsSvc::getClients() const 00192 { 00193 return m_catalogue.getClients(); 00194 } 00195 // ============================================================================ 00197 void JobOptionsSvc::fillParserCatalogue(){ 00198 std::vector<std::string> clients = getClients(); 00199 for(std::vector<std::string>::const_iterator client = clients.begin(); 00200 client != clients.end(); client++){ 00201 const PropertiesT* props = getProperties(*client); 00202 for(PropertiesT::const_iterator prop = props->begin(); 00203 prop != props->end(); prop++){ 00204 m_pcatalogue.addProperty(*client,(*prop)->name(), 00205 (*prop)->toString()); 00206 } 00207 } 00208 } 00209 // ============================================================================ 00211 void JobOptionsSvc::fillServiceCatalogue() 00212 { 00213 typedef Gaudi::Parsers::Catalogue::CatalogueT _Catalogue ; 00214 typedef std::vector<Gaudi::Parsers::PropertyEntry> _Entries ; 00215 _Catalogue catalogue = m_pcatalogue.catalogue(); 00216 for ( _Catalogue::const_iterator curObj = catalogue.begin() ; 00217 curObj!=catalogue.end(); ++curObj) 00218 { 00219 for ( _Entries::const_iterator curProp = curObj->second.begin() ; 00220 curProp!=curObj->second.end(); ++curProp) 00221 { 00222 StringProperty tmp (curProp->name(), curProp->value() ) ; 00223 addPropertyToCatalogue ( curObj->first , tmp ) ; 00224 } 00225 } 00226 } 00227 // ============================================================================ 00228 MSG::Level 00229 JobOptionsSvc::convertSeverity 00230 ( const Gaudi::Parsers::Message::Severity& severity ) 00231 { 00232 switch ( severity ) 00233 { 00234 case Gaudi::Parsers::Message::E_ERROR : 00235 return MSG::FATAL ; 00236 case Gaudi::Parsers::Message::E_WARNING : 00237 return MSG::WARNING ; 00238 case Gaudi::Parsers::Message::E_NOTICE : 00239 return MSG::INFO ; 00240 default: 00241 return MSG::DEBUG ; 00242 } 00243 } 00244 // ============================================================================ 00245 StatusCode JobOptionsSvc::finalize() 00246 { 00247 // dump the properties catalogue if needed 00248 if ( !m_dump.empty() && !m_dumped ) { dump() ;} 00249 // finalize the base class 00250 return Service::finalize() ; 00251 } 00252 // ============================================================================ 00253 void JobOptionsSvc::dump () 00254 { 00255 // avoid double dump 00256 if ( m_dump.empty() || m_dumped ) { return ; } // RETURN 00257 dump ( m_dump ) ; 00258 // avoid double dump 00259 m_dumped = true ; 00260 } 00261 // ============================================================================ 00262 void JobOptionsSvc::dump( const std::string& file ) const 00263 { 00264 MsgStream log ( msgSvc() , name() ) ; 00265 std::ofstream out 00266 ( file.c_str() , std::ios_base::out | std::ios_base::trunc ) ; 00267 // perform the actual dumping 00268 if ( !out ) 00269 { 00270 log << MSG::ERROR << "Unable to open dump-file \""+file+"\"" << endreq ; 00271 return ; // RETURN 00272 } 00273 else 00274 { log << MSG::INFO << "Properties are dumped into \""+file+"\"" << endreq ; } 00275 // perform the actual dump: 00276 fillStream ( out ) ; 00277 } 00278 // ============================================================================ 00279 std::ostream& JobOptionsSvc::fillStream ( std::ostream& o ) const 00280 { return o << m_pcatalogue ; } 00281 // ============================================================================ 00282 JobOptionsSvc::~JobOptionsSvc() 00283 { 00284 // dump needed? @attention JobOptionsSvc::finalize is never invoked! 00285 if ( !m_dump.empty() && !m_dumped ) { dump() ;} 00286 } 00287 // ============================================================================ 00288 00289 00290 // ============================================================================ 00291 // The END 00292 // ============================================================================ 00293 00294 00295