Преглед на файлове

Improve logic to pick embedded server

Eddú Meléndez преди 6 години
родител
ревизия
c03fb701ce

+ 20 - 72
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<String, EmbeddedLdapServer> embeddedServers;
-
-	public LdapServerBeanDefinitionParser() {
-		Map<String, EmbeddedLdapServer> 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<String, EmbeddedLdapServer> 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<String, EmbeddedLdapServer> 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;
-		}
-	}
 }

+ 0 - 8
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("<ldap-user-service user-search-filter='(uid={0})' /><ldap-server mode='unboundid'/>", "5.2", null);
-		String[] beanNames = this.context.getBeanNamesForType(UnboundIdContainer.class);
-		assertThat(beanNames).hasSize(1);
-		assertThat(beanNames[0]).isEqualTo(BeanIds.EMBEDDED_UNBOUNDID);
-	}
 }