Skip to content

Commit d2c0885

Browse files
committed
StandardServletEnvironment supports "spring.jndi.ignore" flag for efficient property lookups
Issue: SPR-14026
1 parent 35eb52e commit d2c0885

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

spring-context/src/main/java/org/springframework/jndi/JndiLocatorDelegate.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
1919
import javax.naming.InitialContext;
2020
import javax.naming.NamingException;
2121

22+
import org.springframework.core.SpringProperties;
23+
2224
/**
2325
* {@link JndiLocatorSupport} subclass with public lookup methods,
2426
* for convenient use as a delegate.
@@ -28,6 +30,29 @@
2830
*/
2931
public class JndiLocatorDelegate extends JndiLocatorSupport {
3032

33+
/**
34+
* System property that instructs Spring to ignore a default JNDI environment, i.e.
35+
* to always return {@code false} from {@link #isDefaultJndiEnvironmentAvailable()}.
36+
* <p>The default is "false", allowing for regular default JNDI access e.g. in
37+
* {@link JndiPropertySource}. Switching this flag to {@code true} is an optimization
38+
* for scenarios where nothing is ever to be found for such JNDI fallback searches
39+
* to begin with, avoiding the repeated JNDI lookup overhead.
40+
* <p>Note that this flag just affects JNDI fallback searches, not explicitly configured
41+
* JNDI lookups such as for a {@code DataSource} or some other environment resource.
42+
* The flag literally just affects code which attempts JNDI searches based on the
43+
* {@code JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()} check: in particular,
44+
* {@code StandardServletEnvironment} and {@code StandardPortletEnvironment}.
45+
* @since 4.3
46+
* @see #isDefaultJndiEnvironmentAvailable()
47+
* @see JndiPropertySource
48+
*/
49+
public static final String IGNORE_JNDI_PROPERTY_NAME = "spring.jndi.ignore";
50+
51+
52+
private static final boolean shouldIgnoreDefaultJndiEnvironment =
53+
SpringProperties.getFlag(IGNORE_JNDI_PROPERTY_NAME);
54+
55+
3156
@Override
3257
public Object lookup(String jndiName) throws NamingException {
3358
return super.lookup(jndiName);
@@ -57,6 +82,9 @@ public static JndiLocatorDelegate createDefaultResourceRefLocator() {
5782
* {@code false} if not
5883
*/
5984
public static boolean isDefaultJndiEnvironmentAvailable() {
85+
if (shouldIgnoreDefaultJndiEnvironment) {
86+
return false;
87+
}
6088
try {
6189
new InitialContext().getEnvironment();
6290
return true;

0 commit comments

Comments
 (0)