Can someone explain to me why this piece of code accidentally consoles LDAP connections? I see that the number of established TCP / IP connections grows over time, and at some point this starts to cause problems. I tried to tinker with the properties of the com.sun.jndi.ldap.connect environment (turning on the pool, disconnecting it, etc.), but it did not seem to help.
This means that my lame part of the code has errors. How to do this generally better and make sure that I never "console" LDAP connections?
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class LdapUtil {
private String ldap_context = "ou=myunit,dc=com";
protected static String ldap_server = "ldap://ldapserver:389/";
private String ldap_prefix = "(&(uid=";
private String ldap_postfix = ")(objectclass=inetOrgPerson))";
private String[] ldap_attributes = {"uid","departmentNumber","cn","postOfficeBox","mail"};
private Properties ldap_properties;
private SearchControls ldap_searchcontrols;
private static String ldap_principal = "uid=bind_account,cn=users,ou=myunit,dc=com";
private static String ldap_credentials = "qwerty";
private List<String> getUserAttributes(final String userId) {
List<String> UserAttributes = new ArrayList<String>();
InitialDirContext ctx = null;
NamingEnumeration<SearchResult> resultsEnum = null;
NamingEnumeration<String> atrEnum = null;
try {
ctx = new InitialDirContext(this.ldap_properties);
String query = this.ldap_prefix+ userId+ this.ldap_postfix;
resultsEnum = ctx.search(this.ldap_context, query, this.ldap_searchcontrols);
while (resultsEnum.hasMore()) {
SearchResult sr = (SearchResult) resultsEnum.nextElement();
Attributes atr = sr.getAttributes();
atrEnum = atr.getIDs();
while (atrEnum.hasMore()) {
String nextid = atrEnum.nextElement();
String nextattribute = atr.get(nextid).toString();
UserAttributes.add(nextattribute);
}
}
} catch ( Exception eom ) {
System.out.println("LDAP exception");
} finally {
try {
if (atrEnum!=null)
atrEnum.close();
if (resultsEnum!=null)
resultsEnum.close();
if (ctx!=null)
ctx.close();
} catch (NamingException eo) {
} catch (NullPointerException eo) {
}
}
return UserAttributes;
}
private String getAttribute (final List<String> attributes,final String attribuutti) {
String result = null;
for (int i = 0; i < attributes.size(); i++) {
String attribute = attributes.get(i).toString();
if (attribute.startsWith(attribuutti)) {
int k = attribute.indexOf(" ");
result = attribute.substring(k+1,attribute.length());
}
}
return result;
}
public LdapUtil(String remoteuser) {
this.ldap_properties = new Properties();
this.ldap_properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
this.ldap_properties.put(Context.PROVIDER_URL, ldap_server) ;
this.ldap_properties.put(Context.REFERRAL, "follow" );
this.ldap_properties.put(Context.SECURITY_AUTHENTICATION, "simple");
this.ldap_properties.put(Context.SECURITY_PRINCIPAL,ldap_principal);
this.ldap_properties.put(Context.SECURITY_CREDENTIALS,ldap_credentials);
this.ldap_properties.put("com.sun.jndi.ldap.read.timeout", "10000");
this.ldap_properties.put("com.sun.jndi.ldap.connect.timeout", "10000");
this.ldap_properties.put("com.sun.jndi.ldap.connect.pool", "false");
this.ldap_searchcontrols = new SearchControls();
this.ldap_searchcontrols.setSearchScope(SearchControls.SUBTREE_SCOPE);
this.ldap_searchcontrols.setReturningAttributes(this.ldap_attributes);
List<String> attributes = null;
attributes = getUserAttributes(remoteuser);
}
}
source
share