我找到了这个用于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 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服务器的无限连接并保持打开状态吗?
答案 0 :(得分:4)
你能帮助我改进这段代码吗?
您没有关闭NamingEnumeration.
在finally
块中关闭它以确保它已关闭。关闭finally块中的Context
以确保它已关闭。遗憾的是,这些类没有实现AutoCloseable
,因此您无法使用try().
我可以使用一个连接为许多搜索请求使用连接池吗?
是。 JNDI LDAP提供程序可以为您执行此操作。只需将系统属性com.sun.jndi.ldap.connect.pool
设置为true
即可。有关联的属性:请参阅JNDI LDAP提供程序文档。
还有提高LDAP搜索性能的标准技术吗?
确保您搜索的属性在LDAP服务器上编制索引。
我可以打开与LDAP服务器的无限连接并保持打开状态吗?
不是个好主意。最好使用连接池。见上文。