简体   繁体   English

如何处理LDAP连接?

[英]How to handle LDAP connection?

I'm trying to implement the following: 我正在尝试实现以下内容:

I have an Importer class which registers so called Processor s, and then executes them by iterating over them and calling execute ( Processor is an interface and contains a method void execute() ). 我有一个Importer类,它注册所谓的Processor s,然后通过遍历它们并调用execute来执行它们( Processor是一个接口并包含一个方法void execute() )。

What those processors do is connect to an LDAP, and do certain tasks, eg retrieves a list of persons and stores them into a database, or reads certain privileges and maps them to groups. 这些处理器所做的是连接到LDAP,并执行某些任务,例如检索人员列表并将其存储到数据库中,或者读取某些特权并将它们映射到组。

This all works out quite well... except I just don't know how to handle the LDAP connection. 一切都很好...除了我只是不知道如何处理LDAP连接。

Here are some ideas: 以下是一些想法:

  1. initialize the connection in the constructor (and assign it to a field) and provide a dispose()` method, which closes the connection or 初始化构造函数中的连接(并将其分配给一个字段)并提供一个dispose()`方法,它关闭连接
  2. do not initialize the connection field in the constructor, but initialize and close it in the execute() method or 不初始化连接领域的构造函数,但初始化并在关闭它execute()方法
  3. create the connection in the execute method and pass it to all methods which are called 在execute方法中创建连接并将其传递给所有被调用的方法
  4. even uglier (create the connection in the Importer and pass it to all processors, then, after execution, close it) 甚至更丑(在Importer中创建连接并将其传递给所有处理器,然后在执行后关闭它)

Handle the connection to the LDAP server with a Strategy method that is a required parameter. 使用作为必需参数的Strategy方法处理与LDAP服务器的连接。 This de-couples and isolates the code. 这解耦并隔离了代码。 You should prefer the UnboundID LDAP SDK for this work - and the SDK supports the ability to create an in-memory directory server so there is no need to use "mock" or "fake" connections. 您应该更喜欢UnboundID LDAP SDK来完成这项工作 - 并且SDK支持创建内存中目录服务器的功能,因此不需要使用“模拟”或“假”连接。 Also, you can examine the blog post "LDAP: Programming Practices" . 另外,您可以查看博客文章“ LDAP:编程实践”

Build it somewhere else and inject it into the application. 在其他地方构建它并将其注入应用程序。

If you don't have JEE6 which provides dependency injection via CDDI then you can use Spring framework as a workaround. 如果您没有通过CDDI提供依赖注入的JEE6,那么您可以使用Spring框架作为解决方法。

It has two benefits. 它有两个好处。

  1. the lifecycle of the connection is outside the class that uses it 连接的生命周期在使用它的类之外
  2. the connection can be replaced with a mock or fake connection for testing. 可以使用模拟或伪连接替换连接以进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM