1 package org.apache.archiva.consumers.core;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.checksum.ChecksumAlgorithm;
24 import org.apache.archiva.checksum.ChecksummedFile;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
28 import org.apache.archiva.consumers.ConsumerException;
29 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.context.annotation.Scope;
33 import org.springframework.stereotype.Service;
34
35 import javax.annotation.PostConstruct;
36 import javax.inject.Inject;
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.Date;
41 import java.util.List;
42
43
44
45
46 @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
47 @Scope( "prototype" )
48 public class ArtifactMissingChecksumsConsumer
49 extends AbstractMonitoredConsumer
50 implements KnownRepositoryContentConsumer
51
52
53 {
54
55 private Logger log = LoggerFactory.getLogger( ArtifactMissingChecksumsConsumer.class );
56
57 private String id = "create-missing-checksums";
58
59 private String description = "Create Missing and/or Fix Invalid Checksums (.sha1, .md5)";
60
61 private ArchivaConfiguration configuration;
62
63 private FileTypes filetypes;
64
65 private ChecksummedFile checksum;
66
67 private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
68
69 private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
70
71 private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
72
73 private File repositoryDir;
74
75 private List<String> includes = new ArrayList<>( 0 );
76
77 @Inject
78 public ArtifactMissingChecksumsConsumer( ArchivaConfiguration configuration, FileTypes filetypes )
79 {
80 this.configuration = configuration;
81 this.filetypes = filetypes;
82
83
84
85 initIncludes();
86 }
87
88 @Override
89 public String getId()
90 {
91 return this.id;
92 }
93
94 @Override
95 public String getDescription()
96 {
97 return this.description;
98 }
99
100 @Override
101 public void beginScan( ManagedRepository repo, Date whenGathered )
102 throws ConsumerException
103 {
104 this.repositoryDir = new File( repo.getLocation() );
105 }
106
107 @Override
108 public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
109 throws ConsumerException
110 {
111 beginScan( repo, whenGathered );
112 }
113
114 @Override
115 public void completeScan()
116 {
117
118 }
119
120 @Override
121 public void completeScan( boolean executeOnEntireRepo )
122 {
123 completeScan();
124 }
125
126 @Override
127 public List<String> getExcludes()
128 {
129 return getDefaultArtifactExclusions();
130 }
131
132 @Override
133 public List<String> getIncludes()
134 {
135 return includes;
136 }
137
138 @Override
139 public void processFile( String path )
140 throws ConsumerException
141 {
142 createFixChecksum( path, ChecksumAlgorithm.SHA1 );
143 createFixChecksum( path, ChecksumAlgorithm.MD5 );
144 }
145
146 @Override
147 public void processFile( String path, boolean executeOnEntireRepo )
148 throws ConsumerException
149 {
150 processFile( path );
151 }
152
153 private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm )
154 {
155 File artifactFile = new File( this.repositoryDir, path );
156 File checksumFile = new File( this.repositoryDir, path + "." + checksumAlgorithm.getExt() );
157
158 if ( checksumFile.exists() )
159 {
160 checksum = new ChecksummedFile( artifactFile );
161 try
162 {
163 if ( !checksum.isValidChecksum( checksumAlgorithm ) )
164 {
165 checksum.fixChecksums( new ChecksumAlgorithm[]{ checksumAlgorithm } );
166 log.info( "Fixed checksum file {}", checksumFile.getAbsolutePath() );
167 triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
168 }
169 }
170 catch ( IOException e )
171 {
172 log.error( "Cannot calculate checksum for file {} :", checksumFile, e );
173 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
174 ": " + e.getMessage() );
175 }
176 }
177 else if ( !checksumFile.exists() )
178 {
179 checksum = new ChecksummedFile( artifactFile );
180 try
181 {
182 checksum.createChecksum( checksumAlgorithm );
183 log.info( "Created missing checksum file {}", checksumFile.getAbsolutePath() );
184 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
185 }
186 catch ( IOException e )
187 {
188 log.error( "Cannot create checksum for file {} :", checksumFile, e );
189 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
190 ": " + e.getMessage() );
191 }
192 }
193 else
194 {
195 log.warn( "Checksum file {} is not a file. ", checksumFile.getAbsolutePath() );
196 triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
197 "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
198 }
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 private void initIncludes()
221 {
222 includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
223
224 }
225
226 @PostConstruct
227 public void initialize()
228 {
229
230
231 initIncludes();
232 }
233 }