Source

mana-core-clidcomps / src / ClassIDSvc.cxx

The default branch has multiple heads

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include <algorithm>  /* distance */
#include <cstdlib>    /* getenv */
#include <fstream>
#include <iostream>
#include <iterator>
#ifndef HAVE_NEW_IOSTREAMS      /*gnu-specific*/
#define BOOST_NO_STRINGSTREAM 1 /*FIXME should come from boost config */
#endif
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>

#include "GaudiKernel/IIncidentSvc.h"
#include "GaudiKernel/Incident.h"
#include "GaudiKernel/ModuleIncident.h"
#include "GaudiKernel/MsgStream.h"
#include "CLIDSvc/tools/CLIDRegistry.h"
#include "PathResolver/PathResolver.h"

#include "ClassIDSvc.h"

using namespace std;

namespace {
  inline string processName(string s) {
    //remove blanks
    string::size_type i(s.find(' '));
    while (i != string::npos) {
      s.erase(i, 1);
      i = s.find(' ',i);
    }
    return s;
  }
}


/// Standard Constructor
ClassIDSvc::ClassIDSvc(const std::string& name,ISvcLocator* svc)
  : Service(name,svc), m_outputFileName("NULL"),
    m_lastBegin (CLIDRegistry::begin()),
    m_lastEnd (CLIDRegistry::begin())
{
  // Property Default values
  //  m_DBFiles.push_back("./CLIDDB.txt");
    
  // Get user's input	
  declareProperty("CLIDDBFiles",  m_DBFiles, 
		  "list of db files with (CLID, class_name) entries. Loaded at init in svc maps");
  declareProperty("OutputFileName",  m_outputFileName,
		  "path to clid.db file in which write at finalize entries in m_clidMap. Default ('NULL') is not to write output clid.db");

}

// Query the interfaces.
//   Input: riid, Requested interface ID
//          ppvInterface, Pointer to requested interface
//   Return: StatusCode indicating SUCCESS or FAILURE.
// N.B. Don't forget to release the interface after use!!!

StatusCode 
ClassIDSvc::queryInterface(const InterfaceID& riid, void** ppvInterface) 
{
    if ( IClassIDSvc::interfaceID().versionMatch(riid) )    {
        *ppvInterface = (IClassIDSvc*)this;
    }
    else  {
	// Interface is not directly available: try out a base class
	return Service::queryInterface(riid, ppvInterface);
    }
    addRef();
    return StatusCode::SUCCESS;
}

StatusCode 
ClassIDSvc::initialize()
{
  MsgStream log( messageService(), name() );
  log << MSG::INFO << "Initializing " << name() 
      << " - package version " << PACKAGE_VERSION << endreq ;

  // set up the incident service:
  IIncidentSvc* pIncSvc(0);
  const bool CREATEIF(true);
  if (!(service("IncidentSvc", pIncSvc, CREATEIF)).isSuccess()) {
    log << MSG::ERROR 
	<< "Could not locate IncidentSvc "
	<< endreq;
    return StatusCode::FAILURE;
  }
  assert( 0 != pIncSvc );

  const int PRIORITY = 100;
  pIncSvc->addListener(this, ModuleLoadedIncident::TYPE(), PRIORITY);
  pIncSvc->release();

  if (!(Service::initialize()).isSuccess()) return StatusCode::FAILURE;

  return fillDB();
}

bool ClassIDSvc::getRegistryEntries(const std::string& moduleName) {
  bool allOK(true);

  m_lastBegin = CLIDRegistry::begin();
  m_lastEnd = CLIDRegistry::end();
    
  //FIXME this is too simple and wasteful. One should modify CLIDRegistry
  //FIXME so that it returns only the entries corresponding to moduleName
  //FIXME OTH, as a quick hack it works
  CLIDRegistry::const_iterator iEntry(CLIDRegistry::begin());
  CLIDRegistry::const_iterator endEntry(CLIDRegistry::end());
  while (iEntry != endEntry) {
    allOK &= setTypePackageForID(boost::get<0>(*iEntry), 
				 boost::get<1>(*iEntry),
				 boost::get<2>(*iEntry)).isSuccess();
    ++iEntry;
  }
  
  MsgStream log( messageService(), name() );
  if (allOK) {
    int nE = distance(CLIDRegistry::begin(), CLIDRegistry::end());
    log << MSG::INFO
	<< " getRegistryEntries: read " << nE 
	<< " CLIDRegistry entries for module " << moduleName << endreq;
  } else {
    log << MSG::ERROR
	<< " getRegistryEntries: can not read " 
	<< " CLIDRegistry entries for module " << moduleName << endreq;
  }

  return allOK;
}


