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.

CleanupReleasedSnapshotsRepositoryPurge.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.MetadataRepository;
  23. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  24. import org.apache.archiva.metadata.repository.RepositorySession;
  25. import org.apache.archiva.model.ArtifactReference;
  26. import org.apache.archiva.model.ProjectReference;
  27. import org.apache.archiva.model.VersionedReference;
  28. import org.apache.archiva.repository.ContentNotFoundException;
  29. import org.apache.archiva.repository.LayoutException;
  30. import org.apache.archiva.repository.ManagedRepositoryContent;
  31. import org.apache.archiva.repository.ReleaseScheme;
  32. import org.apache.archiva.repository.RepositoryException;
  33. import org.apache.archiva.repository.RepositoryRegistry;
  34. import org.apache.archiva.metadata.audit.RepositoryListener;
  35. import org.apache.archiva.repository.metadata.base.MetadataTools;
  36. import org.apache.archiva.repository.metadata.RepositoryMetadataException;
  37. import java.io.IOException;
  38. import java.nio.file.Files;
  39. import java.nio.file.Path;
  40. import java.nio.file.Paths;
  41. import java.util.ArrayList;
  42. import java.util.Collection;
  43. import java.util.Collections;
  44. import java.util.List;
  45. /**
  46. * <p>
  47. * This will look in a single managed repository, and purge any snapshots that are present
  48. * that have a corresponding released version on the same repository.
  49. * </p>
  50. * <p>
  51. * So, if you have the following (presented in the m2/default layout form) ...
  52. * <pre>
  53. * /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
  54. * /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
  55. * /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
  56. * /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
  57. * /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
  58. * /com/foo/foo-tool/2.0/foo-tool-2.0.jar
  59. * /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
  60. * </pre>
  61. * then the current highest ranked released (non-snapshot) version is 2.0, which means
  62. * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
  63. * be purged. Leaving 2.1-SNAPSHOT in alone.
  64. */
  65. public class CleanupReleasedSnapshotsRepositoryPurge
  66. extends AbstractRepositoryPurge
  67. {
  68. private MetadataTools metadataTools;
  69. private RepositoryRegistry repositoryRegistry;
  70. public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
  71. RepositoryRegistry repositoryRegistry,
  72. RepositorySession repositorySession,
  73. List<RepositoryListener> listeners )
  74. {
  75. super( repository, repositorySession, listeners );
  76. this.metadataTools = metadataTools;
  77. this.repositoryRegistry = repositoryRegistry;
  78. }
  79. @Override
  80. public void process( String path )
  81. throws RepositoryPurgeException
  82. {
  83. try
  84. {
  85. Path artifactFile = Paths.get( repository.getRepoRoot( ), path );
  86. if ( !Files.exists(artifactFile) )
  87. {
  88. // Nothing to do here, file doesn't exist, skip it.
  89. return;
  90. }
  91. ArtifactReference artifactRef = repository.toArtifactReference( path );
  92. if ( !VersionUtil.isSnapshot( artifactRef.getVersion( ) ) )
  93. {
  94. // Nothing to do here, not a snapshot, skip it.
  95. return;
  96. }
  97. ProjectReference reference = new ProjectReference( );
  98. reference.setGroupId( artifactRef.getGroupId( ) );
  99. reference.setArtifactId( artifactRef.getArtifactId( ) );
  100. // Gether the released versions
  101. List<String> releasedVersions = new ArrayList<>( );
  102. Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories( );
  103. for ( org.apache.archiva.repository.ManagedRepository repo : repos )
  104. {
  105. if ( repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ))
  106. {
  107. try
  108. {
  109. ManagedRepositoryContent repoContent = repo.getContent();
  110. for ( String version : repoContent.getVersions( reference ) )
  111. {
  112. if ( !VersionUtil.isSnapshot( version ) )
  113. {
  114. releasedVersions.add( version );
  115. }
  116. }
  117. }
  118. catch ( RepositoryException e )
  119. {
  120. // swallow
  121. }
  122. }
  123. }
  124. Collections.sort( releasedVersions, VersionComparator.getInstance( ) );
  125. // Now clean out any version that is earlier than the highest released version.
  126. boolean needsMetadataUpdate = false;
  127. VersionedReference versionRef = new VersionedReference( );
  128. versionRef.setGroupId( artifactRef.getGroupId( ) );
  129. versionRef.setArtifactId( artifactRef.getArtifactId( ) );
  130. MetadataRepository metadataRepository = repositorySession.getRepository( );
  131. if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion( ) ) ) )
  132. {
  133. versionRef.setVersion( artifactRef.getVersion( ) );
  134. repository.deleteVersion( versionRef );
  135. for ( RepositoryListener listener : listeners )
  136. {
  137. listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getGroupId( ),
  138. artifactRef.getArtifactId( ), artifactRef.getVersion( ),
  139. artifactFile.getFileName().toString() );
  140. }
  141. metadataRepository.removeProjectVersion( repositorySession, repository.getId( ),
  142. artifactRef.getGroupId( ), artifactRef.getArtifactId( ), artifactRef.getVersion( ) );
  143. needsMetadataUpdate = true;
  144. }
  145. if ( needsMetadataUpdate )
  146. {
  147. updateMetadata( artifactRef );
  148. }
  149. }
  150. catch ( LayoutException e )
  151. {
  152. log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) );
  153. }
  154. catch ( ContentNotFoundException e )
  155. {
  156. throw new RepositoryPurgeException( e.getMessage( ), e );
  157. }
  158. catch ( MetadataRepositoryException e )
  159. {
  160. log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e );
  161. }
  162. }
  163. /*
  164. * TODO: Uses a deprecated API, but if we use the API with location string, it does not work as expected
  165. * -> not sure what needs to be changed here.
  166. */
  167. @SuppressWarnings( "deprecation" )
  168. private void updateMetadata( ArtifactReference artifact )
  169. {
  170. VersionedReference versionRef = new VersionedReference( );
  171. versionRef.setGroupId( artifact.getGroupId( ) );
  172. versionRef.setArtifactId( artifact.getArtifactId( ) );
  173. versionRef.setVersion( artifact.getVersion( ) );
  174. ProjectReference projectRef = new ProjectReference( );
  175. projectRef.setGroupId( artifact.getGroupId( ) );
  176. projectRef.setArtifactId( artifact.getArtifactId( ) );
  177. try
  178. {
  179. metadataTools.updateMetadata( repository, versionRef );
  180. }
  181. catch ( ContentNotFoundException e )
  182. {
  183. // Ignore. (Just means we have no snapshot versions left to reference).
  184. }
  185. catch ( RepositoryMetadataException e )
  186. {
  187. // Ignore.
  188. }
  189. catch ( IOException e )
  190. {
  191. // Ignore.
  192. }
  193. catch ( LayoutException e )
  194. {
  195. // Ignore.
  196. }
  197. try
  198. {
  199. metadataTools.updateMetadata( repository, projectRef );
  200. }
  201. catch ( ContentNotFoundException e )
  202. {
  203. // Ignore. (Just means we have no snapshot versions left to reference).
  204. }
  205. catch ( RepositoryMetadataException e )
  206. {
  207. // Ignore.
  208. }
  209. catch ( IOException e )
  210. {
  211. // Ignore.
  212. }
  213. catch ( LayoutException e )
  214. {
  215. // Ignore.
  216. }
  217. }
  218. }