From c03fb701ce8d27d5c22fa50ba3dc3da211dfca3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Tue, 13 Aug 2019 20:36:15 -0500 Subject: [PATCH] Improve logic to pick embedded server --- .../ldap/LdapServerBeanDefinitionParser.java | 92 ++++--------------- .../LdapServerBeanDefinitionParserTest.java | 8 -- 2 files changed, 20 insertions(+), 80 deletions(-) diff --git a/config/src/main/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParser.java b/config/src/main/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParser.java index f1f1342714..a84ad1eb22 100644 --- a/config/src/main/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParser.java +++ b/config/src/main/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParser.java @@ -17,9 +17,6 @@ package org.springframework.security.config.ldap; import java.io.IOException; import java.net.ServerSocket; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -78,16 +75,6 @@ public class LdapServerBeanDefinitionParser implements BeanDefinitionParser { private static final String APACHEDS_CONTAINER_CLASSNAME = "org.springframework.security.ldap.server.ApacheDSContainer"; private static final String UNBOUNDID_CONTAINER_CLASSNAME = "org.springframework.security.ldap.server.UnboundIdContainer"; - private Map embeddedServers; - - public LdapServerBeanDefinitionParser() { - Map embeddedLdapServers = new HashMap<>(); - embeddedLdapServers.put("apacheds", new EmbeddedLdapServer(BeanIds.EMBEDDED_APACHE_DS, APACHEDS_CLASSNAME, APACHEDS_CONTAINER_CLASSNAME)); - embeddedLdapServers.put("unboundid", new EmbeddedLdapServer(BeanIds.EMBEDDED_UNBOUNDID, UNBOUNID_CLASSNAME, UNBOUNDID_CONTAINER_CLASSNAME)); - - this.embeddedServers = Collections.unmodifiableMap(embeddedLdapServers); - } - public BeanDefinition parse(Element elt, ParserContext parserContext) { String url = elt.getAttribute(ATT_URL); @@ -189,55 +176,42 @@ public class LdapServerBeanDefinitionParser implements BeanDefinitionParser { element); } - EmbeddedLdapServer embeddedLdapServer = resolveEmbeddedLdapServer(mode); - if (embeddedLdapServer != null) { - parserContext.getRegistry().registerBeanDefinition(embeddedLdapServer.getBeanId(), - ldapContainer); + String beanId = resolveBeanId(mode); + if (beanId != null) { + parserContext.getRegistry().registerBeanDefinition(beanId, ldapContainer); } return (RootBeanDefinition) contextSource.getBeanDefinition(); } private RootBeanDefinition getRootBeanDefinition(String mode) { - if (StringUtils.hasLength(mode)) { - if (isEmbeddedServerEnabled(mode)) { - return new RootBeanDefinition(this.embeddedServers.get(mode).getContainerClass(), null, null); - } + if (isApacheDsEnabled(mode)) { + return new RootBeanDefinition(APACHEDS_CONTAINER_CLASSNAME, null, null); } - else { - for (Map.Entry entry : this.embeddedServers.entrySet()) { - EmbeddedLdapServer ldapServer = entry.getValue(); - if (ClassUtils.isPresent(ldapServer.getClassName(), getClass().getClassLoader())) { - return new RootBeanDefinition(ldapServer.getContainerClass(), null, null); - } - } + else if (isUnboundidEnabled(mode)) { + return new RootBeanDefinition(UNBOUNDID_CONTAINER_CLASSNAME, null, null); } throw new IllegalStateException("Embedded LDAP server is not provided"); } - private boolean isEmbeddedServerEnabled(String mode) { - EmbeddedLdapServer server = resolveEmbeddedLdapServer(mode); - return server != null; - } - - private EmbeddedLdapServer resolveEmbeddedLdapServer(String mode) { - if (StringUtils.hasLength(mode)) { - if (this.embeddedServers.containsKey(mode) || - ClassUtils.isPresent(this.embeddedServers.get(mode).getClassName(), getClass().getClassLoader())) { - return this.embeddedServers.get(mode); - } + private String resolveBeanId(String mode) { + if (isApacheDsEnabled(mode)) { + return BeanIds.EMBEDDED_APACHE_DS; } - else { - for (Map.Entry entry : this.embeddedServers.entrySet()) { - EmbeddedLdapServer ldapServer = entry.getValue(); - if (ClassUtils.isPresent(ldapServer.getClassName(), getClass().getClassLoader())) { - return ldapServer; - } - } + else if (isUnboundidEnabled(mode)) { + return BeanIds.EMBEDDED_UNBOUNDID; } return null; } + private boolean isApacheDsEnabled(String mode) { + return "apacheds".equals(mode) || ClassUtils.isPresent(APACHEDS_CLASSNAME, getClass().getClassLoader()); + } + + private boolean isUnboundidEnabled(String mode) { + return "unboundid".equals(mode) || ClassUtils.isPresent(UNBOUNID_CLASSNAME, getClass().getClassLoader()); + } + private String getDefaultPort() { ServerSocket serverSocket = null; try { @@ -265,30 +239,4 @@ public class LdapServerBeanDefinitionParser implements BeanDefinitionParser { } } - private class EmbeddedLdapServer { - - private String beanId; - - private String className; - - private String containerClass; - - public EmbeddedLdapServer(String beanId, String className, String containerClass) { - this.beanId = beanId; - this.className = className; - this.containerClass = containerClass; - } - - public String getBeanId() { - return this.beanId; - } - - public String getClassName() { - return this.className; - } - - public String getContainerClass() { - return this.containerClass; - } - } } diff --git a/config/src/test/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParserTest.java b/config/src/test/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParserTest.java index 9c5d3eefba..54e655e7f6 100644 --- a/config/src/test/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParserTest.java +++ b/config/src/test/java/org/springframework/security/config/ldap/LdapServerBeanDefinitionParserTest.java @@ -22,7 +22,6 @@ import org.junit.Test; import org.springframework.security.config.BeanIds; import org.springframework.security.config.util.InMemoryXmlApplicationContext; import org.springframework.security.ldap.server.ApacheDSContainer; -import org.springframework.security.ldap.server.UnboundIdContainer; import static org.assertj.core.api.Assertions.assertThat; @@ -57,11 +56,4 @@ public class LdapServerBeanDefinitionParserTest { assertThat(beanNames[0]).isEqualTo(BeanIds.EMBEDDED_APACHE_DS); } - @Test - public void unboundidIsStartedWhenModeIsSet() { - this.context = new InMemoryXmlApplicationContext("", "5.2", null); - String[] beanNames = this.context.getBeanNamesForType(UnboundIdContainer.class); - assertThat(beanNames).hasSize(1); - assertThat(beanNames[0]).isEqualTo(BeanIds.EMBEDDED_UNBOUNDID); - } }