Skip to content

Commit

Permalink
Add start/stop methods
Browse files Browse the repository at this point in the history
Closes spring-projects#1025

Signed-off-by: emanueltrandafir1993 <[email protected]>
  • Loading branch information
etrandafir93 committed Feb 20, 2025
1 parent cc1a15f commit 41b6afc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@
import com.unboundid.ldap.listener.InMemoryListenerConfig;
import com.unboundid.ldap.sdk.DN;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPException;

/**
* Helper class for embedded Unboundid ldap server.
*
* @author Eddu Melendez
* @since 2.1.0
*/
public final class EmbeddedLdapServer {
public final class EmbeddedLdapServer implements AutoCloseable {

private InMemoryDirectoryServer directoryServer;
private final InMemoryDirectoryServer directoryServer;

private EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) {
this.directoryServer = directoryServer;
}

/**
* Creates and starts new embedded LDAP server.
*/
public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix,
int port) throws Exception {
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(defaultPartitionSuffix);
Expand All @@ -56,7 +60,36 @@ public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName,
return new EmbeddedLdapServer(directoryServer);
}

public void shutdown() throws Exception {
/**
* Starts the embedded LDAP server.
*
* @since 3.3
*/
public void start() {
try {
this.directoryServer.startListening();
}
catch (LDAPException ex) {
throw new RuntimeException(ex);
}
}

/**
* Closes the embedded LDAP server and releases resource, closing existing
* connections.
*
* @since 3.3
*/
@Override
public void close() {
this.directoryServer.shutDown(true);
}

/**
* @deprecated Use {@link #close()} instead.
*/
@Deprecated(since = "3.3")
public void shutdown() {
this.directoryServer.shutDown(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected EmbeddedLdapServer createInstance() throws Exception {

@Override
protected void destroyInstance(EmbeddedLdapServer instance) throws Exception {
instance.shutdown();
instance.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static void startEmbeddedServer(int port, String defaultPartitionSuffix,
*/
public static void shutdownEmbeddedServer() throws Exception {
if (embeddedServer != null) {
embeddedServer.shutdown();
embeddedServer.close();
embeddedServer = null;
}
}
Expand Down

0 comments on commit 41b6afc

Please sign in to comment.