LDAP Framework
This small opensource framework is intended to simplify accessing and processing data stored in a LDAP data source. It is built on Java javax.naming.ldap
package and allows you to write LDAP queries in SQL style.
Common syntax of queries:
CREATE IN ? SET param1=? param2=? ....
UPDATE ? SET param1=? param2=? ....
SEARCH attrName1 attrName2 ... IN ? WHERE filter scope [ORDER BY attrName]
DELETE ?
Take a look at the example code:
public void createOrganization(Organization organization) throws DAOException {
String parentDN = "topOrganizationTree";
try {
String query = "CREATE IN OU=? SET objectClass=fooOrganziation name=? orgName=? " +
"orgBusinessUnit=? orgType=? isActive=? st=? statusDate=?";
LDAPStatement s = new LDAPStatement(query, ldapDAO);
s.setString(1, organization.getOrgId() + "," + parentDN);
s.setString(2, organization.getOrgId());
s.setString(3, organization.getName());
s.setString(4, organization.getBusinessUnit());
s.setString(5, organization.getIdentityType().getSubType());
s.setBoolean(6, organization.isActive());
s.setString(7, organization.getState());
s.setDate(8, organization.getStatusDate());
String pk = s.create();
organization.setId(pk);
}
catch(Exception ex) {
throw new DAOException(ex);
}
}
public void updateOrganization(Organization organization) throws DAOException {
try {
String query = "UPDATE ? SET orgName=? orgBusinessUnit=? orgType=? " +
"isActive=? st=? statusDate=?";
LDAPStatement s = new LDAPStatement(query, ldapDAO);
s.setString(1, organization.getId());
s.setString(2, organization.getName());
s.setString(3, organization.getBusinessUnit());
s.setString(4, organization.getIdentityType());
s.setBoolean(5, organization.isActive());
s.setString(6, organization.getState());
s.setDate(7, organization.getStatusDate());
s.execute();
}
catch(Exception ex) {
throw new DAOException(ex);
}
}
public Organization readOrganization(String id) throws DAOException {
Organization organization = null;
try {
String parentDN = "topOrganizationTree";
String query = "SEARCH name orgName orgBusinessUnit orgType isActive " +
"st statusDate IN ? WHERE (distinguishedName=?) 2";
LDAPStatement s = new LDAPStatement(query, ldapDAO);
s.setString(1, parentDN);
s.setString(2, id);
LDAPResultSet resultSet = s.execute();
if(resultSet.next()) {
organization = load(resultSet);
}
}
catch(Exception ex) {
throw new DAOException(ex);
}
return organization;
}
public List findByName(String name, String identitySubType, String state, String businessUnit) throws DAOException {
if(isEmpty(name)) name = "*";
if(isEmpty(identitySubType)) identitySubType = "*";
if(isEmpty(state)) state = "*";
try {
String parentDN = "topOrganizationTree";
String query = "SEARCH name orgName orgBusinessUnit orgType isActive st statusDate " +
"IN ? WHERE (&(objectClass=fooOrganziation)(&(&(&(orgName=?)(orgType=?))(st=?))(orgBusinessUnit=?))) 2 " +
"ORDER BY orgName";
LDAPStatement s = new LDAPStatement(query, ldapDAO);
s.setString(1, parentDN);
s.setString(2, name);
s.setString(3, identitySubType);
s.setString(4, state);
s.setString(5, businessUnit);
LDAPResultSet resultSet = s.execute();
List results = new ArrayList();
while(resultSet.next()) {
results.add(load(resultSet));
}
return results;
}
catch(Exception ex) {
throw new DAOException(ex);
}
}
public void deleteOrganization(String organizationId) throws DAOException {
try {
String query = "DELETE ?";
LDAPStatement s = new LDAPStatement(query, ldapDAO);
s.setString(1, organizationId);
s.execute();
}
catch(Exception ex) {
throw new DAOException(ex);
}
}
private Organization load(LDAPResultSet resultSet) throws DAOException {
if(resultSet == null ) return null;
Organization org = new Organization();
org.setId(resultSet.getDN());
org.setOrgId(resultSet.getString(1));
org.setName(resultSet.getString(2));
org.setBusinessUnit(resultSet.getString(3));
org.setIdentitySubType(resultSet.getString(4));
org.setActive(resultSet.getBoolean(5));
org.setState(resultSet.getString(6));
org.setStatusDate(resultSet.getDate(7));
return org;
}
Download framework.