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

LDAPSynchConnection.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *  filename  : LDAPSynchConnection.h
00003  *  authors   : Salvatore Monforte <salvatore.monforte@ct.infn.it>
00004  *  copyright : (C) 2001, 2002 by INFN
00005  ***************************************************************************/
00006 
00007 // $Id:
00008 
00016 #ifndef _LDAPSYNCHCONNECTION_
00017 #define _LDAPSYNCHCONNECTION_ 
00018 
00019 #include <sys/time.h>
00020 #include <ldap.h>
00021 #include <lber.h>
00022 
00023 #ifdef WITH_LDAP_EXCEPTIONS
00024 #include "edg/workload/common/ldif2classad/exceptions.h"
00025 #endif 
00026 
00028 #include "edg/workload/common/ldif2classad/LDAPConnection.h"
00029 #include "edg/workload/common/ldif2classad/LDIFObject.h"
00030 
00031 namespace edg {
00032 namespace workload {
00033 namespace common {
00034 namespace ldif2classad {
00035 
00041 class ldap_result_entry_t : public generic_result_entry_t
00042 {
00043 public:
00049   ldap_result_entry_t(LDAPMessage* ldentry, LDAP* ld)
00050   { 
00051     this -> ldentry = ldentry;
00052     this -> ld      = ld; 
00053   }  
00054   
00059   bool next()
00060   {
00061     if( this -> good() ) {
00062       LDAPMessage* lne = ldap_next_entry( ld, ldentry );
00063       return ( (ldentry = lne) != NULL );
00064     }
00065     return false;
00066   }
00071   bool good () const 
00072   {
00073     return (ld != NULL && ldentry != NULL);
00074   }
00079   std::string distinguished_name() const 
00080   {
00081     char *dn_ptr = ldap_get_dn( ld, ldentry );
00082     std::string dn(dn_ptr);
00083     ber_memfree(dn_ptr);
00084     return dn;
00085   }
00090   LDIFObject* value() 
00091   {
00092     BerElement *ber = NULL;
00093     
00094     object = LDIFObject();
00095     char *attribute = NULL;
00096     for(attribute = ldap_first_attribute(ld, ldentry, &ber); 
00097         attribute; attribute = ldap_next_attribute(ld, ldentry, ber) ) {
00098       
00099       char **values;
00100       values = ldap_get_values(ld, ldentry, attribute);
00101       if( values ) {
00102         for(int i=0; values[i]!=NULL; i++)
00103           
00104           object.add( (std::string)attribute, (std::string)values[i] );
00105         
00106         ber_memfree(attribute);
00107         ldap_value_free(values);
00108       }
00109       
00110 #ifdef WITH_LDAP_EXCEPTIONS
00111       else {
00112         
00113         if( ber != NULL ) ber_free( ber, 0 );
00114         std::string error_msg( ldap_err2string( ldap_result2error(ld, ldentry, 0) ) );
00115         throw UndefinedValueException(std::string("LDIFObject::value()"), std::string("ldap_get_values()"), error_msg);
00116       }
00117 #endif
00118     }
00119     if( ber != NULL ) ber_free( ber, 0 );
00120     
00121     return &object;
00122   } 
00123 private:
00124   /* A pointer to the concrete LDAP result entry */
00125   LDAPMessage *ldentry;
00126   /* A pointer to the concrete LDAP connection */
00127   LDAP        *ld;    
00128   /* A pointer to the object representation of the entry @see LDIFObject.h */
00129   LDIFObject  object;
00130 };
00131 
00137 class ldap_search_result_t : public generic_search_result_t 
00138 {
00139 public:
00143   ldap_search_result_t(LDAPMessage* ldresult, LDAP* ld) 
00144   {
00145     this -> ldresult = ldresult;
00146     this -> ld       = ld;
00147   }
00148  
00149   ~ldap_search_result_t() 
00150   {
00151     if( ldresult != NULL )
00152       ldap_msgfree(ldresult);
00153   }
00157   bool good() const { return ( ldresult != NULL && ld != NULL ); }
00158   bool empty() const { return ( good() && (ldap_count_entries(ld, ldresult) == 0) ); }
00163   generic_result_entry_t* make_first_entry() const
00164   {
00165     ldap_result_entry_t* r = NULL;
00166     
00167     if( good() ) {
00168       LDAPMessage* ldap_1st_entry = ldap_first_entry( ld, ldresult );
00169 #ifdef WITH_LDAP_EXCEPTIONS      
00170       if( ldap_1st_entry == NULL ) {
00171               std::string error_msg( ldap_err2string( ldap_result2error(ld, ldresult, 0) ) );
00172         throw QueryException(std::string("make_first_entry"), std::string("ldap_first_entry()"), error_msg);
00173       }
00174 #endif
00175       r = new ldap_result_entry_t( ldap_1st_entry, ld );
00176     }
00177     return (r);
00178   }
00179   
00180 private:
00181   /* A pointer to the concrete LDAP result returned by ldap search */
00182   LDAPMessage *ldresult;
00183   /* A pointer to the concrete LDAP connection */
00184   LDAP        *ld;    
00185 };
00186 
00194 class LDAPSynchConnection : public LDAPConnection
00195 {
00196 public:
00200   LDAPSynchConnection(const std::string&, const std::string&, int, long = 15);
00201   
00205   virtual ~LDAPSynchConnection();
00206   
00212   bool open();
00218   bool close();
00225   generic_search_result_t* execute(LDAPQuery* q); 
00230   bool is_established() const;
00231   std::string error() const { return std::string( ldap_err2string(ldap_last_error) ); }
00232 
00233 private:
00234   char** make_topics( const std::vector<std::string>& );
00235 
00236 private:
00238   struct timeval timeout;
00239   int source_port;
00240   std::string source_name;
00241   std::string base_dn;
00242   LDAP* handle;
00243   int ldap_last_error;
00244 };
00245 
00246 } // namespace ldif2classad 
00247 } // namespace common
00248 } // namespace workload
00249 } // namespace edg 
00250 
00251 #endif
00252 
00253 /*
00254   Local Variables:
00255   mode: c++
00256   End:
00257 */

Generated on Fri May 14 14:18:26 2004 for COMMON API - configuration, jobid, ldif2classadi, logger, process, requestad, socket++i, task, utilities by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002