/*
   * When finding the default persistence unit, the first persistence unit encountered is returned.
   */
  private static PersistenceUnitMetadata findWithinDeployment(
      DeploymentUnit unit, String persistenceUnitName) {
    if (traceEnabled) {
      ROOT_LOGGER.tracef("pu findWithinDeployment searching for %s", persistenceUnitName);
    }

    for (ResourceRoot root : DeploymentUtils.allResourceRoots(unit)) {
      PersistenceUnitMetadataHolder holder =
          root.getAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS);
      if (holder == null || holder.getPersistenceUnits() == null) {
        if (traceEnabled) {
          ROOT_LOGGER.tracef(
              "pu findWithinDeployment skipping empty pu holder for %s", persistenceUnitName);
        }
        continue;
      }

      ambiguousPUError(unit, persistenceUnitName, holder);
      persistenceUnitName = defaultPersistenceUnitName(persistenceUnitName, holder);

      for (PersistenceUnitMetadata persistenceUnit : holder.getPersistenceUnits()) {
        if (traceEnabled) {
          ROOT_LOGGER.tracef(
              "findWithinDeployment check '%s' against pu '%s'",
              persistenceUnitName, persistenceUnit.getPersistenceUnitName());
        }
        if (persistenceUnitName == null
            || persistenceUnitName.length() == 0
            || persistenceUnit.getPersistenceUnitName().equals(persistenceUnitName)) {
          if (traceEnabled) {
            ROOT_LOGGER.tracef(
                "findWithinDeployment matched '%s' against pu '%s'",
                persistenceUnitName, persistenceUnit.getPersistenceUnitName());
          }
          return persistenceUnit;
        }
      }
    }
    return null;
  }
  private static PersistenceUnitMetadata findWithinLibraryJar(
      DeploymentUnit unit, ResourceRoot moduleResourceRoot, String persistenceUnitName) {

    final ResourceRoot deploymentRoot = moduleResourceRoot;
    PersistenceUnitMetadataHolder holder =
        deploymentRoot.getAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS);
    if (holder == null || holder.getPersistenceUnits() == null) {
      if (traceEnabled) {
        ROOT_LOGGER.tracef(
            "findWithinLibraryJar checking for '%s' found no persistence units",
            persistenceUnitName);
      }
      return null;
    }

    ambiguousPUError(unit, persistenceUnitName, holder);
    persistenceUnitName = defaultPersistenceUnitName(persistenceUnitName, holder);

    for (PersistenceUnitMetadata persistenceUnit : holder.getPersistenceUnits()) {
      if (traceEnabled) {
        ROOT_LOGGER.tracef(
            "findWithinLibraryJar check '%s' against pu '%s'",
            persistenceUnitName, persistenceUnit.getPersistenceUnitName());
      }
      if (persistenceUnitName == null
          || persistenceUnitName.length() == 0
          || persistenceUnit.getPersistenceUnitName().equals(persistenceUnitName)) {
        if (traceEnabled) {
          ROOT_LOGGER.tracef(
              "findWithinLibraryJar matched '%s' against pu '%s'",
              persistenceUnitName, persistenceUnit.getPersistenceUnitName());
        }
        return persistenceUnit;
      }
    }
    return null;
  }