This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch feat/WW-5864-struts-maven-plugin-watch in repository https://gitbox.apache.org/repos/asf/struts.git
commit 18b430ea53f6cd02ab79207735f105730a9db11e Author: Lukasz Lenart <[email protected]> AuthorDate: Sat Nov 14 08:48:56 2026 +0100 WW-5864 feat(tools): warn when Struts reload switches are off Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --- .../struts2/maven/watch/StrutsReloadInspector.java | 119 +++++++++++++++++++++ .../maven/watch/StrutsReloadInspectorTest.java | 118 ++++++++++++++++++++ 2 files changed, 237 insertions(+) diff --git a/tools/struts-maven-plugin/src/main/java/org/apache/struts2/maven/watch/StrutsReloadInspector.java b/tools/struts-maven-plugin/src/main/java/org/apache/struts2/maven/watch/StrutsReloadInspector.java new file mode 100644 index 000000000..45920cbe0 --- /dev/null +++ b/tools/struts-maven-plugin/src/main/java/org/apache/struts2/maven/watch/StrutsReloadInspector.java @@ -0,0 +1,119 @@ +/* + * 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.struts2.maven.watch; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +public class StrutsReloadInspector { + + private static final String DEV_MODE = "struts.devMode"; + private static final String XML_RELOAD = "struts.configuration.xml.reload"; + private static final String I18N_RELOAD = "struts.i18n.reload"; + + private final Map<String, String> settings = new HashMap<>(); + + public StrutsReloadInspector(Path outputDirectory) { + readXml(outputDirectory.resolve("struts.xml")); + readProperties(outputDirectory.resolve("struts.properties")); + } + + public List<String> warnings() { + List<String> warnings = new ArrayList<>(); + String devMode = settings.get(DEV_MODE); + if (devMode == null) { + warnings.add("struts.devMode was not found in struts.xml or struts.properties; " + + "if it is set elsewhere, ignore this. Otherwise pushed changes may not be picked up."); + } + for (String key : new String[]{XML_RELOAD, I18N_RELOAD}) { + String explicit = settings.get(key); + String effective = explicit != null ? explicit : devMode; + if (effective != null && !"true".equalsIgnoreCase(effective.trim())) { + warnings.add(key + " is not true; pushed changes will not be picked up " + + "until the application is restarted. Set struts.devMode=true or " + key + "=true."); + } + } + return warnings; + } + + private void readXml(Path file) { + Document document = parse(file); + if (document == null) { + return; + } + NodeList constants = document.getDocumentElement().getElementsByTagName("constant"); + for (int i = 0; i < constants.getLength(); i++) { + Element constant = (Element) constants.item(i); + if (constant.hasAttribute("name") && constant.hasAttribute("value")) { + settings.put(constant.getAttribute("name"), constant.getAttribute("value")); + } + } + } + + private Document parse(Path file) { + if (!Files.isRegularFile(file)) { + return null; + } + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + factory.setFeature("http://xml.org/sax/features/external-general-entities", false); + factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + factory.setXIncludeAware(false); + factory.setExpandEntityReferences(false); + DocumentBuilder builder = factory.newDocumentBuilder(); + builder.setEntityResolver((publicId, systemId) -> new InputSource(new StringReader(""))); + try (InputStream in = Files.newInputStream(file)) { + return builder.parse(in); + } + } catch (Exception e) { + return null; + } + } + + private void readProperties(Path file) { + if (!Files.isRegularFile(file)) { + return; + } + Properties properties = new Properties(); + try (InputStream in = Files.newInputStream(file)) { + properties.load(in); + } catch (IOException | IllegalArgumentException e) { + return; + } + for (String name : properties.stringPropertyNames()) { + settings.put(name, properties.getProperty(name)); + } + } +} diff --git a/tools/struts-maven-plugin/src/test/java/org/apache/struts2/maven/watch/StrutsReloadInspectorTest.java b/tools/struts-maven-plugin/src/test/java/org/apache/struts2/maven/watch/StrutsReloadInspectorTest.java new file mode 100644 index 000000000..7b00cff66 --- /dev/null +++ b/tools/struts-maven-plugin/src/test/java/org/apache/struts2/maven/watch/StrutsReloadInspectorTest.java @@ -0,0 +1,118 @@ +/* + * 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.struts2.maven.watch; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class StrutsReloadInspectorTest { + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + private void xml(String... constants) throws IOException { + xmlWithDoctype("", constants); + } + + private void xmlWithDoctype(String doctype, String... constants) throws IOException { + StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") + .append(doctype).append("<struts>\n"); + for (int i = 0; i < constants.length; i += 2) { + sb.append("<constant name=\"").append(constants[i]).append("\" value=\"") + .append(constants[i + 1]).append("\"/>\n"); + } + sb.append("</struts>\n"); + write("struts.xml", sb.toString()); + } + + private void write(String name, String content) throws IOException { + Files.write(tmp.getRoot().toPath().resolve(name), content.getBytes(StandardCharsets.UTF_8)); + } + + private List<String> warnings() { + return new StrutsReloadInspector(tmp.getRoot().toPath()).warnings(); + } + + @Test + public void devModeTrueNoWarnings() throws IOException { + xml("struts.devMode", "true"); + + assertThat(warnings()).isEmpty(); + } + + @Test + public void devModeFalseWarnsForBothReloadSwitches() throws IOException { + xml("struts.devMode", "false"); + + List<String> warnings = warnings(); + + assertThat(warnings).hasSize(2); + assertThat(warnings.get(0)).contains("struts.configuration.xml.reload"); + assertThat(warnings.get(1)).contains("struts.i18n.reload"); + } + + @Test + public void explicitFalseOverridesDevMode() throws IOException { + xml("struts.devMode", "true", "struts.i18n.reload", "false"); + + List<String> warnings = warnings(); + + assertThat(warnings).hasSize(1); + assertThat(warnings.get(0)).contains("struts.i18n.reload"); + } + + @Test + public void propertiesFileOverridesXml() throws IOException { + xml("struts.devMode", "false"); + write("struts.properties", "struts.devMode=true\n"); + + assertThat(warnings()).isEmpty(); + } + + @Test + public void devModeNotFoundGivesSoftWarning() { + List<String> warnings = warnings(); + + assertThat(warnings).hasSize(1); + assertThat(warnings.get(0)).contains("struts.devMode"); + } + + @Test + public void doesNotFetchExternalDtd() throws IOException { + xmlWithDoctype("<!DOCTYPE struts PUBLIC \"x\" \"http://127.0.0.1:1/never.dtd\">\n", + "struts.devMode", "true"); + + assertThat(warnings()).isEmpty(); + } + + @Test + public void malformedXmlProducesNoException() throws IOException { + write("struts.xml", "<struts><constant name="); + + assertThat(warnings()).hasSize(1); + } +}