inline
void ClassIDSvc::maybeRescan() const
{
  if (m_lastBegin != CLIDRegistry::begin() ||
      m_lastEnd != CLIDRegistry::end())
  {
    const_cast<ClassIDSvc*>(this)->getRegistryEntries ("ALL");
  }
}


void ClassIDSvc::handle(const Incident &inc) {
  MsgStream log( messageService(), name() );
  const ModuleLoadedIncident& modInc(dynamic_cast<const ModuleLoadedIncident&>(inc));

  getRegistryEntries(modInc.module());
}


StatusCode 
ClassIDSvc::finalize()
{
  if (m_outputFileName != "NULL") {
    ofstream outfile( m_outputFileName.c_str());
    if ( !outfile ) {
      MsgStream log(messageService(), name());
      log << MSG::ERROR << "unable to open output CLIDDB file: " << m_outputFileName << endreq;
      return StatusCode::FAILURE;
    } else {
      //    ostream_iterator< pair<CLID, string> > os(outfile, ':');
      //    copy(m_clidMap.begin(), m_clidMap,end(), os);
      CLIDMap::const_iterator i(m_clidMap.begin()), iE(m_clidMap.end());
      while (i != iE) {
	outfile << i->first << ' ' << i->second << endl;
	++i;
      }
      MsgStream log(messageService(), name());
      log << MSG::DEBUG << "finalize: wrote " << m_clidMap.size() 
	  << " entries to output CLIDDB file: " << m_outputFileName << endreq;
    }
    outfile.close();
  } //outputfilename != NULL
  return Service::finalize();
}

/// get next available CLID 
/// @throws std::runtime_error if no CLID can be allocated
CLID 
ClassIDSvc::nextAvailableID() const {
  maybeRescan();
  CLID valid(CLIDRegistry::MINCLID);
  while (valid <= CLIDRegistry::MAXCLID && isIDInUse(valid)) ++valid;
  if (valid > CLIDRegistry::MAXCLID) throw runtime_error("ClassIDSvc::nextAvailableID: none in range");
  return valid;
}

bool
ClassIDSvc::isIDInUse(const CLID& id ) const {
  maybeRescan();
  return 0 != m_clidMap.count(id);
}

bool
ClassIDSvc::isNameInUse(const string& name ) const {
  maybeRescan();
  return 0 != m_nameMap.count(name);
}

/// get type name associated with clID (if any)
StatusCode 
ClassIDSvc::getTypeNameOfID(const CLID& id, std::string& typeName) const {
  maybeRescan();
  StatusCode sc(StatusCode::FAILURE);
  CLIDMap::const_iterator iID = m_clidMap.find(id);
  if (iID != m_clidMap.end()) {
    typeName = iID->second;
#ifndef NDEBUG		
    MsgStream log(messageService(), name());
    log << MSG::VERBOSE
	<< "getTypeNameOfID(" << id 
	<< ") type name is " << typeName << endreq;
#endif			
    sc = StatusCode::SUCCESS;
  } else {
#ifndef NDEBUG		
    MsgStream log(messageService(), name());
    log << MSG::VERBOSE
	<< "getTypeNameOfID(" << id 
	<< ") no associated type name found " << endreq;
#endif			
  }
  return sc;
}
/// get PackageInfo associated with clID (if any)
StatusCode 
ClassIDSvc::getPackageInfoForID(const CLID& id, Athena::PackageInfo& info) const {
  maybeRescan();
  StatusCode sc(StatusCode::FAILURE);
  PackageMap::const_iterator iID = m_packageMap.find(id);
  if (iID != m_packageMap.end()) {
    info = iID->second;
#ifndef NDEBUG		
    MsgStream log(messageService(), name());
    log << MSG::VERBOSE
	<< "getPackageInfoForID(" << id 
	<< ") package name is " << info.name() 
	<< " package version is " << info.version()
	<< endreq;
#endif			
    sc = StatusCode::SUCCESS;
  } else {
#ifndef NDEBUG		
    MsgStream log(messageService(), name());
    log << MSG::VERBOSE
	<< "getPackageInfoForID(" << id 
	<< ") no associated type name found " << endreq;
#endif			
  }
  return sc;
}
/// get id associated with type name (if any)
StatusCode 
ClassIDSvc::getIDOfTypeName(const std::string& typeName, CLID& id) const {
  maybeRescan();
  StatusCode sc(StatusCode::FAILURE);
  NameMap::const_iterator iID = m_nameMap.find(typeName);
  if (iID != m_nameMap.end()) {
    id = iID->second;
#ifndef NDEBUG		
    MsgStream log(messageService(), name());
    log << MSG::VERBOSE
	<< "getIDOfTypeName(" << typeName
	<< ") CLID is " << id << endreq;
#endif			
    sc = StatusCode::SUCCESS;
  } else {
#ifndef NDEBUG		
    MsgStream log(messageService(), name());
    log << MSG::VERBOSE
	<< "getIDOfTypeName(" << typeName 
	<< ") no associated CLID found " << endreq;
#endif			
  }
  return sc;
}

