Scripting LDAP with Jython -- Load Java Objects from LDAP
A common way to store user preference data for applications is to store them in LDAP as a serialized Java Hastable object. The application then reads that back from ldap on startup using the authenticated userid as the key. Its a convenient way to store application settings. But what if you need to adjust or migrate settings from one place to the other? Here is an approach to reading that data out using Jython. Look for a future post about how to save it back using the context's bind method ...
# Jython LDAP - Retrieve Stored Object from LDAP Example
from javax.naming import *
from java.util import *
from javax.naming.directory import *
# Credentials to access LDAP
user = "cn=master"
passwd = "password"
# Setup LDAP Context Options
settings = Hashtable()
# Connect to LDAP Server
ctx = InitialDirContext(settings)
# load the java Hashtable out of the ldap server
prefs = ctx.lookup("cn=ccm_root,ou=Preferences,dc=Company,dc=com")
for pref in prefs.keys():
print "PREF: %s\n VALUE: %s " % (pref, prefs[pref])