[MRM-60] add an "old snapshot artifact" report

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@442502 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2006-09-12 07:20:42 +00:00
parent 7e1fe8ef3c
commit 9c27e8b4f7
5 changed files with 378 additions and 0 deletions

View File

@ -27,6 +27,7 @@ import java.io.File;
* Find artifacts in the repository that are considered old.
*
* @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="old-artifact"
* @todo make this configurable from the web interface
*/
public class OldArtifactReportProcessor
implements ArtifactReportProcessor

View File

@ -0,0 +1,179 @@
package org.apache.maven.archiva.reporting;
/*
* Copyright 2005-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Model;
import java.io.File;
import java.io.FilenameFilter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
/**
* Find snapshot artifacts in the repository that are considered old.
*
* @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="old-snapshot-artifact"
* @todo make this configurable from the web interface
*/
public class OldSnapshotArtifactReportProcessor
implements ArtifactReportProcessor
{
private static final String ROLE_HINT = "old-snapshot-artifact";
/**
* The maximum age of an artifact before it is reported old, specified in seconds. The default is 1 year.
*
* @plexus.configuration default-value="31536000"
*/
private int maxAge;
/**
* The maximum number of snapshots to retain within a given version. The default is 0, which keeps all snapshots
* that are within the age limits.
*
* @plexus.configuration default-value="0"
*/
private int maxSnapshots;
public void processArtifact( final Artifact artifact, Model model, ReportingDatabase reporter )
{
ArtifactRepository repository = artifact.getRepository();
if ( !"file".equals( repository.getProtocol() ) )
{
// We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
throw new UnsupportedOperationException(
"Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
}
adjustDistributionArtifactHandler( artifact );
String artifactPath = repository.pathOf( artifact );
//get the location of the artifact itself
File file = new File( repository.getBasedir(), artifactPath );
if ( file.exists() )
{
if ( artifact.isSnapshot() )
{
Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
if ( m.matches() )
{
long timestamp;
try
{
timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).parse( m.group( 2 ) ).getTime();
}
catch ( ParseException e )
{
throw new IllegalStateException(
"Shouldn't match timestamp pattern and not be able to parse it: " + m.group( 2 ) );
}
if ( System.currentTimeMillis() - timestamp > maxAge * 1000 )
{
addNotice( reporter, artifact, "snapshot-expired-time",
"The artifact is older than the maximum age of " + maxAge + " seconds." );
}
else if ( maxSnapshots > 0 )
{
File[] files = file.getParentFile().listFiles( new FilenameFilter()
{
public boolean accept( File file, String string )
{
return string.startsWith( artifact.getArtifactId() + "-" ) &&
string.endsWith( "." + artifact.getArtifactHandler().getExtension() );
}
} );
List/*<Integer>*/ buildNumbers = new ArrayList();
Integer currentBuild = null;
for ( Iterator i = Arrays.asList( files ).iterator(); i.hasNext(); )
{
File f = (File) i.next();
// trim to version
int startIndex = artifact.getArtifactId().length() + 1;
int extensionLength = artifact.getArtifactHandler().getExtension().length() + 1;
int endIndex = f.getName().length() - extensionLength;
String name = f.getName().substring( startIndex, endIndex );
Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( name );
if ( matcher.matches() )
{
Integer buildNumber = Integer.valueOf( matcher.group( 3 ) );
buildNumbers.add( buildNumber );
if ( name.equals( artifact.getVersion() ) )
{
currentBuild = buildNumber;
}
}
}
// Prune back to expired build numbers
Collections.sort( buildNumbers );
for ( int i = 0; i < maxSnapshots && !buildNumbers.isEmpty(); i++ )
{
buildNumbers.remove( buildNumbers.size() - 1 );
}
if ( buildNumbers.contains( currentBuild ) )
{
addNotice( reporter, artifact, "snapshot-expired-count",
"The artifact is older than the maximum number of retained snapshot builds." );
}
}
}
}
}
else
{
throw new IllegalStateException( "Couldn't find artifact " + file );
}
}
private static void addNotice( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
{
// TODO: reason could be an i18n key derived from the processor and the problem ID and the
reporter.addNotice( artifact, ROLE_HINT, problem, reason );
}
private static void adjustDistributionArtifactHandler( Artifact artifact )
{
// need to tweak these as they aren't currently in the known type converters. TODO - add them in Maven
if ( "distribution-zip".equals( artifact.getType() ) )
{
artifact.setArtifactHandler( new DefaultArtifactHandler( "zip" ) );
}
else if ( "distribution-tgz".equals( artifact.getType() ) )
{
artifact.setArtifactHandler( new DefaultArtifactHandler( "tar.gz" ) );
}
}
}

View File

@ -66,6 +66,8 @@ public abstract class AbstractRepositoryReportsTestCase
artifact.setRepository(
factory.createArtifactRepository( "repository", repository.toURL().toString(), layout, null, null ) );
artifact.isSnapshot();
return artifact;
}
@ -78,6 +80,7 @@ public abstract class AbstractRepositoryReportsTestCase
{
Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
artifact.setRepository( repository );
artifact.isSnapshot();
return artifact;
}

View File

