Skip to content

Commit

Permalink
Fix BEANUTILS-566
Browse files Browse the repository at this point in the history
  • Loading branch information
tssmith04 authored and volosied committed Aug 21, 2024
1 parent b78e67f commit 2c46e41
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.function.Predicate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* <p>Predicate implementation that applies the given {@code Predicate}
Expand All @@ -32,7 +31,7 @@
*/
public class BeanPredicate<T> implements Predicate<T> {

private final Log log = LogFactory.getLog(this.getClass());
private final Log log = LoggerUtil.createLoggerWithContextClassLoader(this.getClass());

/** Name of the property whose value will be predicated */
private String propertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.function.Consumer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

/** For logging. Each subclass gets its own log instance. */
private final Log log = LogFactory.getLog(this.getClass());
private final Log log = LoggerUtil.createLoggerWithContextClassLoader(this.getClass());

/**
* The name of the property which will be updated when this {@code Closure} executes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.function.Predicate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

/** For logging. Each subclass gets its own log instance. */
private final Log log = LogFactory.getLog(this.getClass());
private final Log log = LoggerUtil.createLoggerWithContextClassLoader(this.getClass());

/**
* The name of the property which will be evaluated when this {@code Predicate} is executed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.function.Function;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

/** For logging. Each subclass gets its own log instance. */
private final Log log = LogFactory.getLog(this.getClass());
private final Log log = LoggerUtil.createLoggerWithContextClassLoader(this.getClass());

/** The name of the property that will be used in the transformation of the object. */
private final String propertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import org.apache.commons.beanutils2.expression.Resolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* <p>JavaBean property population methods.</p>
Expand Down Expand Up @@ -60,7 +59,7 @@ protected BeanUtilsBean initialValue() {
/**
* Logging for this instance
*/
private static final Log LOG = LogFactory.getLog(BeanUtilsBean.class);
private static final Log LOG = LoggerUtil.createLoggerWithContextClassLoader(BeanUtilsBean.class);

/** A reference to Throwable's initCause method, or null if it's not there in this JVM */
private static final Method INIT_CAUSE_METHOD = getInitCauseMethod();
Expand Down Expand Up @@ -95,13 +94,13 @@ private static Method getInitCauseMethod() {
final Class<?>[] paramsClasses = { Throwable.class };
return Throwable.class.getMethod("initCause", paramsClasses);
} catch (final NoSuchMethodException e) {
final Log log = LogFactory.getLog(BeanUtils.class);
final Log log = LoggerUtil.createLoggerWithContextClassLoader(BeanUtils.class);
if (log.isWarnEnabled()) {
log.warn("Throwable does not have initCause() method in JDK 1.3");
}
return null;
} catch (final Throwable e) {
final Log log = LogFactory.getLog(BeanUtils.class);
final Log log = LoggerUtil.createLoggerWithContextClassLoader(BeanUtils.class);
if (log.isWarnEnabled()) {
log.warn("Error getting the Throwable initCause() method", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
import org.apache.commons.beanutils2.sql.converters.SqlTimeConverter;
import org.apache.commons.beanutils2.sql.converters.SqlTimestampConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* <p>Utility methods for converting String scalar values to objects of the
Expand Down Expand Up @@ -196,7 +195,7 @@ public class ConvertUtilsBean {
/**
* The {@code Log} instance for this class.
*/
private static final Log LOG = LogFactory.getLog(ConvertUtilsBean.class);
private static final Log LOG = LoggerUtil.createLoggerWithContextClassLoader(ConvertUtilsBean.class);

/**
* Gets singleton instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

/** For logging. Each subclass gets its own log instance. */
private final Log log = LogFactory.getLog(getClass());
private final Log log = LoggerUtil.createLoggerWithContextClassLoader(getClass());

/**
* Private constructor so that no instances can be created.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Objects;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* <p>
Expand Down Expand Up @@ -81,7 +80,7 @@ public class FluentPropertyBeanIntrospector implements BeanIntrospector {
public static final String DEFAULT_WRITE_METHOD_PREFIX = "set";

/** The logger. */
private final Log log = LogFactory.getLog(getClass());
private final Log log = LoggerUtil.createLoggerWithContextClassLoader(getClass());

/** The prefix of write methods to search for. */
private final String writeMethodPrefix;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/apache/commons/beanutils2/LazyDynaBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.util.Objects;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* <p>DynaBean which automatically adds properties to the {@code DynaClass}
* and provides <i>Lazy List</i> and <i>Lazy Map</i> features.</p>
Expand Down Expand Up @@ -128,7 +126,7 @@ public class LazyDynaBean implements DynaBean, Serializable {
/**
* Commons Logging
*/
private static transient Log LOG = LogFactory.getLog(LazyDynaBean.class);
private static transient Log LOG = LoggerUtil.createLoggerWithContextClassLoader(LazyDynaBean.class);

/** BigInteger Zero */
protected static final BigInteger BigInteger_ZERO = new BigInteger("0");
Expand Down Expand Up @@ -692,7 +690,7 @@ protected boolean isDynaProperty(final String name) {
*/
private Log logger() {
if (LOG == null) {
LOG = LogFactory.getLog(LazyDynaBean.class);
LOG = LoggerUtil.createLoggerWithContextClassLoader(LazyDynaBean.class);
}
return LOG;
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/org/apache/commons/beanutils2/LoggerUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.beanutils2;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LoggerUtil {

@SuppressWarnings("rawtypes")
public static Log createLoggerWithContextClassLoader(Class clazz) {
ClassLoader currentContextClassLoader = Thread.currentThread().getContextClassLoader();
Log log = null;
try {
Thread.currentThread().setContextClassLoader(clazz.getClassLoader());
log = LogFactory.getLog(clazz);
} finally {
Thread.currentThread().setContextClassLoader(currentContextClassLoader);
}

if (log == null) {
return LogFactory.getLog(clazz); // fallback -- is this needed?
}

return log;
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/apache/commons/beanutils2/MethodUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import org.apache.commons.lang3.SystemProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* <p>Utility reflection methods focused on methods in general rather than properties in particular.</p>
Expand Down Expand Up @@ -110,7 +109,7 @@ public int hashCode() {
}
}

private static final Log LOG = LogFactory.getLog(MethodUtils.class);
private static final Log LOG = LoggerUtil.createLoggerWithContextClassLoader(MethodUtils.class);

/**
* Only log warning about accessibility work around once.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.commons.beanutils2.expression.DefaultResolver;
import org.apache.commons.beanutils2.expression.Resolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Utility methods for using Java Reflection APIs to facilitate generic
Expand Down Expand Up @@ -91,7 +90,7 @@
public class PropertyUtilsBean {

/** Log instance */
private static final Log LOG = LogFactory.getLog(PropertyUtilsBean.class);
private static final Log LOG = LoggerUtil.createLoggerWithContextClassLoader(PropertyUtilsBean.class);

/**
* Gets the PropertyUtils bean instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import org.apache.commons.beanutils2.ConversionException;
import org.apache.commons.beanutils2.ConvertUtils;
import org.apache.commons.beanutils2.Converter;
import org.apache.commons.beanutils2.LoggerUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Base {@link Converter} implementation that provides the structure
Expand Down Expand Up @@ -420,7 +420,7 @@ public boolean isUseDefault() {
*/
Log log() {
if (log == null) {
log = LogFactory.getLog(getClass());
log = LoggerUtil.createLoggerWithContextClassLoader(getClass());
}
return log;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

import org.apache.commons.beanutils2.ConversionException;
import org.apache.commons.beanutils2.ConvertUtils;
import org.apache.commons.beanutils2.LoggerUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* The base class for all standard type locale-sensitive converters. It has {@link LocaleConverter} and {@link org.apache.commons.beanutils2.Converter}
Expand Down Expand Up @@ -132,7 +132,7 @@ public B setUseDefault(final boolean useDefault) {
}

/** All logging goes through this logger */
private static final Log LOG = LogFactory.getLog(BaseLocaleConverter.class);
private static final Log LOG = LoggerUtil.createLoggerWithContextClassLoader(BaseLocaleConverter.class);

/**
* Checks whether the result of a conversion is conform to the specified target type. If this is the case, the passed in result object is cast to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
import org.apache.commons.beanutils2.DynaBean;
import org.apache.commons.beanutils2.DynaClass;
import org.apache.commons.beanutils2.DynaProperty;
import org.apache.commons.beanutils2.LoggerUtil;
import org.apache.commons.beanutils2.MappedPropertyDescriptor;
import org.apache.commons.beanutils2.PropertyUtilsBean;
import org.apache.commons.beanutils2.expression.Resolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* <p>Utility methods for populating JavaBeans properties
Expand All @@ -56,7 +56,7 @@ protected LocaleBeanUtilsBean initialValue() {
};

/** All logging goes through this logger */
private static final Log LOG = LogFactory.getLog(LocaleBeanUtilsBean.class);
private static final Log LOG = LoggerUtil.createLoggerWithContextClassLoader(LocaleBeanUtilsBean.class);

/**
* Gets singleton instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Map;

import org.apache.commons.beanutils2.ConversionException;
import org.apache.commons.beanutils2.LoggerUtil;
import org.apache.commons.beanutils2.WeakFastHashMap;
import org.apache.commons.beanutils2.locale.converters.BigDecimalLocaleConverter;
import org.apache.commons.beanutils2.locale.converters.BigIntegerLocaleConverter;
Expand All @@ -38,7 +39,6 @@
import org.apache.commons.beanutils2.sql.converters.locale.SqlTimeLocaleConverter;
import org.apache.commons.beanutils2.sql.converters.locale.SqlTimestampLocaleConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* <p>Utility methods for converting locale-sensitive String scalar values to objects of the
Expand Down Expand Up @@ -82,7 +82,7 @@
public class LocaleConvertUtilsBean {

/** The {@code Log} instance for this class. */
private static final Log LOG = LogFactory.getLog(LocaleConvertUtilsBean.class);
private static final Log LOG = LoggerUtil.createLoggerWithContextClassLoader(LocaleConvertUtilsBean.class);

/**
* Gets singleton instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import java.util.Locale;

import org.apache.commons.beanutils2.ConversionException;
import org.apache.commons.beanutils2.LoggerUtil;
import org.apache.commons.beanutils2.locale.BaseLocaleConverter;
import org.apache.commons.beanutils2.locale.LocaleConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a
Expand Down Expand Up @@ -94,7 +94,7 @@ public B setLenient(final boolean lenient) {
private static final String DEFAULT_PATTERN_CHARS = DateLocaleConverter.initDefaultChars();

/** All logging goes through this logger */
private static final Log LOG = LogFactory.getLog(DateLocaleConverter.class);
private static final Log LOG = LoggerUtil.createLoggerWithContextClassLoader(DateLocaleConverter.class);

/**
* Constructs a new builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import java.util.Locale;

import org.apache.commons.beanutils2.ConversionException;
import org.apache.commons.beanutils2.LoggerUtil;
import org.apache.commons.beanutils2.locale.BaseLocaleConverter;
import org.apache.commons.beanutils2.locale.LocaleConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Standard {@link LocaleConverter} implementation that converts an incoming locale-sensitive String into a {@link Number} object, optionally using a
Expand Down Expand Up @@ -62,7 +62,7 @@ public DecimalLocaleConverter<?> get() {
}

/** All logging goes through this logger */
private static final Log LOG = LogFactory.getLog(DecimalLocaleConverter.class);
private static final Log LOG = LoggerUtil.createLoggerWithContextClassLoader(DecimalLocaleConverter.class);

/**
* Constructs a new builder.
Expand Down
Loading

0 comments on commit 2c46e41

Please sign in to comment.