Defensive reference to JNDI API for JDK 9+ (optional java.naming module)

Closes gh-27483
This commit is contained in:
Juergen Hoeller
2021-09-28 18:15:22 +02:00
parent 2feedb98cc
commit 86b010a6b2
2 changed files with 41 additions and 11 deletions

View File

@@ -27,6 +27,7 @@ import org.springframework.core.env.StandardEnvironment;
import org.springframework.jndi.JndiLocatorDelegate;
import org.springframework.jndi.JndiPropertySource;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.web.context.ConfigurableWebEnvironment;
/**
@@ -39,6 +40,7 @@ import org.springframework.web.context.ConfigurableWebEnvironment;
* documentation for details.
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
* @see StandardEnvironment
*/
@@ -54,6 +56,11 @@ public class StandardServletEnvironment extends StandardEnvironment implements C
public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties";
// Defensive reference to JNDI API for JDK 9+ (optional java.naming module)
private static final boolean jndiPresent = ClassUtils.isPresent(
"javax.naming.InitialContext", StandardServletEnvironment.class.getClassLoader());
/**
* Create a new {@code StandardServletEnvironment} instance.
*/
@@ -100,7 +107,7 @@ public class StandardServletEnvironment extends StandardEnvironment implements C
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
if (jndiPresent && JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
}
super.customizePropertySources(propertySources);