2014-01-09 78 views
1

我發現這個用於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

一個更好的網站這種問題:http://codereview.stackexchange.com/ –

+0

嘗試使用已被廣泛測試的代碼,在春季ldap中使用:http://projects.spring.io/spring -ldap/ –

回答

3

你能幫我一下,我可以改進這個代碼嗎?

您未關閉NamingEnumeration.將其關閉在finally區塊中以確保其關閉。在finally塊中關閉Context以確保其關閉。可悲的是這些類不實現AutoCloseable,所以你不能用try().

我可以使用連接池使用一個連接許多搜索請求?

是的。 JNDI LDAP提供者可以爲你做到這一點。只需將系統屬性com.sun.jndi.ldap.connect.pool設置爲true即可。有相關的屬性:請參閱JNDI LDAP提供程序文檔。

還有沒有任何標準技術來提高LDAP搜索性能?

確保您正在搜索的屬性在LDAP服務器上編入索引。

我可以打開與LDAP服務器的無限連接並保持打開狀態嗎?

不是一個好主意。最好使用連接池。往上看。

+0

@jeemster您的評論與我的回答無關。我建議你把它列爲你自己的答案,所以可以對它進行評論,投票等。 – EJP