@Override
 public void onCollectionEnter(WalkerContext context, StorageCollectionItem coll) {
   try {
     mdHelper.onDirEnter(coll.getPath());
   } catch (Exception e) {
     logger.warn("Error occured while entering collection '" + coll.getPath() + "'.", e);
   }
 }
Пример #2
0
    private void updateMetadataIfNecessary(WalkerContext context, StorageCollectionItem coll)
        throws Exception {
      // all snapshot files are deleted
      if (!deletableSnapshotsAndFiles.isEmpty() && remainingSnapshotsAndFiles.isEmpty()) {
        String parentPath = ItemPathUtils.getParentPath(coll.getPath());

        request.getMetadataRebuildPaths().add(parentPath);
      } else {
        request.getMetadataRebuildPaths().add(coll.getPath());
      }
    }
Пример #3
0
 @Override
 public void onCollectionExit(WalkerContext context, StorageCollectionItem coll) {
   try {
     doOnCollectionExit(context, coll);
   } catch (Exception e) {
     // we always simply log the exception and continue
     getLogger().warn("SnapshotRemover is failed to process path: '" + coll.getPath() + "'.", e);
   }
 }
  @Override
  public void onCollectionExit(WalkerContext context, StorageCollectionItem coll) {
    try {
      mdHelper.onDirExit(coll.getPath());

      if (coll.list().size() == 0) {
        ResourceStoreRequest request = new ResourceStoreRequest(coll);
        if (deleteOperation != null) {
          request
              .getRequestContext()
              .put(DeleteOperation.DELETE_OPERATION_CTX_KEY, this.deleteOperation);
        }

        repository.deleteItem(false, request);
      }
    } catch (Exception e) {
      logger.warn("Error occured while existing collection '" + coll.getPath() + "'.", e);
    }
  }
Пример #5
0
    private void removeDirectoryIfEmpty(StorageCollectionItem coll)
        throws StorageException, IllegalOperationException, UnsupportedStorageOperationException {
      try {
        if (repository.list(false, coll).size() > 0) {
          return;
        }

        if (getLogger().isDebugEnabled()) {
          getLogger()
              .debug(
                  "Removing the empty directory leftover: UID="
                      + coll.getRepositoryItemUid().toString());
        }

        repository.deleteItem(false, new ResourceStoreRequest(coll));
      } catch (ItemNotFoundException e) {
        // silent, this happens if whole GAV is removed and the dir is removed too
      }
    }
