新增缺失的過載 API 方法

本節介紹如何新增您自己的過載 API 方法以實現新功能。

實現自定義搜尋方法

LdapTemplate 包含了 DirContext 中最常見操作的幾個過載版本。然而,我們並未為每個方法簽名都提供替代方案,主要原因是方法數量太多。不過,我們提供了一種方法,讓您可以呼叫任何您想要的 DirContext 方法,並仍然獲得 LdapTemplate 提供的優勢。

假設您想呼叫以下 DirContext 方法

NamingEnumeration search(Name name, String filterExpr, Object[] filterArgs, SearchControls ctls)

LdapTemplate 中沒有相應的過載方法。解決此問題的方法是使用自定義的 SearchExecutor 實現,如下所示

public interface SearchExecutor {
   public NamingEnumeration executeSearch(DirContext ctx) throws NamingException;
}

在您的自定義執行器中,您可以訪問一個 DirContext 物件,您可以使用它來呼叫您想要的方法。然後您可以提供一個處理程式,負責對映屬性並收集結果。例如,您可以使用 CollectingNameClassPairCallbackHandler 的一個可用實現,它將對映結果收集到一個內部列表中。為了實際執行搜尋,您需要呼叫 LdapTemplate 中以執行器和處理程式作為引數的 search 方法。最後,您需要返回您的處理程式收集到的任何內容。以下示例展示瞭如何完成所有這些操作

示例 1. 使用 SearchExecutorAttributesMapper 的自定義搜尋方法
public class PersonRepoImpl implements PersonRepo {
   ...
   public List search(final Name base, final String filter, final String[] params,
         final SearchControls ctls) {
      SearchExecutor executor = new SearchExecutor() {
         public NamingEnumeration executeSearch(DirContext ctx) {
            return ctx.search(base, filter, params, ctls);
         }
      };

      CollectingNameClassPairCallbackHandler handler =
         new AttributesMapperCallbackHandler(new PersonAttributesMapper());

      ldapTemplate.search(executor, handler);
      return handler.getList();
   }
}

如果您更喜歡 ContextMapper 而不是 AttributesMapper,以下示例展示了它的樣子

示例 2. 使用 SearchExecutorContextMapper 的自定義搜尋方法
public class PersonRepoImpl implements PersonRepo {
   ...
   public List search(final Name base, final String filter, final String[] params,
         final SearchControls ctls) {
      SearchExecutor executor = new SearchExecutor() {
         public NamingEnumeration executeSearch(DirContext ctx) {
            return ctx.search(base, filter, params, ctls);
         }
      };

      CollectingNameClassPairCallbackHandler handler =
         new ContextMapperCallbackHandler(new PersonContextMapper());

      ldapTemplate.search(executor, handler);
      return handler.getList();
   }
}
當您使用 ContextMapperCallbackHandler 時,您必須確保已在您的 SearchControls 例項上呼叫了 setReturningObjFlag(true)

實現其他自定義上下文方法

與自定義 search 方法相同,您可以透過使用 ContextExecutor 來呼叫 DirContext 中的任何方法,如下所示

public interface ContextExecutor {
   public Object executeWithContext(DirContext ctx) throws NamingException;
}

在實現自定義 ContextExecutor 時,您可以選擇使用 executeReadOnly()executeReadWrite() 方法。假設您想呼叫以下方法

Object lookupLink(Name name)

該方法在 DirContext 中可用,但在 LdapTemplate 中沒有匹配的方法。它是一個查詢方法,因此它應該是隻讀的。我們可以這樣實現它

示例 3. 使用 ContextExecutor 的自定義 DirContext 方法
public class PersonRepoImpl implements PersonRepo {
   ...
   public Object lookupLink(final Name name) {
      ContextExecutor executor = new ContextExecutor() {
         public Object executeWithContext(DirContext ctx) {
            return ctx.lookupLink(name);
         }
      };

      return ldapTemplate.executeReadOnly(executor);
   }
}

以同樣的方式,您可以使用 executeReadWrite() 方法執行讀寫操作。

© . This site is unofficial and not affiliated with VMware.