diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java index e05273ef..054b352a 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java @@ -29,6 +29,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; +import java.util.Optional; import java.util.Set; import java.util.Map; import java.util.Properties; @@ -43,6 +44,7 @@ import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; +import io.openliberty.tools.common.plugins.util.PluginExecutionException; import org.apache.commons.io.comparator.NameFileComparator; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -72,6 +74,8 @@ public class ServerConfigDocument { private Properties defaultProps; private Map libertyDirectoryPropertyToFile = null; + Optional springBootAppNodeLocation=Optional.empty(); + private static final XPathExpression XPATH_SERVER_APPLICATION; private static final XPathExpression XPATH_SERVER_WEB_APPLICATION; private static final XPathExpression XPATH_SERVER_SPRINGBOOT_APPLICATION; @@ -135,7 +139,7 @@ public File getServerXML() { * @param originalServerXMLFile * @param libertyDirPropertyFiles */ - public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map libertyDirPropertyFiles) { + public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map libertyDirPropertyFiles) throws PluginExecutionException { this.log = log; if (libertyDirPropertyFiles != null) { libertyDirectoryPropertyToFile = new HashMap(libertyDirPropertyFiles); @@ -157,7 +161,7 @@ public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map1){ + throw new PluginExecutionException("Found multiple springBootApplication elements specified in the server configuration. Only one springBootApplication can be configured per Liberty server."); + } for (int i = 0; i < nodeList.getLength(); i++) { String nodeValue = nodeList.item(i).getAttributes().getNamedItem("location").getNodeValue(); - // add unique values only if (!nodeValue.isEmpty()) { + if(expression.equals(XPATH_SERVER_SPRINGBOOT_APPLICATION)){ + springBootAppNodeLocation = Optional.of(nodeValue); + } String resolved = VariableUtility.resolveVariables(log, nodeValue, null, getProperties(), getDefaultProperties(), getLibertyDirPropertyFiles()); if (resolved == null) { // location could not be resolved, log message and add location as is @@ -508,7 +519,7 @@ private void parseApplication(Document doc, XPathExpression expression) throws X } } - private void parseInclude(Document doc) throws XPathExpressionException, IOException, SAXException { + private void parseInclude(Document doc) throws XPathExpressionException, IOException, SAXException, PluginExecutionException { // parse include document in source server xml NodeList nodeList = (NodeList) XPATH_SERVER_INCLUDE.evaluate(doc, XPathConstants.NODESET); @@ -539,7 +550,7 @@ private void parseInclude(Document doc) throws XPathExpressionException, IOExcep } } - private void parseConfigDropinsDir() throws XPathExpressionException, IOException, SAXException { + private void parseConfigDropinsDir() throws XPathExpressionException, IOException, SAXException, PluginExecutionException { File configDropins = getConfigDropinsDir(); if (configDropins == null || !configDropins.exists()) { @@ -557,7 +568,7 @@ private void parseConfigDropinsDir() throws XPathExpressionException, IOExceptio } } - private void parseDropinsFiles(File[] files) throws XPathExpressionException, IOException, SAXException { + private void parseDropinsFiles(File[] files) throws XPathExpressionException, IOException, SAXException, PluginExecutionException { Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_COMPARATOR); for (File file : files) { if (file.isFile()) { @@ -566,7 +577,7 @@ private void parseDropinsFiles(File[] files) throws XPathExpressionException, IO } } - private void parseDropinsFile(File file) throws IOException, XPathExpressionException, SAXException { + private void parseDropinsFile(File file) throws IOException, XPathExpressionException, SAXException, PluginExecutionException { // get input XML Document Document doc = parseDocument(file); if (doc != null) { @@ -906,4 +917,13 @@ public File getOriginalServerXMLFile() { public void setOriginalServerXMLFile(File originalServerXMLFile) { this.originalServerXMLFile = originalServerXMLFile; } + + public Optional getSpringBootAppNodeLocation() { + return springBootAppNodeLocation; + } + + public void setSpringBootAppNodeLocation(Optional springBootAppNodeLocation) { + this.springBootAppNodeLocation = springBootAppNodeLocation; + } + } \ No newline at end of file diff --git a/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentOverridesTest.java b/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentOverridesTest.java index d3995f62..126c3980 100644 --- a/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentOverridesTest.java +++ b/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentOverridesTest.java @@ -1,5 +1,7 @@ package io.openliberty.tools.common.config; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -7,14 +9,17 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.xml.xpath.XPathExpressionException; +import io.openliberty.tools.common.plugins.util.PluginExecutionException; import org.junit.Test; import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -206,7 +211,7 @@ public void variablesDir() throws FileNotFoundException, Exception { // Run the method @Test - public void initializeAppsLocationTest() { + public void initializeAppsLocationTest() throws PluginExecutionException { File serverConfigDir = SERVER_CONFIG_DIR.toFile(); Map libertyDirPropMap = new HashMap(); libertyDirPropMap.put(ServerFeatureUtil.SERVER_CONFIG_DIR, serverConfigDir); @@ -236,4 +241,31 @@ public void initializeAppsLocationTest() { // configDropins, overrides variable dir assertEquals("The value is expected to be overriden from 9080 to 7777","7777", properties.get("httpPort")); } + + @Test + public void testProcessServerXmlWithSpringBootApplicationNode() throws IOException, PluginExecutionException { + File serversResourceDir = SERVERS_RESOURCES_DIR.toFile(); + File springBootServerXmlDir = new File(serversResourceDir, "springBootApplicationTest"); + File serverXml = new File(springBootServerXmlDir, "valid_server.xml"); + File newServerXml=new File(springBootServerXmlDir, "server.xml"); + Files.copy(serverXml.toPath(),newServerXml.toPath(), StandardCopyOption.REPLACE_EXISTING); + Map libertyDirPropMap = new HashMap<>(); + libertyDirPropMap.put(ServerFeatureUtil.SERVER_CONFIG_DIR, springBootServerXmlDir); + ServerConfigDocument configDocument = new ServerConfigDocument(new TestLogger(), null, libertyDirPropMap); + assertTrue("ServerConfigDocument getSpringBootAppNodeLocation should not be empty ",configDocument.getSpringBootAppNodeLocation().isPresent()); + assertEquals("ServerConfigDocument locations does not contain expected ", "guide-spring-boot-0.1.0.jar", configDocument.getSpringBootAppNodeLocation().get()); + } + + @Test + public void testProcessServerXmlWithMultipleSpringBootNodes() throws IOException { + File serversResourceDir = SERVERS_RESOURCES_DIR.toFile(); + File springBootServerXmlDir = new File(serversResourceDir, "springBootApplicationTest"); + File serverXml = new File(springBootServerXmlDir, "invalid_server.xml"); + File newServerXml = new File(springBootServerXmlDir, "server.xml"); + Files.copy(serverXml.toPath(), newServerXml.toPath(), StandardCopyOption.REPLACE_EXISTING); + Map libertyDirPropMap = new HashMap<>(); + libertyDirPropMap.put(ServerFeatureUtil.SERVER_CONFIG_DIR, springBootServerXmlDir); + assertThrows("Found multiple springBootApplication elements specified in the server configuration. Only one springBootApplication can be configured per Liberty server.", + PluginExecutionException.class, () -> new ServerConfigDocument(new TestLogger(), null, libertyDirPropMap)); + } } \ No newline at end of file diff --git a/src/test/resources/servers/springBootApplicationTest/invalid_server.xml b/src/test/resources/servers/springBootApplicationTest/invalid_server.xml new file mode 100644 index 00000000..2e53c904 --- /dev/null +++ b/src/test/resources/servers/springBootApplicationTest/invalid_server.xml @@ -0,0 +1,19 @@ + + + + + servlet-6.0 + springBoot-3.0 + + + + + + diff --git a/src/test/resources/servers/springBootApplicationTest/server.xml b/src/test/resources/servers/springBootApplicationTest/server.xml new file mode 100644 index 00000000..12f0287c --- /dev/null +++ b/src/test/resources/servers/springBootApplicationTest/server.xml @@ -0,0 +1,19 @@ + + + + + servlet-6.0 + springBoot-3.0 + + + + + + diff --git a/src/test/resources/servers/springBootApplicationTest/valid_server.xml b/src/test/resources/servers/springBootApplicationTest/valid_server.xml new file mode 100644 index 00000000..37e21799 --- /dev/null +++ b/src/test/resources/servers/springBootApplicationTest/valid_server.xml @@ -0,0 +1,16 @@ + + + + + servlet-6.0 + springBoot-3.0 + + + + +