This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new 8916311ca5 Review "Report a bean property value which cannot be 
converted..."
8916311ca5 is described below

commit 8916311ca511c8e14b0ae6f4327bdb00e8191db1
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 78e6e0a0e4..030e525b89 100644
--- a/java/org/apache/naming/factory/LocalStrings.properties
+++ b/java/org/apache/naming/factory/LocalStrings.properties
@@ -19,6 +19,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]

Reply via email to