@SuppressWarnings("unchecked")
  private void removeDeletedResources(
      Map<Integer, List<AppdefEntityID>> agentCache, Collection<String> typeNames)
      throws ApplicationException, VetoException {
    final boolean debug = log.isDebugEnabled();
    final StopWatch watch = new StopWatch();
    final AuthzSubject subject = authzSubjectManager.findSubjectById(AuthzConstants.overlordId);
    if (debug) watch.markTimeBegin("unscheduleMeasurementsForAsyncDelete");
    unscheduleMeasurementsForAsyncDelete(agentCache);
    if (debug) watch.markTimeEnd("unscheduleMeasurementsForAsyncDelete");

    // Look through services, servers, platforms, applications, and groups
    if (debug) watch.markTimeBegin("removeApplications");
    Collection<Application> applications = applicationManager.findDeletedApplications();
    removeApplications(subject, applications);
    if (debug) watch.markTimeEnd("removeApplications");

    if (debug) watch.markTimeBegin("removeResourceGroups");
    Collection<ResourceGroup> groups = resourceGroupManager.findDeletedGroups();
    removeResourceGroups(subject, groups);
    if (debug) watch.markTimeEnd("removeResourceGroups");

    typeNames = (typeNames == null) ? Collections.EMPTY_LIST : typeNames;
    if (debug) watch.markTimeBegin("removeGroupsCompatibleWith");
    for (String name : typeNames) {
      resourceGroupManager.removeGroupsCompatibleWith(name);
    }
    if (debug) watch.markTimeEnd("removeGroupsCompatibleWith");

    Collection<Service> services = serviceManager.findDeletedServices();
    removeServices(subject, services);

    Collection<Server> servers = serverManager.findDeletedServers();
    removeServers(subject, servers);

    if (debug) watch.markTimeBegin("removePlatforms");
    Collection<Platform> platforms = platformManager.findDeletedPlatforms();
    removePlatforms(subject, platforms);
    if (debug) watch.markTimeEnd("removePlatforms");
    if (debug) log.debug("removeDeletedResources: " + watch);
  }
 private final void removeServices(AuthzSubject subject, Collection<Service> services) {
   final StopWatch watch = new StopWatch();
   watch.markTimeBegin("removeServices");
   final List<Service> svcs = new ArrayList<Service>(services);
   // can't use iterator for loop here. Since we are modifying the
   // internal hibernate collection, which this collection is based on,
   // it will throw a ConcurrentModificationException
   // This occurs even if you disassociate the Collection by trying
   // something like new ArrayList(services). Not sure why.
   for (int i = 0; i < svcs.size(); i++) {
     try {
       final Service service = svcs.get(i);
       appdefBoss.removeService(subject, service.getId());
     } catch (Exception e) {
       log.error("Unable to remove service: " + e, e);
     }
   }
   watch.markTimeEnd("removeServices");
   if (log.isDebugEnabled()) {
     log.debug("Removed " + services.size() + " services");
   }
 }