This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/11.0.x by this push:
new 43854c7595 Review "Report a bean property value which cannot be
converted..."
43854c7595 is described below
commit 43854c75958ebc717bec391914e8b0f627d2c50d
Author: Mark Thomas <[email protected]>
AuthorDate: Fri Nov 6 17:51:32 2026 +0000
Review "Report a bean property value which cannot be converted..."
Improve error logging for bean setter failure
---
java/org/apache/naming/factory/BeanFactory.java | 15 +++++++++++++-
.../apache/naming/factory/LocalStrings.properties | 1 +
.../org/apache/naming/factory/TestBeanFactory.java | 23 ++++++++++++++++++++++
3 files changed, 38 insertions(+), 1 deletion(-)
diff --git a/java/org/apache/naming/factory/BeanFactory.java
b/java/org/apache/naming/factory/BeanFactory.java
index 54e34f5b3c..5f7b36ab7a 100644
--- a/java/org/apache/naming/factory/BeanFactory.java
+++ b/java/org/apache/naming/factory/BeanFactory.java
@@ -19,6 +19,7 @@ package org.apache.naming.factory;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Hashtable;
@@ -34,6 +35,7 @@ import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.naming.ResourceRef;
import org.apache.naming.StringManager;
+import org.apache.tomcat.util.ExceptionUtils;
/**
* Object factory for any Resource conforming to the JavaBean spec.
@@ -188,7 +190,18 @@ public class BeanFactory implements ObjectFactory {
}
if (setProp != null) {
- setProp.invoke(bean, valueArray);
+ try {
+ setProp.invoke(bean, valueArray);
+ } catch (InvocationTargetException ite) {
+ ExceptionUtils.handleThrowable(ite);
+ // The message of the wrapper is always
null, so name the property and report
+ // the exception thrown by the set method
as the cause.
+ Throwable cause = ite.getCause() != null ?
ite.getCause() : ite;
+ NamingException ne = new NamingException(
+
sm.getString("beanFactory.setFailed", propName, cause.toString()));
+ ne.initCause(cause);
+ throw ne;
+ }
} else {
throw new
NamingException(sm.getString("beanFactory.readOnlyProperty", propName));
}
diff --git a/java/org/apache/naming/factory/LocalStrings.properties
b/java/org/apache/naming/factory/LocalStrings.properties
index b46da5b2b8..3fae028197 100644
--- a/java/org/apache/naming/factory/LocalStrings.properties
+++ b/java/org/apache/naming/factory/LocalStrings.properties
@@ -22,6 +22,7 @@ beanFactory.noForceString=The forceString option has been
removed as a security
beanFactory.noSetMethod=No set method found for property [{0}]
beanFactory.noStringConversion=String conversion for property [{0}] of type
[{1}] not available
beanFactory.readOnlyProperty=Write not allowed for property [{0}]
+beanFactory.setFailed=Setting the property [{0}] failed: [{1}]
dataSourceLinkFactory.badWrapper=Not a wrapper for type [{0}]
diff --git a/test/org/apache/naming/factory/TestBeanFactory.java
b/test/org/apache/naming/factory/TestBeanFactory.java
index ceb2211a7b..2821cb6815 100644
--- a/test/org/apache/naming/factory/TestBeanFactory.java
+++ b/test/org/apache/naming/factory/TestBeanFactory.java
@@ -142,4 +142,27 @@ public class TestBeanFactory {
e.getCause() instanceof StringIndexOutOfBoundsException);
}
}
+
+
+ /*
+ * A set method which rejects the value has to be reported with the name of
+ * the property, which the exception of the reflective call does not carry.
+ */
+ @Test
+ public void testSetMethodFailureNamesTheProperty() throws Exception {
+
+ ResourceRef resourceRef = new ResourceRef(TesterBean.class.getName(),
null, null, null, false);
+ resourceRef.add(new StringRefAddr("server", "no such host.invalid"));
+
+ BeanFactory factory = new BeanFactory();
+
+ try {
+ Object obj = factory.getObjectInstance(resourceRef, null, null,
null);
+ Assert.fail("The invalid value was accepted: " + obj);
+ } catch (NamingException e) {
+ Assert.assertTrue("The property is not named: " + e.getMessage(),
e.getMessage().contains("server"));
+ Assert.assertTrue("The reason for the failure is not reported",
+ e.getCause() instanceof IllegalArgumentException);
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]