Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

LcgInfoInterface.cpp

00001                               /* LcgInfoInterface.cpp */
00002 
00003 /*
00004  *  Implementation of LcgInfoInterface.h
00005  */
00006 #include <iostream>
00007 #include "PSEUDO_SQLtoLDAPQuerier.h"
00008 #include "LibFactory.h"
00009 #include "ConfigBuffer.h"
00010 #include "QueryTranslationException.h"
00011 #include "TranslatorsUtils.h"
00012 #include "LcgInfoInterface.h"
00013 
00014 using namespace std;
00015 using namespace LcgInfo;
00016 
00017 LcgInfoInterface::LcgInfoInterface(){
00018 }
00019 
00020 LcgInfoInterface::~LcgInfoInterface(){
00021 }
00022 
00023 
00024 void LcgInfoInterface::initialize(LcgConfigBuffer::ConfigBuffer & pConf){
00025    mConf=&pConf;   
00026    prepareAll();
00027 }
00028 
00029 void LcgInfoInterface::initialize(string const & pConfFile){
00030    //First, get the mConfiguration data from the file
00031    mConf = new LcgConfigBuffer::ConfigBuffer();
00032    try{
00033       mConf->populate_from_config(pConfFile);
00034    }catch (LcgConfigBuffer::CBException& e){
00035        cerr << "initialize: Problems reading the main configuration file." << endl;
00036        cerr << e.what() << " +line: " << e.get_line() << " +file: " << e.get_file() << endl;
00037        exit (-1);
00038    }
00039 
00040    prepareAll();
00041 }//end of initialize
00042 
00043 void LcgInfoInterface::prepareAll(){   
00044    string library;
00045    string constructor_method;
00046    string destructor_method;
00047    vector<string> ifacesList;
00048 
00049    //Get the list of interfaces
00050    try{
00051       string ifaceAttr=mConf->get_attribute_value("INTERFACE_LIST");
00052       tokenizeStr(ifaceAttr,ifacesList);
00053    }catch (LcgConfigBuffer::CBException& e){
00054        cerr << "initialize: Problems loading the list of interfaces to be loaded." << endl;
00055        cout << e.what() << " +line: " << e.get_line() << " +file: " << e.get_file() << endl;
00056        exit (-1);
00057    }
00058 
00059    //Now, register all the specified libraries
00060    bool registered=false;
00061    for(vector<string>::iterator iface=ifacesList.begin(); iface!=ifacesList.end(); iface++){
00062       try{
00063          library=mConf->get_attribute_value(*iface+"_LIBRARY");
00064          constructor_method=mConf->get_attribute_value(*iface+"_LIBRARY_CONSTRUCTOR_METHOD");
00065          destructor_method=mConf->get_attribute_value(*iface+"_LIBRARY_DESTRUCTOR_METHOD");
00066          mFactory.Register(*iface, library, constructor_method, destructor_method);
00067          pair<string, destroy_Querier*> thePair(*iface, 0);
00068          mIfaces.insert(thePair);
00069          registered=true;
00070       }catch (LcgConfigBuffer::CBException& e){
00071           cerr << "initialize: Problems retrieving the library name for the interface \"";
00072           cerr << *iface << "\".\n";
00073           cerr << e.what() << " +line: " << e.get_line() << " +file: " << e.get_file() << endl;
00074       }
00075    }
00076    if(!registered){
00077       string msg="initialize: No library interface could be loaded. Aborting.";
00078       throw LcgInfoException(msg,__FILE__,__LINE__);
00079    }
00080 }//end of prepareAll()
00081 
00082 
00083 Querier * LcgInfoInterface::connect(){
00084    return connect((*(mIfaces.begin())).first);
00085 }
00086 
00087 
00088 Querier * LcgInfoInterface::connect(string const & pIface){
00089    Querier * querier=0;
00090    create_Querier* querierCreator=0;
00091    destroy_Querier* querierDestructor=0;
00092    
00093    map<string, destroy_Querier*>::iterator ifacePointer;
00094    if((ifacePointer=mIfaces.find(pIface))==mIfaces.end()){
00095       string msg="connect: The specified interface ("+pIface+") was not registered. Aborting.";
00096       throw LcgInfoException(msg,__FILE__,__LINE__);
00097    }
00098    if((*ifacePointer).second!=0){
00099       string msg="connect: A connection to the specified interface ("+pIface;
00100       msg += ") exist already. Aborting.";
00101       throw LcgInfoException(msg,__FILE__,__LINE__);            
00102    }
00103 
00104    try{
00105       // Load the appropriate library (appropriate interface) for the desired query
00106       
00107       if(mFactory.LibCreate(pIface, &querierCreator, &querierDestructor)==0){
00108          string msg="connect: The libraries for the specified interface ("+pIface;
00109          msg+= ") could not be loaded. Aborting.";
00110          throw LcgInfoException(msg,__FILE__,__LINE__);
00111       };   
00112 
00113       querier = querierCreator();
00114       mIfaces.erase(pIface);
00115       pair<string, destroy_Querier*> thePair(pIface, querierDestructor);
00116       mIfaces.insert(thePair);
00117    
00118       // Extract the interface specific mConfiguration information, and pass it to the Querier
00119       LcgConfigBuffer::ConfigBuffer * ifaceConf = new LcgConfigBuffer::ConfigBuffer();
00120       vector<string> attrList;
00121       attrList = mConf->list_attributes();
00122 
00123 //debug
00124 //for(int i=0; i<attrList.size(); i++){
00125 //   cout << attrList.at(i) << endl;
00126 //}
00127 //end of debug
00128 
00129       for(vector<string>::iterator attr=attrList.begin(); attr!=attrList.end(); attr++){
00130          if(attr->find(pIface)==0){  //starts with correct prefix ==> relevant attr
00131             string newAttr=attr->substr(pIface.size()+1);
00132             ifaceConf->create_new_attribute(newAttr);  //store it without the prefix
00133             ifaceConf->set_attribute_value(newAttr, mConf->get_attribute_value(*attr));
00134          }
00135       }
00136       querier->setConfig(*ifaceConf);
00137       
00138    }catch(LcgConfigBuffer::CBException& e){
00139        string msg="connect: Problems when creating the connection (interface: " + pIface+ ").";
00140        msg += "\nUnderlying catched CBException message: " + e.what() + " +line: ";
00141        msg += int2str(e.get_line()) + " +file: " + e.get_file();
00142        throw LcgInfoException(msg,__FILE__,__LINE__);
00143     }
00144     catch(LcgInfo::QueryTranslationException & f){
00145        string msg="connect: Problems when creating the connection (interface: " + pIface+ "). ";
00146        msg += "\nUnderlying catched QueryTranslationException message: " + f.what() + " +line: ";
00147        msg += int2str(f.get_line()) + " +file: " + f.get_file();
00148        throw LcgInfoException(msg,__FILE__,__LINE__);
00149     }
00150 
00151     return querier;
00152 }
00153 
00154 
00155 void LcgInfoInterface::disconnect(string const & pIface, Querier * pIfaceHandler){
00156    
00157    map<string, destroy_Querier*>::iterator ifacePointer;
00158    if((ifacePointer=mIfaces.find(pIface))==mIfaces.end()){
00159       string msg="disconnect: The specified interface ("+pIface+") was not registered. Aborting.";
00160       throw LcgInfoException(msg,__FILE__,__LINE__);
00161    }
00162    if((*ifacePointer).second==0){
00163       string msg="disconnect: Trying to disconnect from a connection that did not exist";
00164       msg+= "(interface: " + pIface+ "). Aborting.";
00165       throw LcgInfoException(msg,__FILE__,__LINE__);            
00166    }
00167   
00168    //Destroy the Querier *
00169    (*ifacePointer).second(pIfaceHandler);
00170    //Deregister the interface
00171    (*ifacePointer).second=0;
00172 }
00173 
00174 
00175 

Generated on Tue Oct 5 14:42:45 2004 for LCG Information System Interface by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002