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

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