有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

改进LDAP的Java代码

我找到了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服务器的无限连接并保持其打开吗


共 (1) 个答案

  1. # 1 楼答案

    Can you help me how I can improve this code?

    您没有关闭NamingEnumeration.finally块中关闭它以确保它被关闭。关闭finally块中的Context以确保它已关闭。遗憾的是,这些类没有实现AutoCloseable,所以不能使用try().

    Can I use connection pool for many search requests using one connection?

    对。JNDI LDAP提供程序可以为您做到这一点。只需将系统属性com.sun.jndi.ldap.connect.pool设置为true。有相关属性:请参阅JNDI LDAP提供程序文档

    And also is there any standard technique to improve LDAP search performance?

    确保您正在搜索的属性在LDAP服务器上建立了索引

    Can I open a infinite connection to the LDAP server and keep it open?

    这不是个好主意。最好使用连接池。见上文