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

Create override file to set applicationMonitor updateTrigger #408

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* (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 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<String> APP_MON_VALUE_LIST = Arrays.asList("polled", "mbean", "disabled");

String tool = null;

HashMap<String, String> attributes = new HashMap<>();

public ApplicationMonitorConfigXmlDocument(String tool) {
this.tool = tool;
}

public void writeAppMonitorConfigXmlDocument(File serverDirectory, String appMonitorTrigger) throws IOException, TransformerException, ParserConfigurationException, PluginExecutionException {
File appMonXml = getAppMonitorConfigXmlFile(serverDirectory);
// if applicationMonitor not set, return
if (appMonitorTrigger == null) {
if (appMonXml.exists()) {
appMonXml.delete();
}
return;
}
// continue with creating configDropins/override file
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", appMonitorTrigger);

if (!appMonXml.getParentFile().exists()) {
appMonXml.getParentFile().mkdirs();
}

ServerConfigXmlDocument configDocument = ServerConfigXmlDocument.newInstance();
configDocument.createGeneratedByComment(tool);
configDocument.createServerElementWithAttributes("applicationMonitor", attributes);
configDocument.writeXMLDocument(appMonXml);
}

public File getAppMonitorConfigXmlFile(File serverDirectory) {
File f = new File(serverDirectory, "configDropins/overrides/" + APPLICATION_CONFIG_XML_FILENAME);
return f;
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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() {
}
Expand Down Expand Up @@ -71,13 +70,22 @@ public static ServerConfigXmlDocument newInstance(File f) throws ParserConfigura
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 createGeneratedByComment(String tool) {
String comment = String.format(HEADER, tool);
createComment(comment);
}

private void appendBeforeBlanks(Element elem, Node childElement) {
Node lastchild = elem.getLastChild();
if (isWhitespace(lastchild)) {
Expand Down Expand Up @@ -143,6 +151,19 @@ public boolean removeFMComment(String comment) {
return false;
}

/**
* Creates element of `elementName`, with attributes/values.
* @param elementName - name of the element, ie: <applicationMonitor>
* @param attributes - HashMap of the <attributes,values>
*/
public void createServerElementWithAttributes(String elementName, HashMap<String, String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.openliberty.tools.common.plugins.config;

import static org.junit.Assert.assertFalse;
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, "mbean");

File newFile = appMonXml.getAppMonitorConfigXmlFile(serverDirectory);
String content = Files.readString(newFile.toPath());
assertTrue(content.contains("Generated by liberty-test-plugin"));
assertTrue(content.contains("<applicationMonitor updateTrigger=\"mbean\"/>"));
}

@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("The appMonitorTrigger value \"asdf\" 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());
}
}