]> source.dussan.org Git - archiva.git/blob
b067e6c56967190b56efe87c1d3d626d64074c9b
[archiva.git] /
1 package org.apache.maven.archiva.reporting.processor;
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.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;
25
26 import java.io.File;
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;
36
37 /**
38  * Find snapshot artifacts in the repository that are considered old.
39  *
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
42  */
43 public class OldSnapshotArtifactReportProcessor
44     implements ArtifactReportProcessor
45 {
46     private static final String ROLE_HINT = "old-snapshot-artifact";
47
48     /**
49      * The maximum age of an artifact before it is reported old, specified in seconds. The default is 1 year.
50      *
51      * @plexus.configuration default-value="31536000"
52      */
53     private int maxAge;
54
55     /**
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.
58      *
59      * @plexus.configuration default-value="0"
60      */
61     private int maxSnapshots;
62
63     public void processArtifact( final Artifact artifact, Model model, ReportingDatabase reporter )
64     {
65         ArtifactRepository repository = artifact.getRepository();
66
67         if ( !"file".equals( repository.getProtocol() ) )
68         {
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" );
72         }
73
74         adjustDistributionArtifactHandler( artifact );
75
76         String artifactPath = repository.pathOf( artifact );
77
78         //get the location of the artifact itself
79         File file = new File( repository.getBasedir(), artifactPath );
80
81         if ( file.exists() )
82         {
83             if ( artifact.isSnapshot() )
84             {
85                 Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
86                 if ( m.matches() )
87                 {
88                     long timestamp;
89                     try
90                     {
91                         timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).parse( m.group( 2 ) ).getTime();
92                     }
93                     catch ( ParseException e )
94                     {
95                         throw new IllegalStateException(
96                             "Shouldn't match timestamp pattern and not be able to parse it: " + m.group( 2 ) );
97                     }
98
99                     if ( System.currentTimeMillis() - timestamp > maxAge * 1000 )
100                     {
101                         addNotice( reporter, artifact, "snapshot-expired-time",
102                                    "The artifact is older than the maximum age of " + maxAge + " seconds." );
103                     }
104                     else if ( maxSnapshots > 0 )
105                     {
106                         File[] files = file.getParentFile().listFiles( new FilenameFilter()
107                         {
108                             public boolean accept( File file, String string )
109                             {
110                                 return string.startsWith( artifact.getArtifactId() + "-" ) &&
111                                     string.endsWith( "." + artifact.getArtifactHandler().getExtension() );
112                             }
113                         } );
114
115                         List/*<Integer>*/ buildNumbers = new ArrayList();
116                         Integer currentBuild = null;
117                         for ( Iterator i = Arrays.asList( files ).iterator(); i.hasNext(); )
118                         {
119                             File f = (File) i.next();
120
121                             // trim to version
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 );
126
127                             Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( name );
128
129                             if ( matcher.matches() )
130                             {
131                                 Integer buildNumber = Integer.valueOf( matcher.group( 3 ) );
132
133                                 buildNumbers.add( buildNumber );
134                                 if ( name.equals( artifact.getVersion() ) )
135                                 {
136                                     currentBuild = buildNumber;
137                                 }
138                             }
139                         }
140
141                         // Prune back to expired build numbers
142                         Collections.sort( buildNumbers );
143                         for ( int i = 0; i < maxSnapshots && !buildNumbers.isEmpty(); i++ )
144                         {
145                             buildNumbers.remove( buildNumbers.size() - 1 );
146                         }
147
148                         if ( buildNumbers.contains( currentBuild ) )
149                         {
150                             addNotice( reporter, artifact, "snapshot-expired-count",
151                                        "The artifact is older than the maximum number of retained snapshot builds." );
152                         }
153                     }
154                 }
155             }
156         }
157         else
158         {
159             throw new IllegalStateException( "Couldn't find artifact " + file );
160         }
161     }
162
163     private static void addNotice( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
164     {
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 );
167     }
168
169     private static void adjustDistributionArtifactHandler( Artifact artifact )
170     {
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() ) )
173         {
174             artifact.setArtifactHandler( new DefaultArtifactHandler( "zip" ) );
175         }
176         else if ( "distribution-tgz".equals( artifact.getType() ) )
177         {
178             artifact.setArtifactHandler( new DefaultArtifactHandler( "tar.gz" ) );
179         }
180     }
181 }