This is an automated email from the ASF dual-hosted git repository. markt-asf pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 56c949e2da826492d76c8aeaf8f9afaef3236b73 Author: Mark Thomas <[email protected]> AuthorDate: Wed Oct 21 08:38:50 2026 +0100 Review "Report failure when storing a context class without store..." Add similar reporting for Server Co-authored by: CoPilot / GPT-5.6 Sol --- .../apache/catalina/storeconfig/StoreConfig.java | 9 +++++- .../catalina/storeconfig/TestStoreConfig.java | 32 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/java/org/apache/catalina/storeconfig/StoreConfig.java b/java/org/apache/catalina/storeconfig/StoreConfig.java index 8290e02082..39ac34d2f9 100644 --- a/java/org/apache/catalina/storeconfig/StoreConfig.java +++ b/java/org/apache/catalina/storeconfig/StoreConfig.java @@ -97,7 +97,9 @@ public class StoreConfig implements IStoreConfig { @Override public void storeConfig() { - store(server); + if (!store(server)) { + throw new IllegalStateException(sm.getString("config.storeServerError")); + } } /** @@ -201,6 +203,11 @@ public class StoreConfig implements IStoreConfig { @Override public synchronized boolean store(Server aServer) { + StoreDescription desc = getRegistry().findDescription(aServer.getClass()); + if (desc == null) { + log.warn(sm.getString("factory.storeNoDescriptor", aServer.getClass())); + return false; + } StoreFileMover mover = new StoreFileMover(Bootstrap.getCatalinaBase(), getServerFilename(), getRegistry().getEncoding()); // Open an output writer for the new configuration file diff --git a/test/org/apache/catalina/storeconfig/TestStoreConfig.java b/test/org/apache/catalina/storeconfig/TestStoreConfig.java index d48b4a79b2..3e7b1322a0 100644 --- a/test/org/apache/catalina/storeconfig/TestStoreConfig.java +++ b/test/org/apache/catalina/storeconfig/TestStoreConfig.java @@ -21,6 +21,7 @@ import java.io.FileReader; import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; +import java.lang.reflect.Proxy; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -31,6 +32,7 @@ import org.junit.Test; import org.apache.catalina.Context; import org.apache.catalina.Host; +import org.apache.catalina.Server; import org.apache.catalina.connector.Connector; import org.apache.catalina.core.StandardContext; import org.apache.catalina.realm.LockOutRealm; @@ -687,6 +689,36 @@ public class TestStoreConfig extends TomcatBaseTest { Assert.assertFalse(storeConfig.store(context)); } + /** + * Verify that storing a Server whose class has no store description fails without replacing the existing + * configuration, and that the MBean operation reports the failure. + * + * @throws Exception if the test experiences an unexpected error + */ + @Test + public void testStoreServerWithoutDescriptionFails() throws Exception { + StoreLoader loader = new StoreLoader(); + loader.load(null); + StoreConfig storeConfig = new StoreConfig(); + storeConfig.setRegistry(loader.getRegistry()); + + Server server = (Server) Proxy.newProxyInstance(Server.class.getClassLoader(), new Class<?>[] { Server.class }, + (proxy, method, args) -> null); + Assert.assertNull(loader.getRegistry().findDescription(server.getClass())); + + File serverXml = new File(getTemporaryDirectory(), "server-custom.xml"); + String original = "<Server port=\"8005\"/>"; + Files.writeString(serverXml.toPath(), original, StandardCharsets.UTF_8); + storeConfig.setServerFilename(serverXml.getAbsolutePath()); + + Assert.assertFalse(storeConfig.store(server)); + Assert.assertEquals(original, Files.readString(serverXml.toPath(), StandardCharsets.UTF_8)); + + storeConfig.setServer(server); + Assert.assertThrows(IllegalStateException.class, storeConfig::storeConfig); + Assert.assertEquals(original, Files.readString(serverXml.toPath(), StandardCharsets.UTF_8)); + } + private static String storeToString(StoreDescription desc, Object element) throws Exception { StringWriter buffer = new StringWriter(); desc.getStoreFactory().store(new PrintWriter(buffer), -2, element); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
