You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2026/09/29 18:54:50 UTC

(tomcat) 10/13: Fail with a clear BuildException when a JMX string-to-type conversion is impossible instead of sending the raw String to the server

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

rmaucher pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 90a5cc73f854809c4804ca66d113fcbab95b0cc2
Author: opencode <re...@apache.org>
AuthorDate: Tue Sep 29 19:28:23 2026 +0200

    Fail with a clear BuildException when a JMX string-to-type conversion is impossible instead of sending the raw String to the server
---
 .../catalina/ant/jmx/JMXAccessorSetTask.java       |  4 ++
 .../apache/catalina/ant/jmx/JMXAccessorTask.java   | 46 +++++++++-------------
 .../catalina/ant/jmx/TestJMXAccessorTask.java      | 45 +++++++++++++++++++++
 3 files changed, 68 insertions(+), 27 deletions(-)

diff --git a/java/org/apache/catalina/ant/jmx/JMXAccessorSetTask.java b/java/org/apache/catalina/ant/jmx/JMXAccessorSetTask.java
index 96f1bae2f5..2d69b1fd8b 100644
--- a/java/org/apache/catalina/ant/jmx/JMXAccessorSetTask.java
+++ b/java/org/apache/catalina/ant/jmx/JMXAccessorSetTask.java
@@ -179,6 +179,10 @@ public class JMXAccessorSetTask extends JMXAccessorTask {
         } else {
             if (isConvert()) {
                 String mType = getMBeanAttributeType(jmxServerConnection, name, attribute);
+                if (mType == null) {
+                    throw new BuildException(
+                            "Attribute '" + attribute + "' not found on MBean '" + name + "' for set");
+                }
                 realValue = convertStringToType(value, mType);
             } else {
                 realValue = value;
diff --git a/java/org/apache/catalina/ant/jmx/JMXAccessorTask.java b/java/org/apache/catalina/ant/jmx/JMXAccessorTask.java
index 5ec41eb5dc..bcefcda81f 100644
--- a/java/org/apache/catalina/ant/jmx/JMXAccessorTask.java
+++ b/java/org/apache/catalina/ant/jmx/JMXAccessorTask.java
@@ -610,65 +610,57 @@ public class JMXAccessorTask extends BaseRedirectorHelperTask {
      * @param valueType The type
      *
      * @return The converted object
+     *
+     * @throws BuildException The value cannot be converted to the requested type, or the type is not supported
      */
     protected Object convertStringToType(String value, String valueType) {
         if ("java.lang.String".equals(valueType)) {
             return value;
         }
+        if (valueType == null) {
+            throw new BuildException("Unable to convert value '" + value + "': no target type available");
+        }
 
-        Object convertValue = value;
         if ("java.lang.Integer".equals(valueType) || "int".equals(valueType)) {
             try {
-                convertValue = Integer.valueOf(value);
+                return Integer.valueOf(value);
             } catch (NumberFormatException ex) {
-                if (isEcho()) {
-                    handleErrorOutput("Unable to convert to integer:" + value);
-                }
+                throw new BuildException("Unable to convert to integer:" + value, ex);
             }
         } else if ("java.lang.Long".equals(valueType) || "long".equals(valueType)) {
             try {
-                convertValue = Long.valueOf(value);
+                return Long.valueOf(value);
             } catch (NumberFormatException ex) {
-                if (isEcho()) {
-                    handleErrorOutput("Unable to convert to long:" + value);
-                }
+                throw new BuildException("Unable to convert to long:" + value, ex);
             }
         } else if ("java.lang.Boolean".equals(valueType) || "boolean".equals(valueType)) {
-            convertValue = Boolean.valueOf(value);
+            return Boolean.valueOf(value);
         } else if ("java.lang.Float".equals(valueType) || "float".equals(valueType)) {
             try {
-                convertValue = Float.valueOf(value);
+                return Float.valueOf(value);
             } catch (NumberFormatException ex) {
-                if (isEcho()) {
-                    handleErrorOutput("Unable to convert to float:" + value);
-                }
+                throw new BuildException("Unable to convert to float:" + value, ex);
             }
         } else if ("java.lang.Double".equals(valueType) || "double".equals(valueType)) {
             try {
-                convertValue = Double.valueOf(value);
+                return Double.valueOf(value);
             } catch (NumberFormatException ex) {
-                if (isEcho()) {
-                    handleErrorOutput("Unable to convert to double:" + value);
-                }
+                throw new BuildException("Unable to convert to double:" + value, ex);
             }
         } else if ("javax.management.ObjectName".equals(valueType) || "name".equals(valueType)) {
             try {
-                convertValue = new ObjectName(value);
+                return new ObjectName(value);
             } catch (MalformedObjectNameException e) {
-                if (isEcho()) {
-                    handleErrorOutput("Unable to convert to ObjectName:" + value);
-                }
+                throw new BuildException("Unable to convert to ObjectName:" + value, e);
             }
         } else if ("java.net.InetAddress".equals(valueType)) {
             try {
-                convertValue = InetAddress.getByName(value);
+                return InetAddress.getByName(value);
             } catch (UnknownHostException exc) {
-                if (isEcho()) {
-                    handleErrorOutput("Unable to resolve host name:" + value);
-                }
+                throw new BuildException("Unable to resolve host name:" + value, exc);
             }
         }
-        return convertValue;
+        throw new BuildException("Unsupported conversion type '" + valueType + "' for value '" + value + "'");
     }
 
     /**
diff --git a/test/org/apache/catalina/ant/jmx/TestJMXAccessorTask.java b/test/org/apache/catalina/ant/jmx/TestJMXAccessorTask.java
index 3531a52ac6..b7104604d7 100644
--- a/test/org/apache/catalina/ant/jmx/TestJMXAccessorTask.java
+++ b/test/org/apache/catalina/ant/jmx/TestJMXAccessorTask.java
@@ -27,6 +27,8 @@ import javax.management.openmbean.TabularType;
 import org.junit.Assert;
 import org.junit.Test;
 
+import org.apache.tools.ant.BuildException;
+
 public class TestJMXAccessorTask {
 
     @Test
@@ -44,6 +46,49 @@ public class TestJMXAccessorTask {
     }
 
 
+    @Test
+    public void testConvertStringToType() {
+        JMXAccessorTask task = new JMXAccessorTask();
+
+        Assert.assertEquals("x", task.convertStringToType("x", "java.lang.String"));
+        Assert.assertEquals(Integer.valueOf(7), task.convertStringToType("7", "int"));
+        Assert.assertEquals(Long.valueOf(7), task.convertStringToType("7", "java.lang.Long"));
+        Assert.assertEquals(Boolean.TRUE, task.convertStringToType("true", "boolean"));
+        Assert.assertEquals(Double.valueOf(1.5), task.convertStringToType("1.5", "double"));
+    }
+
+
+    @Test
+    public void testConvertStringToTypeInvalid() {
+        JMXAccessorTask task = new JMXAccessorTask();
+
+        assertConvertFails(task, "x", "int");
+        assertConvertFails(task, "x", "java.lang.Long");
+        assertConvertFails(task, "1.5", "java.lang.Integer");
+        assertConvertFails(task, "x", "javax.management.ObjectName");
+    }
+
+
+    @Test
+    public void testConvertStringToTypeUnsupportedType() {
+        JMXAccessorTask task = new JMXAccessorTask();
+
+        assertConvertFails(task, "x", "java.lang.Int");
+        assertConvertFails(task, "x", "java.util.List");
+        assertConvertFails(task, "x", null);
+    }
+
+
+    private static void assertConvertFails(JMXAccessorTask task, String value, String type) {
+        try {
+            task.convertStringToType(value, type);
+            Assert.fail("Expected a BuildException for value '" + value + "' and type '" + type + "'");
+        } catch (BuildException expected) {
+            // Expected
+        }
+    }
+
+
     private static TabularDataSupport createTabularData() throws OpenDataException {
         CompositeType detailsType = new CompositeType("details", "details", new String[] { "name" },
                 new String[] { "name" }, new OpenType<?>[] { SimpleType.STRING });


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org