Skip to content

Commit e276edc

Browse files
committed
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.
1 parent a754fc0 commit e276edc

3 files changed

Lines changed: 40 additions & 17 deletions

File tree

java/org/apache/juli/ClassLoaderLogManager.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ public synchronized boolean addLogger(final Logger logger) {
133133

134134
final String loggerName = logger.getName();
135135

136-
ClassLoader classLoader =
137-
Thread.currentThread().getContextClassLoader();
136+
ClassLoader classLoader = getClassLoader();
138137
ClassLoaderLogInfo info = getClassLoaderInfo(classLoader);
139138
if (info.loggers.containsKey(loggerName)) {
140139
return false;
@@ -224,8 +223,7 @@ public synchronized boolean addLogger(final Logger logger) {
224223
*/
225224
@Override
226225
public synchronized Logger getLogger(final String name) {
227-
ClassLoader classLoader = Thread.currentThread()
228-
.getContextClassLoader();
226+
ClassLoader classLoader = getClassLoader();
229227
return getClassLoaderInfo(classLoader).loggers.get(name);
230228
}
231229

@@ -236,8 +234,7 @@ public synchronized Logger getLogger(final String name) {
236234
*/
237235
@Override
238236
public synchronized Enumeration<String> getLoggerNames() {
239-
ClassLoader classLoader = Thread.currentThread()
240-
.getContextClassLoader();
237+
ClassLoader classLoader = getClassLoader();
241238
return Collections.enumeration(getClassLoaderInfo(classLoader).loggers.keySet());
242239
}
243240

@@ -280,7 +277,7 @@ public String getProperty(String name) {
280277

281278

282279
private synchronized String findProperty(String name) {
283-
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
280+
ClassLoader classLoader = getClassLoader();
284281
ClassLoaderLogInfo info = getClassLoaderInfo(classLoader);
285282
String result = info.props.getProperty(name);
286283
// If the property was not found, and the current classloader had no
@@ -313,7 +310,7 @@ public void readConfiguration()
313310

314311
checkAccess();
315312

316-
readConfiguration(Thread.currentThread().getContextClassLoader());
313+
readConfiguration(getClassLoader());
317314

318315
}
319316

@@ -324,7 +321,7 @@ public void readConfiguration(InputStream is)
324321
checkAccess();
325322
reset();
326323

327-
readConfiguration(is, Thread.currentThread().getContextClassLoader());
324+
readConfiguration(is, getClassLoader());
328325

329326
}
330327

@@ -337,7 +334,7 @@ public void reset() throws SecurityException {
337334
// because we have our own shutdown hook
338335
return;
339336
}
340-
ClassLoader classLoader = thread.getContextClassLoader();
337+
ClassLoader classLoader = getClassLoader();
341338
ClassLoaderLogInfo clLogInfo = getClassLoaderInfo(classLoader);
342339
resetLoggers(clLogInfo);
343340
// Do not call super.reset(). It should be a NO-OP as all loggers should
@@ -389,16 +386,17 @@ private void resetLoggers(ClassLoaderLogInfo clLogInfo) {
389386

390387
/**
391388
* Retrieve the configuration associated with the specified classloader. If
392-
* it does not exist, it will be created.
389+
* it does not exist, it will be created. If no class loader is specified,
390+
* the class loader used to load this class is used.
393391
*
394-
* @param classLoader The classloader for which we will retrieve or build the
395-
* configuration
392+
* @param classLoader The class loader for which we will retrieve or build
393+
* the configuration
396394
* @return the log configuration
397395
*/
398396
protected synchronized ClassLoaderLogInfo getClassLoaderInfo(ClassLoader classLoader) {
399397

400398
if (classLoader == null) {
401-
classLoader = ClassLoader.getSystemClassLoader();
399+
classLoader = this.getClass().getClassLoader();
402400
}
403401
ClassLoaderLogInfo info = classLoaderLoggers.get(classLoader);
404402
if (info == null) {
@@ -652,7 +650,7 @@ protected String replace(String str) {
652650

653651

654652
private String replaceWebApplicationProperties(String propName) {
655-
ClassLoader cl = Thread.currentThread().getContextClassLoader();
653+
ClassLoader cl = getClassLoader();
656654
if (cl instanceof WebappProperties) {
657655
WebappProperties wProps = (WebappProperties) cl;
658656
if ("classloader.webappName".equals(propName)) {
@@ -670,9 +668,28 @@ private String replaceWebApplicationProperties(String propName) {
670668
}
671669

672670

673-
// ---------------------------------------------------- LogNode Inner Class
671+
/**
672+
* Obtain the class loader to use to lookup loggers, obtain configuration
673+
* etc. The search order is:
674+
* <ol>
675+
* <li>Thread.currentThread().getContextClassLoader()</li>
676+
* <li>The class laoder of this class</li>
677+
* </ol>
678+
*
679+
* @return The class loader to use to lookup loggers, obtain configuration
680+
* etc.
681+
*/
682+
static ClassLoader getClassLoader() {
683+
ClassLoader result = Thread.currentThread().getContextClassLoader();
684+
if (result == null) {
685+
result = ClassLoaderLogManager.class.getClassLoader();
686+
}
687+
return result;
688+
}
674689

675690

691+
// ---------------------------------------------------- LogNode Inner Class
692+
676693
protected static final class LogNode {
677694
Logger logger;
678695

java/org/apache/juli/FileHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ private void configure() {
377377

378378
String className = this.getClass().getName(); //allow classes to override
379379

380-
ClassLoader cl = Thread.currentThread().getContextClassLoader();
380+
ClassLoader cl = ClassLoaderLogManager.getClassLoader();
381381

382382
// Retrieve configuration of logging file name
383383
if (rotatable == null) {

webapps/docs/changelog.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@
128128
<code>TomcatPrincipal</code> and <code>GenericPrincipal</code>.
129129
Patch provided by Carsten Klein. (michaelo)
130130
</add>
131+
<fix>
132+
<pr>464</pr>: Fall back to the class loader used to load JULI when the
133+
thread context class loader is not set. In a normal Tomcat
134+
configuration, this will be the system class loader. Based on a pull
135+
request by jackshirazi. (markt)
136+
</fix>
131137
<fix>
132138
<pr>469</pr>: Include the Jakarata Annotations API in the classes that
133139
Tomcat will not load from web applications. Pull request provided by

0 commit comments

Comments
 (0)