Skip to content

Commit

Permalink
Merge pull request #463 from arunvenmany-ibm/spring-boot-application-…
Browse files Browse the repository at this point in the history
…node-bug-fix

fix for generating configDropins even if user specify springbootappli…
  • Loading branch information
arunvenmany-ibm authored Nov 20, 2024
2 parents c6771a2 + 49caf46 commit 34f64e2
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -72,6 +74,8 @@ public class ServerConfigDocument {
private Properties defaultProps;
private Map<String, File> libertyDirectoryPropertyToFile = null;

Optional<String> 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;
Expand Down Expand Up @@ -135,7 +139,7 @@ public File getServerXML() {
* @param originalServerXMLFile
* @param libertyDirPropertyFiles
*/
public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map<String, File> libertyDirPropertyFiles) {
public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map<String, File> libertyDirPropertyFiles) throws PluginExecutionException {
this.log = log;
if (libertyDirPropertyFiles != null) {
libertyDirectoryPropertyToFile = new HashMap<String, File>(libertyDirPropertyFiles);
Expand All @@ -157,7 +161,7 @@ public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map<S

// LCLS constructor
// TODO: populate libertyDirectoryPropertyToFile with workspace information
public ServerConfigDocument(CommonLoggerI log) {
public ServerConfigDocument(CommonLoggerI log) throws PluginExecutionException {
this(log, null, null);
}

Expand Down Expand Up @@ -227,7 +231,7 @@ private DocumentBuilder getDocumentBuilder() {
// c. ${server.config.dir}/configDropins/overrides/
// 7. variables declared on the command line
*/
public void initializeAppsLocation() {
public void initializeAppsLocation() throws PluginExecutionException {
try {
// 1. Need to parse variables in the server.xml for default values before trying to
// find the include files in case one of the variables is used in the location.
Expand Down Expand Up @@ -265,6 +269,9 @@ public void initializeAppsLocation() {
parseConfigDropinsDir();

} catch (Exception e) {
if(e instanceof PluginExecutionException){
throw (PluginExecutionException)e;
}
e.printStackTrace();
}
}
Expand Down Expand Up @@ -484,15 +491,19 @@ public String findNameForLocation(String location) {
return appName;
}

private void parseApplication(Document doc, XPathExpression expression) throws XPathExpressionException {
private void parseApplication(Document doc, XPathExpression expression) throws XPathExpressionException, PluginExecutionException {

NodeList nodeList = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);

if(expression.equals(XPATH_SERVER_SPRINGBOOT_APPLICATION) && nodeList.getLength()>1){
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
Expand All @@ -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);

Expand Down Expand Up @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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) {
Expand Down Expand Up @@ -906,4 +917,13 @@ public File getOriginalServerXMLFile() {
public void setOriginalServerXMLFile(File originalServerXMLFile) {
this.originalServerXMLFile = originalServerXMLFile;
}

public Optional<String> getSpringBootAppNodeLocation() {
return springBootAppNodeLocation;
}

public void setSpringBootAppNodeLocation(Optional<String> springBootAppNodeLocation) {
this.springBootAppNodeLocation = springBootAppNodeLocation;
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
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;

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;
Expand Down Expand Up @@ -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<String, File> libertyDirPropMap = new HashMap<String, File>();
libertyDirPropMap.put(ServerFeatureUtil.SERVER_CONFIG_DIR, serverConfigDir);
Expand Down Expand Up @@ -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<String, File> 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<String, File> 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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">

<featureManager>
<feature>servlet-6.0</feature>
<feature>springBoot-3.0</feature>
</featureManager>

<httpEndpoint id="defaultHttpEndpoint"
host="*"
httpPort="9080"
httpsPort="9443" />
<springBootApplication id="guide-spring-boot"
location="guide-spring-boot-0.1.0.jar"
name="guide-spring-boot" />
<springBootApplication id="guide-spring-boot"
location="guide-spring-boot-0.2.0.jar"
name="guide-spring-boot" />
</server>
19 changes: 19 additions & 0 deletions src/test/resources/servers/springBootApplicationTest/server.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">

<featureManager>
<feature>servlet-6.0</feature>
<feature>springBoot-3.0</feature>
</featureManager>

<httpEndpoint id="defaultHttpEndpoint"
host="*"
httpPort="9080"
httpsPort="9443" />
<springBootApplication id="guide-spring-boot"
location="guide-spring-boot-0.1.0.jar"
name="guide-spring-boot" />
<springBootApplication id="guide-spring-boot"
location="guide-spring-boot-0.1.0.jar"
name="guide-spring-boot" />
</server>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">

<featureManager>
<feature>servlet-6.0</feature>
<feature>springBoot-3.0</feature>
</featureManager>

<httpEndpoint id="defaultHttpEndpoint"
host="*"
httpPort="9080"
httpsPort="9443" />
<springBootApplication id="guide-spring-boot"
location="guide-spring-boot-0.1.0.jar"
name="guide-spring-boot" />
</server>

0 comments on commit 34f64e2

Please sign in to comment.