Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

springbootapplication node multiple occurrence fix #467

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public class ServerConfigDocument {
private Properties defaultProps;
private Map<String, File> libertyDirectoryPropertyToFile = null;

Optional<String> springBootAppNodeLocation=Optional.empty();
Optional<String> springBootAppNodeLocation = Optional.empty();
Optional<String> springBootAppNodeDocumentURI = Optional.empty();

private static final XPathExpression XPATH_SERVER_APPLICATION;
private static final XPathExpression XPATH_SERVER_WEB_APPLICATION;
Expand Down Expand Up @@ -505,19 +506,13 @@ private void parseApplication(Document doc, XPathExpression expression) throws X

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.");
throw new PluginExecutionException(String.format("Found multiple springBootApplication elements specified in the server configuration file %s. Only one springBootApplication can be configured per Liberty server.", doc.getDocumentURI()));
}
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)){
// checking whether any springBootAppNodeLocation already configured from other server configuration files
if(springBootAppNodeLocation.isPresent()){
throw new PluginExecutionException("Found multiple springBootApplication elements specified in the server configuration. Only one springBootApplication can be configured per Liberty server.");
}
springBootAppNodeLocation = Optional.of(nodeValue);
}
checkForSpringBootApplicationNode(doc, expression, 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 @@ -533,6 +528,20 @@ private void parseApplication(Document doc, XPathExpression expression) throws X
}
}

private void checkForSpringBootApplicationNode(Document doc, XPathExpression expression, String nodeValue) throws PluginExecutionException {
if(expression.equals(XPATH_SERVER_SPRINGBOOT_APPLICATION)){
// checking whether any springBootAppNodeLocation already configured from other server configuration files
if(springBootAppNodeLocation.isPresent() && springBootAppNodeDocumentURI.isPresent()){
throw new PluginExecutionException(String.format("SpringBootApplication elements are specified in multiple server configuration files [%s, %s]. Only one springBootApplication can be configured per Liberty server.", springBootAppNodeDocumentURI.get(), doc.getDocumentURI()));
cherylking marked this conversation as resolved.
Show resolved Hide resolved
}
else {
log.debug("Setting springBootApplication location as "+ nodeValue);
springBootAppNodeLocation = Optional.of(nodeValue);
springBootAppNodeDocumentURI = Optional.of(doc.getDocumentURI());
}
}
}

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 @@ -711,7 +720,9 @@ private void parseDocumentsInDirectory(File directory, ArrayList<Document> docs)
*/
public Document parseDocument(File file) throws FileNotFoundException, IOException {
try (FileInputStream is = new FileInputStream(file)) {
return parseDocument(is);
Document document= parseDocument(is);
document.setDocumentURI(file.getCanonicalPath());
return document;
} catch (SAXException ex) {
// If the file was not valid XML, assume it was some other non XML
// file in dropins.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public void testProcessServerXmlWithMultipleSpringBootNodesInMultipleConfigFiles
File validServerXml = new File(springBootServerXmlDir, "valid_server.xml");
File newValidServerXml = new File(SERVER_CONFIG_INCLUDE_DIR.toFile(), "valid_server.xml");
Files.copy(validServerXml.toPath(), newValidServerXml.toPath(), StandardCopyOption.REPLACE_EXISTING);
assertThrows("Found multiple springBootApplication elements specified in the server configuration. Only one springBootApplication can be configured per Liberty server.",
assertThrows("Expected multiple springBootApplication elements specified in the server configuration. Only one springBootApplication node is found.",
PluginExecutionException.class, () -> new ServerConfigDocument(new TestLogger(), null, libertyDirPropMap));
Files.delete(newIncludedServerXml.toPath());
Files.delete(newValidServerXml.toPath());
Expand Down
Loading