class GatewaySenderDestroyFunction implements InternalFunction<GatewaySenderDestroyFunctionArgs> {

  @Override
  @SuppressWarnings("deprecation")
  public void execute(FunctionContext<GatewaySenderDestroyFunctionArgs> context) {
    ResultSender<Object> resultSender = context.getResultSender();

    Cache cache = context.getCache();
    String memberNameOrId =
        CliUtil.getMemberNameOrId(cache.getDistributedSystem().getDistributedMember());

    GatewaySenderDestroyFunctionArgs gatewaySenderDestroyFunctionArgs =
        context.getArguments();

    String senderId = gatewaySenderDestroyFunctionArgs.getId();
    boolean ifExists = gatewaySenderDestroyFunctionArgs.isIfExists();
    GatewaySender gatewaySender = cache.getGatewaySender(senderId);
    if (gatewaySender == null) {
      String message = "Gateway sender " + senderId + " not found.";
      if (ifExists) {
        resultSender
            .lastResult(new CliFunctionResult(memberNameOrId, true, "Skipping: " + message));
      } else {
        resultSender.lastResult(new CliFunctionResult(memberNameOrId, false, message));
      }
      return;
    }

    try {
      gatewaySender.stop();
      gatewaySender.destroy();
      XmlEntity xmlEntity = new XmlEntity(CacheXml.GATEWAY_SENDER, "id", senderId);
      resultSender.lastResult(new CliFunctionResult(memberNameOrId, xmlEntity,
          String.format("GatewaySender \"%s\" destroyed on \"%s\"", senderId, memberNameOrId)));
    } catch (Exception e) {
      resultSender.lastResult(new CliFunctionResult(memberNameOrId, e, ""));
    }
  }

}