Support Property Placeholders in XML Configuration

Fixes gh-370
This commit is contained in:
Eddú Meléndez
2016-02-18 22:36:00 +10:00
committed by Rob Winch
parent dd8c62b6ba
commit 1ffd11acd8
6 changed files with 72 additions and 9 deletions

View File

@@ -219,6 +219,8 @@ public class ContextSourceParser implements BeanDefinitionParser {
builder.addPropertyValue("maxWait", getInt(poolingElement, ATT_MAX_WAIT, DEFAULT_MAX_WAIT));
String whenExhausted = getString(poolingElement, ATT_WHEN_EXHAUSTED, PoolExhaustedAction.BLOCK.name());
builder.addPropertyValue("whenExhaustedAction", PoolExhaustedAction.valueOf(whenExhausted).getValue());
builder.addPropertyValue("timeBetweenEvictionRunsMillis", getString(poolingElement, ATT_EVICTION_RUN_MILLIS, String.valueOf(DEFAULT_EVICTION_RUN_MILLIS)));
builder.addPropertyValue("minEvictableIdleTimeMillis", getString(poolingElement, ATT_EVICTABLE_TIME_MILLIS, String.valueOf(DEFAULT_EVICTABLE_MILLIS)));
boolean testOnBorrow = getBoolean(poolingElement, ATT_TEST_ON_BORROW, false);
boolean testOnReturn = getBoolean(poolingElement, ATT_TEST_ON_RETURN, false);
@@ -249,9 +251,9 @@ public class ContextSourceParser implements BeanDefinitionParser {
}
builder.addPropertyValue("dirContextValidator", validatorBuilder.getBeanDefinition());
builder.addPropertyValue("timeBetweenEvictionRunsMillis", getInt(element, ATT_EVICTION_RUN_MILLIS, DEFAULT_EVICTION_RUN_MILLIS));
builder.addPropertyValue("timeBetweenEvictionRunsMillis", getString(element, ATT_EVICTION_RUN_MILLIS, String.valueOf(DEFAULT_EVICTION_RUN_MILLIS)));
builder.addPropertyValue("numTestsPerEvictionRun", getInt(element, ATT_TESTS_PER_EVICTION_RUN, DEFAULT_TESTS_PER_EVICTION_RUN));
builder.addPropertyValue("minEvictableIdleTimeMillis", getInt(element, ATT_EVICTABLE_TIME_MILLIS, DEFAULT_EVICTABLE_MILLIS));
builder.addPropertyValue("minEvictableIdleTimeMillis", getString(element, ATT_EVICTABLE_TIME_MILLIS, String.valueOf(DEFAULT_EVICTABLE_MILLIS)));
String nonTransientExceptions = getString(element, ATT_NON_TRANSIENT_EXCEPTIONS, CommunicationException.class.getName());
String[] strings = StringUtils.commaDelimitedListToStringArray(nonTransientExceptions);
@@ -316,9 +318,9 @@ public class ContextSourceParser implements BeanDefinitionParser {
configBuilder.addPropertyValue("testOnCreate", getBoolean(element, ATT_TEST_ON_CREATE, false));
configBuilder.addPropertyValue("testOnReturn", getBoolean(element, ATT_TEST_ON_RETURN, false));
configBuilder.addPropertyValue("testWhileIdle", getBoolean(element, ATT_TEST_WHILE_IDLE, false));
configBuilder.addPropertyValue("timeBetweenEvictionRunsMillis", getInt(element, ATT_EVICTION_RUN_MILLIS, DEFAULT_EVICTION_RUN_MILLIS));
configBuilder.addPropertyValue("timeBetweenEvictionRunsMillis", getString(element, ATT_EVICTION_RUN_MILLIS, String.valueOf(DEFAULT_EVICTION_RUN_MILLIS)));
configBuilder.addPropertyValue("numTestsPerEvictionRun", getInt(element, ATT_TESTS_PER_EVICTION_RUN, DEFAULT_TESTS_PER_EVICTION_RUN));
configBuilder.addPropertyValue("minEvictableIdleTimeMillis", getInt(element, ATT_EVICTABLE_TIME_MILLIS, DEFAULT_EVICTABLE_MILLIS));
configBuilder.addPropertyValue("minEvictableIdleTimeMillis", getString(element, ATT_EVICTABLE_TIME_MILLIS, String.valueOf(DEFAULT_EVICTABLE_MILLIS)));
configBuilder.addPropertyValue("softMinEvictableIdleTimeMillis", getInt(element, ATT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS, DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS));
builder.addConstructorArgValue(configBuilder.getBeanDefinition());

View File

@@ -206,7 +206,7 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="eviction-run-interval-millis" type="xs:int">
<xs:attribute name="eviction-run-interval-millis" type="xs:string">
<xs:annotation>
<xs:documentation>
The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive,
@@ -222,7 +222,7 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="min-evictable-time-millis" type="xs:int">
<xs:attribute name="min-evictable-time-millis" type="xs:string">
<xs:annotation>
<xs:documentation>
The minimum amount of time an object may sit idle in the pool before it is eligible
@@ -347,7 +347,7 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="eviction-run-interval-millis" type="xs:int">
<xs:attribute name="eviction-run-interval-millis" type="xs:string">
<xs:annotation>
<xs:documentation>
The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive,
@@ -363,7 +363,7 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="min-evictable-time-millis" type="xs:int">
<xs:attribute name="min-evictable-time-millis" type="xs:string">
<xs:annotation>
<xs:documentation>
The minimum amount of time an object may sit idle in the pool before it is eligible
@@ -682,4 +682,4 @@
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
</xs:schema>

View File

@@ -494,4 +494,33 @@ public class LdapTemplateNamespaceHandlerTest {
public void verifyParseWithPool1AndPool2WillFail() {
new ClassPathXmlApplicationContext("/ldap-namespace-config-pool2-with-pool1.xml");
}
@Test
public void verifyParsePoolWithPlaceholders() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling-config-with-placeholders.xml");
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
assertNotNull(outerContextSource);
ContextSource pooledContextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
assertNotNull(pooledContextSource);
GenericKeyedObjectPool objectPool = (GenericKeyedObjectPool) getInternalState(pooledContextSource, "keyedObjectPool");
assertEquals(10, objectPool.getTimeBetweenEvictionRunsMillis());
assertEquals(20, objectPool.getMinEvictableIdleTimeMillis());
}
@Test
public void verifyParsePool2WithPlaceholders() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling2-config-with-placeholders.xml");
ContextSource outerContextSource = ctx.getBean(ContextSource.class);
assertNotNull(outerContextSource);
ContextSource pooledContextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
assertNotNull(pooledContextSource);
org.apache.commons.pool2.impl.GenericKeyedObjectPool objectPool =
(org.apache.commons.pool2.impl.GenericKeyedObjectPool) getInternalState(pooledContextSource, "keyedObjectPool");
assertEquals(10, objectPool.getTimeBetweenEvictionRunsMillis());
assertEquals(20, objectPool.getMinEvictableIdleTimeMillis());
}
}

View File

@@ -0,0 +1,15 @@
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:ldap.properties" />
<ldap:context-source password="apassword" url="ldap://localhost:389" username="uid=admin">
<ldap:pooling eviction-run-interval-millis="${ldap.eviction.run.internal.milis}" min-evictable-time-millis="${ldap.min.evictable.time.milis}"/>
</ldap:context-source>
<ldap:ldap-template />
</beans>

View File

@@ -0,0 +1,15 @@
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:ldap.properties" />
<ldap:context-source password="apassword" url="ldap://localhost:389" username="uid=admin">
<ldap:pooling2 eviction-run-interval-millis="${ldap.eviction.run.internal.milis}" min-evictable-time-millis="${ldap.min.evictable.time.milis}"/>
</ldap:context-source>
<ldap:ldap-template />
</beans>

View File

@@ -0,0 +1,2 @@
ldap.eviction.run.internal.milis=10
ldap.min.evictable.time.milis=20