Class 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
    It does not support the following:
    • high performance
    • secure password storage
    • schema checking
    • persistence
    • indexing
    This class can be used in conjunction with the factories defined in 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.
    • 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 the hasSubordinates and numSubordinates 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.
      • passwordGenerator

        public void passwordGenerator​(Supplier<byte[]> passwordGenerator)
        Adds a password generator to the backend.
        Parameters:
        passwordGenerator - The password generator of the 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)
        Returns true 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)
        Returns true 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, or null 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, or null 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)
        Returns true 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)
        Returns true 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, or 0 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, or 0 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 io.reactivex.rxjava3.core.Flowable<Response> handleRequest​(int msgId,
                                                                          Request request)
        Returns a Flowable which, when subscribed, will apply the provided request to this memory backend. This method is provided to facilitate creation of simple LDAP servers using LdapServer as well as in-memory connections for unit testing using Connections.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 provided request to this memory backend.
      • handleRequest

        public io.reactivex.rxjava3.core.Flowable<Response> handleRequest​(Request request)
        Returns a Flowable which, when subscribed, will apply the provided request to this memory backend.
        Parameters:
        request - The request.
        Returns:
        A Flowable which, when subscribed, will apply the provided request to this memory backend.
      • isEmpty

        public boolean isEmpty()
        Returns true 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.