From 389628c0bccdb515b6ea0bc2640e12a131d7f051 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Fri, 24 Apr 2015 13:48:25 -0500 Subject: [PATCH] LDAP-317: ldap:context-source@url supports SpEL --- .../ldap/config/ContextSourceParser.java | 16 +++++-- .../LdapTemplateNamespaceHandlerTest.java | 47 +++++++++++++++++++ .../ldap-namespace-config-multiurls.xml | 13 +++++ .../ldap-namespace-config-spel-multiurls.xml | 17 +++++++ .../resources/ldap-namespace-config-spel.xml | 21 +++++++++ 5 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 core/src/test/resources/ldap-namespace-config-multiurls.xml create mode 100644 core/src/test/resources/ldap-namespace-config-spel-multiurls.xml create mode 100644 core/src/test/resources/ldap-namespace-config-spel.xml diff --git a/core/src/main/java/org/springframework/ldap/config/ContextSourceParser.java b/core/src/main/java/org/springframework/ldap/config/ContextSourceParser.java index d06a542c..30679215 100644 --- a/core/src/main/java/org/springframework/ldap/config/ContextSourceParser.java +++ b/core/src/main/java/org/springframework/ldap/config/ContextSourceParser.java @@ -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); + } + } } diff --git a/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java b/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java index 255de250..0511148e 100644 --- a/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java +++ b/core/src/test/java/org/springframework/ldap/config/LdapTemplateNamespaceHandlerTest.java @@ -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"); diff --git a/core/src/test/resources/ldap-namespace-config-multiurls.xml b/core/src/test/resources/ldap-namespace-config-multiurls.xml new file mode 100644 index 00000000..718c3f17 --- /dev/null +++ b/core/src/test/resources/ldap-namespace-config-multiurls.xml @@ -0,0 +1,13 @@ + + + + + \ No newline at end of file diff --git a/core/src/test/resources/ldap-namespace-config-spel-multiurls.xml b/core/src/test/resources/ldap-namespace-config-spel-multiurls.xml new file mode 100644 index 00000000..f8bdf80f --- /dev/null +++ b/core/src/test/resources/ldap-namespace-config-spel-multiurls.xml @@ -0,0 +1,17 @@ + + + + + + + ldap://a.localhost:389,ldap://b.localhost:389 + + \ No newline at end of file diff --git a/core/src/test/resources/ldap-namespace-config-spel.xml b/core/src/test/resources/ldap-namespace-config-spel.xml new file mode 100644 index 00000000..eff6809c --- /dev/null +++ b/core/src/test/resources/ldap-namespace-config-spel.xml @@ -0,0 +1,21 @@ + + + + + + + ldap://localhost:389 + uid=admin + apassword + dc=261consulting,dc=com + + \ No newline at end of file