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.

ArtifactMissingChecksumsConsumer.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package org.apache.archiva.consumers.core;
  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.checksum.ChecksumAlgorithm;
  21. import org.apache.archiva.checksum.ChecksummedFile;
  22. import org.apache.archiva.configuration.ArchivaConfiguration;
  23. import org.apache.archiva.configuration.FileTypes;
  24. import org.apache.archiva.consumers.AbstractMonitoredConsumer;
  25. import org.apache.archiva.consumers.ConsumerException;
  26. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  27. import org.apache.archiva.repository.ManagedRepository;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.springframework.context.annotation.Scope;
  31. import org.springframework.stereotype.Service;
  32. import javax.annotation.PostConstruct;
  33. import javax.inject.Inject;
  34. import java.io.IOException;
  35. import java.nio.file.Files;
  36. import java.nio.file.Path;
  37. import java.nio.file.Paths;
  38. import java.util.ArrayList;
  39. import java.util.Date;
  40. import java.util.List;
  41. /**
  42. * ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
  43. */
  44. @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
  45. @Scope( "prototype" )
  46. public class ArtifactMissingChecksumsConsumer
  47. extends AbstractMonitoredConsumer
  48. implements KnownRepositoryContentConsumer
  49. // it's prototype bean so we assume configuration won't change during a run
  50. //, RegistryListener
  51. {
  52. private Logger log = LoggerFactory.getLogger( ArtifactMissingChecksumsConsumer.class );
  53. private String id = "create-missing-checksums";
  54. private String description = "Create Missing and/or Fix Invalid Checksums (.sha1, .md5)";
  55. private ArchivaConfiguration configuration;
  56. private FileTypes filetypes;
  57. private ChecksummedFile checksum;
  58. private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
  59. private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
  60. private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
  61. private Path repositoryDir;
  62. private List<String> includes = new ArrayList<>( 0 );
  63. @Inject
  64. public ArtifactMissingChecksumsConsumer( ArchivaConfiguration configuration, FileTypes filetypes )
  65. {
  66. this.configuration = configuration;
  67. this.filetypes = filetypes;
  68. //configuration.addChangeListener( this );
  69. initIncludes( );
  70. }
  71. @Override
  72. public String getId( )
  73. {
  74. return this.id;
  75. }
  76. @Override
  77. public String getDescription( )
  78. {
  79. return this.description;
  80. }
  81. @Override
  82. public void beginScan( ManagedRepository repo, Date whenGathered )
  83. throws ConsumerException
  84. {
  85. this.repositoryDir = Paths.get( repo.getLocation( ) );
  86. }
  87. @Override
  88. public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
  89. throws ConsumerException
  90. {
  91. beginScan( repo, whenGathered );
  92. }
  93. @Override
  94. public void completeScan( )
  95. {
  96. /* do nothing */
  97. }
  98. @Override
  99. public void completeScan( boolean executeOnEntireRepo )
  100. {
  101. completeScan( );
  102. }
  103. @Override
  104. public List<String> getExcludes( )
  105. {
  106. return getDefaultArtifactExclusions( );
  107. }
  108. @Override
  109. public List<String> getIncludes( )
  110. {
  111. return includes;
  112. }
  113. @Override
  114. public void processFile( String path )
  115. throws ConsumerException
  116. {
  117. createFixChecksum( path, ChecksumAlgorithm.SHA1 );
  118. createFixChecksum( path, ChecksumAlgorithm.MD5 );
  119. }
  120. @Override
  121. public void processFile( String path, boolean executeOnEntireRepo )
  122. throws ConsumerException
  123. {
  124. processFile( path );
  125. }
  126. private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm )
  127. {
  128. Path artifactFile = repositoryDir.resolve(path);
  129. Path checksumFile = repositoryDir.resolve(path + "." + checksumAlgorithm.getExt( ) );
  130. if ( Files.exists(checksumFile) )
  131. {
  132. checksum = new ChecksummedFile( artifactFile);
  133. try
  134. {
  135. if ( !checksum.isValidChecksum( checksumAlgorithm ) )
  136. {
  137. checksum.fixChecksums( new ChecksumAlgorithm[]{checksumAlgorithm} );
  138. log.info( "Fixed checksum file {}", checksumFile.toAbsolutePath( ) );
  139. triggerConsumerInfo( "Fixed checksum file " + checksumFile.toAbsolutePath( ) );
  140. }
  141. }
  142. catch ( IOException e )
  143. {
  144. log.error( "Cannot calculate checksum for file {} :", checksumFile, e );
  145. triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
  146. ": " + e.getMessage( ) );
  147. }
  148. }
  149. else if ( !Files.exists(checksumFile) )
  150. {
  151. checksum = new ChecksummedFile( artifactFile);
  152. try
  153. {
  154. checksum.createChecksum( checksumAlgorithm );
  155. log.info( "Created missing checksum file {}", checksumFile.toAbsolutePath( ) );
  156. triggerConsumerInfo( "Created missing checksum file " + checksumFile.toAbsolutePath( ) );
  157. }
  158. catch ( IOException e )
  159. {
  160. log.error( "Cannot create checksum for file {} :", checksumFile, e );
  161. triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
  162. ": " + e.getMessage( ) );
  163. }
  164. }
  165. else
  166. {
  167. log.warn( "Checksum file {} is not a file. ", checksumFile.toAbsolutePath( ) );
  168. triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
  169. "Checksum file " + checksumFile.toAbsolutePath( ) + " is not a file." );
  170. }
  171. }
  172. /*
  173. @Override
  174. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  175. {
  176. if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
  177. {
  178. initIncludes();
  179. }
  180. }
  181. @Override
  182. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  183. {
  184. // do nothing
  185. }
  186. */
  187. private void initIncludes( )
  188. {
  189. includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
  190. }
  191. @PostConstruct
  192. public void initialize( )
  193. {
  194. //configuration.addChangeListener( this );
  195. initIncludes( );
  196. }
  197. }