Skip to content

Commit ea2db65

Browse files
committed
Fix attribute notification listener removal
1 parent 6a24ef8 commit ea2db65

6 files changed

Lines changed: 198 additions & 24 deletions

File tree

‎TODO.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,16 @@ These are bugs, correctness issues, or missing functionality that may affect pro
9696

9797
---
9898

99-
### 7. ManagedBean / BaseModelMBean Issues (3 items)
99+
### 7. ManagedBean Issues (2 items)
100100

101101
| # | File:Line | Description | Fix Idea | Effort | Difficulty |
102102
|---|-----------|-------------|----------|--------|------------|
103-
| 7.1 | `BaseModelMBean.java:520` | `removeAttributeChangeNotificationListener` removes ALL notifications for a listener | Track per-attribute listener registrations. Only remove the listener for the specified attribute name. | 1 day | Medium |
104103
| 7.2 | `ManagedBean.java:606` | Method signature from `opInfo` not used for reflection lookup | Use the signature from `opInfo` to locate the method, falling back to the MBean descriptor signature. | 0.5-1 day | Low |
105104
| 7.3 | `ManagedBean.java:614` | Methods declared in superinterfaces not found by reflection | Walk the class hierarchy and all implemented interfaces when searching for the method. | 0.5-1 day | Low |
106105

107-
**Summary:** JMX MBean infrastructure issues. 7.1 is a behavioral bug; 7.2 and 7.3 are limitations.
106+
**Summary:** JMX MBean infrastructure limitations.
108107

109-
**Total estimated effort: 2-3 days, Low-Medium difficulty**
108+
**Total estimated effort: 1-2 days, Low difficulty**
110109

111110
---
112111

