Skip to content

Instantly share code, notes, and snippets.

@talhaHavadar
Last active February 19, 2018 09:57
Show Gist options
  • Save talhaHavadar/fe1286cbf673bcc3f633ed8c083675b7 to your computer and use it in GitHub Desktop.
Save talhaHavadar/fe1286cbf673bcc3f633ed8c083675b7 to your computer and use it in GitHub Desktop.
WildflyCLIHelper to execute jboss cli from Java
package be.telenetgroup.services.tgapi.testing.support;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
/**
* Created by thavadar on 16/02/2018.
*
* Builds an operation command for WildFly CLI with the format below:
* /node-type=node-name:operation-name [( [parameter-name=parameter-value (,parameter-name=parameter-value)*] )]
*/
public class OperationTextBuilder {
private static OperationTextBuilder instance;
private Node node;
private OperationBuilder operationBuilder;
private static class Node {
String nodeType;
String nodeName;
Node(String nodeType, String nodeName) {
this.nodeType = nodeType;
this.nodeName = nodeName;
}
@Override
public String toString() {
return new StringBuilder().append("/")
.append(this.nodeType)
.append("=")
.append(this.nodeName).toString();
}
}
public static class OperationBuilder {
private Operation operation;
private static class Operation {
String operationName;
HashMap<String, String> parameters;
Operation(String operationName) {
this.operationName = operationName;
this.parameters = new HashMap<>();
}
void addParameter(String key, String value) {
this.parameters.put(key, value);
}
private String parametersString() {
if (CollectionUtils.isEmpty(this.parameters)) {
return "";
} else {
StringBuilder sb = new StringBuilder().append("(");
Iterator<Map.Entry<String, String>> iterator = this.parameters.entrySet().iterator();
Map.Entry<String, String> next;
while (Objects.nonNull(next = iterator.next())) {
sb.append(next.getKey())
.append("=")
.append(next.getValue());
if (iterator.hasNext()) {
sb.append(",");
}
}
return sb.append(")").toString();
}
}
@Override
public String toString() {
return new StringBuilder().append(":")
.append(operationName)
.append(this.parametersString()).toString();
}
}
private OperationBuilder(OperationBuilder.Operation operation) {
this.operation = operation;
}
public OperationBuilder addParameter(String key, String value) {
this.operation.addParameter(key, value);
return this;
}
public OperationTextBuilder then() {
return OperationTextBuilder.getInstance();
}
private Operation build() {
return this.operation;
}
}
private OperationTextBuilder(Node node) {
this.node = node;
}
private static OperationTextBuilder getInstance() {
return instance;
}
public static OperationTextBuilder withNode(String nodeType, String nodeName) {
instance = new OperationTextBuilder(new Node(nodeType, nodeName));
return instance;
}
public OperationBuilder operationName(String operationName) {
this.operationBuilder = new OperationBuilder(new OperationBuilder.Operation(operationName));
return this.operationBuilder;
}
public String build() {
OperationBuilder.Operation operation = this.operationBuilder.build();
if (Objects.nonNull(this.node) &&
Objects.nonNull(operation)) {
return new StringBuilder().append(this.node.toString()).append(operation.toString()).toString();
}
return "";
}
}
package be.telenetgroup.services.tgapi.testing.support;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
/**
* Created by thavadar on 16/02/2018.
*/
@Component
public class WildflyCLIHelper {
@Value("${wildfly.bin-path}")
private String WILDFLY_HOME;
public String readSystemProperty(String key) throws Exception {
StringBuffer output= new StringBuffer();
String scriptPath = getCLIScriptPath();
String text = OperationTextBuilder
.withNode("system-property", key)
.operationName("read-resource").then().build();
Process exec = Runtime.getRuntime().exec(scriptPath + " -c " + text);
exec.waitFor(1000, TimeUnit.MILLISECONDS);
exec.destroy();
BufferedReader reader =
new BufferedReader(new InputStreamReader(exec.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
return processResult(output.toString());
}
private String processResult(String result) {
JsonParser jsonParser = new JsonParser();
JsonObject jObj = jsonParser.parse(result.toString().replaceAll("=>", ":")).getAsJsonObject();
String outcome = jObj.get("outcome").getAsString();
switch (outcome) {
case "success":
return jObj.get("result").getAsJsonObject().get("value").getAsString();
case "failed":
throw new IllegalArgumentException(jObj.get("failure-description").getAsString());
}
return null;
}
private String getCLIScriptPath() {
return new StringBuilder().append(WILDFLY_HOME).append("\\").append("jboss-cli.bat").toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment