// GenericRepository.cxx #include "dataset_catalog/GenericRepository.h" #include "dataset_util/FileStatus.h" #include "dataset_util/ThreadCredential.h" #include "dataset_catalog/ConnectionResolver.h" #include #include #include using std::string; using std::cout; using std::cerr; using std::endl; using std::vector; using std::map; using dset::ConnectionResolver; using dset::GenericRepository; typedef GenericRepository::Create Create; typedef map CMap; typedef GenericRepository::IdList IdList; typedef GenericRepository::ElementList ElementList; typedef vector NameList; //********************************************************************** // Local defintions. //********************************************************************** namespace { CMap& cmap() { static CMap val; return val; } bool debug() { return FileStatus("debug_GenericRepository").exists(); } } // end unnamed namespace //********************************************************************** // Static member functions. //********************************************************************** // Register a creator. int GenericRepository::register_creator(string name, Create pfun) { CMap::const_iterator icre = cmap().find(name); if ( icre != cmap().end() ) { if ( icre->second == pfun ) return 0; return 1; } cmap()[name] = pfun; return 0; } //********************************************************************** // Create a generic repository. GenericRepository* GenericRepository::create(string type, string conn) { string prefix = "GenericRepository::create: "; ConnectionResolver resolver; string newconn = resolver.resolve(type, conn); string name = ConnectionResolver::accessor_name(newconn); if ( name == "" ) { name = ConnectionResolver::location_name(newconn); } if ( name == "" ) { cerr << prefix << "Unable to resolve connection " << conn << " for type " << type << endl; return 0; } CMap::const_iterator icre = cmap().find(name); // Temporary patch to load WS libraries until we have plugin manager. bool doload = true; if ( icre == cmap().end() && name == "WS" && doload ) { NameList libs; libs.push_back("dial_ws_repository"); for ( NameList::const_iterator ilib=libs.begin(); ilib!=libs.end(); ++ilib ) { string lib = "lib" + *ilib + ".so"; void* handle = dlopen(lib.c_str(), RTLD_NOW|RTLD_GLOBAL); if ( handle == 0 ) { cerr << prefix << "Unable to load library " << lib << endl; cerr << " " << dlerror() << endl; } } icre = cmap().find(name); doload = false; } // End patch. if ( icre == cmap().end() ) { cerr << prefix << "Unable locate creator for name " << name << endl; return 0; } Create pfun = icre->second; if ( pfun == 0 ) { cerr << prefix << "Null creator for name " << name << endl; return 0; } return pfun(type, newconn); } //********************************************************************** // Member functions. //********************************************************************** // Constructor. GenericRepository::GenericRepository() { } //********************************************************************** // Destructor. GenericRepository::~GenericRepository() { } //********************************************************************** // Validity. bool GenericRepository::is_valid() const { return error() == 0; } //********************************************************************** // Cache connection string. string GenericRepository::cache_connection() const { return ""; } //********************************************************************** // Get multiple elements. int GenericRepository::mget(const IdList& sids, ElementList& eles) const { int stat = 0; for ( IdList::const_iterator isid=sids.begin(); isid!=sids.end(); ++isid ) { string ele = get(*isid); if ( ele.size() ) { eles.push_back(ele); } else { ++stat; } } return stat; } //********************************************************************** // Authorization. bool GenericRepository::authorized(string) const { string prefix = "GenericRepository::authorized: "; ThreadCredential tc; //bool auth = tc.owner().size(); bool auth = true; if ( debug() ) { cout << "GenericRepository::authorized: " << endl; cout << " thread owner: " << tc.owner() << endl; cout << " auth: " << auth << endl; } else if ( ! auth ) { cerr << prefix << "Authorization denied" << endl; cout << " thread owner: " << tc.owner() << endl; } return auth; } //**********************************************************************