@ -0,0 +1,166 @@
package org.apache.maven.archiva.reporting;
/*
* Copyright 2005-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.archiva.reporting.model.ArtifactResults;
import org.apache.maven.archiva.reporting.model.Result;
import org.apache.maven.artifact.Artifact;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
/**
* This class tests the OldArtifactReportProcessor.
*/
public class OldSnapshotArtifactReportProcessorTest
extends AbstractRepositoryReportsTestCase
{
private ArtifactReportProcessor artifactReportProcessor;
private ReportingDatabase reportDatabase;
private File tempRepository;
public void setUp()
throws Exception
{
super.setUp();
artifactReportProcessor =
(ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "old-snapshot-artifact" );
ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "old-artifact" );
reportDatabase = new ReportingDatabase( reportGroup );
tempRepository = getTestFile( "target/test-repository" );
FileUtils.deleteDirectory( tempRepository );
}
public void testOldSnapshotArtifact()
{
Artifact artifact = createArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-20050611.202024-1", "pom" );
artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
assertEquals( 0, reportDatabase.getNumFailures() );
assertEquals( 0, reportDatabase.getNumWarnings() );
assertEquals( "Check notices", 1, reportDatabase.getNumNotices() );
Iterator artifactIterator = reportDatabase.getArtifactIterator();
assertArtifactResults( artifactIterator, artifact );
}
private static void assertArtifactResults( Iterator artifactIterator, Artifact artifact )
{
ArtifactResults results = (ArtifactResults) artifactIterator.next();
assertEquals( artifact.getArtifactId(), results.getArtifactId() );
assertEquals( artifact.getGroupId(), results.getGroupId() );
assertEquals( artifact.getVersion(), results.getVersion() );
assertFalse( artifact.getVersion().indexOf( "SNAPSHOT" ) >= 0 );
assertEquals( 1, results.getNotices().size() );
Iterator i = results.getNotices().iterator();
Result result = (Result) i.next();
assertEquals( "old-snapshot-artifact", result.getProcessor() );
}
public void testSNAPSHOTArtifact()
{
Artifact artifact = createArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "pom" );
artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
assertEquals( 0, reportDatabase.getNumFailures() );
assertEquals( 0, reportDatabase.getNumWarnings() );
assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
}
public void testNonSnapshotArtifact()
{
Artifact artifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1" );
artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
assertEquals( 0, reportDatabase.getNumFailures() );
assertEquals( 0, reportDatabase.getNumWarnings() );
assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
}
public void testNewSnapshotArtifact()
throws Exception
{
File repository = getTestFile( "target/test-repository" );
File dir = new File( repository, "groupId/artifactId/1.0-alpha-1-SNAPSHOT" );
dir.mkdirs();
String date = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( new Date() );
FileUtils.fileWrite( new File( dir, "artifactId-1.0-alpha-1-" + date + "-1.jar" ).getAbsolutePath(), "foo" );
Artifact artifact =
createArtifactFromRepository( repository, "groupId", "artifactId", "1.0-alpha-1-" + date + "-1" );
artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
assertEquals( 0, reportDatabase.getNumFailures() );
assertEquals( 0, reportDatabase.getNumWarnings() );
assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
}
public void testTooManySnapshotArtifact()
throws Exception
{
File dir = new File( tempRepository, "groupId/artifactId/1.0-alpha-1-SNAPSHOT" );
dir.mkdirs();
String date = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( new Date() );
for ( int i = 1; i <= 5; i++ )
{
FileUtils.fileWrite( new File( dir, "artifactId-1.0-alpha-1-" + date + "-" + i + ".jar" ).getAbsolutePath(),
"foo" );
}
for ( int i = 1; i <= 5; i++ )
{
Artifact artifact = createArtifactFromRepository( tempRepository, "groupId", "artifactId",
"1.0-alpha-1-" + date + "-" + i );
artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
}
assertEquals( 0, reportDatabase.getNumFailures() );
assertEquals( 0, reportDatabase.getNumWarnings() );
assertEquals( "Check notices", 3, reportDatabase.getNumNotices() );
Iterator artifactIterator = reportDatabase.getArtifactIterator();
for ( int i = 1; i <= 3; i++ )
{
String version = "1.0-alpha-1-" + date + "-" + i;
Artifact artifact = createArtifactFromRepository( tempRepository, "groupId", "artifactId", version );
assertArtifactResults( artifactIterator, artifact );
}
}
public void testMissingArtifact()
throws Exception
{
Artifact artifact = createArtifact( "foo", "bar", "XP" );
try
{
artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
fail( "Should not have passed" );
}
catch ( IllegalStateException e )
{
assertTrue( true );
}
}
}

View File

@ -0,0 +1,29 @@
<!--
~ Copyright 2005-2006 The Apache Software Foundation.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<component-set>
<components>
<component>
<role>org.apache.maven.archiva.reporting.ArtifactReportProcessor</role>
<role-hint>old-snapshot-artifact</role-hint>
<implementation>org.apache.maven.archiva.reporting.OldSnapshotArtifactReportProcessor</implementation>
<configuration>
<maxAge>3600</maxAge>
<maxSnapshots>2</maxSnapshots>
</configuration>
</component>
</components>
</component-set>