1 package org.apache.maven.archiva.reporting;
4 * Copyright 2005-2006 The Apache Software Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.model.Model;
25 import java.io.FilenameFilter;
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.regex.Matcher;
36 * Find snapshot artifacts in the repository that are considered old.
38 * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="old-snapshot-artifact"
39 * @todo make this configurable from the web interface
41 public class OldSnapshotArtifactReportProcessor
42 implements ArtifactReportProcessor
44 private static final String ROLE_HINT = "old-snapshot-artifact";
47 * The maximum age of an artifact before it is reported old, specified in seconds. The default is 1 year.
49 * @plexus.configuration default-value="31536000"
54 * The maximum number of snapshots to retain within a given version. The default is 0, which keeps all snapshots
55 * that are within the age limits.
57 * @plexus.configuration default-value="0"
59 private int maxSnapshots;
61 public void processArtifact( final Artifact artifact, Model model, ReportingDatabase reporter )
63 ArtifactRepository repository = artifact.getRepository();
65 if ( !"file".equals( repository.getProtocol() ) )
67 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
68 throw new UnsupportedOperationException(
69 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
72 adjustDistributionArtifactHandler( artifact );
74 String artifactPath = repository.pathOf( artifact );
76 //get the location of the artifact itself
77 File file = new File( repository.getBasedir(), artifactPath );
81 if ( artifact.isSnapshot() )
83 Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
89 timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).parse( m.group( 2 ) ).getTime();
91 catch ( ParseException e )
93 throw new IllegalStateException(
94 "Shouldn't match timestamp pattern and not be able to parse it: " + m.group( 2 ) );
97 if ( System.currentTimeMillis() - timestamp > maxAge * 1000 )
99 addNotice( reporter, artifact, "snapshot-expired-time",
100 "The artifact is older than the maximum age of " + maxAge + " seconds." );
102 else if ( maxSnapshots > 0 )
104 File[] files = file.getParentFile().listFiles( new FilenameFilter()
106 public boolean accept( File file, String string )
108 return string.startsWith( artifact.getArtifactId() + "-" ) &&
109 string.endsWith( "." + artifact.getArtifactHandler().getExtension() );
113 List/*<Integer>*/ buildNumbers = new ArrayList();
114 Integer currentBuild = null;
115 for ( Iterator i = Arrays.asList( files ).iterator(); i.hasNext(); )
117 File f = (File) i.next();
120 int startIndex = artifact.getArtifactId().length() + 1;
121 int extensionLength = artifact.getArtifactHandler().getExtension().length() + 1;
122 int endIndex = f.getName().length() - extensionLength;
123 String name = f.getName().substring( startIndex, endIndex );
125 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( name );
127 if ( matcher.matches() )
129 Integer buildNumber = Integer.valueOf( matcher.group( 3 ) );
131 buildNumbers.add( buildNumber );
132 if ( name.equals( artifact.getVersion() ) )
134 currentBuild = buildNumber;
139 // Prune back to expired build numbers
140 Collections.sort( buildNumbers );
141 for ( int i = 0; i < maxSnapshots && !buildNumbers.isEmpty(); i++ )
143 buildNumbers.remove( buildNumbers.size() - 1 );
146 if ( buildNumbers.contains( currentBuild ) )
148 addNotice( reporter, artifact, "snapshot-expired-count",
149 "The artifact is older than the maximum number of retained snapshot builds." );
157 throw new IllegalStateException( "Couldn't find artifact " + file );
161 private static void addNotice( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
163 // TODO: reason could be an i18n key derived from the processor and the problem ID and the
164 reporter.addNotice( artifact, ROLE_HINT, problem, reason );
167 private static void adjustDistributionArtifactHandler( Artifact artifact )
169 // need to tweak these as they aren't currently in the known type converters. TODO - add them in Maven
170 if ( "distribution-zip".equals( artifact.getType() ) )
172 artifact.setArtifactHandler( new DefaultArtifactHandler( "zip" ) );
174 else if ( "distribution-tgz".equals( artifact.getType() ) )
176 artifact.setArtifactHandler( new DefaultArtifactHandler( "tar.gz" ) );