Commit 65cc7c72 authored by Stephane Nicoll's avatar Stephane Nicoll

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
parent c12f8298
......@@ -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
......
......@@ -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(
......
......@@ -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));
}
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment