/** * Iterate over a "operations" resource and extract the resource address * * <pre> * { * "operation-date" => "2016-07-11T16:00:30.930Z", * "domain-uuid" => "7be474f5-5be9-4040-9f19-8959cf603be0", * "access-mechanism" => "HTTP", * "remote-address" => "127.0.0.1/127.0.0.1", * "outcome" => "success", * "operations" => [{ * "operation" => "composite", * "address" => [], * "steps" => [{ * "address" => [ * ("profile" => "default"), * ("subsystem" => "mail"), * ("mail-session" => "default"), * ("server" => "smtp") * ], * "operation" => "write-attribute", * "name" => "ssl", * "value" => true * }], * "operation-headers" => { * "access-mechanism" => "HTTP", * "caller-type" => "user" * } * }] * }, * * </pre> * * and concatenate to the following form * * <pre>profile = default / subsystem = mail / mail-session = default / server = smtp</pre> * * @param changeItem The ModelNode * @return The formatted resource address as in * <pre>profile = default / subsystem = mail / mail-session = default / server = smtp</pre> */ private String extractResourceAddress(ModelNode changeItem) { StringBuilder address = new StringBuilder(); ModelNode operations = changeItem.get(OPERATIONS); for (ModelNode op1 : operations.asList()) { String opName = op1.get(OP).asString(); if (COMPOSITE.equals(opName)) { List<ModelNode> steps = op1.get(STEPS).asList(); for (int idxStep = 0; idxStep < steps.size(); idxStep++) { ModelNode step = steps.get(idxStep); if (step.hasDefined(OP_ADDR)) { ModelNode addressNode = step.get(OP_ADDR); List<ModelNode> modelNodes = addressNode.asList(); for (int i = 0; i < modelNodes.size(); i++) { ModelNode addr = modelNodes.get(i); Property p = addr.asProperty(); address.append(p.getName()).append(" = ").append(p.getValue().asString()); if (i + 1 < modelNodes.size()) address.append(" / "); } } // separates each step resource address if (idxStep + 1 < steps.size()) address.append(" | "); } } else { if (op1.hasDefined(OP_ADDR)) { ModelNode addressNode = op1.get(OP_ADDR); List<ModelNode> modelNodes = addressNode.asList(); for (int i = 0; i < modelNodes.size(); i++) { ModelNode addr = modelNodes.get(i); Property p = addr.asProperty(); address.append(p.getName()).append(" = ").append(p.getValue().asString()); if (i + 1 < modelNodes.size()) address.append(" / "); } } } } return address.toString(); }
public static Object downCast(ModelNode value, ModelNode metadata) { Object result = null; ModelType targetType = resolveTypeFromMetaData(metadata); switch (targetType) { case STRING: result = value.asString(); break; case INT: result = value.asInt(); break; case LONG: result = value.asLong(); break; case BOOLEAN: result = value.asBoolean(); break; case BIG_DECIMAL: result = value.asBigDecimal(); break; case BIG_INTEGER: result = value.asBigInteger(); break; case DOUBLE: result = value.asDouble(); break; case LIST: { try { List<ModelNode> items = value.asList(); List<String> list = new ArrayList<String>(items.size()); for (ModelNode item : items) list.add(item.asString()); result = list; } catch (Throwable t) { t.printStackTrace(); result = new ArrayList<>(); // syntax errors } break; } case PROPERTY: { // it's actually interpreted as a property list, but that ttype doesn'ty really exist // (yet) try { List<Property> properties = value.asPropertyList(); Map<String, String> map = new HashMap<>(); for (Property item : properties) map.put(item.getName(), item.getValue().asString()); result = map; } catch (Throwable t) { t.printStackTrace(); result = new HashMap(); // syntax errors } break; } case UNDEFINED: break; default: throw new RuntimeException("Unexpected targetType " + targetType); } return result; }