|
1 | 1 | /* |
2 | | - * Copyright 2002-2012 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
19 | 19 | import javax.naming.InitialContext; |
20 | 20 | import javax.naming.NamingException; |
21 | 21 |
|
| 22 | +import org.springframework.core.SpringProperties; |
| 23 | + |
22 | 24 | /** |
23 | 25 | * {@link JndiLocatorSupport} subclass with public lookup methods, |
24 | 26 | * for convenient use as a delegate. |
|
28 | 30 | */ |
29 | 31 | public class JndiLocatorDelegate extends JndiLocatorSupport { |
30 | 32 |
|
| 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 | + |
31 | 56 | @Override |
32 | 57 | public Object lookup(String jndiName) throws NamingException { |
33 | 58 | return super.lookup(jndiName); |
@@ -57,6 +82,9 @@ public static JndiLocatorDelegate createDefaultResourceRefLocator() { |
57 | 82 | * {@code false} if not |
58 | 83 | */ |
59 | 84 | public static boolean isDefaultJndiEnvironmentAvailable() { |
| 85 | + if (shouldIgnoreDefaultJndiEnvironment) { |
| 86 | + return false; |
| 87 | + } |
60 | 88 | try { |
61 | 89 | new InitialContext().getEnvironment(); |
62 | 90 | return true; |
|
0 commit comments