Skip to content

Commit

Permalink
OAK-9417 : ClassCastException in ConsolidatedListenerMBeanImpl (patch…
Browse files Browse the repository at this point in the history
… provided by Carsten Ziegeler)

git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/oak/trunk@1889232 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
anchela committed Apr 27, 2021
1 parent 77f243b commit 3352d1a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
Expand Down Expand Up @@ -186,7 +187,7 @@ private Collection<BackgroundObserverMBean> collectNonJcrObservers() {
List<BackgroundObserverMBean> observers = Lists.newArrayList();

for (Map.Entry<ObjectName, BackgroundObserverMBean> o : bgObservers.entrySet()){
String listenerId = getListenerId(o.getKey());
String listenerId = getListenerId(o.getKey());
if (listenerId == null){
observers.add(o.getValue());
}
Expand Down Expand Up @@ -271,12 +272,12 @@ protected void unbindBackgroundObserverMBean(BackgroundObserverMBean mbean, Map<

@SuppressWarnings("unused")
protected void bindChangeProcessorMBean(ChangeProcessorMBean mbean, Map<String, ?> config){
changeProcessors.put(getObjectName(config), mbean);
changeProcessors.put(getObjectName(config), mbean);
}

@SuppressWarnings("unused")
protected void unbindChangeProcessorMBean(ChangeProcessorMBean mbean, Map<String, ?> config){
changeProcessors.remove(getObjectName(config));
changeProcessors.remove(getObjectName(config));
}

@SuppressWarnings("unused")
Expand All @@ -299,9 +300,19 @@ protected void unbindFilterConfigMBean(FilterConfigMBean mbean, Map<String, ?> c
filterConfigs.remove(getObjectName(config));
}

private static ObjectName getObjectName(Map<String, ?> config){
return checkNotNull((ObjectName) config.get("jmx.objectname"),
"No 'jmx.objectname' property defined for MBean %s", config);
static ObjectName getObjectName(Map<String, ?> config) {
Object value = config.get("jmx.objectname");
ObjectName name = null;
if (value instanceof String) {
try {
name = new ObjectName((String) value);
} catch (MalformedObjectNameException e) {
// ignore name will be null in this case
}
} else if (value instanceof ObjectName) {
name = (ObjectName) value;
}
return checkNotNull(name, "No 'jmx.objectname' property defined for MBean %s", config);
}

private static String getListenerId(ObjectName name){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
*/
package org.apache.jackrabbit.oak.jcr.observation;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -50,6 +53,32 @@ public void testListenerStatsData() throws MalformedObjectNameException {
assertNotNull(data);
}

@Test(expected = NullPointerException.class)
public void testGetConfigNameNoValue() {
final Map<String, Object> properties = Collections.emptyMap();
ConsolidatedListenerMBeanImpl.getObjectName(properties);
}

@Test
public void testGetConfigNameObjectNameValue() throws MalformedObjectNameException {
final ObjectName name = new ObjectName("*:*");
final Map<String, Object> properties = Collections.singletonMap("jmx.objectname", name);
assertSame(name, ConsolidatedListenerMBeanImpl.getObjectName(properties));
}

@Test
public void testGetConfigNameStringValue() throws MalformedObjectNameException {
final String name = "*:*";
final Map<String, Object> properties = Collections.singletonMap("jmx.objectname", name);
assertEquals(new ObjectName(name), ConsolidatedListenerMBeanImpl.getObjectName(properties));
}

@Test(expected = NullPointerException.class)
public void testGetConfigNameInvalidValue() {
final Map<String, Object> properties = Collections.singletonMap("jmx.objectname", Boolean.TRUE);
ConsolidatedListenerMBeanImpl.getObjectName(properties);
}

private static class Listener implements EventListener {
private final AtomicInteger eventCount;

Expand Down

0 comments on commit 3352d1a

Please sign in to comment.