/// associate type name with clID
StatusCode 
ClassIDSvc::setTypePackageForID(const CLID& id, const std::string& typeName,
				const Athena::PackageInfo& info) {
  if (id < CLIDRegistry::MINCLID || id > CLIDRegistry::MAXCLID) {
    MsgStream log(messageService(), name());
    log << MSG::ERROR 
	<< "setTypeNameForID: input id " << id 
	<< " is out of allowed range " 
	<< CLIDRegistry::MINCLID << " : " << CLIDRegistry::MAXCLID << endreq;
    return StatusCode::FAILURE;
  }
  return uncheckedSetTypePackageForID(id, typeName, info);
}
StatusCode 
ClassIDSvc::uncheckedSetTypePackageForID(const CLID& id, 
					 const std::string& typeName,
					 const Athena::PackageInfo& info) {
  StatusCode sc(StatusCode::SUCCESS);
  //process "raw" typeName
  string procName(processName(typeName));
  //first the id->name map
  string knownName("_____++++");
  if (getTypeNameOfID(id, knownName).isSuccess() && procName != knownName) {
    MsgStream log(messageService(), name());
    log << MSG::ERROR 
	<< "uncheckedSetTypePackageForID: " << info
	<< " can not set type name <" << procName 
	<< "> for CLID " << id
	<< ": Known name for this ID <" << knownName << '>';
    Athena::PackageInfo existInfo;
    if (getPackageInfoForID(id, existInfo).isSuccess()) {
      log << MSG::ERROR 
	  << " It was set by " << existInfo;
    }
    log << MSG::ERROR << endreq;
    sc = StatusCode::FAILURE;
  } else if (procName == knownName) {
#ifndef NDEBUG		
    MsgStream log(messageService(), name());
    log << MSG::VERBOSE
	<< "uncheckedSetTypePackageForID: type name <" << procName 
	<< "> already set for CLID " << id;
    Athena::PackageInfo existInfo;
    if (getPackageInfoForID(id, existInfo).isSuccess()) {
      log << MSG::VERBOSE 
	  << " It was set by " << existInfo;
    }
    log << MSG::VERBOSE << endreq;
#endif			
  } 
  if (!sc.isSuccess()) return StatusCode::FAILURE;

  //now the name->id map
  CLID knownID(0);
  if (getIDOfTypeName(procName, knownID).isSuccess() && id != knownID) {
    MsgStream log(messageService(), name());
    log << MSG::ERROR 
	<< "uncheckedSetTypePackageForID: " << info
	<< " can not set CLID <" << id
	<< "> for type name " << procName
	<< ": Known CLID for this name <" << knownID << '>' << endreq;
    Athena::PackageInfo existInfo;
    if (getPackageInfoForID(knownID, existInfo).isSuccess()) {
      log << MSG::ERROR 
	  << " It was set by " << existInfo; 
    }
    log << MSG::ERROR << endreq;
    sc = StatusCode::FAILURE;
  } else if (id == knownID) {
#ifndef NDEBUG		
    MsgStream log(messageService(), name());
    log << MSG::VERBOSE
	<< "uncheckedSetTypePackageForID: CLID <" << id 
	<< "> already set for type name " << procName;
    Athena::PackageInfo existInfo;
    if (getPackageInfoForID(id, existInfo).isSuccess()) {
      log << MSG::VERBOSE 
	  << " It was set by " << existInfo;
    }
    log << MSG::VERBOSE << endreq;
#endif			
  } 
//   //finally the id->package map
//   Athena::PackageInfo knownInfo;
//   if (getPackageInfoForID(id, knownInfo).isSuccess() && info != knownInfo) {
//     MsgStream log(messageService(), name());
//     log << MSG::ERROR 
// 	<< "uncheckedSetTypePackageForID: can not set package info <" << info
// 	<< "> for CLID " << id
// 	<< ": Known info for this CLID <" << knownInfo << '>' << endreq;
//     sc = StatusCode::FAILURE;
//   } else if (info == knownInfo) {
// #ifndef NDEBUG		
//     MsgStream log(messageService(), name());
//     log << MSG::VERBOSE
// 	<< "uncheckedSetTypePackageForID: package info <" << info
// 	<< "> already set for CLID " << id<< endreq;
// #endif			
//   } else {
    m_clidMap[id] = procName;
    m_nameMap[procName] = id;
    m_packageMap[id] = info;
#ifndef NDEBUG		
    MsgStream log(messageService(), name());
    log << MSG::VERBOSE
	<< "uncheckedSetTypePackageForID: set type name <" << procName 
	<< "> for CLID " << id << endreq;
#endif					
    //  }
  return sc;
}
bool
ClassIDSvc::processCLIDDB(const char* fileName) {
  maybeRescan();
  bool allOK(true);
  ifstream ifile(fileName);
  if (!ifile) {
    MsgStream log(messageService(), name());
    log << MSG::WARNING << "processCLIDDB: unable to open input CLIDDB " 
	<< fileName << endreq;
  } else {
#ifndef NDEBUG
    unsigned int newEntries(0);
#endif
    string line;
    while (std::getline(ifile, line)) {
      //not yet if ("#" == line.substr(0,0)) continue; //skip comments
      //split the record in 2 fields:	
      //      cout << "record " << line << endl;
      bool readOK(true);
      long id(-1);       
      try {
	id = boost::lexical_cast<long>(line.substr(0,line.find(' ')));
      } catch (boost::bad_lexical_cast e) {
	readOK = false;
      }
      //      cout << "id " << id << endl;
      if (readOK) {
	string typeName(line.substr(line.find(' ') + 1, string::npos));
	//	cout << "typeName " << typeName << endl;
	Athena::PackageInfo info("FIXME-00-00-00"); //FIXME get from file
	if ((readOK = !typeName.empty())) {
	  if (uncheckedSetTypePackageForID(id, typeName, info).isSuccess()) {
#ifndef	 NDEBUG
	    MsgStream log(messageService(), name());
	    log << MSG::VERBOSE 
		<< "processCLIDDB(" << fileName 
		<< ")\n    added entry for CLID <" << id 
		<< "> type name <" << typeName << '>' << endreq;
	    ++newEntries;
#endif	
	  }
	}
#ifndef	 NDEBUG
#endif	
      }
      allOK &= readOK;
    } //while records
    if (!allOK) {
      MsgStream log(messageService(), name());
      log << MSG::ERROR << "processCLIDDB: processing record " << line 
	  << " 	from CLIDDB file: " << fileName << endreq;
    } else {
#ifndef NDEBUG
      MsgStream log(messageService(), name());
      log << MSG::DEBUG << "processCLIDDB: read " << newEntries 
	  << " entries from CLIDDB file: " << fileName << endreq;
#endif	
    }
    ifile.close();
  } //input file open
      
  return allOK;
}

