Skip to content

Commit d07cd88

Browse files
fix for generating configDropins even if user specify springbootapplication node in server.xml
Signed-off-by: Arun Venmany <[email protected]>
1 parent c6771a2 commit d07cd88

File tree

5 files changed

+116
-11
lines changed

5 files changed

+116
-11
lines changed

src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Arrays;
3030
import java.util.HashMap;
3131
import java.util.HashSet;
32+
import java.util.Optional;
3233
import java.util.Set;
3334
import java.util.Map;
3435
import java.util.Properties;
@@ -43,6 +44,7 @@
4344
import javax.xml.xpath.XPathExpressionException;
4445
import javax.xml.xpath.XPathFactory;
4546

47+
import io.openliberty.tools.common.plugins.util.PluginExecutionException;
4648
import org.apache.commons.io.comparator.NameFileComparator;
4749
import org.w3c.dom.Document;
4850
import org.w3c.dom.Element;
@@ -72,6 +74,8 @@ public class ServerConfigDocument {
7274
private Properties defaultProps;
7375
private Map<String, File> libertyDirectoryPropertyToFile = null;
7476

77+
Optional<String> springBootAppNodeLocation=Optional.empty();
78+
7579
private static final XPathExpression XPATH_SERVER_APPLICATION;
7680
private static final XPathExpression XPATH_SERVER_WEB_APPLICATION;
7781
private static final XPathExpression XPATH_SERVER_SPRINGBOOT_APPLICATION;
@@ -135,7 +139,7 @@ public File getServerXML() {
135139
* @param originalServerXMLFile
136140
* @param libertyDirPropertyFiles
137141
*/
138-
public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map<String, File> libertyDirPropertyFiles) {
142+
public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map<String, File> libertyDirPropertyFiles) throws PluginExecutionException {
139143
this.log = log;
140144
if (libertyDirPropertyFiles != null) {
141145
libertyDirectoryPropertyToFile = new HashMap<String, File>(libertyDirPropertyFiles);
@@ -157,7 +161,7 @@ public ServerConfigDocument(CommonLoggerI log, File originalServerXMLFile, Map<S
157161

158162
// LCLS constructor
159163
// TODO: populate libertyDirectoryPropertyToFile with workspace information
160-
public ServerConfigDocument(CommonLoggerI log) {
164+
public ServerConfigDocument(CommonLoggerI log) throws PluginExecutionException {
161165
this(log, null, null);
162166
}
163167

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

267271
} catch (Exception e) {
272+
if(e instanceof PluginExecutionException){
273+
throw (PluginExecutionException)e;
274+
}
268275
e.printStackTrace();
269276
}
270277
}
@@ -484,15 +491,19 @@ public String findNameForLocation(String location) {
484491
return appName;
485492
}
486493

487-
private void parseApplication(Document doc, XPathExpression expression) throws XPathExpressionException {
494+
private void parseApplication(Document doc, XPathExpression expression) throws XPathExpressionException, PluginExecutionException {
488495

489496
NodeList nodeList = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
490-
497+
if(expression.equals(XPATH_SERVER_SPRINGBOOT_APPLICATION) && nodeList.getLength()>1){
498+
throw new PluginExecutionException("Multiple <springBootApplication/> nodes found in Liberty Config server.xml. Please specify only one");
499+
}
491500
for (int i = 0; i < nodeList.getLength(); i++) {
492501
String nodeValue = nodeList.item(i).getAttributes().getNamedItem("location").getNodeValue();
493-
494502
// add unique values only
495503
if (!nodeValue.isEmpty()) {
504+
if(expression.equals(XPATH_SERVER_SPRINGBOOT_APPLICATION)){
505+
springBootAppNodeLocation = Optional.of(nodeValue);
506+
}
496507
String resolved = VariableUtility.resolveVariables(log, nodeValue, null, getProperties(), getDefaultProperties(), getLibertyDirPropertyFiles());
497508
if (resolved == null) {
498509
// 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
508519
}
509520
}
510521

511-
private void parseInclude(Document doc) throws XPathExpressionException, IOException, SAXException {
522+
private void parseInclude(Document doc) throws XPathExpressionException, IOException, SAXException, PluginExecutionException {
512523
// parse include document in source server xml
513524
NodeList nodeList = (NodeList) XPATH_SERVER_INCLUDE.evaluate(doc, XPathConstants.NODESET);
514525

@@ -539,7 +550,7 @@ private void parseInclude(Document doc) throws XPathExpressionException, IOExcep
539550
}
540551
}
541552

542-
private void parseConfigDropinsDir() throws XPathExpressionException, IOException, SAXException {
553+
private void parseConfigDropinsDir() throws XPathExpressionException, IOException, SAXException, PluginExecutionException {
543554
File configDropins = getConfigDropinsDir();
544555

545556
if (configDropins == null || !configDropins.exists()) {
@@ -557,7 +568,7 @@ private void parseConfigDropinsDir() throws XPathExpressionException, IOExceptio
557568
}
558569
}
559570

560-
private void parseDropinsFiles(File[] files) throws XPathExpressionException, IOException, SAXException {
571+
private void parseDropinsFiles(File[] files) throws XPathExpressionException, IOException, SAXException, PluginExecutionException {
561572
Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_COMPARATOR);
562573
for (File file : files) {
563574
if (file.isFile()) {
@@ -566,7 +577,7 @@ private void parseDropinsFiles(File[] files) throws XPathExpressionException, IO
566577
}
567578
}
568579

569-
private void parseDropinsFile(File file) throws IOException, XPathExpressionException, SAXException {
580+
private void parseDropinsFile(File file) throws IOException, XPathExpressionException, SAXException, PluginExecutionException {
570581
// get input XML Document
571582
Document doc = parseDocument(file);
572583
if (doc != null) {
@@ -906,4 +917,13 @@ public File getOriginalServerXMLFile() {
906917
public void setOriginalServerXMLFile(File originalServerXMLFile) {
907918
this.originalServerXMLFile = originalServerXMLFile;
908919
}
920+
921+
public Optional<String> getSpringBootAppNodeLocation() {
922+
return springBootAppNodeLocation;
923+
}
924+
925+
public void setSpringBootAppNodeLocation(Optional<String> springBootAppNodeLocation) {
926+
this.springBootAppNodeLocation = springBootAppNodeLocation;
927+
}
928+
909929
}

src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentOverridesTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package io.openliberty.tools.common.config;
22

3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assert.assertThrows;
35
import static org.junit.Assert.assertTrue;
46
import static org.junit.Assert.assertEquals;
57
import static org.junit.Assert.assertFalse;
68

79
import java.io.File;
810
import java.io.FileNotFoundException;
911
import java.io.IOException;
12+
import java.nio.file.Files;
1013
import java.nio.file.Path;
1114
import java.nio.file.Paths;
15+
import java.nio.file.StandardCopyOption;
1216
import java.util.HashMap;
1317
import java.util.Map;
1418
import java.util.Properties;
1519

1620
import javax.xml.xpath.XPathExpressionException;
1721

22+
import io.openliberty.tools.common.plugins.util.PluginExecutionException;
1823
import org.junit.Test;
1924
import org.w3c.dom.Document;
2025
import org.xml.sax.SAXException;
@@ -206,7 +211,7 @@ public void variablesDir() throws FileNotFoundException, Exception {
206211

207212
// Run the method
208213
@Test
209-
public void initializeAppsLocationTest() {
214+
public void initializeAppsLocationTest() throws PluginExecutionException {
210215
File serverConfigDir = SERVER_CONFIG_DIR.toFile();
211216
Map<String, File> libertyDirPropMap = new HashMap<String, File>();
212217
libertyDirPropMap.put(ServerFeatureUtil.SERVER_CONFIG_DIR, serverConfigDir);
@@ -236,4 +241,30 @@ public void initializeAppsLocationTest() {
236241
// configDropins, overrides variable dir
237242
assertEquals("The value is expected to be overriden from 9080 to 7777","7777", properties.get("httpPort"));
238243
}
244+
245+
@Test
246+
public void testProcessServerXmlWithSpringBootApplicationNode() throws IOException, PluginExecutionException {
247+
File serversResourceDir = SERVERS_RESOURCES_DIR.toFile();
248+
File springBootServerXmlDir = new File(serversResourceDir, "springBootApplicationTest");
249+
File serverXml = new File(springBootServerXmlDir, "valid_server.xml");
250+
File newServerXml=new File(springBootServerXmlDir, "server.xml");
251+
Files.copy(serverXml.toPath(),newServerXml.toPath(), StandardCopyOption.REPLACE_EXISTING);
252+
Map<String, File> libertyDirPropMap = new HashMap<>();
253+
libertyDirPropMap.put(ServerFeatureUtil.SERVER_CONFIG_DIR, springBootServerXmlDir);
254+
ServerConfigDocument configDocument = new ServerConfigDocument(new TestLogger(), null, libertyDirPropMap);
255+
assertTrue("ServerConfigDocument getSpringBootAppNodeLocation should not be empty ",configDocument.getSpringBootAppNodeLocation().isPresent());
256+
assertEquals("ServerConfigDocument locations does not contain expected ", "guide-spring-boot-0.1.0.jar", configDocument.getSpringBootAppNodeLocation().get());
257+
}
258+
259+
@Test
260+
public void testProcessServerXmlWithMultipleSpringBootNodes() throws IOException {
261+
File serversResourceDir = SERVERS_RESOURCES_DIR.toFile();
262+
File springBootServerXmlDir = new File(serversResourceDir, "springBootApplicationTest");
263+
File serverXml = new File(springBootServerXmlDir, "invalid_server.xml");
264+
File newServerXml=new File(springBootServerXmlDir, "server.xml");
265+
Files.copy(serverXml.toPath(),newServerXml.toPath(),StandardCopyOption.REPLACE_EXISTING);
266+
Map<String, File> libertyDirPropMap = new HashMap<>();
267+
libertyDirPropMap.put(ServerFeatureUtil.SERVER_CONFIG_DIR, springBootServerXmlDir);
268+
assertThrows(PluginExecutionException.class, ()->new ServerConfigDocument(new TestLogger(), null, libertyDirPropMap));
269+
}
239270
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<server description="new server">
3+
4+
<featureManager>
5+
<feature>servlet-6.0</feature>
6+
<feature>springBoot-3.0</feature>
7+
</featureManager>
8+
9+
<httpEndpoint id="defaultHttpEndpoint"
10+
host="*"
11+
httpPort="9080"
12+
httpsPort="9443" />
13+
<springBootApplication id="guide-spring-boot"
14+
location="guide-spring-boot-0.1.0.jar"
15+
name="guide-spring-boot" />
16+
<springBootApplication id="guide-spring-boot"
17+
location="guide-spring-boot-0.1.0.jar"
18+
name="guide-spring-boot" />
19+
</server>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<server description="new server">
3+
4+
<featureManager>
5+
<feature>servlet-6.0</feature>
6+
<feature>springBoot-3.0</feature>
7+
</featureManager>
8+
9+
<httpEndpoint id="defaultHttpEndpoint"
10+
host="*"
11+
httpPort="9080"
12+
httpsPort="9443" />
13+
<springBootApplication id="guide-spring-boot"
14+
location="guide-spring-boot-0.1.0.jar"
15+
name="guide-spring-boot" />
16+
<springBootApplication id="guide-spring-boot"
17+
location="guide-spring-boot-0.1.0.jar"
18+
name="guide-spring-boot" />
19+
</server>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<server description="new server">
3+
4+
<featureManager>
5+
<feature>servlet-6.0</feature>
6+
<feature>springBoot-3.0</feature>
7+
</featureManager>
8+
9+
<httpEndpoint id="defaultHttpEndpoint"
10+
host="*"
11+
httpPort="9080"
12+
httpsPort="9443" />
13+
<springBootApplication id="guide-spring-boot"
14+
location="guide-spring-boot-0.1.0.jar"
15+
name="guide-spring-boot" />
16+
</server>

0 commit comments

Comments
 (0)