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 85a4a3d967743f76e365ddff90f0e8ffc54cdb50 Author: opencode <[email protected]> AuthorDate: Tue Sep 29 19:32:05 2026 +0200 Validate the operation and type in the JMX condition and report non-numeric values as a BuildException instead of an uncaught NumberFormatException or a silent false --- .../catalina/ant/jmx/JMXAccessorCondition.java | 81 ++++++++++++++-------- .../catalina/ant/jmx/TestJMXAccessorCondition.java | 62 +++++++++++++++++ 2 files changed, 116 insertions(+), 27 deletions(-) diff --git a/java/org/apache/catalina/ant/jmx/JMXAccessorCondition.java b/java/org/apache/catalina/ant/jmx/JMXAccessorCondition.java index b09a323546..56415ebeec 100644 --- a/java/org/apache/catalina/ant/jmx/JMXAccessorCondition.java +++ b/java/org/apache/catalina/ant/jmx/JMXAccessorCondition.java @@ -212,38 +212,65 @@ public class JMXAccessorCondition extends JMXAccessorConditionBase { throw new BuildException("Must specify an MBean name and attribute for condition"); } if (testIfCondition() && testUnlessCondition()) { + String op = getOperation(); + boolean equals = "==".equals(op); + boolean notEquals = "!=".equals(op); + boolean greater = ">".equals(op); + boolean greaterOrEquals = ">=".equals(op); + boolean lesser = "<".equals(op); + boolean lesserOrEquals = "<=".equals(op); + if (!equals && !notEquals && !greater && !greaterOrEquals && !lesser && !lesserOrEquals) { + throw new BuildException("Unsupported operation '" + op + + "'. Supported operations are ==, !=, >, >=, < and <=."); + } + boolean relational = !equals && !notEquals; + if (relational && !"long".equals(type) && !"double".equals(type)) { + throw new BuildException("Unsupported type '" + type + "' for operation '" + op + + "'. Supported types for relational operations are long and double."); + } String jmxValue = accessJMXValue(); if (jmxValue != null) { - String op = getOperation(); - if ("==".equals(op)) { + if (equals) { return jmxValue.equals(value); - } else if ("!=".equals(op)) { + } else if (notEquals) { return !jmxValue.equals(value); + } else if ("double".equals(type)) { + double jvalue; + double dvalue; + try { + jvalue = Double.parseDouble(jmxValue); + dvalue = Double.parseDouble(value); + } catch (NumberFormatException e) { + throw new BuildException("Unable to compare the JMX attribute value '" + jmxValue + + "' with the expected value '" + value + "' as a double", e); + } + if (greater) { + return jvalue > dvalue; + } else if (greaterOrEquals) { + return jvalue >= dvalue; + } else if (lesser) { + return jvalue < dvalue; + } else { + return jvalue <= dvalue; + } } else { - if ("long".equals(type)) { - long jvalue = Long.parseLong(jmxValue); - long lvalue = Long.parseLong(value); - if (">".equals(op)) { - return jvalue > lvalue; - } else if (">=".equals(op)) { - return jvalue >= lvalue; - } else if ("<".equals(op)) { - return jvalue < lvalue; - } else if ("<=".equals(op)) { - return jvalue <= lvalue; - } - } else if ("double".equals(type)) { - double jvalue = Double.parseDouble(jmxValue); - double dvalue = Double.parseDouble(value); - if (">".equals(op)) { - return jvalue > dvalue; - } else if (">=".equals(op)) { - return jvalue >= dvalue; - } else if ("<".equals(op)) { - return jvalue < dvalue; - } else if ("<=".equals(op)) { - return jvalue <= dvalue; - } + long jvalue; + long lvalue; + try { + jvalue = Long.parseLong(jmxValue); + lvalue = Long.parseLong(value); + } catch (NumberFormatException e) { + throw new BuildException("Unable to compare the JMX attribute value '" + jmxValue + + "' with the expected value '" + value + "' as a long", e); + } + if (greater) { + return jvalue > lvalue; + } else if (greaterOrEquals) { + return jvalue >= lvalue; + } else if (lesser) { + return jvalue < lvalue; + } else { + return jvalue <= lvalue; } } } diff --git a/test/org/apache/catalina/ant/jmx/TestJMXAccessorCondition.java b/test/org/apache/catalina/ant/jmx/TestJMXAccessorCondition.java new file mode 100644 index 0000000000..bdf02c8cfe --- /dev/null +++ b/test/org/apache/catalina/ant/jmx/TestJMXAccessorCondition.java @@ -0,0 +1,62 @@ +/* + * 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.catalina.ant.jmx; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.tools.ant.BuildException; + +public class TestJMXAccessorCondition { + + @Test + public void testEvalUnsupportedOperation() { + JMXAccessorCondition condition = createCondition(); + condition.setOperation(">>="); + + try { + condition.eval(); + Assert.fail("Expected a BuildException for an unsupported operation"); + } catch (BuildException expected) { + // Expected + } + } + + + @Test + public void testEvalUnsupportedTypeForRelationalOperation() { + JMXAccessorCondition condition = createCondition(); + condition.setOperation("<"); + condition.setType("string"); + + try { + condition.eval(); + Assert.fail("Expected a BuildException for an unsupported type"); + } catch (BuildException expected) { + // Expected + } + } + + + private static JMXAccessorCondition createCondition() { + JMXAccessorCondition condition = new JMXAccessorCondition(); + condition.setName("Catalina:type=Test"); + condition.setAttribute("startupTime"); + condition.setValue("250"); + return condition; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
