class RegionDestroyFunction implements InternalFunction<String> {

  @Override
  @SuppressWarnings("deprecation")
  public void execute(FunctionContext<String> context) {
    String regionPath = context.getArguments();
    String memberName = context.getMemberName();

    try {
      String functionId = context.getFunctionId();

      if (!getId().equals(functionId) || regionPath == null) {
        context.getResultSender().lastResult(new CliFunctionResult("", false,
            "Function Id mismatch or arguments is not available."));
        return;
      }

      Cache cache = ((InternalCache) context.getCache()).getCacheForProcessingClientRequests();
      Region<?, ?> region = cache.getRegion(regionPath);
      // the region is already destroyed by another member
      if (region == null) {
        context.getResultSender().lastResult(new CliFunctionResult(memberName, true,
            String.format("Region '%s' already destroyed", regionPath)));
        return;
      }

      region.destroyRegion();

      String regionName =
          regionPath.startsWith(SEPARATOR) ? regionPath.substring(1) : regionPath;
      XmlEntity xmlEntity = new XmlEntity(CacheXml.REGION, "name", regionName);
      context.getResultSender().lastResult(new CliFunctionResult(memberName, xmlEntity,
          String.format("Region '%s' destroyed successfully", regionPath)));

    } catch (IllegalStateException ex) {
      // user is trying to destroy something that can't destroyed, like co-location
      context.getResultSender()
          .lastResult(new CliFunctionResult(memberName, false, ex.getMessage()));
    } catch (RegionDestroyedException ex) {
      context.getResultSender().lastResult(new CliFunctionResult(memberName, true,
          String.format("Region '%s' already destroyed", regionPath)));
    } catch (Exception ex) {
      LogService.getLogger().error(ex.getMessage(), ex);
      context.getResultSender().lastResult(new CliFunctionResult(memberName, ex, ex.getMessage()));
    }
  }

}