|
16 | 16 |
|
17 | 17 | package org.springframework.ldap.test.unboundid;
|
18 | 18 |
|
| 19 | +import java.util.function.Consumer; |
| 20 | + |
19 | 21 | import com.unboundid.ldap.listener.InMemoryDirectoryServer;
|
20 | 22 | import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
|
21 | 23 | import com.unboundid.ldap.listener.InMemoryListenerConfig;
|
@@ -46,25 +48,29 @@ public EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) {
|
46 | 48 | }
|
47 | 49 |
|
48 | 50 | /**
|
49 |
| - * Creates a new {@link EmbeddedLdapServerBuilder}. |
| 51 | + * Creates a new {@link Builder} with a given partition suffix. |
50 | 52 | *
|
51 | 53 | * @since 3.3
|
52 | 54 | */
|
53 |
| - public static EmbeddedLdapServerBuilder builder() { |
54 |
| - return new EmbeddedLdapServerBuilder(); |
| 55 | + public static Builder withPartitionSuffix(String partitionSuffix) { |
| 56 | + return new Builder(partitionSuffix); |
55 | 57 | }
|
56 | 58 |
|
57 | 59 | /**
|
58 | 60 | * Creates and starts new embedded LDAP server.
|
| 61 | + * @deprecated Use the builder pattern exposed via |
| 62 | + * {@link #withPartitionSuffix(String)} instead. |
59 | 63 | */
|
| 64 | + @Deprecated(since = "3.3") |
60 | 65 | public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix,
|
61 |
| - int port) throws Exception { |
62 |
| - InMemoryDirectoryServerConfig config = inMemoryDirectoryServerConfig(defaultPartitionSuffix, port); |
63 |
| - Entry entry = ldapEntry(defaultPartitionName, defaultPartitionSuffix); |
64 |
| - |
65 |
| - InMemoryDirectoryServer directoryServer = inMemoryDirectoryServer(config, entry); |
66 |
| - directoryServer.startListening(); |
67 |
| - return new EmbeddedLdapServer(directoryServer); |
| 66 | + int port) { |
| 67 | + EmbeddedLdapServer server = EmbeddedLdapServer.withPartitionSuffix(defaultPartitionSuffix) |
| 68 | + .partitionName(defaultPartitionName) |
| 69 | + .port(port) |
| 70 | + .build(); |
| 71 | + |
| 72 | + server.start(); |
| 73 | + return server; |
68 | 74 | }
|
69 | 75 |
|
70 | 76 | /**
|
@@ -101,28 +107,88 @@ public void shutdown() {
|
101 | 107 | this.directoryServer.shutDown(true);
|
102 | 108 | }
|
103 | 109 |
|
104 |
| - static InMemoryDirectoryServerConfig inMemoryDirectoryServerConfig(String partitionSuffix, int port) |
105 |
| - throws LDAPException { |
106 |
| - InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(partitionSuffix); |
107 |
| - config.addAdditionalBindCredentials("uid=admin,ou=system", "secret"); |
108 |
| - config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port)); |
109 |
| - config.setEnforceSingleStructuralObjectClass(false); |
110 |
| - config.setEnforceAttributeSyntaxCompliance(true); |
111 |
| - return config; |
112 |
| - } |
| 110 | + /** |
| 111 | + * Helper class for embedded Unboundid ldap server. |
| 112 | + * |
| 113 | + * @author Emanuel Trandafir |
| 114 | + * @since 3.3 |
| 115 | + */ |
| 116 | + public static final class Builder { |
113 | 117 |
|
114 |
| - static Entry ldapEntry(String defaultPartitionName, String defaultPartitionSuffix) throws LDAPException { |
115 |
| - Entry entry = new Entry(new DN(defaultPartitionSuffix)); |
116 |
| - entry.addAttribute("objectClass", "top", "domain", "extensibleObject"); |
117 |
| - entry.addAttribute("dc", defaultPartitionName); |
118 |
| - return entry; |
119 |
| - } |
| 118 | + private int port = 0; |
| 119 | + |
| 120 | + private Consumer<InMemoryDirectoryServerConfig> configurationCustomizer = (__) -> { |
| 121 | + }; |
| 122 | + |
| 123 | + private String partitionName = "jayway"; |
| 124 | + |
| 125 | + private final String partitionSuffix; |
| 126 | + |
| 127 | + private Builder(String partitionSuffix) { |
| 128 | + this.partitionSuffix = partitionSuffix; |
| 129 | + } |
| 130 | + |
| 131 | + public Builder port(int port) { |
| 132 | + this.port = port; |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + public Builder configurationCustomizer(Consumer<InMemoryDirectoryServerConfig> configurationCustomizer) { |
| 137 | + this.configurationCustomizer = configurationCustomizer; |
| 138 | + return this; |
| 139 | + } |
| 140 | + |
| 141 | + public Builder partitionName(String defaultPartitionName) { |
| 142 | + this.partitionName = defaultPartitionName; |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Builds and returns a {@link EmbeddedLdapServer}. |
| 148 | + * <p> |
| 149 | + * In order to start the server, you should call |
| 150 | + * {@link EmbeddedLdapServer#start()}. |
| 151 | + * @return a new {@link EmbeddedLdapServer}. |
| 152 | + */ |
| 153 | + public EmbeddedLdapServer build() { |
| 154 | + try { |
| 155 | + InMemoryDirectoryServerConfig config = inMemoryDirectoryServerConfig(this.partitionSuffix, this.port); |
| 156 | + this.configurationCustomizer.accept(config); |
| 157 | + |
| 158 | + Entry entry = ldapEntry(this.partitionName, this.partitionSuffix); |
| 159 | + InMemoryDirectoryServer directoryServer = inMemoryDirectoryServer(config, entry); |
| 160 | + return new EmbeddedLdapServer(directoryServer); |
| 161 | + } |
| 162 | + catch (Exception ex) { |
| 163 | + throw new RuntimeException(ex); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private static InMemoryDirectoryServerConfig inMemoryDirectoryServerConfig(String partitionSuffix, int port) |
| 168 | + throws LDAPException { |
| 169 | + InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(partitionSuffix); |
| 170 | + config.addAdditionalBindCredentials("uid=admin,ou=system", "secret"); |
| 171 | + config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port)); |
| 172 | + config.setEnforceSingleStructuralObjectClass(false); |
| 173 | + config.setEnforceAttributeSyntaxCompliance(true); |
| 174 | + return config; |
| 175 | + } |
| 176 | + |
| 177 | + private static Entry ldapEntry(String defaultPartitionName, String defaultPartitionSuffix) |
| 178 | + throws LDAPException { |
| 179 | + Entry entry = new Entry(new DN(defaultPartitionSuffix)); |
| 180 | + entry.addAttribute("objectClass", "top", "domain", "extensibleObject"); |
| 181 | + entry.addAttribute("dc", defaultPartitionName); |
| 182 | + return entry; |
| 183 | + } |
| 184 | + |
| 185 | + private static InMemoryDirectoryServer inMemoryDirectoryServer(InMemoryDirectoryServerConfig config, |
| 186 | + Entry entry) throws LDAPException { |
| 187 | + InMemoryDirectoryServer directoryServer = new InMemoryDirectoryServer(config); |
| 188 | + directoryServer.add(entry); |
| 189 | + return directoryServer; |
| 190 | + } |
120 | 191 |
|
121 |
| - static InMemoryDirectoryServer inMemoryDirectoryServer(InMemoryDirectoryServerConfig config, Entry entry) |
122 |
| - throws LDAPException { |
123 |
| - InMemoryDirectoryServer directoryServer = new InMemoryDirectoryServer(config); |
124 |
| - directoryServer.add(entry); |
125 |
| - return directoryServer; |
126 | 192 | }
|
127 | 193 |
|
128 | 194 | }
|
0 commit comments