private static void _backout(VM vm, Manager manager, Caller caller) {
   try {
     manager.trash(vm.getID(), Manager.INSTANCE, caller);
   } catch (Throwable t) {
     final String msg = "Problem backing out id-" + vm.getID() + ": ";
     if (logger.isDebugEnabled()) {
       logger.error(msg + t.getMessage(), t);
     } else {
       logger.error(msg + t.getMessage());
     }
   }
 }
  public TerminateInstancesResponseType terminate(
      TerminateInstancesType req, Caller caller, Manager manager) throws RemoteException {

    if (manager == null) {
      throw new IllegalArgumentException("manager may not be null");
    }
    if (caller == null) {
      throw new IllegalArgumentException("caller may not be null");
    }
    if (req == null) {
      throw new IllegalArgumentException("req may not be null");
    }

    final String[] elasticInstIDs = this.getElasticIDs(req);
    if (elasticInstIDs.length == 0) {
      throw new RemoteException("No instance IDs in termination request");
    }

    final String[] managerInstances = this.translateIDs(elasticInstIDs);

    // currentStates array needs to be same length as managerInstances and
    // elasticInstanceIDs, used to correlate later

    final InstanceStateType[] currentStates = new InstanceStateType[managerInstances.length];

    for (int i = 0; i < managerInstances.length; i++) {

      final String mgrInstanceID = managerInstances[i];

      try {
        final VM vm = manager.getInstance(mgrInstanceID);

        if (vm != null) {
          final State state = vm.getState();
          if (state != null) {
            final String mgrState = state.getState();
            final InstanceStateType ist = new InstanceStateType();
            ist.setName(StateMap.managerStringToElasticString(mgrState));
            ist.setCode(StateMap.managerStringToElasticInt(mgrState));
            currentStates[i] = ist;
          }
        }

      } catch (DoesNotExistException e) {
        currentStates[i] = null;
      } catch (ManageException e) {
        currentStates[i] = null;
        logger.error(e.getMessage());
      }
    }

    for (int i = 0; i < managerInstances.length; i++) {

      if (currentStates[i] == null) {
        continue;
      }

      final String mgrID = managerInstances[i];
      try {
        manager.trash(mgrID, Manager.INSTANCE, caller);
      } catch (DoesNotExistException e) {
        // do nothing, already accomplished
      } catch (ManageException e) {
        if (logger.isDebugEnabled()) {
          logger.error(e.getMessage(), e);
        } else {
          logger.error(e.getMessage());
        }
      }
    }

    final InstanceStateType terminated = new InstanceStateType();
    terminated.setCode(StateMap.STATE_TERMINATED.intValue());
    terminated.setName(StateMap.STATE_TERMINATED_STR);

    final InstanceStateType[] newStates = new InstanceStateType[managerInstances.length];

    for (int i = 0; i < managerInstances.length; i++) {

      if (currentStates[i] == null) {
        continue;
      }

      final String mgrInstanceID = managerInstances[i];

      try {
        final VM vm = manager.getInstance(mgrInstanceID);

        if (vm != null) {
          final State state = vm.getState();
          if (state != null) {
            final String mgrState = state.getState();
            final InstanceStateType ist = new InstanceStateType();
            ist.setName(StateMap.managerStringToElasticString(mgrState));
            ist.setCode(StateMap.managerStringToElasticInt(mgrState));
            newStates[i] = ist;
          }
        }

      } catch (DoesNotExistException e) {
        newStates[i] = terminated;
      } catch (ManageException e) {
        newStates[i] = terminated;
        logger.error(e.getMessage());
      }
    }

    final List retList = new LinkedList();

    for (int i = 0; i < newStates.length; i++) {

      if (currentStates[i] == null) {
        continue;
      }

      retList.add(
          new TerminateInstancesResponseItemType(
              elasticInstIDs[i], currentStates[i], newStates[i]));
    }

    final TerminateInstancesResponseItemType[] tirits =
        (TerminateInstancesResponseItemType[])
            retList.toArray(new TerminateInstancesResponseItemType[retList.size()]);

    final TerminateInstancesResponseInfoType tirtSet = new TerminateInstancesResponseInfoType();
    tirtSet.setItem(tirits);
    final TerminateInstancesResponseType tirt = new TerminateInstancesResponseType();
    tirt.setInstancesSet(tirtSet);
    return tirt;
  }