Browse Source

JndiPropertySource defensively skips invalid JNDI lookup for property name with colon in resource-ref mode

Issue: SPR-14518
pull/1124/head
Juergen Hoeller 8 years ago
parent
commit
328e04f167
  1. 11
      spring-context/src/main/java/org/springframework/jndi/JndiPropertySource.java
  2. 36
      spring-context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java

11
spring-context/src/main/java/org/springframework/jndi/JndiPropertySource.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -78,6 +78,15 @@ public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> { @@ -78,6 +78,15 @@ public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> {
*/
@Override
public Object getProperty(String name) {
if (getSource().isResourceRef() && name.indexOf(':') != -1) {
// We're in resource-ref (prefixing with "java:comp/env") mode. Let's not bother
// with property names with a colon it since they're probably just containing a
// default value clause, very unlikely to match including the colon part even in
// a textual property source, and effectively never meant to match that way in
// JNDI where a colon indicates a separator between JNDI scheme and actual name.
return null;
}
try {
Object value = this.source.lookup(name);
if (logger.isDebugEnabled()) {

36
spring-context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,6 +30,7 @@ import static org.junit.Assert.*; @@ -30,6 +30,7 @@ import static org.junit.Assert.*;
* Unit tests for {@link JndiPropertySource}.
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
*/
public class JndiPropertySourceTests {
@ -56,7 +57,7 @@ public class JndiPropertySourceTests { @@ -56,7 +57,7 @@ public class JndiPropertySourceTests {
jndiLocator.setJndiTemplate(jndiTemplate);
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
assertThat((String)ps.getProperty("p1"), equalTo("v1"));
assertThat(ps.getProperty("p1"), equalTo("v1"));
}
@Test
@ -75,7 +76,36 @@ public class JndiPropertySourceTests { @@ -75,7 +76,36 @@ public class JndiPropertySourceTests {
jndiLocator.setJndiTemplate(jndiTemplate);
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
assertThat((String)ps.getProperty("p1"), equalTo("v1"));
assertThat(ps.getProperty("p1"), equalTo("v1"));
}
@Test
public void propertyWithDefaultClauseInResourceRefMode() {
JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate() {
@Override
public Object lookup(String jndiName) throws NamingException {
throw new IllegalStateException("Should not get called");
}
};
jndiLocator.setResourceRef(true);
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
assertThat(ps.getProperty("propertyKey:defaultValue"), nullValue());
}
@Test
public void propertyWithColonInNonResourceRefMode() {
JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate() {
@Override
public Object lookup(String jndiName) throws NamingException {
assertEquals("my:key", jndiName);
return "my:value";
}
};
jndiLocator.setResourceRef(false);
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
assertThat(ps.getProperty("my:key"), equalTo("my:value"));
}
}

Loading…
Cancel
Save