1 package org.apache.maven.archiva.reporting.processor;
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;
23 import org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor;
24 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
27 import java.io.FilenameFilter;
28 import java.text.ParseException;
29 import java.text.SimpleDateFormat;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.regex.Matcher;
38 * Find snapshot artifacts in the repository that are considered old.
40 * @plexus.component role="org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor" role-hint="old-snapshot-artifact"
41 * @todo make this configurable from the web interface
43 public class OldSnapshotArtifactReportProcessor
44 implements ArtifactReportProcessor
46 private static final String ROLE_HINT = "old-snapshot-artifact";
49 * The maximum age of an artifact before it is reported old, specified in seconds. The default is 1 year.
51 * @plexus.configuration default-value="31536000"
56 * The maximum number of snapshots to retain within a given version. The default is 0, which keeps all snapshots
57 * that are within the age limits.
59 * @plexus.configuration default-value="0"
61 private int maxSnapshots;
63 public void processArtifact( final Artifact artifact, Model model, ReportingDatabase reporter )
65 ArtifactRepository repository = artifact.getRepository();
67 if ( !"file".equals( repository.getProtocol() ) )
69 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
70 throw new UnsupportedOperationException(
71 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
74 adjustDistributionArtifactHandler( artifact );
76 String artifactPath = repository.pathOf( artifact );
78 //get the location of the artifact itself
79 File file = new File( repository.getBasedir(), artifactPath );
83 if ( artifact.isSnapshot() )
85 Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
91 timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).parse( m.group( 2 ) ).getTime();
93 catch ( ParseException e )
95 throw new IllegalStateException(
96 "Shouldn't match timestamp pattern and not be able to parse it: " + m.group( 2 ) );
99 if ( System.currentTimeMillis() - timestamp > maxAge * 1000 )
101 addNotice( reporter, artifact, "snapshot-expired-time",
102 "The artifact is older than the maximum age of " + maxAge + " seconds." );
104 else if ( maxSnapshots > 0 )
106 File[] files = file.getParentFile().listFiles( new FilenameFilter()
108 public boolean accept( File file, String string )
110 return string.startsWith( artifact.getArtifactId() + "-" ) &&
111 string.endsWith( "." + artifact.getArtifactHandler().getExtension() );
115 List/*<Integer>*/ buildNumbers = new ArrayList();
116 Integer currentBuild = null;
117 for ( Iterator i = Arrays.asList( files ).iterator(); i.hasNext(); )
119 File f = (File) i.next();
122 int startIndex = artifact.getArtifactId().length() + 1;
123 int extensionLength = artifact.getArtifactHandler().getExtension().length() + 1;
124 int endIndex = f.getName().length() - extensionLength;
125 String name = f.getName().substring( startIndex, endIndex );
127 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( name );
129 if ( matcher.matches() )
131 Integer buildNumber = Integer.valueOf( matcher.group( 3 ) );
133 buildNumbers.add( buildNumber );
134 if ( name.equals( artifact.getVersion() ) )
136 currentBuild = buildNumber;
141 // Prune back to expired build numbers
142 Collections.sort( buildNumbers );
143 for ( int i = 0; i < maxSnapshots && !buildNumbers.isEmpty(); i++ )
145 buildNumbers.remove( buildNumbers.size() - 1 );
148 if ( buildNumbers.contains( currentBuild ) )
150 addNotice( reporter, artifact, "snapshot-expired-count",
151 "The artifact is older than the maximum number of retained snapshot builds." );
159 throw new IllegalStateException( "Couldn't find artifact " + file );
163 private static void addNotice( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
165 // TODO: reason could be an i18n key derived from the processor and the problem ID and the
166 reporter.addNotice( artifact, ROLE_HINT, problem, reason );
169 private static void adjustDistributionArtifactHandler( Artifact artifact )
171 // need to tweak these as they aren't currently in the known type converters. TODO - add them in Maven
172 if ( "distribution-zip".equals( artifact.getType() ) )
174 artifact.setArtifactHandler( new DefaultArtifactHandler( "zip" ) );
176 else if ( "distribution-tgz".equals( artifact.getType() ) )
178 artifact.setArtifactHandler( new DefaultArtifactHandler( "tar.gz" ) );