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;
  }
  @Override
  public ModelNode buildRequest(CommandContext ctx) throws OperationFormatException {

    DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
    ParsedArguments args = ctx.getParsedArguments();

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

    final String name;
    try {
      name = this.name.getValue(args);
    } catch (IllegalArgumentException e) {
      throw new OperationFormatException(e.getLocalizedMessage());
    }

    builder.addNode("subsystem", "jms");
    builder.addNode("queue", name);
    builder.setOperationName("add");

    ModelNode entriesNode = builder.getModelNode().get("entries");
    final String entriesStr = this.entries.getValue(args);
    if (entriesStr == null) {
      entriesNode.add(name);
    } else {
      String[] split = entriesStr.split(",");
      for (int i = 0; i < split.length; ++i) {
        String entry = split[i].trim();
        if (!entry.isEmpty()) {
          entriesNode.add(entry);
        }
      }
    }

    final String selector = this.selector.getValue(args);
    if (selector != null) {
      builder.addProperty("selector", selector);
    }

    final String durable = this.durable.getValue(args);
    if (durable != null) {
      builder.addProperty("durable", durable);
    }

    return builder.buildRequest();
  }
  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();
  }
Пример #4
0
  public ModelNode buildRequest(CommandContext ctx) throws OperationFormatException {

    if (!ctx.hasArguments()) {
      throw new OperationFormatException("Required arguments are missing.");
    }

    String filePath = null;
    String name = null;
    String runtimeName = null;

    for (String arg : ctx.getArguments()) {
      if (filePath == null) {
        filePath = arg;
      } else if (name == null) {
        name = arg;
      } else {
        runtimeName = arg;
      }
    }

    if (filePath == null) {
      throw new OperationFormatException("File path is missing.");
    }

    File f = new File(filePath);
    if (!f.exists()) {
      throw new OperationFormatException(f.getAbsolutePath() + " doesn't exist.");
    }

    if (name == null) {
      name = f.getName();
    }

    if (Util.isDeployed(name, ctx.getModelControllerClient())) {
      if (ctx.hasSwitch("f")) {
        DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();

        // replace
        builder = new DefaultOperationRequestBuilder();
        builder.setOperationName("full-replace-deployment");
        builder.addProperty("name", name);
        if (runtimeName != null) {
          builder.addProperty("runtime-name", runtimeName);
        }

        byte[] bytes = readBytes(f);
        builder.getModelNode().get("bytes").set(bytes);
        return builder.buildRequest();
      } else {
        throw new OperationFormatException(
            "'" + name + "' is already deployed (use -f to force re-deploy).");
      }
    }

    ModelNode composite = new ModelNode();
    composite.get("operation").set("composite");
    composite.get("address").setEmptyList();
    ModelNode steps = composite.get("steps");

    DefaultOperationRequestBuilder builder;

    // add
    builder = new DefaultOperationRequestBuilder();
    builder.setOperationName("add");
    builder.addNode("deployment", name);
    if (runtimeName != null) {
      builder.addProperty("runtime-name", runtimeName);
    }

    byte[] bytes = readBytes(f);
    builder.getModelNode().get("bytes").set(bytes);
    steps.add(builder.buildRequest());

    // deploy
    builder = new DefaultOperationRequestBuilder();
    builder.setOperationName("deploy");
    builder.addNode("deployment", name);
    steps.add(builder.buildRequest());

    return composite;
  }