Harmonize JNDI lookups to enable resourceRef

This commit makes sure that JMS and Mail JNDI lookups behave the same
way as DataSource JNDI lookups by enabling the "resourceRef" flag.

This will make sure to add "java:comp/env" to the lookup if the JNDI
name doesn't already contain it. If that name does not exist, a second
attempt to the original name will be issued automatically.

Closes gh-12803
This commit is contained in:
Stephane Nicoll
2018-06-19 17:47:26 +02:00
parent c12f8298e6
commit 65cc7c72f4
4 changed files with 29 additions and 6 deletions

View File

@@ -57,14 +57,17 @@ public class JndiConnectionFactoryAutoConfiguration {
private final JmsProperties properties;
private final JndiLocatorDelegate jndiLocatorDelegate;
public JndiConnectionFactoryAutoConfiguration(JmsProperties properties) {
this.properties = properties;
this.jndiLocatorDelegate = JndiLocatorDelegate.createDefaultResourceRefLocator();
}
@Bean
public ConnectionFactory connectionFactory() throws NamingException {
if (StringUtils.hasLength(this.properties.getJndiName())) {
return new JndiLocatorDelegate().lookup(this.properties.getJndiName(),
return this.jndiLocatorDelegate.lookup(this.properties.getJndiName(),
ConnectionFactory.class);
}
return findJndiConnectionFactory();
@@ -73,7 +76,7 @@ public class JndiConnectionFactoryAutoConfiguration {
private ConnectionFactory findJndiConnectionFactory() {
for (String name : JNDI_LOCATIONS) {
try {
return new JndiLocatorDelegate().lookup(name, ConnectionFactory.class);
return this.jndiLocatorDelegate.lookup(name, ConnectionFactory.class);
}
catch (NamingException ex) {
// Swallow and continue

View File

@@ -60,7 +60,8 @@ class MailSenderJndiConfiguration {
public Session session() {
String jndiName = this.properties.getJndiName();
try {
return new JndiLocatorDelegate().lookup(jndiName, Session.class);
return JndiLocatorDelegate.createDefaultResourceRefLocator().lookup(jndiName,
Session.class);
}
catch (NamingException ex) {
throw new IllegalStateException(

View File

@@ -93,7 +93,16 @@ public class JndiConnectionFactoryAutoConfigurationTests {
@Test
public void jndiNamePropertySet() {
ConnectionFactory connectionFactory = configureConnectionFactory("myCF");
ConnectionFactory connectionFactory = configureConnectionFactory(
"java:comp/env/myCF");
this.contextRunner.withPropertyValues("spring.jms.jndi-name=java:comp/env/myCF")
.run(assertConnectionFactory(connectionFactory));
}
@Test
public void jndiNamePropertySetWithResourceRef() {
ConnectionFactory connectionFactory = configureConnectionFactory(
"java:comp/env/myCF");
this.contextRunner.withPropertyValues("spring.jms.jndi-name=myCF")
.run(assertConnectionFactory(connectionFactory));
}

View File

@@ -150,8 +150,18 @@ public class MailSenderAutoConfigurationTests {
@Test
public void jndiSessionAvailable() {
Session session = configureJndiSession("foo");
this.contextRunner.withPropertyValues("spring.mail.jndi-name:foo")
Session session = configureJndiSession("java:comp/env/foo");
testJndiSessionLookup(session, "java:comp/env/foo");
}
@Test
public void jndiSessionAvailableWithResourceRef() {
Session session = configureJndiSession("java:comp/env/foo");
testJndiSessionLookup(session, "foo");
}
private void testJndiSessionLookup(Session session, String jndiName) {
this.contextRunner.withPropertyValues("spring.mail.jndi-name:" + jndiName)
.run((context) -> {
assertThat(context).hasSingleBean(Session.class);
Session sessionBean = context.getBean(Session.class);