Ejemplo n.º 1
0
  protected ModelNode buildWritePropertyRequest(CommandContext ctx) throws CommandFormatException {

    final String name = this.name.getValue(ctx.getParsedCommandLine(), true);

    ModelNode composite = new ModelNode();
    composite.get(Util.OPERATION).set(Util.COMPOSITE);
    composite.get(Util.ADDRESS).setEmptyList();
    ModelNode steps = composite.get(Util.STEPS);

    final ParsedCommandLine args = ctx.getParsedCommandLine();

    final String profile;
    if (isDependsOnProfile() && ctx.isDomainMode()) {
      profile = this.profile.getValue(args);
      if (profile == null) {
        throw new OperationFormatException("--profile argument value is missing.");
      }
    } else {
      profile = null;
    }

    final Map<String, CommandArgument> nodeProps = loadArguments(ctx, null);
    for (String argName : args.getPropertyNames()) {
      if (isDependsOnProfile() && argName.equals("--profile")
          || this.name.getFullName().equals(argName)) {
        continue;
      }

      final ArgumentWithValue arg = (ArgumentWithValue) nodeProps.get(argName);
      if (arg == null) {
        throw new CommandFormatException("Unrecognized argument name '" + argName + "'");
      }

      DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
      if (profile != null) {
        builder.addNode(Util.PROFILE, profile);
      }

      for (OperationRequestAddress.Node node : getRequiredAddress()) {
        builder.addNode(node.getType(), node.getName());
      }
      builder.addNode(getRequiredType(), name);
      builder.setOperationName(Util.WRITE_ATTRIBUTE);
      final String propName;
      if (argName.charAt(1) == '-') {
        propName = argName.substring(2);
      } else {
        propName = argName.substring(1);
      }
      builder.addProperty(Util.NAME, propName);

      final String valueString = args.getPropertyValue(argName);
      ModelNode nodeValue = arg.getValueConverter().fromString(valueString);
      builder.getModelNode().get(Util.VALUE).set(nodeValue);

      steps.add(builder.buildRequest());
    }

    return composite;
  }
Ejemplo n.º 2
0
  protected ModelNode buildOperationRequest(CommandContext ctx, final String operation)
      throws CommandFormatException {

    ParsedCommandLine args = ctx.getParsedCommandLine();

    DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
    if (ctx.isDomainMode()) {
      final String profile = this.profile.getValue(args);
      if (profile == null) {
        throw new OperationFormatException("Required argument --profile is missing.");
      }
      builder.addNode("profile", profile);
    }

    final String name = this.name.getValue(ctx.getParsedCommandLine(), true);

    for (OperationRequestAddress.Node node : nodePath) {
      builder.addNode(node.getType(), node.getName());
    }
    builder.addNode(type, name);
    builder.setOperationName(operation);

    final Map<String, CommandArgument> argsMap = loadArguments(ctx, operation);

    for (String argName : args.getPropertyNames()) {
      if (argName.equals("--profile")) {
        continue;
      }

      if (argsMap == null) {
        if (argName.equals(this.name.getFullName())) {
          continue;
        }
        throw new CommandFormatException(
            "Command '"
                + operation
                + "' is not expected to have arguments other than "
                + this.name.getFullName()
                + ".");
      }

      final ArgumentWithValue arg = (ArgumentWithValue) argsMap.get(argName);
      if (arg == null) {
        if (argName.equals(this.name.getFullName())) {
          continue;
        }
        throw new CommandFormatException(
            "Unrecognized argument " + argName + " for command '" + operation + "'.");
      }

      final String propName;
      if (argName.charAt(1) == '-') {
        propName = argName.substring(2);
      } else {
        propName = argName.substring(1);
      }

      final String valueString = args.getPropertyValue(argName);
      ModelNode nodeValue = arg.getValueConverter().fromString(valueString);
      builder.getModelNode().get(propName).set(nodeValue);
    }

    return builder.buildRequest();
  }