This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch 11.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 0ac94dda10e2a0de77877735d7faa69ab5bacb1a Author: opencode <[email protected]> AuthorDate: Thu Oct 15 11:45:40 2026 +0200 Convert numeric and character values in the JMX proxy conversion helper Registry.convertValue() only mapped String, ObjectName, int/Integer, long/Long and boolean/Boolean type names. Every other type name fell through and the raw String was returned. Its sole consumer, the JMX proxy servlet of the Manager application, passes the returned value directly to MBeanServer.setAttribute() and invoke(), and the modeler MBeans apply no conversion of their own, so setting an attribute or invoking an operation with a double, float, short, byte or char parameter on any modeler MBean always failed with an argument type mismatch. Convert the missing floating point and narrow integral types using the wrapper valueOf methods, giving the same decimal range and format checking the existing integral branches already provide, and convert the first character of a non-empty string for the character types. A null or empty string is left unconverted rather than introducing a new exception path. --- java/org/apache/tomcat/util/modeler/Registry.java | 12 +++ .../util/modeler/TestRegistryConvertValue.java | 100 +++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/java/org/apache/tomcat/util/modeler/Registry.java b/java/org/apache/tomcat/util/modeler/Registry.java index f74885498f..2bae09dfd6 100644 --- a/java/org/apache/tomcat/util/modeler/Registry.java +++ b/java/org/apache/tomcat/util/modeler/Registry.java @@ -551,6 +551,18 @@ public class Registry implements RegistryMBean, MBeanRegistration { objValue = Long.valueOf(value); } else if ("java.lang.Boolean".equals(type) || "boolean".equals(type)) { objValue = Boolean.valueOf(value); + } else if ("java.lang.Double".equals(type) || "double".equals(type)) { + objValue = Double.valueOf(value); + } else if ("java.lang.Float".equals(type) || "float".equals(type)) { + objValue = Float.valueOf(value); + } else if ("java.lang.Short".equals(type) || "short".equals(type)) { + objValue = Short.valueOf(value); + } else if ("java.lang.Byte".equals(type) || "byte".equals(type)) { + objValue = Byte.valueOf(value); + } else if (("java.lang.Character".equals(type) || "char".equals(type)) && + value != null && !value.isEmpty()) { + // A null or empty string cannot be converted to a character + objValue = Character.valueOf(value.charAt(0)); } return objValue; } diff --git a/test/org/apache/tomcat/util/modeler/TestRegistryConvertValue.java b/test/org/apache/tomcat/util/modeler/TestRegistryConvertValue.java new file mode 100644 index 0000000000..bf3ffce1fb --- /dev/null +++ b/test/org/apache/tomcat/util/modeler/TestRegistryConvertValue.java @@ -0,0 +1,100 @@ +/* + * 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.tomcat.util.modeler; + +import org.junit.Assert; +import org.junit.Test; + +public class TestRegistryConvertValue { + + private final Registry registry = new Registry(); + + @Test + public void testConvertStringTypes() { + Assert.assertEquals("value", registry.convertValue(null, "value")); + Assert.assertEquals("value", registry.convertValue("java.lang.String", "value")); + } + + @Test + public void testConvertIntegerTypes() { + Assert.assertEquals(Integer.valueOf(7), registry.convertValue("int", "7")); + Assert.assertEquals(Integer.valueOf(7), registry.convertValue("java.lang.Integer", "7")); + Assert.assertEquals(Long.valueOf(7), registry.convertValue("long", "7")); + Assert.assertEquals(Long.valueOf(7), registry.convertValue("java.lang.Long", "7")); + Assert.assertEquals(Short.valueOf((short) 7), registry.convertValue("short", "7")); + Assert.assertEquals(Short.valueOf((short) 7), registry.convertValue("java.lang.Short", "7")); + Assert.assertEquals(Byte.valueOf((byte) 7), registry.convertValue("byte", "7")); + Assert.assertEquals(Byte.valueOf((byte) 7), registry.convertValue("java.lang.Byte", "7")); + } + + @Test + public void testConvertFloatingPointTypes() { + Assert.assertEquals(Double.valueOf(1.5), registry.convertValue("double", "1.5")); + Assert.assertEquals(Double.valueOf(1.5), registry.convertValue("java.lang.Double", "1.5")); + Assert.assertEquals(Float.valueOf(1.5f), registry.convertValue("float", "1.5")); + Assert.assertEquals(Float.valueOf(1.5f), registry.convertValue("java.lang.Float", "1.5")); + } + + @Test + public void testConvertBooleanType() { + Assert.assertEquals(Boolean.TRUE, registry.convertValue("boolean", "true")); + Assert.assertEquals(Boolean.TRUE, registry.convertValue("java.lang.Boolean", "true")); + } + + @Test + public void testConvertCharacterType() { + Assert.assertEquals(Character.valueOf('x'), registry.convertValue("char", "x")); + Assert.assertEquals(Character.valueOf('x'), registry.convertValue("java.lang.Character", "x")); + } + + @Test + public void testConvertEmptyStringToCharacterIsNotConverted() { + // An empty string has no character to convert to + Object result = registry.convertValue("char", ""); + Assert.assertEquals("", result); + } + + @Test + public void testConvertCharacterTypeTruncatesLongerString() { + // The first character is the only one that can be converted + Assert.assertEquals(Character.valueOf('x'), registry.convertValue("char", "xy")); + } + + @Test + public void testConvertNullValueIsNotConvertedForCharacter() { + // Only the character type leaves a null value unconverted rather than + // throwing, matching the historical behavior for this branch + Assert.assertNull(registry.convertValue("char", null)); + } + + @Test + public void testConvertNumericBoundaries() { + Assert.assertEquals(Short.valueOf(Short.MAX_VALUE), + registry.convertValue("short", "32767")); + Assert.assertEquals(Byte.valueOf(Byte.MAX_VALUE), + registry.convertValue("byte", "127")); + } + + @Test + public void testConvertInvalidNumberThrows() { + Assert.assertThrows(NumberFormatException.class, () -> registry.convertValue("int", "abc")); + Assert.assertThrows(NumberFormatException.class, () -> registry.convertValue("double", "abc")); + Assert.assertThrows(NumberFormatException.class, () -> registry.convertValue("float", "abc")); + Assert.assertThrows(NumberFormatException.class, () -> registry.convertValue("short", "70000")); + Assert.assertThrows(NumberFormatException.class, () -> registry.convertValue("byte", "300")); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
