@Override
    public BatchedCommand toBatchedCommand(String line) throws OperationFormatException {

      if (line.isEmpty()) {
        throw new IllegalArgumentException("Null command line.");
      }

      final String originalCommand = this.cmd;
      final String originalArguments = this.parsedArgs.getArgumentsString();
      if (isOperation(line)) {
        try {
          setArgs(null, line);
          DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder(getPrefix());
          parser.parse(line, builder);
          ModelNode request = builder.buildRequest();
          StringBuilder op = new StringBuilder();
          op.append(prefixFormatter.format(builder.getAddress()));
          op.append(line.substring(line.indexOf(':')));
          return new DefaultBatchedCommand(op.toString(), request);
        } finally {
          setArgs(originalCommand, originalArguments);
        }
      }

      String cmd = line;
      String cmdArgs = null;
      for (int i = 0; i < cmd.length(); ++i) {
        if (Character.isWhitespace(cmd.charAt(i))) {
          cmdArgs = cmd.substring(i + 1).trim();
          cmd = cmd.substring(0, i);
          break;
        }
      }

      CommandHandler handler = cmdRegistry.getCommandHandler(cmd.toLowerCase());
      if (handler == null) {
        throw new OperationFormatException("No command handler for '" + cmd + "'.");
      }
      if (!(handler instanceof OperationCommand)) {
        throw new OperationFormatException("The command is not allowed in a batch.");
      }

      try {
        setArgs(cmd, cmdArgs);
        ModelNode request = ((OperationCommand) handler).buildRequest(this);
        return new DefaultBatchedCommand(line, request);
      } finally {
        setArgs(originalCommand, originalArguments);
      }
    }
  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;
  }
  /* (non-Javadoc)
   * @see org.jboss.as.cli.handlers.CommandHandlerWithHelp#doHandle(org.jboss.as.cli.CommandContext)
   */
  @Override
  protected void doHandle(CommandContext ctx) {

    if (!ctx.hasArguments()) {
      ctx.printLine("Missing required argument 'name'.");
      return;
    }

    String name = ctx.getNamedArgument("name");
    if (name == null) {
      List<String> args = ctx.getArguments();
      if (!args.isEmpty()) {
        name = args.get(0);
      }
    }

    if (name == null) {
      ctx.printLine("Missing required argument 'name'.");
      return;
    }

    ModelControllerClient client = ctx.getModelControllerClient();

    DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();
    builder.addNode("subsystem", "jms");
    builder.addNode("connection-factory", name);
    builder.setOperationName("remove");

    final ModelNode result;
    try {
      ModelNode request = builder.buildRequest();
      result = client.execute(request);
    } catch (Exception e) {
      ctx.printLine("Failed to perform operation: " + e.getLocalizedMessage());
      return;
    }

    if (!Util.isSuccess(result)) {
      ctx.printLine(
          "Failed to delete connection factory '"
              + name
              + "': "
              + Util.getFailureDescription(result));
      return;
    }

    ctx.printLine("Removed connection factory " + name);
  }
  @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();
  }
  @Override
  protected void doHandle(CommandContext ctx) {

    ModelControllerClient client = ctx.getModelControllerClient();
    ParsedArguments args = ctx.getParsedArguments();
    boolean l = this.l.isPresent(args);
    if (!args.hasArguments() || l) {
      printList(ctx, Util.getDeployments(client), l);
      return;
    }

    final String name = this.name.getValue(ctx.getParsedArguments());

    if (name == null) {
      printList(ctx, Util.getDeployments(client), l);
      return;
    }

    DefaultOperationRequestBuilder builder;

    // undeploy
    builder = new DefaultOperationRequestBuilder();
    builder.setOperationName("undeploy");
    builder.addNode("deployment", name);

    ModelNode result;
    try {
      ModelNode request = builder.buildRequest();
      result = client.execute(request);
    } catch (Exception e) {
      ctx.printLine("Failed to undeploy: " + e.getLocalizedMessage());
      return;
    }

    // TODO undeploy may fail if the content failed to deploy but remove should still be executed
    if (!Util.isSuccess(result)) {
      ctx.printLine("Undeploy failed: " + Util.getFailureDescription(result));
      return;
    }

    // remove
    builder = new DefaultOperationRequestBuilder();
    builder.setOperationName("remove");
    builder.addNode("deployment", name);
    try {
      ModelNode request = builder.buildRequest();
      result = client.execute(request);
    } catch (Exception e) {
      ctx.printLine(
          "Failed to remove the deployment content from the repository: "
              + e.getLocalizedMessage());
      return;
    }
    if (!Util.isSuccess(result)) {
      ctx.printLine("Remove failed: " + Util.getFailureDescription(result));
      return;
    }

    ctx.printLine("Successfully undeployed " + name + ".");
  }
  @Override
  public ModelNode buildRequest(CommandContext ctx) throws OperationFormatException {

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

    final String name = this.name.getValue(ctx.getParsedArguments());
    if (name == null) {
      throw new OperationFormatException("Required argument name are missing.");
    }

    DefaultOperationRequestBuilder builder;

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

    builder = new DefaultOperationRequestBuilder();
    builder.setOperationName("remove");
    builder.addNode("deployment", name);
    steps.add(builder.buildRequest());
    return composite;
  }
  protected static void processLine(final CommandContextImpl cmdCtx, String line) {
    if (line.isEmpty()) {
      return;
    }

    if (isOperation(line)) {
      cmdCtx.setArgs(null, line);
      if (cmdCtx.isBatchMode()) {
        DefaultOperationRequestBuilder builder =
            new DefaultOperationRequestBuilder(cmdCtx.getPrefix());
        try {
          cmdCtx.getOperationRequestParser().parse(line, builder);
          ModelNode request = builder.buildRequest();
          StringBuilder op = new StringBuilder();
          op.append(cmdCtx.getPrefixFormatter().format(builder.getAddress()));
          op.append(line.substring(line.indexOf(':')));
          DefaultBatchedCommand batchedCmd = new DefaultBatchedCommand(op.toString(), request);
          Batch batch = cmdCtx.getBatchManager().getActiveBatch();
          batch.add(batchedCmd);
          cmdCtx.printLine("#" + batch.size() + " " + batchedCmd.getCommand());
        } catch (CommandFormatException e) {
          cmdCtx.printLine(e.getLocalizedMessage());
        }
      } else {
        cmdCtx.operationHandler.handle(cmdCtx);
      }

    } else {
      String cmd = line;
      String cmdArgs = null;
      for (int i = 0; i < cmd.length(); ++i) {
        if (Character.isWhitespace(cmd.charAt(i))) {
          cmdArgs = cmd.substring(i + 1).trim();
          cmd = cmd.substring(0, i);
          break;
        }
      }
      cmdCtx.setArgs(cmd, cmdArgs);

      CommandHandler handler = cmdRegistry.getCommandHandler(cmd.toLowerCase());
      if (handler != null) {
        if (cmdCtx.isBatchMode() && handler.isBatchMode()) {
          if (!(handler instanceof OperationCommand)) {
            cmdCtx.printLine("The command is not allowed in a batch.");
          } else {
            try {
              ModelNode request = ((OperationCommand) handler).buildRequest(cmdCtx);
              BatchedCommand batchedCmd = new DefaultBatchedCommand(line, request);
              Batch batch = cmdCtx.getBatchManager().getActiveBatch();
              batch.add(batchedCmd);
              cmdCtx.printLine("#" + batch.size() + " " + batchedCmd.getCommand());
            } catch (OperationFormatException e) {
              cmdCtx.printLine("Failed to add to batch: " + e.getLocalizedMessage());
            }
          }
        } else {
          handler.handle(cmdCtx);
        }
      } else {
        cmdCtx.printLine(
            "Unexpected command '" + line + "'. Type 'help' for the list of supported commands.");
      }
    }
    cmdCtx.setArgs(null, null);
  }
  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();
  }
