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:03:46 UTC
(tomcat) 11/23: Report missing metadata when registering a component
This is an automated email from the ASF dual-hosted git repository.
rmaucher pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit d71ab46c04595845fd1037709f22b123557aea64
Author: opencode <re...@apache.org>
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 a369b1eadf..f74885498f 100644
--- a/java/org/apache/tomcat/util/modeler/Registry.java
+++ b/java/org/apache/tomcat/util/modeler/Registry.java
@@ -639,6 +639,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: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org