Thursday, September 24, 2009

LDAP Change log number

Sorry,

I was inactive on this blog for quite some time but I will try to post on this once again.

In this blog I am trying to put some light on the LDAP change log numbers and how they work.

until now I was under impression that LDAP change log number always start with 1 and increase in sequence. It does increase in sequence but if an LDAP environment has a limit to keep change log only for last N number of days then LDAP first changelog number is not 1 but something else.

Now if you have to check what is the first and last change log number in your directory then how would you do it programatically?

Below is the code which can be used to get the lastchangelognumber. Similarly one can get the firstchangelognumber from the directory.

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.NamingEnumeration;
import javax.naming.directory.SearchResult;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;

public class ReadingLDAPChangeLog {

/**
* @param args
*/

static String RETURN_ATTRIBUTES[] = { "changes"};
static String RETURN_ATTRIBUTES_CHANGELOG[] = { "lastchangenumber"};

public static DirContext makeLDAPConnection (String Hostname, String Port, String BaseDN, String BindDN, String Password)
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://" + Hostname + ":" + Port + "/" + BaseDN);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, BindDN);
env.put(Context.SECURITY_CREDENTIALS, Password);
env.put(Context.BATCHSIZE, "100");
try
{
DirContext ctx = new InitialDirContext(env);
return ctx;
}
catch (Exception e)
{
System.out.println("Unable to connect to LDAP server.");
System.out.println("Please check connection parameters set in my.properties");
e.printStackTrace();
System.exit(0);
}
return null;
}


public static void main(String[] args) throws Exception {
//String SEARCH_FILTER = "(&(changenumber >= 550)(changenumber <= 580))";

String SEARCH_FILTER = "(objectclass = top)";

DirContext ctx_changelog = makeLDAPConnection("localhost","389","","cn=admin","*******");

SearchControls constraints_changelog = new SearchControls();

constraints_changelog.setSearchScope(SearchControls.OBJECT_SCOPE);
NamingEnumeration results_changelog = ctx_changelog.search("", SEARCH_FILTER, constraints_changelog);

while ( results_changelog != null && results_changelog.hasMore() )
{

SearchResult sr = (SearchResult) results_changelog.next();
String DN = sr.getName();

Attributes attrs = ctx_changelog.getAttributes(DN, RETURN_ATTRIBUTES_CHANGELOG);

Attribute attr = attrs.get("lastchangenumber");
if(attr!=null)
{
String lastchangenumber = (String)attr.get();
System.out.println(lastchangenumber);
}


//System.out.println("====================================");

}

}

2 comments:

Anonymous said...

Keystar have been successfully training on all aspects of IT since 1991. The Managing Director Bobbie Lee's desire is to give value for excellence using the experience she has gained over many years in a rapidly moving industry.

Regards.
http://www.keystar.co.uk

Anonymous said...

IS VERY GOOD..............................