Getting connection strings from LDAP

After a recent migration, we found deficiencies with the process of how clients connect. As such, we are trying to standardize our repository of connection strings.

We implemented an OpenLDAP server and added the schema information for Oracle objects from OID (Oracle Internet Directory). We were able to access the strings, but found ourselves concerned with failures. We didn’t want to implement a high availability naming service at this point, as our goal is more standardization rather than speed and availability.

We ended up implementing a process that grabs the string prior to application startup. We had been hardcoding the URL in an application properties file, which all have to be changed when the database location changes of course.

We implemented a simple awk script to grab the URL and pass it to the application via a -Durl= command line property.

URL=$(ldapsearch -x -h linux5 -b "cn=OracleContext,dc=appcrawler,dc=com" cn=tst10g | grep -i orclnetdescstring | awk -F = '{{ for (i = 2; i < NF; i++) printf $i"=" }print $NF}') 

This will return the URL into a host variable. If you have concerns with this method for whatever reason, the following can be used to query LDAP from within Java itself.

import java.util.Hashtable; 
import javax.naming.*; 
import javax.naming.directory.*;  

public class getOraURL {   
  String url;    
  getOraURL() throws Exception{     
    Hashtable env = new Hashtable(11);     
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");        
    DirContext dctx = new InitialDirContext(env);      
    String filter = "(cn=tst10g)";   
    NamingEnumeration result = dctx.search("ldap://192.168.1.60/cn=OracleContext,dc=appcrawler,dc=com", filter, null);      
    while (result.hasMore()) {       
      SearchResult match = (SearchResult) result.next();       
      Attributes attrs = match.getAttributes();       
      NamingEnumeration e = attrs.getAll();       
      while (e.hasMoreElements()) {         
        Attribute attr = (Attribute) e.nextElement();         
        for (int i=0; i < attr.size(); i++) {
          url = attr.get(i).toString();
        }
      }   
    }
  }
}

The url variable that is set in the class above can be returned to the application to use as it sees fit.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.