LDAP-317: ldap:context-source@url supports SpEL
This commit is contained in:
@@ -91,13 +91,17 @@ public class ContextSourceParser implements BeanDefinitionParser {
|
||||
String username = element.getAttribute(ATT_USERNAME);
|
||||
String password = element.getAttribute(ATT_PASSWORD);
|
||||
String url = element.getAttribute(ATT_URL);
|
||||
|
||||
Assert.hasText(url, "url attribute must be specified");
|
||||
|
||||
builder.addPropertyValue("userDn", username);
|
||||
builder.addPropertyValue("password", password);
|
||||
String[] urls = StringUtils.commaDelimitedListToStringArray(url);
|
||||
builder.addPropertyValue("urls", urls);
|
||||
|
||||
BeanDefinitionBuilder urlsBuilder = BeanDefinitionBuilder
|
||||
.rootBeanDefinition(UrlsFactory.class)
|
||||
.setFactoryMethod("urls")
|
||||
.addConstructorArgValue(url);
|
||||
|
||||
builder.addPropertyValue("urls", urlsBuilder.getBeanDefinition());
|
||||
builder.addPropertyValue("base", getString(element, ATT_BASE, ""));
|
||||
builder.addPropertyValue("referral", getString(element, ATT_REFERRAL, null));
|
||||
|
||||
@@ -211,4 +215,10 @@ public class ContextSourceParser implements BeanDefinitionParser {
|
||||
|
||||
builder.addPropertyValue("nonTransientExceptions", nonTransientExceptionClasses);
|
||||
}
|
||||
|
||||
static class UrlsFactory {
|
||||
public static String[] urls(String value) {
|
||||
return StringUtils.commaDelimitedListToStringArray(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,6 +154,53 @@ public class LdapTemplateNamespaceHandlerTest {
|
||||
assertEquals(SearchControls.OBJECT_SCOPE, getInternalState(ldapTemplate, "defaultSearchScope"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsSpel() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-spel.xml");
|
||||
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
|
||||
|
||||
assertNotNull(outerContextSource);
|
||||
|
||||
assertTrue(outerContextSource instanceof TransactionAwareContextSourceProxy);
|
||||
ContextSource contextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
|
||||
|
||||
assertEquals(LdapUtils.newLdapName("dc=261consulting,dc=com"), getInternalState(contextSource, "base"));
|
||||
assertEquals("uid=admin", getInternalState(contextSource, "userDn"));
|
||||
assertEquals("apassword", getInternalState(contextSource, "password"));
|
||||
assertArrayEquals(new String[]{"ldap://localhost:389"}, (Object[]) getInternalState(contextSource, "urls"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsSpelMultiUrls() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-spel-multiurls.xml");
|
||||
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
|
||||
|
||||
assertNotNull(outerContextSource);
|
||||
|
||||
assertTrue(outerContextSource instanceof TransactionAwareContextSourceProxy);
|
||||
ContextSource contextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
|
||||
|
||||
assertArrayEquals(new String[] { "ldap://a.localhost:389", "ldap://b.localhost:389" },
|
||||
(Object[]) getInternalState(contextSource, "urls"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsMultipleUrls() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-multiurls.xml");
|
||||
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
|
||||
|
||||
assertNotNull(outerContextSource);
|
||||
|
||||
assertTrue(outerContextSource instanceof TransactionAwareContextSourceProxy);
|
||||
ContextSource contextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
|
||||
|
||||
assertArrayEquals(new String[] { "ldap://a.localhost:389", "ldap://b.localhost:389" },
|
||||
(Object[]) getInternalState(contextSource, "urls"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyParseWithDefaultTransactions() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-transactional-defaults.xml");
|
||||
|
||||
13
core/src/test/resources/ldap-namespace-config-multiurls.xml
Normal file
13
core/src/test/resources/ldap-namespace-config-multiurls.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:ldap="http://www.springframework.org/schema/ldap"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<ldap:context-source
|
||||
username="username"
|
||||
password="password"
|
||||
url="ldap://a.localhost:389,ldap://b.localhost:389" />
|
||||
</beans>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:ldap="http://www.springframework.org/schema/ldap"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<ldap:context-source
|
||||
url="#{ldapProperties.url}"
|
||||
username="username"
|
||||
password="password" />
|
||||
|
||||
<util:properties id="ldapProperties">
|
||||
<prop key="url">ldap://a.localhost:389,ldap://b.localhost:389</prop>
|
||||
</util:properties>
|
||||
</beans>
|
||||
21
core/src/test/resources/ldap-namespace-config-spel.xml
Normal file
21
core/src/test/resources/ldap-namespace-config-spel.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:ldap="http://www.springframework.org/schema/ldap"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<ldap:context-source
|
||||
url="#{ldapProperties.url}"
|
||||
base="#{ldapProperties.base}"
|
||||
username="#{ldapProperties.username}"
|
||||
password="#{ldapProperties.password}" />
|
||||
|
||||
<util:properties id="ldapProperties">
|
||||
<prop key="url">ldap://localhost:389</prop>
|
||||
<prop key="username">uid=admin</prop>
|
||||
<prop key="password">apassword</prop>
|
||||
<prop key="base">dc=261consulting,dc=com</prop>
|
||||
</util:properties>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user