]> source.dussan.org Git - archiva.git/blob
b009825707ae9a7408f8e324cfde095fb9c54f7b
[archiva.git] /
1 package org.apache.maven.archiva.reporting.database;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.maven.archiva.reporting.model.ArtifactResults;
20 import org.apache.maven.archiva.reporting.model.MetadataResults;
21 import org.apache.maven.archiva.reporting.model.Reporting;
22 import org.apache.maven.archiva.reporting.model.Result;
23 import org.apache.maven.archiva.reporting.group.ReportGroup;
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
27
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.LinkedHashSet;
32 import java.util.Map;
33 import java.util.Set;
34
35 /**
36  * @todo i18n, including message formatting and parameterisation
37  */
38 public class ReportingDatabase
39 {
40     private final Reporting reporting;
41
42     private Map artifactMap;
43
44     private Map metadataMap;
45
46     private int numFailures;
47
48     private int numWarnings;
49
50     private ArtifactRepository repository;
51
52     private boolean inProgress;
53
54     private long startTime;
55
56     private final ReportGroup reportGroup;
57
58     private Set metadataWithProblems;
59
60     private Map filteredDatabases = new HashMap();
61
62     private int numNotices;
63
64     public ReportingDatabase( ReportGroup reportGroup )
65     {
66         this( reportGroup, new Reporting() );
67     }
68
69     public ReportingDatabase( ReportGroup reportGroup, Reporting reporting )
70     {
71         this( reportGroup, reporting, null );
72     }
73
74     public ReportingDatabase( ReportGroup reportGroup, ArtifactRepository repository )
75     {
76         this( reportGroup, new Reporting(), repository );
77     }
78
79     public ReportingDatabase( ReportGroup reportGroup, Reporting reporting, ArtifactRepository repository )
80     {
81         this.reportGroup = reportGroup;
82
83         this.reporting = reporting;
84
85         this.repository = repository;
86
87         initArtifactMap();
88
89         initMetadataMap();
90     }
91
92     public void addFailure( Artifact artifact, String processor, String problem, String reason )
93     {
94         ArtifactResults results = getArtifactResults( artifact );
95         results.addFailure( createResult( processor, problem, reason ) );
96         numFailures++;
97         updateTimings();
98
99         if ( filteredDatabases.containsKey( problem ) )
100         {
101             ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
102
103             reportingDatabase.addFailure( artifact, processor, problem, reason );
104         }
105     }
106
107     public void addNotice( Artifact artifact, String processor, String problem, String reason )
108     {
109         ArtifactResults results = getArtifactResults( artifact );
110         results.addNotice( createResult( processor, problem, reason ) );
111         numNotices++;
112         updateTimings();
113
114         if ( filteredDatabases.containsKey( problem ) )
115         {
116             ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
117
118             reportingDatabase.addNotice( artifact, processor, problem, reason );
119         }
120     }
121
122     public void addWarning( Artifact artifact, String processor, String problem, String reason )
123     {
124         ArtifactResults results = getArtifactResults( artifact );
125         results.addWarning( createResult( processor, problem, reason ) );
126         numWarnings++;
127         updateTimings();
128
129         if ( filteredDatabases.containsKey( problem ) )
130         {
131             ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
132
133             reportingDatabase.addWarning( artifact, processor, problem, reason );
134         }
135     }
136
137     private ArtifactResults getArtifactResults( Artifact artifact )
138     {
139         return getArtifactResults( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
140                                    artifact.getType(), artifact.getClassifier() );
141     }
142
143     private ArtifactResults getArtifactResults( String groupId, String artifactId, String version, String type,
144                                                 String classifier )
145     {
146         Map artifactMap = this.artifactMap;
147
148         String key = getArtifactKey( groupId, artifactId, version, type, classifier );
149         ArtifactResults results = (ArtifactResults) artifactMap.get( key );
150         if ( results == null )
151         {
152             results = new ArtifactResults();
153             results.setArtifactId( artifactId );
154             results.setClassifier( classifier );
155             results.setGroupId( groupId );
156             results.setType( type );
157             results.setVersion( version );
158
159             artifactMap.put( key, results );
160             reporting.getArtifacts().add( results );
161         }
162
163         return results;
164     }
165
166     private void initArtifactMap()
167     {
168         Map map = new HashMap();
169         for ( Iterator i = reporting.getArtifacts().iterator(); i.hasNext(); )
170         {
171             ArtifactResults result = (ArtifactResults) i.next();
172
173             String key = getArtifactKey( result.getGroupId(), result.getArtifactId(), result.getVersion(),
174                                          result.getType(), result.getClassifier() );
175             map.put( key, result );
176
177             numFailures += result.getFailures().size();
178             numWarnings += result.getWarnings().size();
179             numNotices += result.getNotices().size();
180         }
181         artifactMap = map;
182     }
183
184     private static String getArtifactKey( String groupId, String artifactId, String version, String type,
185                                           String classifier )
186     {
187         return groupId + ":" + artifactId + ":" + version + ":" + type + ":" + classifier;
188     }
189
190     private static Result createResult( String processor, String problem, String reason )
191     {
192         Result result = new Result();
193         result.setProcessor( processor );
194         result.setProblem( problem );
195         result.setReason( reason );
196         return result;
197     }
198
199     public void addFailure( RepositoryMetadata metadata, String processor, String problem, String reason )
200     {
201         MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
202         if ( !metadataWithProblems.contains( results ) )
203         {
204             metadataWithProblems.add( results );
205         }
206         results.addFailure( createResult( processor, problem, reason ) );
207         numFailures++;
208         updateTimings();
209
210         if ( filteredDatabases.containsKey( problem ) )
211         {
212             ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
213
214             reportingDatabase.addFailure( metadata, processor, problem, reason );
215         }
216     }
217
218     public void addWarning( RepositoryMetadata metadata, String processor, String problem, String reason )
219     {
220         MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
221         if ( !metadataWithProblems.contains( results ) )
222         {
223             metadataWithProblems.add( results );
224         }
225         results.addWarning( createResult( processor, problem, reason ) );
226         numWarnings++;
227         updateTimings();
228
229         if ( filteredDatabases.containsKey( problem ) )
230         {
231             ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
232
233             reportingDatabase.addWarning( metadata, processor, problem, reason );
234         }
235     }
236
237     public void addNotice( RepositoryMetadata metadata, String processor, String problem, String reason )
238     {
239         MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
240         if ( !metadataWithProblems.contains( results ) )
241         {
242             metadataWithProblems.add( results );
243         }
244         results.addNotice( createResult( processor, problem, reason ) );
245         numNotices++;
246         updateTimings();
247
248         if ( filteredDatabases.containsKey( problem ) )
249         {
250             ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
251
252             reportingDatabase.addNotice( metadata, processor, problem, reason );
253         }
254     }
255
256     public Set getMetadataWithProblems()
257     {
258         return metadataWithProblems;
259     }
260
261     private void initMetadataMap()
262     {
263         Map map = new HashMap();
264         Set problems = new LinkedHashSet();
265
266         for ( Iterator i = reporting.getMetadata().iterator(); i.hasNext(); )
267         {
268             MetadataResults result = (MetadataResults) i.next();
269
270             String key = getMetadataKey( result.getGroupId(), result.getArtifactId(), result.getVersion() );
271
272             map.put( key, result );
273
274             numFailures += result.getFailures().size();
275             numWarnings += result.getWarnings().size();
276             numNotices += result.getNotices().size();
277
278             if ( !result.getFailures().isEmpty() || !result.getWarnings().isEmpty() || !result.getNotices().isEmpty() )
279             {
280                 problems.add( result );
281             }
282         }
283         metadataMap = map;
284         metadataWithProblems = problems;
285     }
286
287     private static String getMetadataKey( String groupId, String artifactId, String version )
288     {
289         return groupId + ":" + artifactId + ":" + version;
290     }
291
292     public int getNumFailures()
293     {
294         return numFailures;
295     }
296
297     public int getNumWarnings()
298     {
299         return numWarnings;
300     }
301
302     public Reporting getReporting()
303     {
304         return reporting;
305     }
306
307     public Iterator getArtifactIterator()
308     {
309         return reporting.getArtifacts().iterator();
310     }
311
312     public Iterator getMetadataIterator()
313     {
314         return reporting.getMetadata().iterator();
315     }
316
317     public boolean isMetadataUpToDate( RepositoryMetadata metadata, long timestamp )
318     {
319         String key = getMetadataKey( metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion() );
320         Map map = metadataMap;
321         MetadataResults results = (MetadataResults) map.get( key );
322         return results != null && results.getLastModified() >= timestamp;
323     }
324
325     /**
326      * Make sure the metadata record exists, but remove any previous reports in preparation for adding new ones.
327      *
328      * @param metadata     the metadata
329      * @param lastModified the modification time of the file being tracked
330      */
331     public void cleanMetadata( RepositoryMetadata metadata, long lastModified )
332     {
333         MetadataResults results = getMetadataResults( metadata, lastModified );
334
335         results.setLastModified( lastModified );
336
337         numFailures -= results.getFailures().size();
338         results.getFailures().clear();
339
340         numWarnings -= results.getWarnings().size();
341         results.getWarnings().clear();
342
343         numNotices -= results.getWarnings().size();
344         results.getNotices().clear();
345
346         metadataWithProblems.remove( results );
347     }
348
349     private MetadataResults getMetadataResults( RepositoryMetadata metadata, long lastModified )
350     {
351         return getMetadataResults( metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion(),
352                                    lastModified );
353     }
354
355     private MetadataResults getMetadataResults( String groupId, String artifactId, String baseVersion,
356                                                 long lastModified )
357     {
358         String key = getMetadataKey( groupId, artifactId, baseVersion );
359         Map metadataMap = this.metadataMap;
360         MetadataResults results = (MetadataResults) metadataMap.get( key );
361         if ( results == null )
362         {
363             results = new MetadataResults();
364             results.setArtifactId( artifactId );
365             results.setGroupId( groupId );
366             results.setVersion( baseVersion );
367             results.setLastModified( lastModified );
368
369             metadataMap.put( key, results );
370             reporting.getMetadata().add( results );
371         }
372         return results;
373     }
374
375     public void removeArtifact( Artifact artifact )
376     {
377         Map map = artifactMap;
378
379         String key = getArtifactKey( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
380                                      artifact.getType(), artifact.getClassifier() );
381         ArtifactResults results = (ArtifactResults) map.get( key );
382         if ( results != null )
383         {
384             for ( Iterator i = reporting.getArtifacts().iterator(); i.hasNext(); )
385             {
386                 if ( results.equals( i.next() ) )
387                 {
388                     i.remove();
389                 }
390             }
391
392             numFailures -= results.getFailures().size();
393             numWarnings -= results.getWarnings().size();
394             numNotices -= results.getNotices().size();
395
396             map.remove( key );
397         }
398     }
399
400     public ArtifactRepository getRepository()
401     {
402         return repository;
403     }
404
405     public boolean isInProgress()
406     {
407         return inProgress;
408     }
409
410     public void setInProgress( boolean inProgress )
411     {
412         this.inProgress = inProgress;
413
414         if ( inProgress )
415         {
416             startTime = System.currentTimeMillis();
417         }
418     }
419
420     public void clear()
421     {
422         // clear the values rather than destroy the instance so that the "inProgress" indicator is in tact.
423         numWarnings = 0;
424         numNotices = 0;
425         numFailures = 0;
426
427         artifactMap.clear();
428         metadataMap.clear();
429         metadataWithProblems.clear();
430         filteredDatabases.clear();
431
432         reporting.getArtifacts().clear();
433         reporting.getMetadata().clear();
434
435         updateTimings();
436     }
437
438     public void setStartTime( long startTime )
439     {
440         this.startTime = startTime;
441     }
442
443     public long getStartTime()
444     {
445         return startTime;
446     }
447
448     public void updateTimings()
449     {
450         long startTime = getStartTime();
451         Date endTime = new Date();
452         if ( startTime > 0 )
453         {
454             getReporting().setExecutionTime( endTime.getTime() - startTime );
455         }
456         getReporting().setLastModified( endTime.getTime() );
457     }
458
459     public ReportGroup getReportGroup()
460     {
461         return reportGroup;
462     }
463
464     public ReportingDatabase getFilteredDatabase( String filter )
465     {
466         ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( filter );
467
468         if ( reportingDatabase == null )
469         {
470             reportingDatabase = new ReportingDatabase( reportGroup, repository );
471
472             Reporting reporting = reportingDatabase.getReporting();
473             reporting.setExecutionTime( this.reporting.getExecutionTime() );
474             reporting.setLastModified( this.reporting.getLastModified() );
475
476             for ( Iterator i = this.reporting.getArtifacts().iterator(); i.hasNext(); )
477             {
478                 ArtifactResults results = (ArtifactResults) i.next();
479                 ArtifactResults targetResults = null;
480                 for ( Iterator j = results.getFailures().iterator(); j.hasNext(); )
481                 {
482                     Result result = (Result) j.next();
483
484                     if ( filter.equals( result.getProcessor() ) )
485                     {
486                         if ( targetResults == null )
487                         {
488                             // lazily create so it is not added unless it has to be
489                             targetResults = createArtifactResults( reportingDatabase, results );
490                         }
491
492                         targetResults.addFailure( result );
493                         reportingDatabase.numFailures++;
494                     }
495                 }
496                 for ( Iterator j = results.getWarnings().iterator(); j.hasNext(); )
497                 {
498                     Result result = (Result) j.next();
499
500                     if ( filter.equals( result.getProcessor() ) )
501                     {
502                         if ( targetResults == null )
503                         {
504                             // lazily create so it is not added unless it has to be
505                             targetResults = createArtifactResults( reportingDatabase, results );
506                         }
507
508                         targetResults.addWarning( result );
509                         reportingDatabase.numWarnings++;
510                     }
511                 }
512                 for ( Iterator j = results.getNotices().iterator(); j.hasNext(); )
513                 {
514                     Result result = (Result) j.next();
515
516                     if ( filter.equals( result.getProcessor() ) )
517                     {
518                         if ( targetResults == null )
519                         {
520                             // lazily create so it is not added unless it has to be
521                             targetResults = createArtifactResults( reportingDatabase, results );
522                         }
523
524                         targetResults.addNotice( result );
525                         reportingDatabase.numNotices++;
526                     }
527                 }
528             }
529             for ( Iterator i = this.reporting.getMetadata().iterator(); i.hasNext(); )
530             {
531                 MetadataResults results = (MetadataResults) i.next();
532                 MetadataResults targetResults = null;
533                 for ( Iterator j = results.getFailures().iterator(); j.hasNext(); )
534                 {
535                     Result result = (Result) j.next();
536
537                     if ( filter.equals( result.getProcessor() ) )
538                     {
539                         if ( targetResults == null )
540                         {
541                             // lazily create so it is not added unless it has to be
542                             targetResults = createMetadataResults( reportingDatabase, results );
543                         }
544
545                         targetResults.addFailure( result );
546                         reportingDatabase.numFailures++;
547                     }
548                 }
549                 for ( Iterator j = results.getWarnings().iterator(); j.hasNext(); )
550                 {
551                     Result result = (Result) j.next();
552
553                     if ( filter.equals( result.getProcessor() ) )
554                     {
555                         if ( targetResults == null )
556                         {
557                             // lazily create so it is not added unless it has to be
558                             targetResults = createMetadataResults( reportingDatabase, results );
559                         }
560
561                         targetResults.addWarning( result );
562                         reportingDatabase.numWarnings++;
563                     }
564                 }
565                 for ( Iterator j = results.getNotices().iterator(); j.hasNext(); )
566                 {
567                     Result result = (Result) j.next();
568
569                     if ( filter.equals( result.getProcessor() ) )
570                     {
571                         if ( targetResults == null )
572                         {
573                             // lazily create so it is not added unless it has to be
574                             targetResults = createMetadataResults( reportingDatabase, results );
575                         }
576
577                         targetResults.addNotice( result );
578                         reportingDatabase.numNotices++;
579                     }
580                 }
581             }
582
583             filteredDatabases.put( filter, reportingDatabase );
584         }
585
586         return reportingDatabase;
587     }
588
589     private static MetadataResults createMetadataResults( ReportingDatabase reportingDatabase, MetadataResults results )
590     {
591         MetadataResults targetResults = reportingDatabase.getMetadataResults( results.getGroupId(),
592                                                                               results.getArtifactId(),
593                                                                               results.getVersion(),
594                                                                               results.getLastModified() );
595         reportingDatabase.metadataWithProblems.add( targetResults );
596         return targetResults;
597     }
598
599     private static ArtifactResults createArtifactResults( ReportingDatabase reportingDatabase, ArtifactResults results )
600     {
601         return reportingDatabase.getArtifactResults( results.getGroupId(), results.getArtifactId(),
602                                                      results.getVersion(), results.getType(), results.getClassifier() );
603     }
604
605     public int getNumNotices()
606     {
607         return numNotices;
608     }
609 }