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/10/15 19:45:11 UTC
(tomcat) 08/25: Invalidate the cached MBeanInfo when attributes or operations are added
This is an automated email from the ASF dual-hosted git repository.
rmaucher pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 0bbbc8e7addabdbf8ab9935d90e5258799b33efe
Author: opencode <re...@apache.org>
AuthorDate: Thu Oct 15 10:49:05 2026 +0200
Invalidate the cached MBeanInfo when attributes or operations are added
ManagedBean caches the MBeanInfo it builds and every other mutator
(setName, setClassName, setDescription, setType, addNotification)
nulls the cache under the mBeanInfoLock write lock, but addAttribute
and addOperation did neither. Adding an attribute or operation after
getMBeanInfo() had been called would therefore keep serving a stale
MBeanInfo, and writing the descriptor maps without the lock was also
inconsistent with getMBeanInfo() reading them under the write lock.
Make both methods mirror addNotification: hold the write lock, update
the map, and clear the cached info. Add a unit test that verifies the
MBeanInfo is rebuilt and contains the newly added descriptor.
---
.../apache/tomcat/util/modeler/ManagedBean.java | 16 +++-
.../tomcat/util/modeler/TestManagedBean.java | 89 ++++++++++++++++++++++
2 files changed, 103 insertions(+), 2 deletions(-)
diff --git a/java/org/apache/tomcat/util/modeler/ManagedBean.java b/java/org/apache/tomcat/util/modeler/ManagedBean.java
index 1d36acfa21..ecd1d5408a 100644
--- a/java/org/apache/tomcat/util/modeler/ManagedBean.java
+++ b/java/org/apache/tomcat/util/modeler/ManagedBean.java
@@ -300,7 +300,13 @@ public class ManagedBean implements java.io.Serializable {
* @param attribute The new attribute descriptor
*/
public void addAttribute(AttributeInfo attribute) {
- attributes.put(attribute.getName(), attribute);
+ mBeanInfoLock.writeLock().lock();
+ try {
+ attributes.put(attribute.getName(), attribute);
+ this.info = null;
+ } finally {
+ mBeanInfoLock.writeLock().unlock();
+ }
}
@@ -329,7 +335,13 @@ public class ManagedBean implements java.io.Serializable {
* @param operation The new operation descriptor
*/
public void addOperation(OperationInfo operation) {
- operations.put(createOperationKey(operation), operation);
+ mBeanInfoLock.writeLock().lock();
+ try {
+ operations.put(createOperationKey(operation), operation);
+ this.info = null;
+ } finally {
+ mBeanInfoLock.writeLock().unlock();
+ }
}
diff --git a/test/org/apache/tomcat/util/modeler/TestManagedBean.java b/test/org/apache/tomcat/util/modeler/TestManagedBean.java
new file mode 100644
index 0000000000..1ff7bd0305
--- /dev/null
+++ b/test/org/apache/tomcat/util/modeler/TestManagedBean.java
@@ -0,0 +1,89 @@
+/*
+ * 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 javax.management.MBeanAttributeInfo;
+import javax.management.MBeanInfo;
+import javax.management.MBeanOperationInfo;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestManagedBean {
+
+ @Test
+ public void testAddAttributeInvalidatesCachedMBeanInfo() {
+ ManagedBean managedBean = new ManagedBean();
+ managedBean.setName("Tester");
+ managedBean.setType("Tester");
+
+ AttributeInfo first = new AttributeInfo();
+ first.setName("first");
+ first.setDescription("First attribute");
+ first.setType("java.lang.String");
+ managedBean.addAttribute(first);
+
+ MBeanInfo info = managedBean.getMBeanInfo();
+ int baseCount = info.getAttributes().length;
+
+ AttributeInfo second = new AttributeInfo();
+ second.setName("second");
+ second.setDescription("Second attribute");
+ second.setType("java.lang.String");
+ managedBean.addAttribute(second);
+
+ MBeanInfo updated = managedBean.getMBeanInfo();
+ Assert.assertNotSame(info, updated);
+ Assert.assertEquals(baseCount + 1, updated.getAttributes().length);
+ boolean found = false;
+ for (MBeanAttributeInfo attributeInfo : updated.getAttributes()) {
+ found |= "second".equals(attributeInfo.getName());
+ }
+ Assert.assertTrue(found);
+ }
+
+ @Test
+ public void testAddOperationInvalidatesCachedMBeanInfo() {
+ ManagedBean managedBean = new ManagedBean();
+ managedBean.setName("Tester");
+ managedBean.setType("Tester");
+
+ OperationInfo first = new OperationInfo();
+ first.setName("first");
+ first.setDescription("First operation");
+ first.setReturnType("void");
+ managedBean.addOperation(first);
+
+ MBeanInfo info = managedBean.getMBeanInfo();
+ Assert.assertEquals(1, info.getOperations().length);
+
+ OperationInfo second = new OperationInfo();
+ second.setName("second");
+ second.setDescription("Second operation");
+ second.setReturnType("void");
+ managedBean.addOperation(second);
+
+ MBeanInfo updated = managedBean.getMBeanInfo();
+ Assert.assertNotSame(info, updated);
+ Assert.assertEquals(2, updated.getOperations().length);
+ boolean found = false;
+ for (MBeanOperationInfo operationInfo : updated.getOperations()) {
+ found |= "second".equals(operationInfo.getName());
+ }
+ Assert.assertTrue(found);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org