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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.metadata.repository.RepositorySession;
  21. import org.apache.archiva.repository.events.RepositoryListener;
  22. import org.apache.archiva.common.utils.VersionComparator;
  23. import org.apache.archiva.common.utils.VersionUtil;
  24. import org.apache.archiva.model.ArtifactReference;
  25. import org.apache.archiva.model.VersionedReference;
  26. import org.apache.archiva.repository.ContentNotFoundException;
  27. import org.apache.archiva.repository.ManagedRepositoryContent;
  28. import org.apache.archiva.repository.layout.LayoutException;
  29. import java.io.File;
  30. import java.util.ArrayList;
  31. import java.util.Collections;
  32. import java.util.List;
  33. import java.util.Set;
  34. /**
  35. * Purge the repository by retention count. Retain only the specified number of snapshots.
  36. */
  37. public class RetentionCountRepositoryPurge
  38. extends AbstractRepositoryPurge
  39. {
  40. private int retentionCount;
  41. public RetentionCountRepositoryPurge( ManagedRepositoryContent repository, int retentionCount,
  42. RepositorySession repositorySession, List<RepositoryListener> listeners )
  43. {
  44. super( repository, repositorySession, listeners );
  45. this.retentionCount = retentionCount;
  46. }
  47. public void process( String path )
  48. throws RepositoryPurgeException
  49. {
  50. try
  51. {
  52. File artifactFile = new File( repository.getRepoRoot(), path );
  53. if ( !artifactFile.exists() )
  54. {
  55. return;
  56. }
  57. ArtifactReference artifact = repository.toArtifactReference( path );
  58. if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
  59. {
  60. VersionedReference reference = new VersionedReference();
  61. reference.setGroupId( artifact.getGroupId() );
  62. reference.setArtifactId( artifact.getArtifactId() );
  63. reference.setVersion( artifact.getVersion() );
  64. List<String> versions = new ArrayList<>( repository.getVersions( reference ) );
  65. Collections.sort( versions, VersionComparator.getInstance() );
  66. if ( retentionCount > versions.size() )
  67. {
  68. // Done. nothing to do here. skip it.
  69. return;
  70. }
  71. int countToPurge = versions.size() - retentionCount;
  72. for ( String version : versions )
  73. {
  74. if ( countToPurge-- <= 0 )
  75. {
  76. break;
  77. }
  78. doPurgeAllRelated( artifact, version );
  79. }
  80. }
  81. }
  82. catch ( LayoutException le )
  83. {
  84. throw new RepositoryPurgeException( le.getMessage(), le );
  85. }
  86. catch ( ContentNotFoundException e )
  87. {
  88. // Nothing to do here.
  89. // TODO: Log this condition?
  90. }
  91. }
  92. private void doPurgeAllRelated( ArtifactReference reference, String version )
  93. throws LayoutException
  94. {
  95. ArtifactReference artifact = new ArtifactReference();
  96. artifact.setGroupId( reference.getGroupId() );
  97. artifact.setArtifactId( reference.getArtifactId() );
  98. artifact.setVersion( version );
  99. artifact.setClassifier( reference.getClassifier() );
  100. artifact.setType( reference.getType() );
  101. try
  102. {
  103. Set<ArtifactReference> related = repository.getRelatedArtifacts( artifact );
  104. purge( related );
  105. }
  106. catch ( ContentNotFoundException e )
  107. {
  108. // Nothing to do here.
  109. // TODO: Log this?
  110. }
  111. }
  112. }