Skip to content

Commit b82753b

Browse files
tssmith04volosied
authored andcommitted
Fix BEANUTILS-566
1 parent b78e67f commit b82753b

19 files changed

+70
-40
lines changed

src/main/java/org/apache/commons/beanutils2/BeanPredicate.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.function.Predicate;
2222

2323
import org.apache.commons.logging.Log;
24-
import org.apache.commons.logging.LogFactory;
2524

2625
/**
2726
* <p>Predicate implementation that applies the given {@code Predicate}
@@ -32,7 +31,7 @@
3231
*/
3332
public class BeanPredicate<T> implements Predicate<T> {
3433

35-
private final Log log = LogFactory.getLog(this.getClass());
34+
private final Log log = LoggerUtil.createLoggerwithContextClassLoader(this.getClass());
3635

3736
/** Name of the property whose value will be predicated */
3837
private String propertyName;

src/main/java/org/apache/commons/beanutils2/BeanPropertyValueChangeConsumer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.function.Consumer;
2222

2323
import org.apache.commons.logging.Log;
24-
import org.apache.commons.logging.LogFactory;
2524

2625
/**
2726
* <p>{@code Closure} that sets a property.</p>
@@ -81,7 +80,7 @@
8180
public class BeanPropertyValueChangeConsumer<T, V> implements Consumer<T> {
8281

8382
/** For logging. Each subclass gets its own log instance. */
84-
private final Log log = LogFactory.getLog(this.getClass());
83+
private final Log log = LoggerUtil.createLoggerwithContextClassLoader(this.getClass());
8584

8685
/**
8786
* The name of the property which will be updated when this {@code Closure} executes.

src/main/java/org/apache/commons/beanutils2/BeanPropertyValueEqualsPredicate.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.function.Predicate;
2323

2424
import org.apache.commons.logging.Log;
25-
import org.apache.commons.logging.LogFactory;
2625

2726
/**
2827
* <p>{@code Predicate} that evaluates a property value against a specified value.</p>
@@ -113,7 +112,7 @@
113112
public class BeanPropertyValueEqualsPredicate<T, V> implements Predicate<T> {
114113

115114
/** For logging. Each subclass gets its own log instance. */
116-
private final Log log = LogFactory.getLog(this.getClass());
115+
private final Log log = LoggerUtil.createLoggerwithContextClassLoader(this.getClass());
117116

118117
/**
119118
* The name of the property which will be evaluated when this {@code Predicate} is executed.

src/main/java/org/apache/commons/beanutils2/BeanToPropertyValueTransformer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.function.Function;
2222

2323
import org.apache.commons.logging.Log;
24-
import org.apache.commons.logging.LogFactory;
2524

2625
/**
2726
* <p>{@code Transformer} that outputs a property value.</p>
@@ -73,7 +72,7 @@
7372
public class BeanToPropertyValueTransformer<T, R> implements Function<T, R> {
7473

7574
/** For logging. Each subclass gets its own log instance. */
76-
private final Log log = LogFactory.getLog(this.getClass());
75+
private final Log log = LoggerUtil.createLoggerwithContextClassLoader(this.getClass());
7776

7877
/** The name of the property that will be used in the transformation of the object. */
7978
private final String propertyName;

src/main/java/org/apache/commons/beanutils2/BeanUtilsBean.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import org.apache.commons.beanutils2.expression.Resolver;
3232
import org.apache.commons.logging.Log;
33-
import org.apache.commons.logging.LogFactory;
3433

3534
/**
3635
* <p>JavaBean property population methods.</p>
@@ -60,7 +59,7 @@ protected BeanUtilsBean initialValue() {
6059
/**
6160
* Logging for this instance
6261
*/
63-
private static final Log LOG = LogFactory.getLog(BeanUtilsBean.class);
62+
private static final Log LOG = LoggerUtil.createLoggerwithContextClassLoader(BeanUtilsBean.class);
6463

6564
/** A reference to Throwable's initCause method, or null if it's not there in this JVM */
6665
private static final Method INIT_CAUSE_METHOD = getInitCauseMethod();
@@ -95,13 +94,13 @@ private static Method getInitCauseMethod() {
9594
final Class<?>[] paramsClasses = { Throwable.class };
9695
return Throwable.class.getMethod("initCause", paramsClasses);
9796
} catch (final NoSuchMethodException e) {
98-
final Log log = LogFactory.getLog(BeanUtils.class);
97+
final Log log = LoggerUtil.createLoggerwithContextClassLoader(BeanUtils.class);
9998
if (log.isWarnEnabled()) {
10099
log.warn("Throwable does not have initCause() method in JDK 1.3");
101100
}
102101
return null;
103102
} catch (final Throwable e) {
104-
final Log log = LogFactory.getLog(BeanUtils.class);
103+
final Log log = LoggerUtil.createLoggerwithContextClassLoader(BeanUtils.class);
105104
if (log.isWarnEnabled()) {
106105
log.warn("Error getting the Throwable initCause() method", e);
107106
}

src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
import org.apache.commons.beanutils2.sql.converters.SqlTimeConverter;
9595
import org.apache.commons.beanutils2.sql.converters.SqlTimestampConverter;
9696
import org.apache.commons.logging.Log;
97-
import org.apache.commons.logging.LogFactory;
9897

9998
/**
10099
* <p>Utility methods for converting String scalar values to objects of the
@@ -196,7 +195,7 @@ public class ConvertUtilsBean {
196195
/**
197196
* The {@code Log} instance for this class.
198197
*/
199-
private static final Log LOG = LogFactory.getLog(ConvertUtilsBean.class);
198+
private static final Log LOG = LoggerUtil.createLoggerwithContextClassLoader(ConvertUtilsBean.class);
200199

201200
/**
202201
* Gets singleton instance

src/main/java/org/apache/commons/beanutils2/DefaultBeanIntrospector.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.List;
2626

2727
import org.apache.commons.logging.Log;
28-
import org.apache.commons.logging.LogFactory;
2928

3029
/**
3130
* <p>
@@ -54,7 +53,7 @@ public class DefaultBeanIntrospector implements BeanIntrospector {
5453
private static final Class<?>[] LIST_CLASS_PARAMETER = new Class[] { java.util.List.class };
5554

5655
/** For logging. Each subclass gets its own log instance. */
57-
private final Log log = LogFactory.getLog(getClass());
56+
private final Log log = LoggerUtil.createLoggerwithContextClassLoader(getClass());
5857

5958
/**
6059
* Private constructor so that no instances can be created.

src/main/java/org/apache/commons/beanutils2/FluentPropertyBeanIntrospector.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Objects;
2525

2626
import org.apache.commons.logging.Log;
27-
import org.apache.commons.logging.LogFactory;
2827

2928
/**
3029
* <p>
@@ -81,7 +80,7 @@ public class FluentPropertyBeanIntrospector implements BeanIntrospector {
8180
public static final String DEFAULT_WRITE_METHOD_PREFIX = "set";
8281

8382
/** The logger. */
84-
private final Log log = LogFactory.getLog(getClass());
83+
private final Log log = LoggerUtil.createLoggerwithContextClassLoader(getClass());
8584

8685
/** The prefix of write methods to search for. */
8786
private final String writeMethodPrefix;

src/main/java/org/apache/commons/beanutils2/LazyDynaBean.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import java.util.Objects;
2929

3030
import org.apache.commons.logging.Log;
31-
import org.apache.commons.logging.LogFactory;
32-
3331
/**
3432
* <p>DynaBean which automatically adds properties to the {@code DynaClass}
3533
* and provides <i>Lazy List</i> and <i>Lazy Map</i> features.</p>
@@ -128,7 +126,7 @@ public class LazyDynaBean implements DynaBean, Serializable {
128126
/**
129127
* Commons Logging
130128
*/
131-
private static transient Log LOG = LogFactory.getLog(LazyDynaBean.class);
129+
private static transient Log LOG = LoggerUtil.createLoggerwithContextClassLoader(LazyDynaBean.class);
132130

133131
/** BigInteger Zero */
134132
protected static final BigInteger BigInteger_ZERO = new BigInteger("0");
@@ -692,7 +690,7 @@ protected boolean isDynaProperty(final String name) {
692690
*/
693691
private Log logger() {
694692
if (LOG == null) {
695-
LOG = LogFactory.getLog(LazyDynaBean.class);
693+
LOG = LoggerUtil.createLoggerwithContextClassLoader(LazyDynaBean.class);
696694
}
697695
return LOG;
698696
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.beanutils2;
19+
20+
import org.apache.commons.logging.Log;
21+
import org.apache.commons.logging.LogFactory;
22+
23+
public class LoggerUtil {
24+
25+
@SuppressWarnings("rawtypes")
26+
public static Log createLoggerwithContextClassLoader(Class clazz) {
27+
ClassLoader currentContextClassLoader = Thread.currentThread().getContextClassLoader();
28+
Log log = null;
29+
try {
30+
Thread.currentThread().setContextClassLoader(clazz.getClassLoader());
31+
log = LogFactory.getLog(clazz);
32+
} finally {
33+
Thread.currentThread().setContextClassLoader(currentContextClassLoader);
34+
}
35+
36+
// if (log == null) {
37+
// return LogFactory.getLog(clazz); // fallback -- is this needed?
38+
// }
39+
40+
return log;
41+
}
42+
}

0 commit comments

Comments
 (0)