@Override
public ModelNode buildRequest(CommandContext ctx) throws OperationFormatException {
if(!ctx.hasArguments()) {
throw new OperationFormatException("Arguments are missing");
}
//String target = null;
String restype = null;
//String description = null;
String propsStr = null;
//boolean enabled = false;
String jndiName = null;
String[] args = ctx.getCommandArguments().split("\\s+");
int i = 0;
while(i < args.length) {
String arg = args[i++];
if(arg.equals("--restype")) {
if(i < args.length) {
restype = args[i++];
}
} else if(arg.equals("--target")) {
// if(i < args.length) {
// target = args[i++];
// }
} else if(arg.equals("--description")) {
// if(i < args.length) {
// restype = args[i++];
// }
} else if(arg.equals("--property")) {
if (i < args.length) {
propsStr = args[i++];
}
} else if(arg.equals("--enabled")) {
// if (i < args.length) {
// enabled = Boolean.parseBoolean(args[i++]);
// }
} else {
jndiName = arg;
}
}
if(restype == null) {
throw new OperationFormatException("Required parameter --restype is missing.");
}
if(jndiName == null) {
throw new OperationFormatException("JNDI name is missing.");
}
String name = null;
final Map<String, String> props;
if(propsStr != null) {
props = new HashMap<String, String>();
String[] propsArr = propsStr.split(":");
for(String prop : propsArr) {
int equalsIndex = prop.indexOf('=');
if(equalsIndex < 0 || equalsIndex == prop.length() - 1) {
throw new OperationFormatException("Failed to parse property '" + prop + "'");
}
String propName = prop.substring(0, equalsIndex).trim();
String propValue = prop.substring(equalsIndex + 1).trim();
if(propName.isEmpty()) {
throw new OperationFormatException("Failed to parse property '" + prop + "'");
}
if(propName.equals("imqDestinationName") ||propName.equalsIgnoreCase("name")) {
name = propValue;
} else if("ClientId".equals(propName)) {
props.put("client-id", propValue);
}
}
} else {
props = Collections.emptyMap();
}
if(name == null) {
name = jndiName.replace('/', '_');
}
if(restype.equals("javax.jms.Queue")) {
DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
builder.addNode("subsystem", "jms");
builder.addNode("queue", name);
builder.setOperationName("add");
builder.getModelNode().get("entries").add(jndiName);
for(String prop : props.keySet()) {
builder.addProperty(prop, props.get(prop));
}
return builder.buildRequest();
} else if(restype.equals("javax.jms.Topic")) {
DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
builder.addNode("subsystem", "jms");
builder.addNode("topic", name);
builder.setOperationName("add");
builder.getModelNode().get("entries").add(jndiName);
for(String prop : props.keySet()) {
builder.addProperty(prop, props.get(prop));
}
return builder.buildRequest();
} else if(restype.equals("javax.jms.ConnectionFactory") ||
restype.equals("javax.jms.TopicConnectionFactory") ||
restype.equals("javax.jms.QueueConnectionFactory")) {
DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
builder.addNode("subsystem", "jms");
builder.addNode("connection-factory", name);
builder.setOperationName("add");
builder.getModelNode().get("entries").add(jndiName);
for(String prop : props.keySet()) {
builder.addProperty(prop, props.get(prop));
}
return builder.buildRequest();
} else {
throw new OperationFormatException("Resource type " + restype + " isn't supported.");
}
}