I’m trying to implement the following:
I have an Importer class which registers so called Processors, and then executes them by iterating over them and calling execute (Processor is an interface and contains a method void execute()).
What those processors do is connect to an LDAP, and do certain tasks, e.g. retrieves a list of persons and stores them into a database, or reads certain privileges and maps them to groups.
This all works out quite well… except I just don’t know how to handle the LDAP connection.
Here are some ideas:
- initialize the connection in the constructor (and assign it to a field) and provide a dispose()` method, which closes the connection or
- do not initialize the connection field in the constructor, but initialize and close it in the
execute()method or - create the connection in the execute method and pass it to all methods which are called
- even uglier (create the connection in the Importer and pass it to all processors, then, after execution, close it)
Handle the connection to the LDAP server with a Strategy method that is a required parameter. 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. Also, you can examine the blog post “LDAP: Programming Practices”.