Skip to content

Commit

Permalink
Port log4j-jndi changes from 2.x (#2163)
Browse files Browse the repository at this point in the history
  • Loading branch information
vy committed Jan 10, 2024
1 parent 6d1d346 commit 986a716
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private JndiManager(final String name, final Context context) {
}

/**
* Gets the default JndiManager using the default {@link javax.naming.InitialContext}.
* Gets the default JndiManager using the default {@link InitialContext}.
*
* @return the default JndiManager
*/
Expand All @@ -76,7 +76,7 @@ public static JndiManager getDefaultManager() {
}

/**
* Gets a named JndiManager using the default {@link javax.naming.InitialContext}.
* Gets a named JndiManager using the default {@link InitialContext}.
*
* @param name the name of the JndiManager instance to create or use if available
* @return a default JndiManager
Expand Down Expand Up @@ -217,7 +217,7 @@ public <T> T lookup(final String name) throws NamingException {
}
LOGGER.warn("Unsupported JNDI URI - {}", name);
} catch (URISyntaxException ex) {
LOGGER.warn("Invalid JNDI URI - {}", name);
LOGGER.warn("Invalid JNDI URI - {}", name);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import javax.naming.Context;
import javax.naming.NamingException;
import org.apache.logging.log4j.core.util.Closer;

/**
* Helper class for closing JNDI resources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public JndiLookup() {

/**
* Looks up the value of the JNDI resource.
*
* @param event The current LogEvent (is ignored by this StrLookup).
* @param key the JNDI resource name to be looked up, may be null
* @param key the JNDI resource name to be looked up, may be null
* @return The String value of the JNDI resource.
*/
@Override
Expand All @@ -77,9 +78,9 @@ public String lookup(final LogEvent event, final String key) {
}

/**
* Convert the given JNDI name to the actual JNDI name to use.
* Default implementation applies the "java:comp/env/" prefix
* unless other scheme like "java:" is given.
* Convert the given JNDI name to the actual JNDI name to use. Default implementation applies the "java:comp/env/"
* prefix unless other scheme like "java:" is given.
*
* @param jndiName The name of the resource.
* @return The fully qualified name to look up.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import javax.naming.NamingException;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.impl.ContextAnchor;
import org.apache.logging.log4j.core.selector.ContextSelector;
import org.apache.logging.log4j.core.selector.NamedContextSelector;
import org.apache.logging.log4j.core.util.Constants;
import org.apache.logging.log4j.jndi.JndiManager;
Expand All @@ -43,6 +42,7 @@
* context to look up the value of the entry. The logging context of the web-application will depend on the value the
* env-entry. The JNDI context which is looked up by this class is <code>java:comp/env/log4j/context-name</code>.
*
* <p>For security reasons, JNDI must be enabled by setting system property <code>log4j2.enableJndiContextSelector=true</code>.</p>
* <p>
* Here is an example of an <code>env-entry</code>:
* </p>
Expand Down Expand Up @@ -96,8 +96,8 @@ public class JndiContextSelector implements NamedContextSelector {
private static final StatusLogger LOGGER = StatusLogger.getLogger();

public JndiContextSelector() {
if (!JndiManager.isJndiEnabled()) {
throw new IllegalStateException("JNDI must be enabled by setting log4j2.enableJndi=true");
if (!JndiManager.isJndiContextSelectorEnabled()) {
throw new IllegalStateException("JNDI must be enabled by setting log4j2.enableJndiContextSelector=true");
}
}

Expand All @@ -112,7 +112,7 @@ public void shutdown(
}
}
if (ctx != null) {
ctx.stop(ContextSelector.DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS);
ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS);
}
}

Expand Down Expand Up @@ -148,7 +148,7 @@ public LoggerContext getContext(
return loggingContextName == null ? CONTEXT : locateContext(loggingContextName, null, configLocation);
}

private String getContextName() {
private static String getContextName() {
String loggingContextName = null;

try (final JndiManager jndiManager = JndiManager.getDefaultManager()) {
Expand Down

0 comments on commit 986a716

Please sign in to comment.