You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RetentionCountRepositoryPurge.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package org.apache.archiva.consumers.core.repository;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.common.utils.VersionComparator;
  21. import org.apache.archiva.common.utils.VersionUtil;
  22. import org.apache.archiva.metadata.repository.RepositorySession;
  23. import org.apache.archiva.model.ArtifactReference;
  24. import org.apache.archiva.model.VersionedReference;
  25. import org.apache.archiva.repository.ContentNotFoundException;
  26. import org.apache.archiva.repository.LayoutException;
  27. import org.apache.archiva.repository.ManagedRepositoryContent;
  28. import org.apache.archiva.repository.events.RepositoryListener;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import java.nio.file.Paths;
  32. import java.util.ArrayList;
  33. import java.util.Collections;
  34. import java.util.HashSet;
  35. import java.util.List;
  36. import java.util.Set;
  37. /**
  38. * Purge the repository by retention count. Retain only the specified number of snapshots.
  39. */
  40. public class RetentionCountRepositoryPurge
  41. extends AbstractRepositoryPurge
  42. {
  43. private int retentionCount;
  44. public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount,
  45. RepositorySession repositorySession, List<RepositoryListener> listeners )
  46. {
  47. super( repository, repositorySession, listeners );
  48. this.retentionCount = retentionCount;
  49. }
  50. @Override
  51. public void process( String path )
  52. throws RepositoryPurgeException
  53. {
  54. try
  55. {
  56. Path artifactFile = Paths.get( repository.getRepoRoot( ), path );
  57. if ( !Files.exists(artifactFile) )
  58. {
  59. return;
  60. }
  61. ArtifactReference artifact = repository.toArtifactReference( path );
  62. if ( VersionUtil.isSnapshot( artifact.getVersion( ) ) )
  63. {
  64. VersionedReference reference = new VersionedReference( );
  65. reference.setGroupId( artifact.getGroupId( ) );
  66. reference.setArtifactId( artifact.getArtifactId( ) );
  67. reference.setVersion( artifact.getVersion( ) );
  68. List<String> versions = new ArrayList<>( repository.getVersions( reference ) );
  69. Collections.sort( versions, VersionComparator.getInstance( ) );
  70. if ( retentionCount > versions.size( ) )
  71. {
  72. log.trace( "No deletion, because retention count is higher than actual number of artifacts." );
  73. // Done. nothing to do here. skip it.
  74. return;
  75. }
  76. int countToPurge = versions.size( ) - retentionCount;
  77. Set<ArtifactReference> artifactsToDelete = new HashSet<>( );
  78. for ( String version : versions )
  79. {
  80. if ( countToPurge-- <= 0 )
  81. {
  82. break;
  83. }
  84. artifactsToDelete.addAll( repository.getRelatedArtifacts( getNewArtifactReference( artifact, version ) ) );
  85. }
  86. purge( artifactsToDelete );
  87. }
  88. }
  89. catch ( LayoutException le )
  90. {
  91. throw new RepositoryPurgeException( le.getMessage( ), le );
  92. }
  93. catch ( ContentNotFoundException e )
  94. {
  95. log.error( "Repostory artifact not found {}", path );
  96. }
  97. }
  98. /*
  99. * Returns a new artifact reference with different version
  100. */
  101. private ArtifactReference getNewArtifactReference( ArtifactReference reference, String version )
  102. throws LayoutException
  103. {
  104. ArtifactReference artifact = new ArtifactReference( );
  105. artifact.setGroupId( reference.getGroupId( ) );
  106. artifact.setArtifactId( reference.getArtifactId( ) );
  107. artifact.setVersion( version );
  108. artifact.setClassifier( reference.getClassifier( ) );
  109. artifact.setType( reference.getType( ) );
  110. return artifact;
  111. }
  112. }