‎java/org/apache/tomcat/util/modeler/BaseAttributeFilter.java‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public class BaseAttributeFilter implements NotificationFilter {
4949
public BaseAttributeFilter(String name) {
5050

5151
super();
52-
if (name != null) {
52+
if (name == null) {
53+
acceptAll = true;
54+
} else {
5355
addAttribute(name);
5456
}
5557

@@ -60,11 +62,15 @@ public BaseAttributeFilter(String name) {
6062

6163

6264
/**
63-
* The set of attribute names that are accepted by this filter. If this list is empty, all attribute names are
64-
* accepted.
65+
* The set of attribute names that are accepted or excluded by this filter, depending on {@link #acceptAll}.
6566
*/
6667
private final Set<String> names = new HashSet<>();
6768

69+
/**
70+
* {@code true} if all attribute names except those in {@link #names} are accepted.
71+
*/
72+
private boolean acceptAll = false;
73+
6874

6975
// --------------------------------------------------------- Public Methods
7076

@@ -77,7 +83,11 @@ public BaseAttributeFilter(String name) {
7783
public void addAttribute(String name) {
7884

7985
synchronized (names) {
80-
names.add(name);
86+
if (acceptAll) {
87+
names.remove(name);
88+
} else {
89+
names.add(name);
90+
}
8191
}
8292

8393
}
@@ -89,21 +99,25 @@ public void addAttribute(String name) {
8999
public void clear() {
90100

91101
synchronized (names) {
102+
acceptAll = true;
92103
names.clear();
93104
}
94105

95106
}
96107

97108

98109
/**
99-
* Return the set of names that are accepted by this filter. If this filter accepts all attribute names, a zero
100-
* length array will be returned.
110+
* Return the set of names that are explicitly accepted by this filter. If this filter is in accept-all mode,
111+
* including when individual names have been excluded, a zero length array will be returned.
101112
*
102113
* @return the array of names
103114
*/
104115
public String[] getNames() {
105116

106117
synchronized (names) {
118+
if (acceptAll) {
119+
return new String[0];
120+
}
107121
return names.toArray(new String[0]);
108122
}
109123

@@ -133,11 +147,7 @@ public boolean isNotificationEnabled(Notification notification) {
133147
return false;
134148
}
135149
synchronized (names) {
136-
if (names.isEmpty()) {
137-
return true;
138-
} else {
139-
return names.contains(acn.getAttributeName());
140-
}
150+
return acceptAll != names.contains(acn.getAttributeName());
141151
}
142152

143153
}
@@ -151,9 +161,27 @@ public boolean isNotificationEnabled(Notification notification) {
151161
public void removeAttribute(String name) {
152162

153163
synchronized (names) {
154-
names.remove(name);
164+
if (acceptAll) {
165+
names.add(name);
166+
} else {
167+
names.remove(name);
168+
}
169+
}
170+
171+
}
172+
173+
174+
boolean isAttributeEnabled(String name) {
175+
synchronized (names) {
176+
return acceptAll != names.contains(name);
155177
}
178+
}
179+
156180

181+
boolean isEmpty() {
182+
synchronized (names) {
183+
return !acceptAll && names.isEmpty();
184+
}
157185
}
158186

159187

‎java/org/apache/tomcat/util/modeler/BaseModelMBean.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,10 @@ public void removeAttributeChangeNotificationListener(NotificationListener liste
517517
throw new IllegalArgumentException(sm.getString("baseModelMBean.nullListener"));
518518
}
519519

520-
// FIXME - currently this removes *all* notifications for this listener
521-
if (attributeBroadcaster != null) {
522-
attributeBroadcaster.removeNotificationListener(listener);
520+
if (attributeBroadcaster == null) {
521+
throw new ListenerNotFoundException();
523522
}
523+
attributeBroadcaster.removeAttributeNotificationListener(listener, name);
524524

525525
}
526526

‎java/org/apache/tomcat/util/modeler/BaseNotificationBroadcaster.java‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
import java.util.ArrayList;
21+
import java.util.Iterator;
2122

2223
import javax.management.ListenerNotFoundException;
2324
import javax.management.MBeanNotificationInfo;
@@ -76,14 +77,11 @@ public void addNotificationListener(NotificationListener listener, NotificationF
7677
if ((item.listener == listener) && (item.filter instanceof BaseAttributeFilter oldFilter) &&
7778
(item.handback == handback)) {
7879
String[] newNames = newFilter.getNames();
79-
String[] oldNames = oldFilter.getNames();
8080
if (newNames.length == 0) {
8181
oldFilter.clear();
8282
} else {
83-
if (oldNames.length != 0) {
84-
for (String newName : newNames) {
85-
oldFilter.addAttribute(newName);
86-
}
83+
for (String newName : newNames) {
84+
oldFilter.addAttribute(newName);
8785
}
8886
}
8987
return;
@@ -124,6 +122,42 @@ public void removeNotificationListener(NotificationListener listener) throws Lis
124122
}
125123

126124

125+
/**
126+
* Remove an attribute change notification listener for the given attribute.
127+
*
128+
* @param listener Listener to remove
129+
* @param name Attribute name, or {@code null} to remove all attribute change registrations for the listener
130+
*
131+
* @throws ListenerNotFoundException if there is no matching registration
132+
*/
133+
void removeAttributeNotificationListener(NotificationListener listener, String name)
134+
throws ListenerNotFoundException {
135+
136+
boolean found = false;
137+
synchronized (entries) {
138+
Iterator<BaseNotificationBroadcasterEntry> iterator = entries.iterator();
139+
while (iterator.hasNext()) {
140+
BaseNotificationBroadcasterEntry item = iterator.next();
141+
if (item.listener == listener && item.filter instanceof BaseAttributeFilter filter &&
142+
(name == null || filter.isAttributeEnabled(name))) {
143+
found = true;
144+
if (name == null) {
145+
iterator.remove();
146+
} else {
147+
filter.removeAttribute(name);
148+
if (filter.isEmpty()) {
149+
iterator.remove();
150+
}
151+
}
152+
}
153+
}
154+
}
155+
if (!found) {
156+
throw new ListenerNotFoundException();
157+
}
158+
}
159+
160+
127161
/**
128162
* Send the specified notification to all interested listeners.
129163
*
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.tomcat.util.modeler;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import javax.management.Attribute;
23+
import javax.management.AttributeChangeNotification;
24+
import javax.management.ListenerNotFoundException;
25+
import javax.management.NotificationListener;
26+
27+
import org.junit.Assert;
28+
import org.junit.Test;
29+
30+
public class TestBaseModelMBean {
31+
32+
@Test
33+
public void testRemoveAttributeChangeNotificationListener() throws Exception {
34+
BaseModelMBean mbean = new BaseModelMBean();
35+
List<String> attributes = new ArrayList<>();
36+
NotificationListener listener = (notification, handback) -> attributes.add(
37+
((AttributeChangeNotification) notification).getAttributeName());
38+
39+
mbean.addAttributeChangeNotificationListener(listener, "first", null);
40+
mbean.addAttributeChangeNotificationListener(listener, "second", null);
41+
mbean.removeAttributeChangeNotificationListener(listener, "first");
42+
43+
sendAttributeChangeNotification(mbean, "first");
44+
sendAttributeChangeNotification(mbean, "second");
45+
46+
Assert.assertEquals(List.of("second"), attributes);
47+
48+
attributes.clear();
49+
mbean.removeAttributeChangeNotificationListener(listener, "second");
50+
sendAttributeChangeNotification(mbean, "second");
51+
Assert.assertTrue(attributes.isEmpty());
52+
}
53+
54+
55+
@Test
56+
public void testRemoveAttributeFromListenerForAllAttributes() throws Exception {
57+
BaseModelMBean mbean = new BaseModelMBean();
58+
List<String> attributes = new ArrayList<>();
59+
NotificationListener listener = (notification, handback) -> attributes.add(
60+
((AttributeChangeNotification) notification).getAttributeName());
61+
62+
mbean.addAttributeChangeNotificationListener(listener, null, null);
63+
mbean.removeAttributeChangeNotificationListener(listener, "first");
64+
65+
sendAttributeChangeNotification(mbean, "first");
66+
sendAttributeChangeNotification(mbean, "second");
67+
68+
Assert.assertEquals(List.of("second"), attributes);
69+
70+
attributes.clear();
71+
mbean.addAttributeChangeNotificationListener(listener, "first", null);
72+
sendAttributeChangeNotification(mbean, "first");
73+
Assert.assertEquals(List.of("first"), attributes);
74+
}
75+
76+
77+
@Test
78+
public void testRemoveAttributeChangeRegistrationPreservesGeneralRegistration() throws Exception {
79+
BaseModelMBean mbean = new BaseModelMBean();
80+
List<String> attributes = new ArrayList<>();
81+
NotificationListener listener = (notification, handback) -> attributes.add(
82+
((AttributeChangeNotification) notification).getAttributeName());
83+
84+
mbean.addNotificationListener(listener, null, null);
85+
mbean.addAttributeChangeNotificationListener(listener, "first", null);
86+
mbean.removeAttributeChangeNotificationListener(listener, "first");
87+
sendAttributeChangeNotification(mbean, "first");
88+
89+
Assert.assertEquals(List.of("first"), attributes);
90+
}
91+
92+
93+
@Test
94+
public void testRemoveUnknownAttributeChangeNotificationListener() throws Exception {
95+
BaseModelMBean mbean = new BaseModelMBean();
96+
NotificationListener listener = (notification, handback) -> {
97+
// NO-OP
98+
};
99+
100+
Assert.assertThrows(ListenerNotFoundException.class,
101+
() -> mbean.removeAttributeChangeNotificationListener(listener, "first"));
102+
}
103+
104+
105+
private static void sendAttributeChangeNotification(BaseModelMBean mbean, String name) throws Exception {
106+
mbean.sendAttributeChangeNotification(new Attribute(name, "old"), new Attribute(name, "new"));
107+
}
108+
}

‎webapps/docs/changelog.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<update>
118118
The minimum Java version has been updated to Java 21. (markt)
119119
</update>
120+
<fix>
121+
Ensure that removing an attribute change notification listener from a
122+
<code>BaseModelMBean</code> only removes the registration for the
123+
requested attribute. (sainadh777)
124+
</fix>
120125
</changelog>
121126
</subsection>
122127
<subsection name="Catalina">

0 commit comments

Comments
 (0)