Gaudi Framework, version v24r2

Home   Generated: Wed Dec 4 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MultiFileCatalog.cpp
Go to the documentation of this file.
4 #include "Reflex/PluginService.h"
5 #include "MultiFileCatalog.h"
6 #include <stdexcept>
7 #include <algorithm>
8 
9 namespace Gaudi { std::string createGuidAsString(); }
10 
11 using ROOT::Reflex::PluginService;
12 using namespace Gaudi;
13 using namespace std;
15 
16 namespace {
17  template <class V,class F>
18  bool _findX0Bool(V& array, F pmf, bool invert) {
19  for(typename V::const_iterator i=array.begin(); i != array.end(); ++i) {
20  bool res = invert ? !((*i)->*pmf)() : ((*i)->*pmf)();
21  if ( !res ) return false;
22  }
23  return true;
24  }
25 }
26 // ----------------------------------------------------------------------------
27 MultiFileCatalog::MultiFileCatalog(const std::string& nam, ISvcLocator* svc)
28  : base_class(nam, svc), m_started(false), m_oldNames()
29 {
30  declareProperty("Catalogs", m_catalogNames, "The list of Catalogs")
31  -> declareUpdateHandler ( &Gaudi::MultiFileCatalog::propHandler, this ) ;
32  m_catalogNames.push_back("xmlcatalog_file:test_catalog.xml");
33 }
34 // ----------------------------------------------------------------------------
35 MultiFileCatalog::~MultiFileCatalog() {
36 }
37 // ----------------------------------------------------------------------------
40  std::string current = "";
41  if ( !Service::initialize().isSuccess() ) {
42  printError("Failed to initialize service base class.",false);
43  return StatusCode::SUCCESS;
44  }
45  try {
46  for(i=m_catalogNames.begin(); i != m_catalogNames.end(); ++i) {
47  current = *i;
48  addCatalog(*i);
49  }
50  init();
51  return StatusCode::SUCCESS;
52  }
53  catch(const std::exception& /* e */) {
54  printError("Cannot add file catalog:"+current,false);
55  }
56  return StatusCode::FAILURE;
57 }
58 // ----------------------------------------------------------------------------
59 StatusCode MultiFileCatalog::finalize() {
60  commit();
61  _exec(&IFileCatalog::release);
62  m_catalogs.clear();
63  m_started = false;
64  return Service::finalize();
65 }
66 // ----------------------------------------------------------------------------
68 std::string MultiFileCatalog::createFID() const {
69  return createGuidAsString();
70 }
71 // ----------------------------------------------------------------------------
72 MultiFileCatalog::CSTR MultiFileCatalog::connectInfo() const {
73  static string s("MultiCatalog");
74  return s;
75 }
76 // ----------------------------------------------------------------------------
77 IFileCatalog* MultiFileCatalog::getCatalog(CSTR fid,
78  bool throw_if_not,
79  bool writable,
80  bool prt) const
81 {
82  for(Catalogs::const_iterator i=m_catalogs.begin(); i != m_catalogs.end(); ++i) {
83  IFileCatalog* c = *i;
84  if ( c ) {
85  if ( writable && c->readOnly() )
86  continue;
87  else if ( fid.empty() )
88  return c;
89  else if ( !fid.empty() && c->existsFID(fid) )
90  return c;
91  }
92  }
93  if ( prt ) {
94  printError("No writable file catalog found which contains FID:"+fid,throw_if_not);
95  }
96  else {
97  MsgStream log(msgSvc(),name());
98  log << MSG::DEBUG << "No writable file catalog found which contains FID:" << fid << endmsg;
99  }
100  return 0;
101 }
102 // ----------------------------------------------------------------------------
103 IFileCatalog* MultiFileCatalog::findCatalog(CSTR connect, bool must_be_writable) const {
104  for(Catalogs::const_iterator i=m_catalogs.begin(); i != m_catalogs.end(); ++i) {
105  if ( connect == (*i)->connectInfo() )
106  return (must_be_writable && (*i)->readOnly()) ? 0 : *i;
107  }
108  return 0;
109 }
110 // ----------------------------------------------------------------------------
112 MultiFileCatalog::i_findCatalog(CSTR connect, bool must_be_writable) {
113  for(Catalogs::iterator i=m_catalogs.begin(); i != m_catalogs.end(); ++i) {
114  if ( connect == (*i)->connectInfo() ) {
115  return (must_be_writable && (*i)->readOnly()) ? m_catalogs.end() : i;
116  }
117  }
118  return m_catalogs.end();
119 }
120 // ----------------------------------------------------------------------------
121 void MultiFileCatalog::printError(CSTR msg, bool rethrow) const {
122  MsgStream log(msgSvc(),name());
123  if ( rethrow ) {
124  log << MSG::FATAL << msg << endmsg;
125  throw runtime_error("Catalog> "+msg);
126  }
127  log << MSG::ERROR << msg << endmsg;
128 }
129 // ----------------------------------------------------------------------------
130 void MultiFileCatalog::addCatalog(CSTR con) {
131  if ( !con.empty() ) {
132  if ( 0 == findCatalog(con,false) ) {
133  static string xml_typ = "Gaudi::XMLFileCatalog";
134  string::size_type id0 = con.find("_");
135  string typ = con.substr(0,id0);
136  string url = con.substr(id0+1);
137  IInterface* cat = 0;
138  if ( strncasecmp("xml",typ.c_str(),3) == 0 ) {
139  cat = PluginService::Create<IInterface*>(xml_typ,url,msgSvc().get());
140  }
141  else {
142  cat = PluginService::Create<IInterface*>(typ,url,serviceLocator().get());
143  if ( !cat ) {
144  cat = PluginService::Create<IInterface*>(typ,url,msgSvc().get());
145  }
146  }
147  if ( cat ) {
148  IFileCatalog* fileCat = 0;
149  if ( cat->queryInterface(IFileCatalog::interfaceID(),pp_cast<void>(&fileCat)).isSuccess() ) {
150  addCatalog(fileCat);
151  cat->release();
152  return;
153  }
154  }
155  printError("Failed to create catalog connection:"+con,true);
156  }
158  return;
159  }
160  printError("Got invalid (empty) catalog connection string.",true);
161 }
162 // ----------------------------------------------------------------------------
163 void MultiFileCatalog::addCatalog(IFileCatalog* cat) {
164  if ( cat ) {
165  cat->addRef();
166  m_catalogs.push_back(cat);
167  return;
168  }
169  printError("Got invalid catalog to be added to multi catalog.",true);
170 }
171 // ----------------------------------------------------------------------------
172 void MultiFileCatalog::removeCatalog(CSTR con) {
173  if ( con.empty() || con == "*" ) {
174  _exec(&IFileCatalog::release);
175  m_catalogs.clear();
176  return;
177  }
178  removeCatalog(findCatalog(con,false));
179 }
180 // ----------------------------------------------------------------------------
181 void MultiFileCatalog::removeCatalog(const IFileCatalog* cat) {
182  if ( cat ) {
183  Catalogs::iterator i=find(m_catalogs.begin(),m_catalogs.end(),cat);
184  if ( i != m_catalogs.end() ) {
185  (*i)->release();
186  m_catalogs.erase(i);
187  return;
188  }
189  printError("Unknown file catalog -- cannot be removed.",true);
190  }
191  printError("Invalid file catalog.",true);
192 }
193 // ----------------------------------------------------------------------------
194 void MultiFileCatalog::setWriteCatalog(IFileCatalog* cat) {
195  if ( cat ) {
196  if ( !cat->readOnly() ) {
197  Catalogs::iterator i=find(m_catalogs.begin(),m_catalogs.end(),cat);
198  if ( i != m_catalogs.end() ) {
199  m_catalogs.erase(i);
200  m_catalogs.insert(m_catalogs.begin(),cat);
201  return;
202  }
203  printError("The catalog "+cat->connectInfo()+" is not known.",true);
204  }
205  printError("The catalog "+cat->connectInfo()+" is not writable.",true);
206  }
207  printError("Invalid file catalog.",true);
208 }
209 // ----------------------------------------------------------------------------
210 void MultiFileCatalog::setWriteCatalog(CSTR connect) {
211  Catalogs::iterator i = i_findCatalog(connect,true);
212  if ( i == m_catalogs.end() ) {
213  addCatalog(connect);
214  setWriteCatalog(findCatalog(connect,true));
215  return;
216  }
217  setWriteCatalog(*i);
218 }
219 // ----------------------------------------------------------------------------
220 string MultiFileCatalog::getMetaDataItem(CSTR fid,CSTR attr) const {
221  std::string result;
222  for(Catalogs::const_iterator i=m_catalogs.begin(); i != m_catalogs.end(); ++i)
223  if ( !(result= (*i)->getMetaDataItem(fid,attr)).empty() ) break;
224  return result;
225 }
227 void MultiFileCatalog::registerPFN(CSTR fid, CSTR pfn, CSTR ftype) const {
228  IFileCatalog* c = getCatalog(fid,false,true,false);
229  if ( !c ) c = getCatalog("",true,true,true);
230  c->registerPFN(fid, pfn, ftype);
231 }
233 void MultiFileCatalog::registerLFN(CSTR fid, CSTR lfn) const {
234  IFileCatalog* c = getCatalog(fid,false,true,false);
235  if ( !c ) c = getCatalog("",true,true,true);
236  c->registerLFN(fid, lfn);
237 }
238 // ----------------------------------------------------------------------------
239 bool MultiFileCatalog::readOnly() const
240 { return _findX0Bool(m_catalogs,&IFileCatalog::readOnly,false); }
241 // ----------------------------------------------------------------------------
242 bool MultiFileCatalog::dirty() const
243 { return _findX0Bool(m_catalogs,&IFileCatalog::dirty,true); }
244 // ----------------------------------------------------------------------------
245 void MultiFileCatalog::propHandler(Property& /* p */)
246 {
247  // not yet initialized
248  if ( !m_started ) { m_oldNames = m_catalogNames; return; } // RETURN
249  // no real change - no action
250  if ( m_catalogNames == m_oldNames ) { return; }
251  m_oldNames = m_catalogNames ;
252  // remove ALL catalogs
253  removeCatalog("") ;
254  // add new catalogs
255  for ( CatalogNames::const_iterator inew = m_catalogNames.begin() ;
256  m_catalogNames.end() != inew ; ++inew ) { addCatalog ( *inew ) ; }
257  // start
258  init() ;
259  //
260  MsgStream log ( msgSvc() , name() ) ;
261  log << MSG::DEBUG
262  << "New catalogs to be used: "
263  << Gaudi::Utils::toString ( m_catalogNames ) << endmsg ;
264 }

Generated at Wed Dec 4 2013 14:33:11 for Gaudi Framework, version v24r2 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004