This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/8.5.x by this push:
new 442bc00 Based on #464. Avoid using null for class loader during
lookups
442bc00 is described below
commit 442bc00a7adad3e08c70c8ee21f4e09231683c7d
Author: Mark Thomas <[email protected]>
AuthorDate: Thu Feb 10 19:53:15 2022 +0000
Based on #464. Avoid using null for class loader during lookups
Fall back to the class loader used to load JULI when the thread context
class loader is not set. In a normal Tomcat configuration, this will be
the system class loader.
Based on a pull request by jackshirazi.
---
java/org/apache/juli/ClassLoaderLogManager.java | 49 +++++++++++++++++--------
java/org/apache/juli/FileHandler.java | 2 +-
webapps/docs/changelog.xml | 6 +++
3 files changed, 40 insertions(+), 17 deletions(-)
diff --git a/java/org/apache/juli/ClassLoaderLogManager.java
b/java/org/apache/juli/ClassLoaderLogManager.java
index 421fce0..2243e57 100644
--- a/java/org/apache/juli/ClassLoaderLogManager.java
+++ b/java/org/apache/juli/ClassLoaderLogManager.java
@@ -150,8 +150,7 @@ public class ClassLoaderLogManager extends LogManager {
final String loggerName = logger.getName();
- ClassLoader classLoader =
- Thread.currentThread().getContextClassLoader();
+ ClassLoader classLoader = getClassLoader();
ClassLoaderLogInfo info = getClassLoaderInfo(classLoader);
if (info.loggers.containsKey(loggerName)) {
return false;
@@ -244,8 +243,7 @@ public class ClassLoaderLogManager extends LogManager {
*/
@Override
public synchronized Logger getLogger(final String name) {
- ClassLoader classLoader = Thread.currentThread()
- .getContextClassLoader();
+ ClassLoader classLoader = getClassLoader();
return getClassLoaderInfo(classLoader).loggers.get(name);
}
@@ -256,8 +254,7 @@ public class ClassLoaderLogManager extends LogManager {
*/
@Override
public synchronized Enumeration<String> getLoggerNames() {
- ClassLoader classLoader = Thread.currentThread()
- .getContextClassLoader();
+ ClassLoader classLoader = getClassLoader();
return
Collections.enumeration(getClassLoaderInfo(classLoader).loggers.keySet());
}
@@ -300,7 +297,7 @@ public class ClassLoaderLogManager extends LogManager {
private synchronized String findProperty(String name) {
- ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ ClassLoader classLoader = getClassLoader();
ClassLoaderLogInfo info = getClassLoaderInfo(classLoader);
String result = info.props.getProperty(name);
// If the property was not found, and the current classloader had no
@@ -333,7 +330,7 @@ public class ClassLoaderLogManager extends LogManager {
checkAccess();
- readConfiguration(Thread.currentThread().getContextClassLoader());
+ readConfiguration(getClassLoader());
}
@@ -344,7 +341,7 @@ public class ClassLoaderLogManager extends LogManager {
checkAccess();
reset();
- readConfiguration(is, Thread.currentThread().getContextClassLoader());
+ readConfiguration(is, getClassLoader());
}
@@ -357,7 +354,7 @@ public class ClassLoaderLogManager extends LogManager {
// because we have our own shutdown hook
return;
}
- ClassLoader classLoader = thread.getContextClassLoader();
+ ClassLoader classLoader = getClassLoader();
ClassLoaderLogInfo clLogInfo = getClassLoaderInfo(classLoader);
resetLoggers(clLogInfo);
// Do not call super.reset(). It should be a NO-OP as all loggers
should
@@ -409,16 +406,17 @@ public class ClassLoaderLogManager extends LogManager {
/**
* Retrieve the configuration associated with the specified classloader. If
- * it does not exist, it will be created.
+ * it does not exist, it will be created. If no class loader is specified,
+ * the class loader used to load this class is used.
*
- * @param classLoader The classloader for which we will retrieve or build
the
- * configuration
+ * @param classLoader The class loader for which we will retrieve or build
+ * the configuration
* @return the log configuration
*/
protected synchronized ClassLoaderLogInfo getClassLoaderInfo(ClassLoader
classLoader) {
if (classLoader == null) {
- classLoader = ClassLoader.getSystemClassLoader();
+ classLoader = this.getClass().getClassLoader();
}
ClassLoaderLogInfo info = classLoaderLoggers.get(classLoader);
if (info == null) {
@@ -679,7 +677,7 @@ public class ClassLoaderLogManager extends LogManager {
private String replaceWebApplicationProperties(String propName) {
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ ClassLoader cl = getClassLoader();
if (cl instanceof WebappProperties) {
WebappProperties wProps = (WebappProperties) cl;
if ("classloader.webappName".equals(propName)) {
@@ -697,9 +695,28 @@ public class ClassLoaderLogManager extends LogManager {
}
- // ---------------------------------------------------- LogNode Inner Class
+ /**
+ * Obtain the class loader to use to lookup loggers, obtain configuration
+ * etc. The search order is:
+ * <ol>
+ * <li>Thread.currentThread().getContextClassLoader()</li>
+ * <li>The class laoder of this class</li>
+ * </ol>
+ *
+ * @return The class loader to use to lookup loggers, obtain configuration
+ * etc.
+ */
+ static ClassLoader getClassLoader() {
+ ClassLoader result = Thread.currentThread().getContextClassLoader();
+ if (result == null) {
+ result = ClassLoaderLogManager.class.getClassLoader();
+ }
+ return result;
+ }
+ // ---------------------------------------------------- LogNode Inner Class
+
protected static final class LogNode {
Logger logger;
diff --git a/java/org/apache/juli/FileHandler.java
b/java/org/apache/juli/FileHandler.java
index 6a742f7..38e50c1 100644
--- a/java/org/apache/juli/FileHandler.java
+++ b/java/org/apache/juli/FileHandler.java
@@ -373,7 +373,7 @@ public class FileHandler extends Handler {
String className = this.getClass().getName(); //allow classes to
override
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ ClassLoader cl = ClassLoaderLogManager.getClassLoader();
// Retrieve configuration of logging file name
rotatable = Boolean.parseBoolean(getProperty(className + ".rotatable",
"true"));
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index e1a2526..e2414fd 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -124,6 +124,12 @@
FIPS compliant configuration. (markt)
</fix>
<fix>
+ <pr>464</pr>: Fall back to the class loader used to load JULI when the
+ thread context class loader is not set. In a normal Tomcat
+ configuration, this will be the system class loader. Based on a pull
+ request by jackshirazi. (markt)
+ </fix>
+ <fix>
<pr>469</pr>: Include the Java Annotations API in the classes that
Tomcat will not load from web applications. Pull request provided by
ppkarwasz. (markt)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]