Listing 1 ldapParam.java
package at.bhitcon.ldap;
public final class ldapParam implements java.io.Serializable
{
public ldapParam()
{
LDAPUser = null;
LDAPPwd = null;
LDAPHost = "localhost";
LDAPPort = 389;
LDAPDN = "o=myorg,c=US";
LDAPAttrNames = new String[0];
}
public ldapParam(String LDAPUser, String LDAPPwd, String LDAPHost, String
LDAPDN, String LDAPAttrNames[])
{
this.LDAPUser = "";
this.LDAPPwd = "";
this.LDAPHost = "localhost";
LDAPPort = 389;
this.LDAPDN = "";
this.LDAPAttrNames = new String[0];
this.LDAPUser = LDAPUser;
this.LDAPPwd = LDAPPwd;
this.LDAPHost = LDAPHost;
LDAPPort = LDAPPort;
this.LDAPDN = LDAPDN;
this.LDAPAttrNames = LDAPAttrNames;
}
public String LDAPUser;
public String LDAPPwd;
public String LDAPHost;
public int LDAPPort;
public String LDAPDN;
public String LDAPAttrNames[];
}

Listing 2 ldapAttrs.java
package at.bhitcon.ldap;
public final class ldapAttrs implements java.io.Serializable{
public String attrName;
public String[] attrValues;
/** Creates new ldapAttrs */
public ldapAttrs() {
}
/** Creates new ldapAttrs */
public ldapAttrs(String attrName, String attrValues[]) {
this.attrName = attrName;
this.attrValues = attrValues;
}
}

Listing 3 ldapReturn.java
package at.ldap;
final public class ldapReturn
extends java.lang.Object {
public ldapReturn() {
}
public boolean ldapsuccess = true;
public String errorString = "";
public String LDAPValue [] = {};
public String LDAPAttrib [] = {};
}

Listing 4 The Remote Interface (LDAPCallerBean)
public interface LDAPCallerBean extends EJBObject {
public ldapReturn ldapAuthenticate(ldapParam ldapparam)
throws RemoteException;
public ldapReturn ldapSearch(ldapParam ldapparam)
throws RemoteException;
public ldapReturn ldapChange(ldapParam ldapparam, ldapAttrs[]
ldapattrs) throws RemoteException;
public ldapReturn ldapAdd(ldapParam ldapparam, ldapAttrs[]
ldapattrs) throws RemoteException;
}

Listing 5 ldapAuthenticate.java
public ldapReturn ldapAuthenticate(ldapParam ldapparam) {
String empty[] = new String[0];
lReturn.LDAPValue = empty;
lReturn.LDAPAttrib = empty;
Hashtable env = new Hashtable(5);
/* Specify the initial context
implementation to use.*/
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
/* Specify host and port to use for
directory service */
env.put(Context.PROVIDER_URL, "ldap://" +
ldapparam.LDAPHost+ ":" +
ldapparam.LDAPPort);
/* specify authentication information */
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, ldapparam.LDAPUser);
env.put(Context.SECURITY_CREDENTIALS, ldapparam.LDAPPwd);
try {
/* get a handle to an Initial DirContext */
ctx = new InitialDirContext(env);
lReturn.ldapsuccess = true;
lReturn.errorString = "";
} catch (javax.naming.AuthenticationException e) {
System.err.println("Authentication failed ");
e.printStackTrace();
lReturn.ldapsuccess = false;
lReturn.errorString = errAuthFailed;
} catch (NamingException e) {
System.err.println("LDAP server not found failed.");
e.printStackTrace();
lReturn.ldapsuccess = false;
lReturn.errorString = errConnFailed;
}
return lReturn;
}

Listing 6 ldapSearch.java
public ldapReturn ldapSearch(ldapParam ldapparam) {
Vector vAttr = new Vector();
Vector vVal = new Vector();
// If connection fails stop
ldapAuthenticate(ldapparam);
if(lReturn.ldapsuccess == false)
return lReturn;
try {
/* specify search constraints to search subtree */
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
//constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
/*Define here the attributes you want to retrieve */
//String[] attrIDs = {"sn"};
//constraints.setReturningAttributes(attrIDs);
/* search for all entries with given parameter */
NamingEnumeration results
= ctx.search(ldapparam.LDAPDN, "(&(objectclass=*))",
constraints);
/* for each entry print out name + all attrs and values */
while (results != null && results.hasMore()) {
SearchResult si = (SearchResult)results.next();
/* print its name */
System.out.println("name: " + si.getName());
Attributes attrs = si.getAttributes();
if (attrs == null) {
System.out.println("No attributes");
} else {
/* print each attribute */
for (NamingEnumeration ae = attrs.getAll();
ae.hasMoreElements();) {
Attribute attr = (Attribute)ae.next();
String attrId = attr.getID();
/* print each value */
for (Enumeration vals = attr.getAll();
vals.hasMoreElements();) {
Object elem = vals.nextElement();
if (elem instanceof String) {
vAttr.add(attrId);
vVal.add((String) elem);
System.out.println(attrId + ": " + elem);
}
}
}
}
System.out.println();
}
lReturn.LDAPValue = (String[])vVal.toArray(new
String[vVal.size()]);
lReturn.LDAPAttrib = (String[])vAttr.toArray(new
String[vAttr.size()]);
lReturn.ldapsuccess = true;
} catch (NameNotFoundException e) {
System.err.println("Search failed.");
lReturn.errorString = errSearchFailed + e.getMessage();
lReturn.ldapsuccess = false;
} catch (NamingException e) {
System.err.println("Search failed.");
lReturn.errorString = errSearchFailed + e.getMessage();
lReturn.ldapsuccess = false;
}
return lReturn;
}

Additional Source Code For This article (~zip file)