1 package org.apache.maven.archiva.reporting.database;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.maven.archiva.reporting.group.ReportGroup;
23 import org.apache.maven.archiva.reporting.model.ArtifactResults;
24 import org.apache.maven.archiva.reporting.model.MetadataResults;
25 import org.apache.maven.archiva.reporting.model.Reporting;
26 import org.apache.maven.archiva.reporting.model.Result;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.LinkedHashSet;
39 * @todo i18n, including message formatting and parameterisation
41 public class ReportingDatabase
43 private final Reporting reporting;
45 private Map artifactMap;
47 private Map metadataMap;
49 private int numFailures;
51 private int numWarnings;
53 private ArtifactRepository repository;
55 private boolean inProgress;
57 private long startTime;
59 private final ReportGroup reportGroup;
61 private Set metadataWithProblems;
63 private Map filteredDatabases = new HashMap();
65 private int numNotices;
67 public ReportingDatabase( ReportGroup reportGroup )
69 this( reportGroup, new Reporting() );
72 public ReportingDatabase( ReportGroup reportGroup, Reporting reporting )
74 this( reportGroup, reporting, null );
77 public ReportingDatabase( ReportGroup reportGroup, ArtifactRepository repository )
79 this( reportGroup, new Reporting(), repository );
82 public ReportingDatabase( ReportGroup reportGroup, Reporting reporting, ArtifactRepository repository )
84 this.reportGroup = reportGroup;
86 this.reporting = reporting;
88 this.repository = repository;
95 public void addFailure( Artifact artifact, String processor, String problem, String reason )
97 ArtifactResults results = getArtifactResults( artifact );
98 Result result = createResult( processor, problem, reason );
99 if ( !results.getFailures().contains( result ) )
101 results.addFailure( result );
106 if ( filteredDatabases.containsKey( problem ) )
108 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
110 reportingDatabase.addFailure( artifact, processor, problem, reason );
114 public void addNotice( Artifact artifact, String processor, String problem, String reason )
116 ArtifactResults results = getArtifactResults( artifact );
117 Result result = createResult( processor, problem, reason );
118 if ( !results.getNotices().contains( result ) )
120 results.addNotice( result );
125 if ( filteredDatabases.containsKey( problem ) )
127 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
129 reportingDatabase.addNotice( artifact, processor, problem, reason );
133 public void addWarning( Artifact artifact, String processor, String problem, String reason )
135 ArtifactResults results = getArtifactResults( artifact );
136 Result result = createResult( processor, problem, reason );
137 if ( !results.getWarnings().contains( result ) )
139 results.addWarning( result );
144 if ( filteredDatabases.containsKey( problem ) )
146 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
148 reportingDatabase.addWarning( artifact, processor, problem, reason );
152 ArtifactResults getArtifactResults( Artifact artifact )
154 return getArtifactResults( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
155 artifact.getType(), artifact.getClassifier() );
158 private ArtifactResults getArtifactResults( String groupId, String artifactId, String version, String type,
161 Map artifactMap = this.artifactMap;
163 String key = getArtifactKey( groupId, artifactId, version, type, classifier );
164 ArtifactResults results = (ArtifactResults) artifactMap.get( key );
165 if ( results == null )
167 results = new ArtifactResults();
168 results.setArtifactId( artifactId );
169 results.setClassifier( classifier );
170 results.setGroupId( groupId );
171 results.setType( type );
172 results.setVersion( version );
174 artifactMap.put( key, results );
175 reporting.getArtifacts().add( results );
181 private void initArtifactMap()
183 Map map = new HashMap();
184 for ( Iterator i = reporting.getArtifacts().iterator(); i.hasNext(); )
186 ArtifactResults result = (ArtifactResults) i.next();
188 String key = getArtifactKey( result.getGroupId(), result.getArtifactId(), result.getVersion(),
189 result.getType(), result.getClassifier() );
190 map.put( key, result );
192 numFailures += result.getFailures().size();
193 numWarnings += result.getWarnings().size();
194 numNotices += result.getNotices().size();
199 private static String getArtifactKey( String groupId, String artifactId, String version, String type,
202 return groupId + ":" + artifactId + ":" + version + ":" + type + ":" + classifier;
205 private static Result createResult( String processor, String problem, String reason )
207 Result result = new Result();
208 result.setProcessor( processor );
209 result.setProblem( problem );
210 result.setReason( reason );
214 public void addFailure( RepositoryMetadata metadata, String processor, String problem, String reason )
216 MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
217 if ( !metadataWithProblems.contains( results ) )
219 metadataWithProblems.add( results );
221 Result result = createResult( processor, problem, reason );
222 if ( !results.getFailures().contains( result ) )
224 results.addFailure( result );
229 if ( filteredDatabases.containsKey( problem ) )
231 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
233 reportingDatabase.addFailure( metadata, processor, problem, reason );
237 public void addWarning( RepositoryMetadata metadata, String processor, String problem, String reason )
239 MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
240 if ( !metadataWithProblems.contains( results ) )
242 metadataWithProblems.add( results );
244 Result result = createResult( processor, problem, reason );
245 if ( !results.getWarnings().contains( result ) )
247 results.addWarning( result );
252 if ( filteredDatabases.containsKey( problem ) )
254 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
256 reportingDatabase.addWarning( metadata, processor, problem, reason );
260 public void addNotice( RepositoryMetadata metadata, String processor, String problem, String reason )
262 MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
263 if ( !metadataWithProblems.contains( results ) )
265 metadataWithProblems.add( results );
267 Result result = createResult( processor, problem, reason );
268 if ( !results.getNotices().contains( result ) )
270 results.addNotice( result );
275 if ( filteredDatabases.containsKey( problem ) )
277 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
279 reportingDatabase.addNotice( metadata, processor, problem, reason );
283 public Set getMetadataWithProblems()
285 return metadataWithProblems;
288 private void initMetadataMap()
290 Map map = new HashMap();
291 Set problems = new LinkedHashSet();
293 for ( Iterator i = reporting.getMetadata().iterator(); i.hasNext(); )
295 MetadataResults result = (MetadataResults) i.next();
297 String key = getMetadataKey( result.getGroupId(), result.getArtifactId(), result.getVersion() );
299 map.put( key, result );
301 numFailures += result.getFailures().size();
302 numWarnings += result.getWarnings().size();
303 numNotices += result.getNotices().size();
305 if ( !result.getFailures().isEmpty() || !result.getWarnings().isEmpty() || !result.getNotices().isEmpty() )
307 problems.add( result );
311 metadataWithProblems = problems;
314 private static String getMetadataKey( String groupId, String artifactId, String version )
316 return groupId + ":" + artifactId + ":" + version;
319 public int getNumFailures()
324 public int getNumWarnings()
329 public Reporting getReporting()
334 public Iterator getArtifactIterator()
336 return reporting.getArtifacts().iterator();
339 public Iterator getMetadataIterator()
341 return reporting.getMetadata().iterator();
344 public boolean isMetadataUpToDate( RepositoryMetadata metadata, long timestamp )
346 String key = getMetadataKey( metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion() );
347 Map map = metadataMap;
348 MetadataResults results = (MetadataResults) map.get( key );
349 return results != null && results.getLastModified() >= timestamp;
353 * Make sure the metadata record exists, but remove any previous reports in preparation for adding new ones.
355 * @param metadata the metadata
356 * @param lastModified the modification time of the file being tracked
358 public void cleanMetadata( RepositoryMetadata metadata, long lastModified )
360 MetadataResults results = getMetadataResults( metadata, lastModified );
362 results.setLastModified( lastModified );
364 numFailures -= results.getFailures().size();
365 results.getFailures().clear();
367 numWarnings -= results.getWarnings().size();
368 results.getWarnings().clear();
370 numNotices -= results.getWarnings().size();
371 results.getNotices().clear();
373 metadataWithProblems.remove( results );
376 MetadataResults getMetadataResults( RepositoryMetadata metadata, long lastModified )
378 return getMetadataResults( metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion(),
382 private MetadataResults getMetadataResults( String groupId, String artifactId, String baseVersion,
385 String key = getMetadataKey( groupId, artifactId, baseVersion );
386 Map metadataMap = this.metadataMap;
387 MetadataResults results = (MetadataResults) metadataMap.get( key );
388 if ( results == null )
390 results = new MetadataResults();
391 results.setArtifactId( artifactId );
392 results.setGroupId( groupId );
393 results.setVersion( baseVersion );
394 results.setLastModified( lastModified );
396 metadataMap.put( key, results );
397 reporting.getMetadata().add( results );
402 public void removeArtifact( Artifact artifact )
404 Map map = artifactMap;
406 String key = getArtifactKey( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
407 artifact.getType(), artifact.getClassifier() );
408 ArtifactResults results = (ArtifactResults) map.get( key );
409 if ( results != null )
411 for ( Iterator i = reporting.getArtifacts().iterator(); i.hasNext(); )
413 if ( results.equals( i.next() ) )
419 numFailures -= results.getFailures().size();
420 numWarnings -= results.getWarnings().size();
421 numNotices -= results.getNotices().size();
427 public ArtifactRepository getRepository()
432 public boolean isInProgress()
437 public void setInProgress( boolean inProgress )
439 this.inProgress = inProgress;
443 startTime = System.currentTimeMillis();
449 // clear the values rather than destroy the instance so that the "inProgress" indicator is in tact.
456 metadataWithProblems.clear();
457 filteredDatabases.clear();
459 reporting.getArtifacts().clear();
460 reporting.getMetadata().clear();
465 public void setStartTime( long startTime )
467 this.startTime = startTime;
470 public long getStartTime()
475 public void updateTimings()
477 long startTime = getStartTime();
478 Date endTime = new Date();
481 getReporting().setExecutionTime( endTime.getTime() - startTime );
483 getReporting().setLastModified( endTime.getTime() );
486 public ReportGroup getReportGroup()
491 public ReportingDatabase getFilteredDatabase( String filter )
493 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( filter );
495 if ( reportingDatabase == null )
497 reportingDatabase = new ReportingDatabase( reportGroup, repository );
499 Reporting reporting = reportingDatabase.getReporting();
500 reporting.setExecutionTime( this.reporting.getExecutionTime() );
501 reporting.setLastModified( this.reporting.getLastModified() );
503 for ( Iterator i = this.reporting.getArtifacts().iterator(); i.hasNext(); )
505 ArtifactResults results = (ArtifactResults) i.next();
506 ArtifactResults targetResults = null;
507 for ( Iterator j = results.getFailures().iterator(); j.hasNext(); )
509 Result result = (Result) j.next();
511 if ( filter.equals( result.getProcessor() ) )
513 if ( targetResults == null )
515 // lazily create so it is not added unless it has to be
516 targetResults = createArtifactResults( reportingDatabase, results );
519 targetResults.addFailure( result );
520 reportingDatabase.numFailures++;
523 for ( Iterator j = results.getWarnings().iterator(); j.hasNext(); )
525 Result result = (Result) j.next();
527 if ( filter.equals( result.getProcessor() ) )
529 if ( targetResults == null )
531 // lazily create so it is not added unless it has to be
532 targetResults = createArtifactResults( reportingDatabase, results );
535 targetResults.addWarning( result );
536 reportingDatabase.numWarnings++;
539 for ( Iterator j = results.getNotices().iterator(); j.hasNext(); )
541 Result result = (Result) j.next();
543 if ( filter.equals( result.getProcessor() ) )
545 if ( targetResults == null )
547 // lazily create so it is not added unless it has to be
548 targetResults = createArtifactResults( reportingDatabase, results );
551 targetResults.addNotice( result );
552 reportingDatabase.numNotices++;
556 for ( Iterator i = this.reporting.getMetadata().iterator(); i.hasNext(); )
558 MetadataResults results = (MetadataResults) i.next();
559 MetadataResults targetResults = null;
560 for ( Iterator j = results.getFailures().iterator(); j.hasNext(); )
562 Result result = (Result) j.next();
564 if ( filter.equals( result.getProcessor() ) )
566 if ( targetResults == null )
568 // lazily create so it is not added unless it has to be
569 targetResults = createMetadataResults( reportingDatabase, results );
572 targetResults.addFailure( result );
573 reportingDatabase.numFailures++;
576 for ( Iterator j = results.getWarnings().iterator(); j.hasNext(); )
578 Result result = (Result) j.next();
580 if ( filter.equals( result.getProcessor() ) )
582 if ( targetResults == null )
584 // lazily create so it is not added unless it has to be
585 targetResults = createMetadataResults( reportingDatabase, results );
588 targetResults.addWarning( result );
589 reportingDatabase.numWarnings++;
592 for ( Iterator j = results.getNotices().iterator(); j.hasNext(); )
594 Result result = (Result) j.next();
596 if ( filter.equals( result.getProcessor() ) )
598 if ( targetResults == null )
600 // lazily create so it is not added unless it has to be
601 targetResults = createMetadataResults( reportingDatabase, results );
604 targetResults.addNotice( result );
605 reportingDatabase.numNotices++;
610 filteredDatabases.put( filter, reportingDatabase );
613 return reportingDatabase;
616 private static MetadataResults createMetadataResults( ReportingDatabase reportingDatabase, MetadataResults results )
618 MetadataResults targetResults = reportingDatabase.getMetadataResults( results.getGroupId(),
619 results.getArtifactId(),
620 results.getVersion(),
621 results.getLastModified() );
622 reportingDatabase.metadataWithProblems.add( targetResults );
623 return targetResults;
626 private static ArtifactResults createArtifactResults( ReportingDatabase reportingDatabase, ArtifactResults results )
628 return reportingDatabase.getArtifactResults( results.getGroupId(), results.getArtifactId(),
629 results.getVersion(), results.getType(), results.getClassifier() );
632 public int getNumNotices()