The UMM API allows you to programmatically create and store application configurations in the UMM database. The UMM GUI uses the same API to create users, passwords, applications configurations and configuration templates. See UMM Java API for UMM objects, constructors and methods.
Programs that create application XML configurations store them in the UMM Configuration Database as shown in the diagram, Architecture for Programming Application Configurations. The UMM Daemon serves the XML configuration to UM applications.
The following sample code provides a framework for creating an application configuration, test_application with two templates. 'template_1
' sets the file descriptor type option for a UM context. 'template_2
' sets the resolver multicast address and port. Use of the application configuration is also authenticated with a username and password.
package umm.api;
import java.io.CharArrayReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import umm.api.NameValue;
import umm.api.UMMAPI;
import umm.api.util.DOMUtil;
import umm.api.util.Util;
public class Example {
String test_application = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<application name=\"test_application\" template=\"\">\n"
+ " <contexts order=\"deny,allow\">\n"
+ "<context rule=\"allow\" template=\"\">\n"
+ "<sources order=\"deny,allow\"/>\n"
+ " <receivers order=\"deny,allow\"/>\n"
+ "<wildcard-receivers order=\"deny,allow\"/>\n"
+ " </context>\n"
+ " </contexts>\n"
+ "<event-queues order=\"deny,allow\">\n"
+ " <event-queue rule=\"allow\"/>\n"
+ "</event-queues>\n"
+ "</application>";
String template_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<template name=\"template_1\">\n"
+ "<options type=\"context\">\n"
+ "<option default-value=\"epoll\" name=\"fd_management_type\"/>\n"
+ "</options>\n"
+ "</template>";
String template_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<template name=\"template_2\">\n"
+ "<options type=\"context\">\n"
+ "<option default-value=\"224.9.10.11\" name=\"resolver_multicast_address\"/>\n"
+ "<option default-value=\"12965\" name=\"resolver_multicast_port\"/>\n"
+ "</options>\n"
+ "</template>";
UMMAPI api;
private void init(String machine, int port) throws Exception {
api = new UMMAPI(machine, port);
System.out.println("Init Success");
}
private void login(String userName, String password) throws Exception {
if (api.login(userName, password)) {
System.out.println("login Success");
return;
}
System.out.println("login Failure");
System.exit(-1);
}
public Example(String args[]) {
try {
String machine = args[0];
int port = Integer.parseInt(args[1]);
String userName = args[2];
String password = args[3];
init(machine, port);
login(userName, password);
ArrayList<NameValue> templateList = new ArrayList();
NameValue nv = api.saveTemplate("template_1", template_1);
if (nv == null) {
System.out.println("saveTemplate Failure");
System.exit(-1);
}
templateList.add(nv);
nv = api.saveTemplate("template_2", template_2);
if (nv == null) {
System.out.println("saveTemplate Failure");
System.exit(-1);
}
templateList.add(nv);
String xml = setTemplateXML(templateList, test_application);
if (xml == null) {
System.out.println("setTemplateXML Failure");
System.exit(-1);
}
if (api.saveApplication("test_application", xml) == null) {
System.out.println("saveApplication Failure");
System.exit(-1);
}
if (api.setTemplates("test_application", templateList) == false) {
System.out.println("setTemplates Failure");
System.exit(-1);
}
System.out.println("##############################################");
System.out.println(api.getConfig("test_application"));
} catch (Exception ex) {
Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
}
api.logOff();
}
private String setTemplateXML(ArrayList<NameValue> list, String xml) {
try {
StringBuilder sb = new StringBuilder();
for (NameValue nv : list) {
sb.append(nv.getName() + ",");
}
if (sb.length() > 0) {
sb.delete(sb.length() - 1, sb.length());
}
CharArrayReader characterStream = new CharArrayReader(xml.toCharArray());
InputSource is = new InputSource(characterStream);
Document document = DOMUtil.createDocument(is);
Node node = (Node) document.getDocumentElement();
node = DOMUtil.findNode("contexts", node);
if (node == null) {
System.out.println("No contexts node");
return null;
}
node = DOMUtil.findNode("context", node);
if (node == null) {
System.out.println("No context node");
return null;
}
if (node.getAttributes().getNamedItem("template") != null) {
node.getAttributes().getNamedItem("template").setNodeValue(sb.toString());
return DOMUtil.getXML(document);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static void main(String args[]) {
new Example(args);
System.exit(0);
}
}