改进LDAP的Java代码
我找到了LDAP连接的Java代码改进LDAP的Java代码,java,ldap,jndi,openldap,Java,Ldap,Jndi,Openldap,我找到了LDAP连接的Java代码 package javaapplication2; import java.util.Properties; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import ja
package javaapplication2;
import java.util.Properties;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class SearchLDAP {
public static void main(String[] args) {
// The search base is the level in the hierarchy
// that our search will start at. Here was use ""
// which indicates the very root of the directory.
String base = "";
// LDAP filters are sort of like a WHERE clause. It
// is constructed in a standard way based on LDAP
// standards. The search here is a simple one that
// says to return any entry with an objectclass value.
// Since all entries must contain an objectclass, all
// entries will be returned.
String filter = "(objectclass=*)";
// Here we set some connection properties for JNDI.
Properties env = new Properties();
// The Sun provider is the most widely used JNDI
// provider and comes with Java 1.3+
env.put(DirContext.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
// The provider URL is an LDAP URL that tells JNDI
// where it will need to connect to.
env.put(DirContext.PROVIDER_URL, "ldap://localhost:389");
try {
// Here we create a DirContext object using
// the environment we setup above. This
// object will be used to communicate with
// the server.
DirContext dc = new InitialDirContext(env);
// Above we mentioned the filter and base.
// Another important part of the search criteria
// is the scope. There are three scopes: base (this
// entry only), onelevel (the direct children of this
// entry), and subtree (this entry and all of its
// decendents in the tree). In JNDI, OBJECT_SCOPE
// indicates a base search.
SearchControls sc = new SearchControls();
sc.setSearchScope(SearchControls.OBJECT_SCOPE);
NamingEnumeration ne = null;
// Here we actually perform the search.
ne = dc.search(base, filter, sc);
// We cycle through the NamingEnumeration
// that is returned by the search.
while (ne.hasMore()) {
// Retrieve the result as a SearchResult
// and print it (not very pretty). There are
// methods for extracting the attributes and
// values without printing, as well.
SearchResult sr = (SearchResult) ne.next();
System.out.println(sr.toString() + "\n");
}
// Here we unbind from the LDAP server.
dc.close();
} catch (NamingException nex) {
// A number of exceptions are subclassed from
// NamingException. In a real application you'd
// probably want to handle many of them
// differently.
System.err.println("Error: " + nex.getMessage());
}
}
}
你能帮助我如何改进这个代码吗?我可以使用一个连接将连接池用于多个搜索请求吗?还有什么标准技术可以提高LDAP搜索性能吗?我可以打开到LDAP服务器的无限连接并保持其打开吗
你能帮助我如何改进这个代码吗
您没有关闭namingumeration。
在finally
块中关闭它以确保它被关闭。关闭finally块中的上下文
,以确保其已关闭。遗憾的是,这些类没有实现AutoCloseable
,因此不能使用try()
我可以使用一个连接将连接池用于多个搜索请求吗
对。JNDI LDAP提供程序可以为您做到这一点。只需将系统属性com.sun.jndi.ldap.connect.pool
设置为true
。有相关属性:请参阅JNDI LDAP提供程序文档
还有什么标准技术可以提高LDAP搜索性能吗
确保您正在搜索的属性在LDAP服务器上建立了索引
我可以打开到LDAP服务器的无限连接并保持其打开吗
这不是个好主意。最好使用连接池。见上文
你能帮助我如何改进这个代码吗
您没有关闭namingumeration。
在finally
块中关闭它以确保它被关闭。关闭finally块中的上下文
,以确保其已关闭。遗憾的是,这些类没有实现AutoCloseable
,因此不能使用try()
我可以使用一个连接将连接池用于多个搜索请求吗
对。JNDI LDAP提供程序可以为您做到这一点。只需将系统属性com.sun.jndi.ldap.connect.pool
设置为true
。有相关属性:请参阅JNDI LDAP提供程序文档
还有什么标准技术可以提高LDAP搜索性能吗
确保您正在搜索的属性在LDAP服务器上建立了索引
我可以打开到LDAP服务器的无限连接并保持其打开吗
这不是个好主意。最好使用连接池。请参见上文。针对此类问题的更好站点:尝试使用已经过广泛测试的代码和在spring ldap中使用的代码:针对此类问题的更好站点:尝试使用已经过广泛测试的代码和在spring ldap中使用的代码:@jeemster您的评论与我的答案无关。我建议你把它列为你自己的答案,这样它就可以被评论、投票等等。@jeemster你的评论与我的答案无关。我建议你把它列为你自己的答案,这样就可以对它进行评论、投票等等。