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
pull/13534/head
Stephane Nicoll 7 years ago
parent c12f8298e6
commit 65cc7c72f4

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

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

@ -93,7 +93,16 @@ public class JndiConnectionFactoryAutoConfigurationTests {
@Test @Test
public void jndiNamePropertySet() { 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") this.contextRunner.withPropertyValues("spring.jms.jndi-name=myCF")
.run(assertConnectionFactory(connectionFactory)); .run(assertConnectionFactory(connectionFactory));
} }

@ -150,8 +150,18 @@ public class MailSenderAutoConfigurationTests {
@Test @Test
public void jndiSessionAvailable() { public void jndiSessionAvailable() {
Session session = configureJndiSession("foo"); Session session = configureJndiSession("java:comp/env/foo");
this.contextRunner.withPropertyValues("spring.mail.jndi-name: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) -> { .run((context) -> {
assertThat(context).hasSingleBean(Session.class); assertThat(context).hasSingleBean(Session.class);
Session sessionBean = context.getBean(Session.class); Session sessionBean = context.getBean(Session.class);

Loading…
Cancel
Save