From 3352d1a6822449d4b8d336ef60d0efcc18472d1f Mon Sep 17 00:00:00 2001 From: Angela Schreiber Date: Tue, 27 Apr 2021 09:05:37 +0000 Subject: [PATCH] OAK-9417 : ClassCastException in ConsolidatedListenerMBeanImpl (patch provided by Carsten Ziegeler) git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/oak/trunk@1889232 13f79535-47bb-0310-9956-ffa450edef68 --- .../ConsolidatedListenerMBeanImpl.java | 23 +++++++++++---- .../ConsolidatedListenerMBeanTest.java | 29 +++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/observation/ConsolidatedListenerMBeanImpl.java b/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/observation/ConsolidatedListenerMBeanImpl.java index 8f4d3e76fd1..e2b199d4a9c 100644 --- a/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/observation/ConsolidatedListenerMBeanImpl.java +++ b/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/observation/ConsolidatedListenerMBeanImpl.java @@ -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; @@ -186,7 +187,7 @@ private Collection collectNonJcrObservers() { List observers = Lists.newArrayList(); for (Map.Entry o : bgObservers.entrySet()){ - String listenerId = getListenerId(o.getKey()); + String listenerId = getListenerId(o.getKey()); if (listenerId == null){ observers.add(o.getValue()); } @@ -271,12 +272,12 @@ protected void unbindBackgroundObserverMBean(BackgroundObserverMBean mbean, Map< @SuppressWarnings("unused") protected void bindChangeProcessorMBean(ChangeProcessorMBean mbean, Map config){ - changeProcessors.put(getObjectName(config), mbean); + changeProcessors.put(getObjectName(config), mbean); } @SuppressWarnings("unused") protected void unbindChangeProcessorMBean(ChangeProcessorMBean mbean, Map config){ - changeProcessors.remove(getObjectName(config)); + changeProcessors.remove(getObjectName(config)); } @SuppressWarnings("unused") @@ -299,9 +300,19 @@ protected void unbindFilterConfigMBean(FilterConfigMBean mbean, Map c filterConfigs.remove(getObjectName(config)); } - private static ObjectName getObjectName(Map config){ - return checkNotNull((ObjectName) config.get("jmx.objectname"), - "No 'jmx.objectname' property defined for MBean %s", config); + static ObjectName getObjectName(Map 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){ diff --git a/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/observation/ConsolidatedListenerMBeanTest.java b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/observation/ConsolidatedListenerMBeanTest.java index 4343c54ae03..759be34e5b4 100644 --- a/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/observation/ConsolidatedListenerMBeanTest.java +++ b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/observation/ConsolidatedListenerMBeanTest.java @@ -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; @@ -50,6 +53,32 @@ public void testListenerStatsData() throws MalformedObjectNameException { assertNotNull(data); } + @Test(expected = NullPointerException.class) + public void testGetConfigNameNoValue() { + final Map properties = Collections.emptyMap(); + ConsolidatedListenerMBeanImpl.getObjectName(properties); + } + + @Test + public void testGetConfigNameObjectNameValue() throws MalformedObjectNameException { + final ObjectName name = new ObjectName("*:*"); + final Map properties = Collections.singletonMap("jmx.objectname", name); + assertSame(name, ConsolidatedListenerMBeanImpl.getObjectName(properties)); + } + + @Test + public void testGetConfigNameStringValue() throws MalformedObjectNameException { + final String name = "*:*"; + final Map properties = Collections.singletonMap("jmx.objectname", name); + assertEquals(new ObjectName(name), ConsolidatedListenerMBeanImpl.getObjectName(properties)); + } + + @Test(expected = NullPointerException.class) + public void testGetConfigNameInvalidValue() { + final Map properties = Collections.singletonMap("jmx.objectname", Boolean.TRUE); + ConsolidatedListenerMBeanImpl.getObjectName(properties); + } + private static class Listener implements EventListener { private final AtomicInteger eventCount;