1 package org.apache.maven.archiva.consumers.database;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.ConfigurationNames;
24 import org.apache.maven.archiva.configuration.FileTypes;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.maven.archiva.consumers.ConsumerException;
28 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.maven.archiva.database.ArchivaDAO;
30 import org.apache.maven.archiva.database.ArchivaDatabaseException;
31 import org.apache.maven.archiva.model.ArchivaArtifact;
32 import org.apache.maven.archiva.model.ArtifactReference;
33 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
34 import org.apache.maven.archiva.repository.RepositoryContentFactory;
35 import org.apache.maven.archiva.repository.RepositoryException;
36 import org.apache.maven.archiva.repository.layout.LayoutException;
37 import org.codehaus.plexus.digest.Digester;
38 import org.codehaus.plexus.digest.DigesterException;
39 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
40 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
41 import org.codehaus.plexus.registry.Registry;
42 import org.codehaus.plexus.registry.RegistryListener;
43
44 import java.io.File;
45 import java.util.ArrayList;
46 import java.util.Date;
47 import java.util.List;
48
49
50
51
52
53
54
55
56
57 public class ArtifactUpdateDatabaseConsumer
58 extends AbstractMonitoredConsumer
59 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
60 {
61 private static final String TYPE_NOT_ARTIFACT = "file-not-artifact";
62
63 private static final String DB_ERROR = "db-error";
64
65 private static final String CHECKSUM_CALCULATION = "checksum-calc";
66
67
68
69
70 private String id;
71
72
73
74
75 private String description;
76
77
78
79
80 private ArchivaDAO dao;
81
82
83
84
85 private ArchivaConfiguration configuration;
86
87
88
89
90 private FileTypes filetypes;
91
92
93
94
95 private RepositoryContentFactory repositoryFactory;
96
97
98
99
100 private Digester digestSha1;
101
102
103
104
105 private Digester digestMd5;
106
107 private ManagedRepositoryContent repository;
108
109 private File repositoryDir;
110
111 private List<String> includes = new ArrayList<String>();
112
113 private Date whenGathered;
114
115 public String getId()
116 {
117 return this.id;
118 }
119
120 public String getDescription()
121 {
122 return this.description;
123 }
124
125 public boolean isPermanent()
126 {
127 return true;
128 }
129
130 public List<String> getExcludes()
131 {
132 return getDefaultArtifactExclusions();
133 }
134
135 public List<String> getIncludes()
136 {
137 return this.includes;
138 }
139
140 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
141 throws ConsumerException
142 {
143 try
144 {
145 this.repository = repositoryFactory.getManagedRepositoryContent( repo.getId() );
146 this.repositoryDir = new File( repository.getRepoRoot() );
147 this.whenGathered = whenGathered;
148 }
149 catch(RepositoryException e)
150 {
151 throw new ConsumerException( "Unable to start ArtifactUpdateDatabaseConsumer: " + e.getMessage(), e );
152 }
153 }
154
155 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
156 throws ConsumerException
157 {
158 beginScan( repository, whenGathered );
159 }
160
161 public void processFile( String path )
162 throws ConsumerException
163 {
164 ArchivaArtifact artifact = getLiveArtifact( path );
165
166 if ( artifact == null )
167 {
168 return;
169 }
170
171 try
172 {
173 if( artifact.getModel().getRepositoryId() == null )
174 {
175 artifact.getModel().setRepositoryId( this.repository.getId() );
176 }
177
178
179 File artifactFile = new File( this.repositoryDir, path );
180 try
181 {
182 artifact.getModel().setChecksumMD5( digestMd5.calc( artifactFile ) );
183 }
184 catch ( DigesterException e )
185 {
186 triggerConsumerWarning( CHECKSUM_CALCULATION,
187 "Unable to calculate the MD5 checksum: " + e.getMessage() );
188 }
189
190 try
191 {
192 artifact.getModel().setChecksumSHA1( digestSha1.calc( artifactFile ) );
193 }
194 catch ( DigesterException e )
195 {
196 triggerConsumerWarning( CHECKSUM_CALCULATION,
197 "Unable to calculate the SHA1 checksum: " + e.getMessage() );
198 }
199
200 artifact.getModel().setLastModified( new Date( artifactFile.lastModified() ) );
201 artifact.getModel().setSize( artifactFile.length() );
202 artifact.getModel().setOrigin( "FileSystem" );
203 artifact.getModel().setWhenProcessed( null );
204
205
206 if ( artifact.getModel().getWhenGathered() == null )
207 {
208 artifact.getModel().setWhenGathered( whenGathered );
209 }
210
211 dao.getArtifactDAO().saveArtifact( artifact );
212 }
213 catch ( ArchivaDatabaseException e )
214 {
215 triggerConsumerError( DB_ERROR, "Unable to save artifact to database: " + e.getMessage() );
216 }
217 }
218
219 public void processFile( String path, boolean executeOnEntireRepo )
220 throws Exception
221 {
222 processFile( path );
223 }
224
225
226
227
228
229
230
231
232
233
234
235 public ArchivaArtifact getLiveArtifact( String path )
236 {
237 try
238 {
239 ArtifactReference artifact = repository.toArtifactReference( path );
240
241 ArchivaArtifact liveArtifact = dao.getArtifactDAO().createArtifact( artifact.getGroupId(),
242 artifact.getArtifactId(),
243 artifact.getVersion(),
244 artifact.getClassifier(),
245 artifact.getType(),
246 repository.getId());
247
248 return liveArtifact;
249 }
250 catch ( LayoutException e )
251 {
252 triggerConsumerError( TYPE_NOT_ARTIFACT,
253 "Path " + path + " cannot be converted to artifact: " + e.getMessage() );
254 return null;
255 }
256 }
257
258 public void completeScan()
259 {
260
261 }
262
263 public void completeScan( boolean executeOnEntireRepo )
264 {
265 completeScan();
266 }
267
268 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
269 {
270 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
271 {
272 initIncludes();
273 }
274 }
275
276 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
277 {
278
279 }
280
281 private void initIncludes()
282 {
283 includes.clear();
284
285 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
286 }
287
288 public void initialize()
289 throws InitializationException
290 {
291 configuration.addChangeListener( this );
292
293 initIncludes();
294 }
295 }