From 8ed92dda3478939a898b29397e4cd03861bc6e31 Mon Sep 17 00:00:00 2001 From: Evie Lau Date: Wed, 5 Jul 2023 16:25:31 -0500 Subject: [PATCH 1/8] Add file to generate applicationMonitor with default value 'polled' --- .../config/MbeanConfigXmlDocument.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/main/java/io/openliberty/tools/common/plugins/config/MbeanConfigXmlDocument.java diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/MbeanConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/MbeanConfigXmlDocument.java new file mode 100644 index 00000000..9c55278c --- /dev/null +++ b/src/main/java/io/openliberty/tools/common/plugins/config/MbeanConfigXmlDocument.java @@ -0,0 +1,61 @@ +/** + * (C) Copyright IBM Corporation 2023. + * + * Licensed 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 io.openliberty.tools.common.plugins.config; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.w3c.dom.Element; + +public class MbeanConfigXmlDocument extends XmlDocument { + public static final String MBEAN_XML_FILENAME = "mbean-config.xml"; + + public MbeanConfigXmlDocument() { + try { + createDocument("server"); + } catch (ParserConfigurationException e) { + // it should never occur + e.printStackTrace(); + } + } + + public void createApplicationMonitorElement() { + Element varElement = doc.createElement("variable"); + varElement.setAttribute("name", "io.openliberty.tools.update.trigger"); + varElement.setAttribute("defaultValue", "polled"); + doc.getDocumentElement().appendChild(varElement); + + Element appMonitor = doc.createElement("applicationMonitor"); + appMonitor.setAttribute("updateTrigger", "${io.openliberty.tools.update.trigger}"); + doc.getDocumentElement().appendChild(appMonitor); + } + + public void writeMbeanConfigXmlDocument(File serverDirectory) throws IOException, TransformerException { + File mbeanXml = getMbeanConfigXmlFile(serverDirectory); + if (!mbeanXml.getParentFile().exists()) { + mbeanXml.getParentFile().mkdirs(); + } + writeXMLDocument(mbeanXml); + } + + public static File getMbeanConfigXmlFile(File serverDirectory) { + File f = new File(serverDirectory, "configDropins/defaults/" + MBEAN_XML_FILENAME); + return f; + } +} From 948887b69a7dba6ee7b249964ab360df23fa1097 Mon Sep 17 00:00:00 2001 From: Evie Lau Date: Wed, 5 Jul 2023 16:58:10 -0500 Subject: [PATCH 2/8] Rename/refactor. Move some code to superclass XmlDocument --- ... ApplicationMonitorConfigXmlDocument.java} | 23 +++++++++-------- .../config/ServerConfigXmlDocument.java | 23 ----------------- .../common/plugins/config/XmlDocument.java | 25 +++++++++++++++++++ 3 files changed, 37 insertions(+), 34 deletions(-) rename src/main/java/io/openliberty/tools/common/plugins/config/{MbeanConfigXmlDocument.java => ApplicationMonitorConfigXmlDocument.java} (67%) diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/MbeanConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java similarity index 67% rename from src/main/java/io/openliberty/tools/common/plugins/config/MbeanConfigXmlDocument.java rename to src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java index 9c55278c..2bcca97b 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/MbeanConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java @@ -23,10 +23,11 @@ import org.w3c.dom.Element; -public class MbeanConfigXmlDocument extends XmlDocument { - public static final String MBEAN_XML_FILENAME = "mbean-config.xml"; +public class ApplicationMonitorConfigXmlDocument extends XmlDocument { + public static final String APPLICATION_CONFIG_XML_FILENAME = "liberty-plugin-app-monitor-config.xml"; + protected static final String HEADER = "# Generated by liberty-maven-plugin"; - public MbeanConfigXmlDocument() { + public ApplicationMonitorConfigXmlDocument() { try { createDocument("server"); } catch (ParserConfigurationException e) { @@ -35,7 +36,7 @@ public MbeanConfigXmlDocument() { } } - public void createApplicationMonitorElement() { + public void createAppMonitorElement() { Element varElement = doc.createElement("variable"); varElement.setAttribute("name", "io.openliberty.tools.update.trigger"); varElement.setAttribute("defaultValue", "polled"); @@ -46,16 +47,16 @@ public void createApplicationMonitorElement() { doc.getDocumentElement().appendChild(appMonitor); } - public void writeMbeanConfigXmlDocument(File serverDirectory) throws IOException, TransformerException { - File mbeanXml = getMbeanConfigXmlFile(serverDirectory); - if (!mbeanXml.getParentFile().exists()) { - mbeanXml.getParentFile().mkdirs(); + public void writeAppMonitorConfigXmlDocument(File serverDirectory) throws IOException, TransformerException { + File appMonXml = getAppMonitorConfigXmlFile(serverDirectory); + if (!appMonXml.getParentFile().exists()) { + appMonXml.getParentFile().mkdirs(); } - writeXMLDocument(mbeanXml); + writeXMLDocument(appMonXml); } - public static File getMbeanConfigXmlFile(File serverDirectory) { - File f = new File(serverDirectory, "configDropins/defaults/" + MBEAN_XML_FILENAME); + public static File getAppMonitorConfigXmlFile(File serverDirectory) { + File f = new File(serverDirectory, "configDropins/defaults/" + APPLICATION_CONFIG_XML_FILENAME); return f; } } diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java index 283c646e..fda422b3 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java @@ -68,26 +68,6 @@ public static ServerConfigXmlDocument newInstance(File f) throws ParserConfigura return configDocument; } - public void createComment(String comment) { - createComment(findServerElement(), comment); - } - - // add comment to the end of the children - public void createComment(Element elem, String comment) { - Comment commentElement = doc.createComment(comment); - appendBeforeBlanks(elem, commentElement); - } - - private void appendBeforeBlanks(Element elem, Node childElement) { - Node lastchild = elem.getLastChild(); - if (isWhitespace(lastchild)) { - // last child is the whitespace preceding the so insert before that - elem.insertBefore(childElement, lastchild); - } else { - elem.appendChild(childElement); - } - } - // Return true if the document was changed and false otherwise. public boolean createFMComment(String comment) { if (featureManager == null) { @@ -214,7 +194,4 @@ public Node findFirstLevelComment(String comment) { return null; } - public Element findServerElement() { - return doc.getDocumentElement(); // defined for this type of file - } } diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java index ba7de16e..d79d16fa 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java @@ -31,6 +31,7 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.w3c.dom.Comment; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -59,6 +60,30 @@ public void createDocument(File xmlFile) throws ParserConfigurationException, SA doc = builder.parse(xmlFile); } + public void createComment(String comment) { + createComment(findServerElement(), comment); + } + + // add comment to the end of the children + public void createComment(Element elem, String comment) { + Comment commentElement = doc.createComment(comment); + appendBeforeBlanks(elem, commentElement); + } + + private void appendBeforeBlanks(Element elem, Node childElement) { + Node lastchild = elem.getLastChild(); + if (isWhitespace(lastchild)) { + // last child is the whitespace preceding the so insert before that + elem.insertBefore(childElement, lastchild); + } else { + elem.appendChild(childElement); + } + } + + public Element findServerElement() { + return doc.getDocumentElement(); // defined for this type of file + } + public void writeXMLDocument(String fileName) throws IOException, TransformerException { File f = new File(fileName); writeXMLDocument(f); From 33f99faa9b4dfe7fec6025d1e7376ceca47c5d85 Mon Sep 17 00:00:00 2001 From: Evie Lau Date: Thu, 6 Jul 2023 10:01:52 -0500 Subject: [PATCH 3/8] Create only applicationMonitor, move to configDropins/overrides --- .../config/ApplicationMonitorConfigXmlDocument.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java index 2bcca97b..281d0f56 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java @@ -37,11 +37,6 @@ public ApplicationMonitorConfigXmlDocument() { } public void createAppMonitorElement() { - Element varElement = doc.createElement("variable"); - varElement.setAttribute("name", "io.openliberty.tools.update.trigger"); - varElement.setAttribute("defaultValue", "polled"); - doc.getDocumentElement().appendChild(varElement); - Element appMonitor = doc.createElement("applicationMonitor"); appMonitor.setAttribute("updateTrigger", "${io.openliberty.tools.update.trigger}"); doc.getDocumentElement().appendChild(appMonitor); @@ -56,7 +51,7 @@ public void writeAppMonitorConfigXmlDocument(File serverDirectory) throws IOExce } public static File getAppMonitorConfigXmlFile(File serverDirectory) { - File f = new File(serverDirectory, "configDropins/defaults/" + APPLICATION_CONFIG_XML_FILENAME); + File f = new File(serverDirectory, "configDropins/overrides/" + APPLICATION_CONFIG_XML_FILENAME); return f; } } From 325ca367f407101fe39321318d0d5558637e879f Mon Sep 17 00:00:00 2001 From: Evie Lau Date: Thu, 6 Jul 2023 12:49:46 -0500 Subject: [PATCH 4/8] Refactor, create tests --- .../ApplicationMonitorConfigXmlDocument.java | 31 +++++------ .../config/ServerConfigXmlDocument.java | 54 +++++++++++++++++-- .../common/plugins/config/XmlDocument.java | 24 --------- .../plugins/config/BaseConfigXmlTest.java | 32 +++++++++++ .../plugins/config/ServerConfigXmlTest.java | 24 +++++++++ 5 files changed, 118 insertions(+), 47 deletions(-) create mode 100644 src/test/java/io/openliberty/tools/common/plugins/config/BaseConfigXmlTest.java create mode 100644 src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java index 281d0f56..0d040096 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java @@ -17,40 +17,35 @@ import java.io.File; import java.io.IOException; +import java.util.HashMap; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; -import org.w3c.dom.Element; - public class ApplicationMonitorConfigXmlDocument extends XmlDocument { public static final String APPLICATION_CONFIG_XML_FILENAME = "liberty-plugin-app-monitor-config.xml"; - protected static final String HEADER = "# Generated by liberty-maven-plugin"; + String tool = null; - public ApplicationMonitorConfigXmlDocument() { - try { - createDocument("server"); - } catch (ParserConfigurationException e) { - // it should never occur - e.printStackTrace(); - } - } + HashMap attributes = new HashMap<>(); - public void createAppMonitorElement() { - Element appMonitor = doc.createElement("applicationMonitor"); - appMonitor.setAttribute("updateTrigger", "${io.openliberty.tools.update.trigger}"); - doc.getDocumentElement().appendChild(appMonitor); + public ApplicationMonitorConfigXmlDocument(String tool) { + this.tool = tool; + attributes.put("updateTrigger", "${io.openliberty.tools.update.trigger}"); } - public void writeAppMonitorConfigXmlDocument(File serverDirectory) throws IOException, TransformerException { + public void writeAppMonitorConfigXmlDocument(File serverDirectory) throws IOException, TransformerException, ParserConfigurationException { File appMonXml = getAppMonitorConfigXmlFile(serverDirectory); if (!appMonXml.getParentFile().exists()) { appMonXml.getParentFile().mkdirs(); } - writeXMLDocument(appMonXml); + + ServerConfigXmlDocument configDocument = ServerConfigXmlDocument.newInstance(); + configDocument.createGeneratedComment(tool); + configDocument.createServerElement("applicationMonitor", attributes);; + configDocument.writeXMLDocument(appMonXml); } - public static File getAppMonitorConfigXmlFile(File serverDirectory) { + public File getAppMonitorConfigXmlFile(File serverDirectory) { File f = new File(serverDirectory, "configDropins/overrides/" + APPLICATION_CONFIG_XML_FILENAME); return f; } diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java index fda422b3..b60acc58 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java @@ -17,23 +17,22 @@ import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Comment; +import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import org.w3c.dom.Text; import org.xml.sax.SAXException; -import org.w3c.dom.Element; -import org.w3c.dom.Comment; public class ServerConfigXmlDocument extends XmlDocument { // Formerly called src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDropinXmlDocument.java public static String DEFAULT_INDENTATION = " "; private Element featureManager = null; + protected static final String HEADER = "# Generated by liberty-%s-plugin"; private ServerConfigXmlDocument() { } @@ -68,6 +67,35 @@ public static ServerConfigXmlDocument newInstance(File f) throws ParserConfigura return configDocument; } + public void createComment(String comment) { + createComment(findServerElement(), comment); + } + + // add comment to the end of the children + public void createComment(Element elem, String comment) { + Comment commentElement = doc.createComment(comment); + appendBeforeBlanks(elem, commentElement); + } + + /** + * Creates comment "# Generated by liberty-{}-plugin", + * @param tool - Which tool, maven or gradle, is generating the comment + */ + public void createGeneratedComment(String tool) { + String comment = String.format(HEADER, tool); + createComment(comment); + } + + private void appendBeforeBlanks(Element elem, Node childElement) { + Node lastchild = elem.getLastChild(); + if (isWhitespace(lastchild)) { + // last child is the whitespace preceding the so insert before that + elem.insertBefore(childElement, lastchild); + } else { + elem.appendChild(childElement); + } + } + // Return true if the document was changed and false otherwise. public boolean createFMComment(String comment) { if (featureManager == null) { @@ -123,6 +151,19 @@ public boolean removeFMComment(String comment) { return false; } + /** + * Creates element of `elementName`, with attributes/values. + * @param elementName - name of the element, ie: + * @param attributes - HashMap of the + */ + public void createServerElement(String elementName, HashMap attributes) { + Element element = doc.createElement(elementName); + for (String attributeString : attributes.keySet()) { + element.setAttribute(attributeString, attributes.get(attributeString)); + } + findServerElement().appendChild(element); + } + public void createVariableWithValue(String varName, String varValue, boolean isDefaultValue) { createVariableWithValue(doc.getDocumentElement(), varName, varValue, isDefaultValue); } @@ -194,4 +235,7 @@ public Node findFirstLevelComment(String comment) { return null; } + public Element findServerElement() { + return doc.getDocumentElement(); // defined for this type of file + } } diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java index d79d16fa..d3a059d5 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java @@ -60,30 +60,6 @@ public void createDocument(File xmlFile) throws ParserConfigurationException, SA doc = builder.parse(xmlFile); } - public void createComment(String comment) { - createComment(findServerElement(), comment); - } - - // add comment to the end of the children - public void createComment(Element elem, String comment) { - Comment commentElement = doc.createComment(comment); - appendBeforeBlanks(elem, commentElement); - } - - private void appendBeforeBlanks(Element elem, Node childElement) { - Node lastchild = elem.getLastChild(); - if (isWhitespace(lastchild)) { - // last child is the whitespace preceding the so insert before that - elem.insertBefore(childElement, lastchild); - } else { - elem.appendChild(childElement); - } - } - - public Element findServerElement() { - return doc.getDocumentElement(); // defined for this type of file - } - public void writeXMLDocument(String fileName) throws IOException, TransformerException { File f = new File(fileName); writeXMLDocument(f); diff --git a/src/test/java/io/openliberty/tools/common/plugins/config/BaseConfigXmlTest.java b/src/test/java/io/openliberty/tools/common/plugins/config/BaseConfigXmlTest.java new file mode 100644 index 00000000..7b39afaf --- /dev/null +++ b/src/test/java/io/openliberty/tools/common/plugins/config/BaseConfigXmlTest.java @@ -0,0 +1,32 @@ +package io.openliberty.tools.common.plugins.config; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; + +public class BaseConfigXmlTest { + + public static final String RESOURCES_DIR = "src/test/resources"; + + private static final String RESOURCES_INSTALL_DIR = RESOURCES_DIR + "/serverConfig"; + + protected File serverConfigDir = new File(RESOURCES_INSTALL_DIR); + public File buildDir; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Before + public void setupInstallDir() throws IOException { + serverConfigDir = temp.newFolder(); + File src = new File(RESOURCES_INSTALL_DIR); + FileUtils.copyDirectory(src, serverConfigDir); + + buildDir = temp.newFolder(); + } + +} diff --git a/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java b/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java new file mode 100644 index 00000000..1ca450b0 --- /dev/null +++ b/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java @@ -0,0 +1,24 @@ +package io.openliberty.tools.common.plugins.config; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.nio.file.Files; + +import org.junit.Test; + +public class ServerConfigXmlTest extends BaseConfigXmlTest { + + @Test + public void createAppMonitorConfigTest() throws Exception { + File serverDirectory = new File(serverConfigDir, "liberty/wlp/usr/servers/defaultServer"); + + ApplicationMonitorConfigXmlDocument appMonXml = new ApplicationMonitorConfigXmlDocument("test"); + appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory); + + File newFile = appMonXml.getAppMonitorConfigXmlFile(serverDirectory); + String content = Files.readString(newFile.toPath()); + assertTrue(content.contains("Generated by liberty-test-plugin")); + assertTrue(content.contains("")); + } +} From 43be07b2791e7e0c3dabc513c8838012e45b8e24 Mon Sep 17 00:00:00 2001 From: Evie Lau Date: Thu, 6 Jul 2023 12:55:26 -0500 Subject: [PATCH 5/8] Rename method --- .../plugins/config/ApplicationMonitorConfigXmlDocument.java | 4 ++-- .../common/plugins/config/ServerConfigXmlDocument.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java index 0d040096..e8fa826e 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java @@ -40,8 +40,8 @@ public void writeAppMonitorConfigXmlDocument(File serverDirectory) throws IOExce } ServerConfigXmlDocument configDocument = ServerConfigXmlDocument.newInstance(); - configDocument.createGeneratedComment(tool); - configDocument.createServerElement("applicationMonitor", attributes);; + configDocument.createGeneratedByComment(tool); + configDocument.createServerElementWithAttributes("applicationMonitor", attributes); configDocument.writeXMLDocument(appMonXml); } diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java index b60acc58..db01a136 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2019, 2022. + * (C) Copyright IBM Corporation 2019, 2023. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -81,7 +81,7 @@ public void createComment(Element elem, String comment) { * Creates comment "# Generated by liberty-{}-plugin", * @param tool - Which tool, maven or gradle, is generating the comment */ - public void createGeneratedComment(String tool) { + public void createGeneratedByComment(String tool) { String comment = String.format(HEADER, tool); createComment(comment); } @@ -156,7 +156,7 @@ public boolean removeFMComment(String comment) { * @param elementName - name of the element, ie: * @param attributes - HashMap of the */ - public void createServerElement(String elementName, HashMap attributes) { + public void createServerElementWithAttributes(String elementName, HashMap attributes) { Element element = doc.createElement(elementName); for (String attributeString : attributes.keySet()) { element.setAttribute(attributeString, attributes.get(attributeString)); From d3274c7a6e560dfd1375629ba4ca31bd685b94bd Mon Sep 17 00:00:00 2001 From: Evie Lau Date: Mon, 7 Aug 2023 14:14:57 -0500 Subject: [PATCH 6/8] Change to use passed-in property for applicationMonitor value --- .../ApplicationMonitorConfigXmlDocument.java | 19 ++++++++++-- .../plugins/config/ServerConfigXmlTest.java | 29 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java index e8fa826e..a58394c4 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java @@ -17,23 +17,38 @@ import java.io.File; import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; +import io.openliberty.tools.common.plugins.util.PluginExecutionException; + public class ApplicationMonitorConfigXmlDocument extends XmlDocument { public static final String APPLICATION_CONFIG_XML_FILENAME = "liberty-plugin-app-monitor-config.xml"; + private static List APP_MON_VALUE_LIST = Arrays.asList("polled", "mbean", "disabled"); + String tool = null; HashMap attributes = new HashMap<>(); public ApplicationMonitorConfigXmlDocument(String tool) { this.tool = tool; - attributes.put("updateTrigger", "${io.openliberty.tools.update.trigger}"); } - public void writeAppMonitorConfigXmlDocument(File serverDirectory) throws IOException, TransformerException, ParserConfigurationException { + public void writeAppMonitorConfigXmlDocument(File serverDirectory, String applicationMonitorValue) throws IOException, TransformerException, ParserConfigurationException, PluginExecutionException { + // if applicationMonitor not set, return + if (applicationMonitorValue == null) { + return; + } + // continue with creating configDropins/override file + if (!APP_MON_VALUE_LIST.contains(applicationMonitorValue)) { + throw new PluginExecutionException("applicationMonitor value \"" + applicationMonitorValue + "\" is not supported. Must be one of: " + APP_MON_VALUE_LIST); + } + attributes.put("updateTrigger", applicationMonitorValue); + File appMonXml = getAppMonitorConfigXmlFile(serverDirectory); if (!appMonXml.getParentFile().exists()) { appMonXml.getParentFile().mkdirs(); diff --git a/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java b/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java index 1ca450b0..0811a0d7 100644 --- a/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java +++ b/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java @@ -1,5 +1,6 @@ package io.openliberty.tools.common.plugins.config; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.File; @@ -14,11 +15,35 @@ public void createAppMonitorConfigTest() throws Exception { File serverDirectory = new File(serverConfigDir, "liberty/wlp/usr/servers/defaultServer"); ApplicationMonitorConfigXmlDocument appMonXml = new ApplicationMonitorConfigXmlDocument("test"); - appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory); + appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory, "mbean"); File newFile = appMonXml.getAppMonitorConfigXmlFile(serverDirectory); String content = Files.readString(newFile.toPath()); assertTrue(content.contains("Generated by liberty-test-plugin")); - assertTrue(content.contains("")); + assertTrue(content.contains("")); + } + + @Test + public void createAppMonitorConfigWithInvalidValueTest() throws Exception { + File serverDirectory = new File(serverConfigDir, "liberty/wlp/usr/servers/defaultServer"); + + ApplicationMonitorConfigXmlDocument appMonXml = new ApplicationMonitorConfigXmlDocument("test"); + try { + appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory, "asdf"); + } catch (Exception e) { + assertTrue(e.getMessage().contains("applicationMonitor value")); + assertTrue(e.getMessage().contains("is not supported")); + } + } + + @Test + public void createAppMonitorConfigWithoutValueTest() throws Exception { + File serverDirectory = new File(serverConfigDir, "liberty/wlp/usr/servers/defaultServer"); + + ApplicationMonitorConfigXmlDocument appMonXml = new ApplicationMonitorConfigXmlDocument("test"); + appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory, null); + + File newFile = appMonXml.getAppMonitorConfigXmlFile(serverDirectory); + assertFalse(newFile.exists()); } } From c415c18b7ed74302473e14a7c9ffc5ba238f16ae Mon Sep 17 00:00:00 2001 From: Evie Lau Date: Mon, 7 Aug 2023 14:48:57 -0500 Subject: [PATCH 7/8] Delete config file if applicationMonitor property not set --- .../plugins/config/ApplicationMonitorConfigXmlDocument.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java index a58394c4..b54aaa5d 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java @@ -39,8 +39,12 @@ public ApplicationMonitorConfigXmlDocument(String tool) { } public void writeAppMonitorConfigXmlDocument(File serverDirectory, String applicationMonitorValue) throws IOException, TransformerException, ParserConfigurationException, PluginExecutionException { + File appMonXml = getAppMonitorConfigXmlFile(serverDirectory); // if applicationMonitor not set, return if (applicationMonitorValue == null) { + if (appMonXml.exists()) { + appMonXml.delete(); + } return; } // continue with creating configDropins/override file @@ -49,7 +53,6 @@ public void writeAppMonitorConfigXmlDocument(File serverDirectory, String applic } attributes.put("updateTrigger", applicationMonitorValue); - File appMonXml = getAppMonitorConfigXmlFile(serverDirectory); if (!appMonXml.getParentFile().exists()) { appMonXml.getParentFile().mkdirs(); } From 08e2e0c641f920ca4cccf74210742287aa6e97e5 Mon Sep 17 00:00:00 2001 From: Evie Lau Date: Mon, 7 Aug 2023 16:25:25 -0500 Subject: [PATCH 8/8] Rename vars and rewrite message. --- .../config/ApplicationMonitorConfigXmlDocument.java | 10 +++++----- .../common/plugins/config/ServerConfigXmlTest.java | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java index b54aaa5d..3f45a0ed 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java @@ -38,20 +38,20 @@ public ApplicationMonitorConfigXmlDocument(String tool) { this.tool = tool; } - public void writeAppMonitorConfigXmlDocument(File serverDirectory, String applicationMonitorValue) throws IOException, TransformerException, ParserConfigurationException, PluginExecutionException { + public void writeAppMonitorConfigXmlDocument(File serverDirectory, String appMonitorTrigger) throws IOException, TransformerException, ParserConfigurationException, PluginExecutionException { File appMonXml = getAppMonitorConfigXmlFile(serverDirectory); // if applicationMonitor not set, return - if (applicationMonitorValue == null) { + if (appMonitorTrigger == null) { if (appMonXml.exists()) { appMonXml.delete(); } return; } // continue with creating configDropins/override file - if (!APP_MON_VALUE_LIST.contains(applicationMonitorValue)) { - throw new PluginExecutionException("applicationMonitor value \"" + applicationMonitorValue + "\" is not supported. Must be one of: " + APP_MON_VALUE_LIST); + if (!APP_MON_VALUE_LIST.contains(appMonitorTrigger)) { + throw new PluginExecutionException("The appMonitorTrigger value \"" + appMonitorTrigger + "\" is not supported. Please use one of: " + APP_MON_VALUE_LIST); } - attributes.put("updateTrigger", applicationMonitorValue); + attributes.put("updateTrigger", appMonitorTrigger); if (!appMonXml.getParentFile().exists()) { appMonXml.getParentFile().mkdirs(); diff --git a/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java b/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java index 0811a0d7..102f9f8a 100644 --- a/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java +++ b/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java @@ -31,8 +31,7 @@ public void createAppMonitorConfigWithInvalidValueTest() throws Exception { try { appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory, "asdf"); } catch (Exception e) { - assertTrue(e.getMessage().contains("applicationMonitor value")); - assertTrue(e.getMessage().contains("is not supported")); + assertTrue(e.getMessage().contains("The appMonitorTrigger value \"asdf\" is not supported.")); } }