This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 7873dd616d0d8faae3f7727b59dc5aa6566a0618 Author: opencode <[email protected]> AuthorDate: Thu Oct 15 11:40:43 2026 +0200 Report missing metadata when registering a component Registry.findManagedBean() is documented to return null when no metadata can be found for the requested type: the introspection fallback logs registry.noTypeMetadata and returns null, and the no descriptor registry always returns null. Registry.registerComponent() dereferenced that result immediately, so the documented failure surfaced as a NullPointerException logged under the generic registry.registerError message instead of the descriptive metadata message. Check the result and throw an IllegalStateException carrying the registry.noTypeMetadata message, still inside the existing try block so the error is logged with the object name before it is rethrown. --- java/org/apache/tomcat/util/modeler/Registry.java | 4 ++ .../tomcat/util/modeler/TestRegisterComponent.java | 74 ++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/java/org/apache/tomcat/util/modeler/Registry.java b/java/org/apache/tomcat/util/modeler/Registry.java index ccf01cf040..5437cd5302 100644 --- a/java/org/apache/tomcat/util/modeler/Registry.java +++ b/java/org/apache/tomcat/util/modeler/Registry.java @@ -642,6 +642,10 @@ public class Registry implements RegistryMBean, MBeanRegistration { ManagedBean managed = findManagedBean(null, bean.getClass(), type); + if (managed == null) { + throw new IllegalStateException(sm.getString("registry.noTypeMetadata", type)); + } + // The real mbean is created and registered DynamicMBean mbean = managed.createMBean(bean); diff --git a/test/org/apache/tomcat/util/modeler/TestRegisterComponent.java b/test/org/apache/tomcat/util/modeler/TestRegisterComponent.java new file mode 100644 index 0000000000..06d6c5a5f0 --- /dev/null +++ b/test/org/apache/tomcat/util/modeler/TestRegisterComponent.java @@ -0,0 +1,74 @@ +/* + * 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.ObjectName; + +import org.junit.Assert; +import org.junit.Test; + +public class TestRegisterComponent { + + public static class TesterBean { + + private String value = "test"; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + } + + /* + * Registry that simulates the documented case where findManagedBean() + * finds no metadata for the requested type and returns null. + */ + private static class NoMetadataRegistry extends Registry { + + @Override + public ManagedBean findManagedBean(Object bean, Class<?> beanClass, String type) throws Exception { + return null; + } + } + + @Test + public void testRegisterComponentWithoutMetadataThrowsIllegalState() throws Exception { + Registry registry = new NoMetadataRegistry(); + ObjectName oname = new ObjectName("tester:type=NoMetadataTester"); + + Assert.assertThrows(IllegalStateException.class, + () -> registry.registerComponent(new TesterBean(), oname, null)); + } + + @Test + public void testRegisterComponentWithIntrospectedMetadata() throws Exception { + Registry registry = new Registry(); + ObjectName oname = new ObjectName("tester:type=RegisterComponentTester"); + + try { + registry.registerComponent(new TesterBean(), oname, null); + Assert.assertTrue(registry.getMBeanServer().isRegistered(oname)); + } finally { + if (registry.getMBeanServer().isRegistered(oname)) { + registry.getMBeanServer().unregisterMBean(oname); + } + } + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
