private final void removeApplications(
     AuthzSubject subject, Collection<Application> applications) {
   for (Application application : applications) {
     try {
       applicationManager.removeApplication(subject, application.getId());
     } catch (Exception e) {
       log.error("Unable to remove application: " + e, e);
     }
   }
   if (log.isDebugEnabled()) {
     log.debug("Removed " + applications.size() + " applications");
   }
 }
  @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);
  }