Beispiel #9
0
  @Override
  protected void doHandle(CommandContext ctx) {

    ModelControllerClient client = ctx.getModelControllerClient();

    if (!ctx.hasArguments()) {
      printList(ctx, Util.getDeployments(client));
      return;
    }

    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) {
      printList(ctx, Util.getDeployments(client));
      return;
    }

    File f = new File(filePath);
    if (!f.exists()) {
      ctx.printLine("The path doesn't exist: " + f.getAbsolutePath());
      return;
    }

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

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

        ModelNode result;

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

        FileInputStream is = null;
        try {
          is = new FileInputStream(f);
          ModelNode request = builder.buildRequest();
          OperationBuilder op = OperationBuilder.Factory.create(request);
          op.addInputStream(is);
          request.get("input-stream-index").set(0);
          result = client.execute(op.build());
        } catch (Exception e) {
          ctx.printLine("Failed to replace the deployment: " + e.getLocalizedMessage());
          return;
        } finally {
          StreamUtils.safeClose(is);
        }
        if (!Util.isSuccess(result)) {
          ctx.printLine(Util.getFailureDescription(result));
          return;
        }

        ctx.printLine("'" + name + "' re-deployed successfully.");
      } else {
        ctx.printLine("'" + name + "' is already deployed (use -f to force re-deploy).");
      }

      return;
    } else {

      DefaultOperationRequestBuilder builder;

      ModelNode result;

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

      FileInputStream is = null;
      try {
        is = new FileInputStream(f);
        ModelNode request = builder.buildRequest();
        OperationBuilder op = OperationBuilder.Factory.create(request);
        op.addInputStream(is);
        request.get("input-stream-index").set(0);
        result = client.execute(op.build());
      } catch (Exception e) {
        ctx.printLine(
            "Failed to add the deployment content to the repository: " + e.getLocalizedMessage());
        return;
      } finally {
        StreamUtils.safeClose(is);
      }
      if (!Util.isSuccess(result)) {
        ctx.printLine(Util.getFailureDescription(result));
        return;
      }

      // deploy
      builder = new DefaultOperationRequestBuilder();
      builder.setOperationName("deploy");
      builder.addNode("deployment", name);
      try {
        ModelNode request = builder.buildRequest();
        result = client.execute(request);
      } catch (Exception e) {
        ctx.printLine("Failed to deploy: " + e.getLocalizedMessage());
        return;
      }
      if (!Util.isSuccess(result)) {
        ctx.printLine(Util.getFailureDescription(result));
        return;
      }
      ctx.printLine("'" + name + "' deployed successfully.");
    }
  }
Beispiel #10
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;
  }