1 | |
package org.apache.maven.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.checksum.ChecksumAlgorithm; |
23 | |
import org.apache.archiva.checksum.ChecksummedFile; |
24 | |
import org.apache.maven.archiva.configuration.ArchivaConfiguration; |
25 | |
import org.apache.maven.archiva.configuration.ConfigurationNames; |
26 | |
import org.apache.maven.archiva.configuration.FileTypes; |
27 | |
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; |
28 | |
import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; |
29 | |
import org.apache.maven.archiva.consumers.ConsumerException; |
30 | |
import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; |
31 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; |
32 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; |
33 | |
import org.codehaus.plexus.registry.Registry; |
34 | |
import org.codehaus.plexus.registry.RegistryListener; |
35 | |
|
36 | |
import java.io.File; |
37 | |
import java.io.IOException; |
38 | |
import java.util.ArrayList; |
39 | |
import java.util.Date; |
40 | |
import java.util.List; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public class ArtifactMissingChecksumsConsumer |
48 | |
extends AbstractMonitoredConsumer |
49 | |
implements KnownRepositoryContentConsumer, RegistryListener, Initializable |
50 | |
{ |
51 | |
private String id; |
52 | |
|
53 | |
private String description; |
54 | |
|
55 | |
private ArchivaConfiguration configuration; |
56 | |
|
57 | |
private FileTypes filetypes; |
58 | |
|
59 | |
private ChecksummedFile checksum; |
60 | |
|
61 | |
private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file"; |
62 | |
|
63 | |
private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure"; |
64 | |
|
65 | |
private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure"; |
66 | |
|
67 | |
private File repositoryDir; |
68 | |
|
69 | 0 | private List<String> includes = new ArrayList<String>(); |
70 | |
|
71 | |
public ArtifactMissingChecksumsConsumer(String id, |
72 | |
String description, |
73 | |
ArchivaConfiguration configuration, |
74 | 0 | FileTypes filetypes) { |
75 | 0 | this.id = id; |
76 | 0 | this.description = description; |
77 | 0 | this.configuration = configuration; |
78 | 0 | this.filetypes = filetypes; |
79 | 0 | } |
80 | |
|
81 | |
public String getId() |
82 | |
{ |
83 | 0 | return this.id; |
84 | |
} |
85 | |
|
86 | |
public String getDescription() |
87 | |
{ |
88 | 0 | return this.description; |
89 | |
} |
90 | |
|
91 | |
public boolean isPermanent() |
92 | |
{ |
93 | 0 | return false; |
94 | |
} |
95 | |
|
96 | |
public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered ) |
97 | |
throws ConsumerException |
98 | |
{ |
99 | 0 | this.repositoryDir = new File( repo.getLocation() ); |
100 | 0 | } |
101 | |
|
102 | |
public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered, boolean executeOnEntireRepo ) |
103 | |
throws ConsumerException |
104 | |
{ |
105 | 0 | beginScan( repo, whenGathered ); |
106 | 0 | } |
107 | |
|
108 | |
public void completeScan() |
109 | |
{ |
110 | |
|
111 | 0 | } |
112 | |
|
113 | |
public void completeScan( boolean executeOnEntireRepo ) |
114 | |
{ |
115 | 0 | completeScan(); |
116 | 0 | } |
117 | |
|
118 | |
public List<String> getExcludes() |
119 | |
{ |
120 | 0 | return getDefaultArtifactExclusions(); |
121 | |
} |
122 | |
|
123 | |
public List<String> getIncludes() |
124 | |
{ |
125 | 0 | return includes; |
126 | |
} |
127 | |
|
128 | |
public void processFile( String path ) |
129 | |
throws ConsumerException |
130 | |
{ |
131 | 0 | createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1 } ); |
132 | 0 | createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5 } ); |
133 | 0 | } |
134 | |
|
135 | |
public void processFile( String path, boolean executeOnEntireRepo ) |
136 | |
throws ConsumerException |
137 | |
{ |
138 | 0 | processFile( path ); |
139 | 0 | } |
140 | |
|
141 | |
private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] ) |
142 | |
{ |
143 | 0 | File artifactFile = new File( this.repositoryDir, path ); |
144 | 0 | File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() ); |
145 | |
|
146 | 0 | if( checksumFile.exists() ) |
147 | |
{ |
148 | 0 | checksum = new ChecksummedFile( artifactFile ); |
149 | |
try |
150 | |
{ |
151 | 0 | if( !checksum.isValidChecksum( checksumAlgorithm[0] ) ) |
152 | |
{ |
153 | 0 | checksum.fixChecksums( checksumAlgorithm ); |
154 | 0 | triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() ); |
155 | |
} |
156 | |
} |
157 | 0 | catch ( IOException e ) |
158 | |
{ |
159 | 0 | triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile + |
160 | |
": " + e.getMessage() ); |
161 | 0 | } |
162 | |
} |
163 | 0 | else if( !checksumFile.exists() ) |
164 | |
{ |
165 | 0 | checksum = new ChecksummedFile( artifactFile ); |
166 | |
try |
167 | |
{ |
168 | 0 | checksum.createChecksum( checksumAlgorithm[0] ); |
169 | 0 | triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() ); |
170 | |
} |
171 | 0 | catch ( IOException e ) |
172 | |
{ |
173 | 0 | triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile + |
174 | |
": " + e.getMessage() ); |
175 | 0 | } |
176 | |
} |
177 | |
else |
178 | |
{ |
179 | 0 | triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE, |
180 | |
"Checksum file " + checksumFile.getAbsolutePath() + " is not a file." ); |
181 | |
} |
182 | 0 | } |
183 | |
|
184 | |
public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) |
185 | |
{ |
186 | 0 | if ( ConfigurationNames.isRepositoryScanning( propertyName ) ) |
187 | |
{ |
188 | 0 | initIncludes(); |
189 | |
} |
190 | 0 | } |
191 | |
|
192 | |
public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) |
193 | |
{ |
194 | |
|
195 | 0 | } |
196 | |
|
197 | |
private void initIncludes() |
198 | |
{ |
199 | 0 | includes.clear(); |
200 | |
|
201 | 0 | includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) ); |
202 | 0 | } |
203 | |
|
204 | |
public void initialize() |
205 | |
throws InitializationException |
206 | |
{ |
207 | 0 | configuration.addChangeListener( this ); |
208 | |
|
209 | 0 | initIncludes(); |
210 | 0 | } |
211 | |
} |