Пример #6
0
    public void doOnCollectionExit(WalkerContext context, StorageCollectionItem coll)
        throws Exception {
      if (getLogger().isDebugEnabled()) {
        getLogger().debug("onCollectionExit() :: " + coll.getRepositoryItemUid().toString());
      }

      shouldProcessCollection = coll.getPath().endsWith("SNAPSHOT");

      if (!shouldProcessCollection) {
        return;
      }

      deletableSnapshotsAndFiles.clear();

      remainingSnapshotsAndFiles.clear();

      removeWholeGAV = false;

      Gav gav = null;

      Collection<StorageItem> items;

      items = repository.list(false, coll);

      HashSet<Long> versionsToRemove = new HashSet<Long>();

      // gathering the facts
      for (StorageItem item : items) {
        if (!item.isVirtual() && !StorageCollectionItem.class.isAssignableFrom(item.getClass())) {
          gav =
              ((MavenRepository) coll.getRepositoryItemUid().getRepository())
                  .getGavCalculator()
                  .pathToGav(item.getPath());

          if (gav != null) {
            // if we find a pom, check for delete on release
            if (!gav.isHash() && !gav.isSignature() && gav.getExtension().equals("pom")) {
              if (request.isRemoveIfReleaseExists()
                  && releaseExistsForSnapshot(gav, item.getItemContext())) {
                getLogger().debug("Found POM and release exists, removing whole gav.");

                removeWholeGAV = true;

                // Will break out and junk whole gav
                break;
              }
            }

            item.getItemContext().put(Gav.class.getName(), gav);

            if (gav.getSnapshotTimeStamp() != null) {
              getLogger().debug("Using GAV snapshot timestamp");

              long itemTimestamp = gav.getSnapshotTimeStamp().longValue();

              getLogger().debug("NOW is " + itemTimestamp);

              // If this timestamp is already marked to be removed, junk it
              if (versionsToRemove.contains(new Long(itemTimestamp))) {
                addStorageFileItemToMap(deletableSnapshotsAndFiles, gav, (StorageFileItem) item);
              } else {
                getLogger()
                    .debug("itemTimestamp=" + itemTimestamp + ", dateTreshold=" + dateThreshold);

                // if dateTreshold is not used (zero days) OR
                // if itemTimestamp is less then dateTreshold (NB: both are positive!)
                // below will the retentionCount overrule if needed this
                if (-1 == dateThreshold || itemTimestamp < dateThreshold) {
                  versionsToRemove.add(new Long(itemTimestamp));
                  addStorageFileItemToMap(deletableSnapshotsAndFiles, gav, (StorageFileItem) item);
                } else {
                  addStorageFileItemToMap(remainingSnapshotsAndFiles, gav, (StorageFileItem) item);
                }
              }
            } else {
              // If no timestamp on gav, then it is a non-unique snapshot
              // and should _not_ be removed
              getLogger()
                  .debug("GAV Snapshot timestamp not available, skipping non-unique snapshot");

              addStorageFileItemToMap(remainingSnapshotsAndFiles, gav, (StorageFileItem) item);
            }
          }
        }
      }

      // and doing the work here
      if (removeWholeGAV) {
        try {
          for (StorageItem item : items) {
            try {
              // preserve possible subdirs
              if (!(item instanceof StorageCollectionItem)) {
                repository.deleteItem(false, new ResourceStoreRequest(item));
              }
            } catch (ItemNotFoundException e) {
              if (getLogger().isDebugEnabled()) {
                getLogger()
                    .debug(
                        "Could not delete whole GAV " + coll.getRepositoryItemUid().toString(), e);
              }
            }
          }
        } catch (Exception e) {
          getLogger()
              .warn("Could not delete whole GAV " + coll.getRepositoryItemUid().toString(), e);
        }
      } else {
        // and now check some things
        if (remainingSnapshotsAndFiles.size() < request.getMinCountOfSnapshotsToKeep()) {
          // do something
          if (remainingSnapshotsAndFiles.size() + deletableSnapshotsAndFiles.size()
              < request.getMinCountOfSnapshotsToKeep()) {
            // delete nothing, since there is less snapshots in total as allowed
            deletableSnapshotsAndFiles.clear();
          } else {
            TreeSet<Version> keys = new TreeSet<Version>(deletableSnapshotsAndFiles.keySet());

            while (!keys.isEmpty()
                && remainingSnapshotsAndFiles.size() < request.getMinCountOfSnapshotsToKeep()) {
              Version keyToMove = keys.last();

              if (remainingSnapshotsAndFiles.containsKey(keyToMove)) {
                remainingSnapshotsAndFiles
                    .get(keyToMove)
                    .addAll(deletableSnapshotsAndFiles.get(keyToMove));
              } else {
                remainingSnapshotsAndFiles.put(
                    keyToMove, deletableSnapshotsAndFiles.get(keyToMove));
              }

              deletableSnapshotsAndFiles.remove(keyToMove);

              keys.remove(keyToMove);
            }
          }
        }

        // NEXUS-814: is this GAV have remaining artifacts?
        boolean gavHasMoreTimestampedSnapshots = remainingSnapshotsAndFiles.size() > 0;

        for (Version key : deletableSnapshotsAndFiles.keySet()) {

          List<StorageFileItem> files = deletableSnapshotsAndFiles.get(key);
          deletedSnapshots++;

          for (StorageFileItem file : files) {
            try {
              // NEXUS-814: mark that we are deleting a TS snapshot, but there are still remaining
              // ones in repository.
              if (gavHasMoreTimestampedSnapshots) {
                file.getItemContext().put(MORE_TS_SNAPSHOTS_EXISTS_FOR_GAV, Boolean.TRUE);
              }

              gav = (Gav) file.getItemContext().get(Gav.class.getName());

              repository.deleteItem(false, new ResourceStoreRequest(file));

              deletedFiles++;
            } catch (ItemNotFoundException e) {
              if (getLogger().isDebugEnabled()) {
                getLogger().debug("Could not delete file:", e);
              }
            } catch (Exception e) {
              getLogger().info("Could not delete file:", e);
            }
          }
        }
      }

      removeDirectoryIfEmpty(coll);

      updateMetadataIfNecessary(context, coll);
    }