Class MemoryBackend

java.lang.Object
org.forgerock.opendj.ldap.MemoryBackend
All Implemented Interfaces:
RequestHandler<RequestContext>

public final class MemoryBackend extends Object implements RequestHandler<RequestContext>
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 = newInternalConnectionFactory(newServerConnectionFactory(backend), null)
         .getConnection();
 
To create a simple LDAP server listening on 0.0.0.0:1389:
 MemoryBackend backend = new MemoryBackend();
 LDAPListener listener = new LDAPListener(1389, Connections
         .<LDAPClientContext> newServerConnectionFactory(backend));
 
  • Constructor Details

    • 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 Details

    • 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.
    • 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 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 named entry.
    • get

      public Entry get(String dn)
      Returns 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 named entry.
    • 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.
    • handleAdd

      public void handleAdd(RequestContext requestContext, AddRequest request, IntermediateResponseHandler intermediateResponseHandler, LdapResultHandler<Result> resultHandler)
      Description copied from interface: RequestHandler
      Invoked when an add request is received from a client.
      Specified by:
      handleAdd in interface RequestHandler<RequestContext>
      Parameters:
      requestContext - The request context.
      request - The add request.
      intermediateResponseHandler - The handler which should be used to send back any intermediate responses to the client.
      resultHandler - The handler which should be used to send back the result to the client.
    • handleBind

      public void handleBind(RequestContext requestContext, int version, BindRequest request, IntermediateResponseHandler intermediateResponseHandler, LdapResultHandler<BindResult> resultHandler)
      Description copied from interface: RequestHandler
      Invoked when a bind request is received from a client.
      Specified by:
      handleBind in interface RequestHandler<RequestContext>
      Parameters:
      requestContext - The request context.
      version - The protocol version included with the bind request.
      request - The bind request.
      intermediateResponseHandler - The handler which should be used to send back any intermediate responses to the client.
      resultHandler - The handler which should be used to send back the result to the client.
    • handleCompare

      public void handleCompare(RequestContext requestContext, CompareRequest request, IntermediateResponseHandler intermediateResponseHandler, LdapResultHandler<CompareResult> resultHandler)
      Description copied from interface: RequestHandler
      Invoked when a compare request is received from a client.
      Specified by:
      handleCompare in interface RequestHandler<RequestContext>
      Parameters:
      requestContext - The request context.
      request - The compare request.
      intermediateResponseHandler - The handler which should be used to send back any intermediate responses to the client.
      resultHandler - The handler which should be used to send back the result to the client.
    • handleDelete

      public void handleDelete(RequestContext requestContext, DeleteRequest request, IntermediateResponseHandler intermediateResponseHandler, LdapResultHandler<Result> resultHandler)
      Description copied from interface: RequestHandler
      Invoked when a delete request is received from a client.
      Specified by:
      handleDelete in interface RequestHandler<RequestContext>
      Parameters:
      requestContext - The request context.
      request - The delete request.
      intermediateResponseHandler - The handler which should be used to send back any intermediate responses to the client.
      resultHandler - The handler which should be used to send back the result to the client.
    • handleExtendedRequest

      public <R extends ExtendedResult> void handleExtendedRequest(RequestContext requestContext, ExtendedRequest<R> request, IntermediateResponseHandler intermediateResponseHandler, LdapResultHandler<R> resultHandler)
      Description copied from interface: RequestHandler
      Invoked when an extended request is received from a client.
      Specified by:
      handleExtendedRequest in interface RequestHandler<RequestContext>
      Type Parameters:
      R - The type of result returned by the extended request.
      Parameters:
      requestContext - The request context.
      request - The extended request.
      intermediateResponseHandler - The handler which should be used to send back any intermediate responses to the client.
      resultHandler - The handler which should be used to send back the result to the client.
    • handleModify

      public void handleModify(RequestContext requestContext, ModifyRequest request, IntermediateResponseHandler intermediateResponseHandler, LdapResultHandler<Result> resultHandler)
      Description copied from interface: RequestHandler
      Invoked when a modify request is received from a client.
      Specified by:
      handleModify in interface RequestHandler<RequestContext>
      Parameters:
      requestContext - The request context.
      request - The modify request.
      intermediateResponseHandler - The handler which should be used to send back any intermediate responses to the client.
      resultHandler - The handler which should be used to send back the result to the client.
    • handleModifyDN

      public void handleModifyDN(RequestContext requestContext, ModifyDNRequest request, IntermediateResponseHandler intermediateResponseHandler, LdapResultHandler<Result> resultHandler)
      Description copied from interface: RequestHandler
      Invoked when a modify DN request is received from a client.
      Specified by:
      handleModifyDN in interface RequestHandler<RequestContext>
      Parameters:
      requestContext - The request context.
      request - The modify DN request.
      intermediateResponseHandler - The handler which should be used to send back any intermediate responses to the client.
      resultHandler - The handler which should be used to send back the result to the client.
    • handleSearch

      public void handleSearch(RequestContext requestContext, SearchRequest request, IntermediateResponseHandler intermediateResponseHandler, SearchResultHandler entryHandler, LdapResultHandler<Result> resultHandler)
      Description copied from interface: RequestHandler
      Invoked when a search request is received from a client.
      Specified by:
      handleSearch in interface RequestHandler<RequestContext>
      Parameters:
      requestContext - The request context.
      request - The search request.
      intermediateResponseHandler - The handler which should be used to send back any intermediate responses to the client.
      entryHandler - The entry handler which should be used to send back the search entries results to the client.
      resultHandler - The handler which should be used to send back the result to the client.
    • 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.
    • load

      public MemoryBackend load(EntryReader reader, boolean overwrite) throws IOException
      Reads all of the entries from the provided entry reader and adds them to the content of this memory backend.
      Parameters:
      reader - The entry reader.
      overwrite - true if existing entries should be replaced, or false if an error should be returned when duplicate entries are encountered.
      Returns:
      This memory backend.
      Throws:
      IOException - If an unexpected IO error occurred while reading the entries, or if duplicate entries are detected and overwrite is false.
    • size

      public int size()
      Returns the number of entries contained in this memory backend.
      Returns:
      The number of entries contained in this memory backend.