/**
   * Accesses the metadata mapping (with 1 retry) to find the metadata file for the specified
   * domainId
   */
  protected RepositoryFile getMetadataRepositoryFile(final String domainId) {
    lock.readLock().lock();
    RepositoryFile domainFile;
    try {
      domainFile = metadataMapping.getDomainFile(domainId);
    } finally {
      lock.readLock().unlock();
    }

    if (domainFile == null) {

      if (logger.isDebugEnabled()) {
        logger.debug(
            "Requested Domain ("
                + domainId
                + ") wasn't found in Metadata Mapping. Domain cache will be reloaded");
      }
      lock.writeLock().lock();
      try {
        domainFile = metadataMapping.getDomainFile(domainId);
        if (domainFile == null) {
          reloadDomainsIfNeeded();
          domainFile = metadataMapping.getDomainFile(domainId);
        }
      } finally {
        lock.writeLock().unlock();
      }
    }

    if (domainFile == null && logger.isDebugEnabled()) {
      logger.debug(
          "Even after reloading all domains, the specified Domain wasn't found in the system: "
              + domainId);
    }

    return domainFile;
  }
  /**
   * remove a domain from disk and memory.
   *
   * @param domainId
   */
  @Override
  public void removeDomain(final String domainId) {
    if (logger.isDebugEnabled()) {
      logger.debug("removeDomain(" + domainId + ")");
    }

    if (StringUtils.isEmpty(domainId)) {
      throw new IllegalArgumentException(
          messages.getErrorString(
              "PentahoMetadataDomainRepository.ERROR_0004_DOMAIN_ID_INVALID", domainId));
    }

    // Get the metadata domain file
    RepositoryFile domainFile;
    Set<RepositoryFile> domainFiles;
    lock.writeLock().lock();
    try {
      domainFiles = metadataMapping.getFiles(domainId);
      domainFile = metadataMapping.getDomainFile(domainId);
      metadataMapping.deleteDomain(domainId);
    } finally {
      lock.writeLock().unlock();
    }

    if (domainFile != null) {
      // it no node exists, nothing would happen
      getAclHelper().removeAclFor(domainFile);
    }

    for (final RepositoryFile file : domainFiles) {
      if (logger.isTraceEnabled()) {
        logger.trace("Deleting repository file " + toString(file));
      }
      repository.deleteFile(file.getId(), true, null);
    }

    // This invalidates any caching
    if (!domainFiles.isEmpty()) {
      flushDomains();
    }
  }