void 
ClassIDSvc::dump() const {
  maybeRescan();
  MsgStream log(messageService(), name());
  log << MSG::INFO << "dump: in memory db\n";
    CLIDMap::const_iterator i(m_clidMap.begin()), iE(m_clidMap.end());
    while (i != iE) {
      log << MSG::INFO 
	  << "CLID: "<< i->first 
	  << " - type name: " << i->second;
      Athena::PackageInfo info;
      if (getPackageInfoForID(i->first, info).isSuccess()) {
	log << MSG::INFO 
	    << "- Package "<< info; 
      }
      log << MSG::INFO << '\n';
      ++i;
    }
    log << MSG::INFO << "------------------------------" << endreq;
}

StatusCode
ClassIDSvc::fillDB() {
  // Process the various clid dbs according to user's request
  vector< string >::const_iterator i(m_DBFiles.begin()), iE(m_DBFiles.end());
  bool allOK(true);
  while (i != iE) {
    std::string filepath(PathResolver::find_file(*i, "DATAPATH"));
    if (filepath.empty()) {
      MsgStream log(messageService(), name());
      log << MSG::WARNING << "Could not resolve file name " 
	  << *i << " using PathResolver DATAPATH [" 
	  << getenv("DATAPATH") << "] ----- SKIPPING" << endmsg;
    } else {
      allOK &= processCLIDDB(filepath.c_str());
    }
    ++i;
  }
  
  return (allOK && getRegistryEntries("ALL")) ? 
    StatusCode::SUCCESS :
    StatusCode::FAILURE;
}

StatusCode
ClassIDSvc::reinitialize() {
  MsgStream log( messageService(), name() );
  log << MSG::INFO << "RE-initializing " << name() 
      << " - package version " << PACKAGE_VERSION << endreq ;  
  return fillDB();
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.