Package org.forgerock.opendj.ldap
Class MemoryBackend
- java.lang.Object
-
- org.forgerock.opendj.ldap.MemoryBackend
-
public final class MemoryBackend extends Object
A simple in-memory back-end which can be used for testing. The back-end implementations supports the following:- add, bind (simple), compare, delete, modify, and search operations, but not modifyDN nor extended operations
- assertion, pre-, and post- read controls, subtree delete control, and permissive modify control
- thread safety - supports concurrent operations
- high performance
- secure password storage
- schema checking
- persistence
- indexing
Connections
to create simple servers as well as mock LDAP connections. For example, to create a mock LDAP connection factory:MemoryBackend backend = new MemoryBackend(); Connection connection = newInternalConnection(backend::handleRequest);
To create a simple LDAP server listening on 0.0.0.0:1389:MemoryBackend backend = new MemoryBackend(); LdapServer server = new LdapServer(1389, session -> backend::handleRequest);
-
-
Constructor Summary
Constructors Constructor Description MemoryBackend()
Creates a new empty memory backend which will use the default schema.MemoryBackend(Schema schema)
Creates a new empty memory backend which will use the provided schema.MemoryBackend(Schema schema, EntryReader reader)
Creates a new memory backend which will use the provided schema, and will contain the entries read from the provided entry reader.MemoryBackend(EntryReader reader)
Creates a new memory backend which will use the default schema, and will contain the entries read from the provided entry reader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MemoryBackend
addVirtualAttributes(AttributeDescription attributeDescription, BiFunction<Dn,MemoryBackend,Attribute> virtualAttributeFactory)
Adds a virtual attribute to the backend.MemoryBackend
clear()
Clears the contents of this memory backend so that it does not contain any entries.boolean
contains(String dn)
Returnstrue
if the named entry exists in this memory backend.boolean
contains(Dn dn)
Returnstrue
if the named entry exists in this memory backend.MemoryBackend
enableVirtualAttributes(boolean enabled)
Indicates whether search responses should include thehasSubordinates
andnumSubordinates
virtual attributes if requested.Entry
get(String dn)
Returns a deep copy of the named entry contained in this memory backend, ornull
if it does not exist.Entry
get(Dn dn)
Returns a deep copy of the named entry contained in this memory backend, ornull
if it does not exist.Collection<Entry>
getAll()
Returns a collection containing all of the entries in this memory backend.Schema
getSchema()
Returns the schema used by this backend.Flowable<Response>
handleRequest(int msgId, Request request)
Returns aFlowable
which, when subscribed, will apply the providedrequest
to this memory backend.Flowable<Response>
handleRequest(Request request)
Returns aFlowable
which, when subscribed, will apply the providedrequest
to this memory backend.boolean
hasSubordinates(String dn)
Returnstrue
if the named entry exists and has at least one child entry.boolean
hasSubordinates(Dn dn)
Returnstrue
if the named entry exists and has at least one child entry.boolean
isEmpty()
Returnstrue
if this memory backend does not contain any entries.MemoryBackend
loadNoOverwrite(EntryReader reader)
Reads all of the entries from the provided entry reader and adds them to the content of this memory backend.MemoryBackend
loadOverwrite(EntryReader reader)
Reads all of the entries from the provided entry reader and adds them to the content of this memory backend replacing any existing entries having the same name.int
numSubordinates(String dn)
Returns the number of entries which are immediately subordinate to the named entry, or0
if the named entry does not exist.int
numSubordinates(Dn dn)
Returns the number of entries which are immediately subordinate to the named entry, or0
if the named entry does not exist.int
size()
Returns the number of entries contained in this memory backend.
-
-
-
Constructor Detail
-
MemoryBackend
public MemoryBackend()
Creates a new empty memory backend which will use the default schema.
-
MemoryBackend
public MemoryBackend(EntryReader reader) throws IOException
Creates a new memory backend which will use the default schema, and will contain the entries read from the provided entry reader.- Parameters:
reader
- The entry reader.- Throws:
IOException
- If an unexpected IO error occurred while reading the entries, or if duplicate entries are detected.
-
MemoryBackend
public MemoryBackend(Schema schema)
Creates a new empty memory backend which will use the provided schema.- Parameters:
schema
- The schema to use for decoding filters, etc.
-
MemoryBackend
public MemoryBackend(Schema schema, EntryReader reader) throws IOException
Creates a new memory backend which will use the provided schema, and will contain the entries read from the provided entry reader.- Parameters:
schema
- The schema to use for decoding filters, etc.reader
- The entry reader.- Throws:
IOException
- If an unexpected IO error occurred while reading the entries, or if duplicate entries are detected.
-
-
Method Detail
-
enableVirtualAttributes
public MemoryBackend enableVirtualAttributes(boolean enabled)
Indicates whether search responses should include thehasSubordinates
andnumSubordinates
virtual attributes if requested.- Parameters:
enabled
-true
if the virtual attributes should be included.- Returns:
- This memory backend.
-
addVirtualAttributes
public MemoryBackend addVirtualAttributes(AttributeDescription attributeDescription, BiFunction<Dn,MemoryBackend,Attribute> virtualAttributeFactory)
Adds a virtual attribute to the backend. The memory backend will return the entries with the virtual attribute if the search filter contains this virtual attribute.- Parameters:
attributeDescription
- The attribute description of the virtual attribute.virtualAttributeFactory
- The factory to create the virtual attribute.- Returns:
- The memory backend.
-
clear
public MemoryBackend clear()
Clears the contents of this memory backend so that it does not contain any entries.- Returns:
- This memory backend.
-
contains
public boolean contains(Dn dn)
Returnstrue
if the named entry exists in this memory backend.- Parameters:
dn
- The name of the entry.- Returns:
true
if the named entry exists in this memory backend.
-
contains
public boolean contains(String dn)
Returnstrue
if the named entry exists in this memory backend.- Parameters:
dn
- The name of the entry.- Returns:
true
if the named entry exists in this memory backend.
-
get
public Entry get(Dn dn)
Returns a deep copy of the named entry contained in this memory backend, ornull
if it does not exist.- Parameters:
dn
- The name of the entry to be returned.- Returns:
- The deep copy of the named entry, or
null
if it does not exist.
-
get
public Entry get(String dn)
Returns a deep copy of the named entry contained in this memory backend, ornull
if it does not exist.- Parameters:
dn
- The name of the entry to be returned.- Returns:
- The deep copy of the named entry, or
null
if it does not exist.
-
getAll
public Collection<Entry> getAll()
Returns a collection containing all of the entries in this memory backend. The returned collection is backed by this memory backend, so changes to the collection are reflected in this memory backend and vice-versa. The returned collection supports entry removal, iteration, and is thread safe, but it does not support addition of new entries.- Returns:
- A collection containing all of the entries in this memory backend.
-
hasSubordinates
public boolean hasSubordinates(String dn)
Returnstrue
if the named entry exists and has at least one child entry.- Parameters:
dn
- The name of the entry.- Returns:
true
if the named entry exists and has at least one child entry.
-
hasSubordinates
public boolean hasSubordinates(Dn dn)
Returnstrue
if the named entry exists and has at least one child entry.- Parameters:
dn
- The name of the entry.- Returns:
true
if the named entry exists and has at least one child entry.
-
numSubordinates
public int numSubordinates(String dn)
Returns the number of entries which are immediately subordinate to the named entry, or0
if the named entry does not exist.- Parameters:
dn
- The name of the entry.- Returns:
- The number of entries which are immediately subordinate to the named entry.
-
numSubordinates
public int numSubordinates(Dn dn)
Returns the number of entries which are immediately subordinate to the named entry, or0
if the named entry does not exist.- Parameters:
dn
- The name of the entry.- Returns:
- The number of entries which are immediately subordinate to the named entry.
-
handleRequest
public Flowable<Response> handleRequest(int msgId, Request request)
Returns aFlowable
which, when subscribed, will apply the providedrequest
to this memory backend. This method is provided to facilitate creation of simple LDAP servers usingLdapServer
as well as in-memory connections for unit testing usingConnections.newInternalConnection(io.reactivex.rxjava3.functions.BiFunction)
.- Parameters:
msgId
- The request's message ID.request
- The request.- Returns:
- A
Flowable
which, when subscribed, will apply the providedrequest
to this memory backend.
-
handleRequest
public Flowable<Response> handleRequest(Request request)
Returns aFlowable
which, when subscribed, will apply the providedrequest
to this memory backend.- Parameters:
request
- The request.- Returns:
- A
Flowable
which, when subscribed, will apply the providedrequest
to this memory backend.
-
isEmpty
public boolean isEmpty()
Returnstrue
if this memory backend does not contain any entries.- Returns:
true
if this memory backend does not contain any entries.
-
loadNoOverwrite
public MemoryBackend loadNoOverwrite(EntryReader reader) throws IOException
Reads all of the entries from the provided entry reader and adds them to the content of this memory backend. An error will be thrown if any duplicate entries are detected.- Parameters:
reader
- The entry reader.- Returns:
- This memory backend.
- Throws:
IOException
- If an unexpected IO error occurred while reading the entries, or if duplicate entries are detected.
-
loadOverwrite
public MemoryBackend loadOverwrite(EntryReader reader) throws IOException
Reads all of the entries from the provided entry reader and adds them to the content of this memory backend replacing any existing entries having the same name.- Parameters:
reader
- The entry reader.- Returns:
- This memory backend.
- Throws:
IOException
- If an unexpected IO error occurred while reading the entries.
-
size
public int size()
Returns the number of entries contained in this memory backend.- Returns:
- The number of entries contained in this memory backend.
-
getSchema
public Schema getSchema()
Returns the schema used by this backend.- Returns:
- The schema used by this backend.
-
-