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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.ConfigurationNames;
  25. import org.apache.archiva.configuration.FileTypes;
  26. import org.apache.archiva.consumers.AbstractMonitoredConsumer;
  27. import org.apache.archiva.consumers.ConsumerException;
  28. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  29. import org.apache.archiva.redback.components.registry.Registry;
  30. import org.apache.archiva.redback.components.registry.RegistryListener;
  31. import org.springframework.context.annotation.Scope;
  32. import org.springframework.stereotype.Service;
  33. import javax.annotation.PostConstruct;
  34. import javax.inject.Inject;
  35. import java.io.File;
  36. import java.io.IOException;
  37. import java.util.ArrayList;
  38. import java.util.Date;
  39. import java.util.List;
  40. /**
  41. * ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
  42. *
  43. *
  44. */
  45. @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
  46. @Scope( "prototype" )
  47. public class ArtifactMissingChecksumsConsumer
  48. extends AbstractMonitoredConsumer
  49. implements KnownRepositoryContentConsumer, RegistryListener
  50. {
  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. public String getId()
  70. {
  71. return this.id;
  72. }
  73. public String getDescription()
  74. {
  75. return this.description;
  76. }
  77. public boolean isPermanent()
  78. {
  79. return false;
  80. }
  81. public void beginScan( ManagedRepository repo, Date whenGathered )
  82. throws ConsumerException
  83. {
  84. this.repositoryDir = new File( repo.getLocation() );
  85. }
  86. public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
  87. throws ConsumerException
  88. {
  89. beginScan( repo, whenGathered );
  90. }
  91. public void completeScan()
  92. {
  93. /* do nothing */
  94. }
  95. public void completeScan( boolean executeOnEntireRepo )
  96. {
  97. completeScan();
  98. }
  99. public List<String> getExcludes()
  100. {
  101. return getDefaultArtifactExclusions();
  102. }
  103. public List<String> getIncludes()
  104. {
  105. return includes;
  106. }
  107. public void processFile( String path )
  108. throws ConsumerException
  109. {
  110. createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1 } );
  111. createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5 } );
  112. }
  113. public void processFile( String path, boolean executeOnEntireRepo )
  114. throws ConsumerException
  115. {
  116. processFile( path );
  117. }
  118. private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] )
  119. {
  120. File artifactFile = new File( this.repositoryDir, path );
  121. File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() );
  122. if ( checksumFile.exists() )
  123. {
  124. checksum = new ChecksummedFile( artifactFile );
  125. try
  126. {
  127. if ( !checksum.isValidChecksum( checksumAlgorithm[0] ) )
  128. {
  129. checksum.fixChecksums( checksumAlgorithm );
  130. triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
  131. }
  132. }
  133. catch ( IOException e )
  134. {
  135. triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
  136. ": " + e.getMessage() );
  137. }
  138. }
  139. else if ( !checksumFile.exists() )
  140. {
  141. checksum = new ChecksummedFile( artifactFile );
  142. try
  143. {
  144. checksum.createChecksum( checksumAlgorithm[0] );
  145. triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
  146. }
  147. catch ( IOException e )
  148. {
  149. triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
  150. ": " + e.getMessage() );
  151. }
  152. }
  153. else
  154. {
  155. triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
  156. "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
  157. }
  158. }
  159. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  160. {
  161. if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
  162. {
  163. initIncludes();
  164. }
  165. }
  166. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  167. {
  168. /* do nothing */
  169. }
  170. private void initIncludes()
  171. {
  172. includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
  173. }
  174. @PostConstruct
  175. public void initialize()
  176. {
  177. configuration.addChangeListener( this );
  178. initIncludes();
  179. }
  180. }