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