-
Notifications
You must be signed in to change notification settings - Fork 485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added config interceptor for in-memory server #1000
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,3 +225,54 @@ To use it, create a bean similar to the following: | |
==== | ||
|
||
Also, `org.springframework.ldap.test.unboundid.LdapTestUtils` provide methods to programmatically work with an embedded LDAP server. | ||
|
||
''' | ||
|
||
Moreover, all the options enumerated ahead will support a variation that allows us to pass an `EmbeddedLdapServer.ConfigInterceptor` as a parameter. | ||
This functional interface extends `Consumer<InMemoryDirectoryServerConfig>` and will be invoked before initializing the server. | ||
|
||
This is how we can implement the `ConfigInterceptor` to make the in-memory server use a temporary file for logging the code: | ||
==== | ||
[source,java] | ||
---- | ||
class UpdateCodeLogDetails implements EmbeddedLdapServer.ConfigInterceptor { | ||
@Override | ||
public void accept(InMemoryDirectoryServerConfig config) { | ||
config.setCodeLogDetails(tempLogFile, true); | ||
} | ||
} | ||
---- | ||
==== | ||
|
||
Now, let's update our configuration to initialize our custom `ConfigInterceptor` and pass as a parameter to `TestContextSourceFactoryBean`: | ||
|
||
==== | ||
[source,xml] | ||
---- | ||
<bean id="updateCodeLogDetails" class="org.springframework.ldap.test.unboundid.TestContextSourceFactoryBeanTests.UpdateCodeLogDetails"> | ||
</bean> | ||
|
||
<bean id="contextSource" class="org.springframework.ldap.test.unboundid.TestContextSourceFactoryBean"> | ||
<property name="defaultPartitionSuffix" value="dc=jayway,dc=se" /> | ||
<property name="defaultPartitionName" value="jayway" /> | ||
<property name="principal" value="uid=admin,ou=system" /> | ||
<property name="password" value="secret" /> | ||
<property name="ldifFile" value="setup_data.ldif" /> | ||
<property name="port" value="18881" /> | ||
<property name="configInterceptor" ref="updateCodeLogDetails" /> | ||
</bean> | ||
---- | ||
==== | ||
|
||
Even if we choose to programmatically start the embedded LDAP server using `LdapTestUtils`, | ||
we can still supply the `ConfigInterceptor` as a lambda expression: | ||
|
||
==== | ||
|
||
[source,java] | ||
---- | ||
LdapTestUtils.startEmbeddedServer(1888, "dc=jayway,dc=se", "jayway", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to focus on using |
||
config -> config.setCodeLogDetails(tempLogFile, true)); | ||
---- | ||
==== | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
|
||
package org.springframework.ldap.test.unboundid; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import com.unboundid.ldap.listener.InMemoryDirectoryServer; | ||
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; | ||
import com.unboundid.ldap.listener.InMemoryListenerConfig; | ||
|
@@ -38,13 +40,18 @@ private EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) { | |
|
||
public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix, | ||
int port) throws Exception { | ||
return newEmbeddedServer(defaultPartitionName, defaultPartitionSuffix, port, ConfigInterceptor.doNothing()); | ||
} | ||
|
||
public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to not continue extending the list of parameters for this method. In fact, I think the existing Instead, let's create a Name suffix = LdapUtils.newLdapName("dc=jayway,dc=se");
EmbeddedLdapServer ldap = EmbeddedLdapServer
.withPartitionSuffix(suffix)
.config((config) -> config.setCodeLogDetails(...)).build(); Separating this concern allows the instance to be started up more strategically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In going this route, will you please in a separate commit add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback @jzheaux, I'll start working on these comments. Since we're add the start() method, what do you think about implementing AutoClosable as well (and call the already existing shotdown() ) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think this is a good idea, @etrandafir93. Will you please open a ticket to add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
int port, ConfigInterceptor configInterceptor) throws Exception { | ||
|
||
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(defaultPartitionSuffix); | ||
config.addAdditionalBindCredentials("uid=admin,ou=system", "secret"); | ||
|
||
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port)); | ||
|
||
config.setEnforceSingleStructuralObjectClass(false); | ||
config.setEnforceAttributeSyntaxCompliance(true); | ||
configInterceptor.accept(config); | ||
|
||
Entry entry = new Entry(new DN(defaultPartitionSuffix)); | ||
entry.addAttribute("objectClass", "top", "domain", "extensibleObject"); | ||
|
@@ -56,6 +63,16 @@ public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, | |
return new EmbeddedLdapServer(directoryServer); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface ConfigInterceptor extends Consumer<InMemoryDirectoryServerConfig> { | ||
etrandafir93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
static ConfigInterceptor doNothing() { | ||
return config -> { | ||
}; | ||
} | ||
|
||
} | ||
|
||
public void shutdown() throws Exception { | ||
this.directoryServer.shutDown(true); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
import org.springframework.ldap.core.support.DefaultDirObjectFactory; | ||
import org.springframework.ldap.core.support.LdapContextSource; | ||
import org.springframework.ldap.support.LdapUtils; | ||
import org.springframework.ldap.test.unboundid.EmbeddedLdapServer.ConfigInterceptor; | ||
|
||
/** | ||
* @author Mattias Hellborg Arthursson | ||
|
@@ -53,6 +54,8 @@ public class TestContextSourceFactoryBean extends AbstractFactoryBean<ContextSou | |
|
||
private ContextSource contextSource; | ||
|
||
private ConfigInterceptor configInterceptor = ConfigInterceptor.doNothing(); | ||
|
||
public void setAuthenticationSource(AuthenticationSource authenticationSource) { | ||
this.authenticationSource = authenticationSource; | ||
} | ||
|
@@ -97,8 +100,13 @@ public void setContextSource(ContextSource contextSource) { | |
this.contextSource = contextSource; | ||
} | ||
|
||
public void setConfigInterceptor(ConfigInterceptor configInterceptor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's please leave this out of the |
||
this.configInterceptor = configInterceptor; | ||
} | ||
|
||
protected ContextSource createInstance() throws Exception { | ||
LdapTestUtils.startEmbeddedServer(this.port, this.defaultPartitionSuffix, this.defaultPartitionName); | ||
LdapTestUtils.startEmbeddedServer(this.port, this.defaultPartitionSuffix, this.defaultPartitionName, | ||
this.configInterceptor); | ||
|
||
if (this.contextSource == null) { | ||
// If not explicitly configured, create a new instance. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> | ||
|
||
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> | ||
<property name="contextSource" ref="contextSource" /> | ||
</bean> | ||
|
||
<bean id="updateCodeLogDetails" class="org.springframework.ldap.test.unboundid.TestContextSourceFactoryBeanTests.UpdateCodeLogDetails"> | ||
</bean> | ||
|
||
<bean id="contextSource" class="org.springframework.ldap.test.unboundid.TestContextSourceFactoryBean"> | ||
<property name="defaultPartitionSuffix" value="dc=jayway,dc=se" /> | ||
<property name="defaultPartitionName" value="jayway" /> | ||
<property name="principal" value="uid=admin,ou=system" /> | ||
<property name="password" value="secret" /> | ||
<property name="ldifFile" value="setup_data.ldif" /> | ||
<property name="port" value="18881" /> | ||
<property name="configInterceptor" ref="updateCodeLogDetails" /> | ||
</bean> | ||
|
||
</beans> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this to earlier in the doc and demonstrate it through the
EmbeddedLdapServerFactoryBean
class?