MRM-594 convert PathParsers to a plexus component, so that it can access the archivaConfiguration and read custom legacyPath 2 artifact references.

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@602922 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nicolas De Loof 2007-12-10 14:58:14 +00:00
parent 5c89de0ca6
commit 678173ca42
11 changed files with 227 additions and 120 deletions

View File

@ -43,10 +43,15 @@ public abstract class AbstractDefaultRepositoryContent
protected static final char ARTIFACT_SEPARATOR = '-';
/**
* @plexus.requirement role-hint="default"
*/
private PathParser defaultPathParser;
public ArtifactReference toArtifactReference( String path )
throws LayoutException
{
return DefaultPathParser.toArtifactReference( path );
return defaultPathParser.toArtifactReference( path );
}
public String toMetadataPath( ProjectReference reference )

View File

@ -49,10 +49,15 @@ public abstract class AbstractLegacyRepositoryContent
typeToDirectoryMap.put( "javadoc", "javadoc.jar" );
}
/**
* @plexus.requirement role-hint="legacy"
*/
private PathParser legacyPathParser;
public ArtifactReference toArtifactReference( String path )
throws LayoutException
{
return LegacyPathParser.toArtifactReference( path );
return legacyPathParser.toArtifactReference( path );
}
public String toPath( ArchivaArtifact reference )

View File

@ -30,20 +30,17 @@ import org.apache.maven.archiva.repository.layout.LayoutException;
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
*
* @plexus.component role="org.apache.maven.archiva.repository.content.DefaultPathParser"
* @plexus.component role="org.apache.maven.archiva.repository.content.PathParser" role-hint="default"
*/
public class DefaultPathParser
public class DefaultPathParser implements PathParser
{
private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
/**
* Convert a path to an ArtifactReference.
*
* @param path
* @return
* @throws LayoutException
* {@inheritDoc}
* @see org.apache.maven.archiva.repository.content.PathParser#toArtifactReference(java.lang.String)
*/
protected static ArtifactReference toArtifactReference( String path )
public ArtifactReference toArtifactReference( String path )
throws LayoutException
{
if ( StringUtils.isBlank( path ) )

View File

@ -19,23 +19,54 @@ package org.apache.maven.archiva.repository.content;
* under the License.
*/
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.LegacyArtifactPath;
import org.apache.maven.archiva.model.ArtifactReference;
import org.apache.maven.archiva.repository.layout.LayoutException;
/**
* LegacyPathParser is a parser for maven 1 (legacy layout) paths to ArtifactReference.
* LegacyPathParser is a parser for maven 1 (legacy layout) paths to
* ArtifactReference.
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
* @plexus.component role="org.apache.maven.archiva.repository.content.PathParser"
* role-hint="legacy"
*/
public class LegacyPathParser
implements PathParser
{
private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
protected static ArtifactReference toArtifactReference( String path )
/**
* @plexus.requirement
*/
protected ArchivaConfiguration configuration;
/**
* {@inheritDoc}
*
* @see org.apache.maven.archiva.repository.content.PathParser#toArtifactReference(java.lang.String)
*/
public ArtifactReference toArtifactReference( String path )
throws LayoutException
{
// First, look if a custom resolution rule has been set for this artifact
Collection legacy = configuration.getConfiguration().getLegacyArtifactPaths();
for ( Iterator iterator = legacy.iterator(); iterator.hasNext(); )
{
LegacyArtifactPath legacyPath = (LegacyArtifactPath) iterator.next();
if ( legacyPath.match( path ) )
{
return legacyPath.getArtifactReference();
}
}
ArtifactReference artifact = new ArtifactReference();
String normalizedPath = StringUtils.replace( path, "\\", "/" );
@ -54,8 +85,8 @@ public class LegacyPathParser
{
// Illegal Path Parts Length.
throw new LayoutException( INVALID_ARTIFACT_PATH
+ "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
+ pathParts.length + " instead." );
+ "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
+ pathParts.length + " instead." );
}
// The Group ID.
@ -68,7 +99,7 @@ public class LegacyPathParser
if ( !expectedType.endsWith( "s" ) )
{
throw new LayoutException( INVALID_ARTIFACT_PATH
+ "legacy paths should have an expected type ending in [s] in the second part of the path." );
+ "legacy paths should have an expected type ending in [s] in the second part of the path." );
}
// The Filename.

View File

@ -0,0 +1,19 @@
/**
*
*/
package org.apache.maven.archiva.repository.content;
import org.apache.maven.archiva.model.ArtifactReference;
import org.apache.maven.archiva.repository.layout.LayoutException;
/**
* @author ndeloof
*
*/
public interface PathParser
{
public ArtifactReference toArtifactReference( String path )
throws LayoutException;
}

View File

@ -59,6 +59,16 @@ public class RepositoryRequest
*/
private ArchivaConfiguration archivaConfiguration;
/**
* @plexus.requirement role-hint="default"
*/
private PathParser defaultPathParser;
/**
* @plexus.requirement role-hint="legacy"
*/
private PathParser legacyPathParser;
private List<String> artifactPatterns;
/**
@ -118,11 +128,11 @@ public class RepositoryRequest
if ( isDefault( path ) )
{
return DefaultPathParser.toArtifactReference( path );
return defaultPathParser.toArtifactReference( path );
}
else if ( isLegacy( path ) )
{
return LegacyPathParser.toArtifactReference( path );
return legacyPathParser.toArtifactReference( path );
}
else
{

View File

@ -14,6 +14,8 @@ import org.apache.maven.archiva.repository.layout.LayoutException;
public class DefaultPathParserTest
extends AbstractRepositoryLayerTestCase
{
private PathParser parser = new DefaultPathParser();
public void testBadPathMissingType()
{
assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
@ -311,7 +313,7 @@ public class DefaultPathParserTest
{
try
{
DefaultPathParser.toArtifactReference( "" );
parser.toArtifactReference( "" );
fail( "Should have failed due to empty path." );
}
catch ( LayoutException e )
@ -324,7 +326,7 @@ public class DefaultPathParserTest
{
try
{
DefaultPathParser.toArtifactReference( null );
parser.toArtifactReference( null );
fail( "Should have failed due to null path." );
}
catch ( LayoutException e )
@ -337,7 +339,7 @@ public class DefaultPathParserTest
{
try
{
DefaultPathParser.toArtifactReference( "" );
parser.toArtifactReference( "" );
fail( "Should have failed due to empty path." );
}
catch ( LayoutException e )
@ -350,7 +352,7 @@ public class DefaultPathParserTest
{
try
{
DefaultPathParser.toArtifactReference( null );
parser.toArtifactReference( null );
fail( "Should have failed due to null path." );
}
catch ( LayoutException e )
@ -367,7 +369,7 @@ public class DefaultPathParserTest
throws LayoutException
{
// Path to Artifact Reference.
ArtifactReference testReference = DefaultPathParser.toArtifactReference( path );
ArtifactReference testReference = parser.toArtifactReference( path );
assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
}
@ -393,7 +395,7 @@ public class DefaultPathParserTest
{
try
{
DefaultPathParser.toArtifactReference( path );
parser.toArtifactReference( path );
fail( "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
}
catch ( LayoutException e )

View File

@ -20,6 +20,9 @@ package org.apache.maven.archiva.repository.content;
*/
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.DefaultArchivaConfiguration;
import org.apache.maven.archiva.configuration.LegacyArtifactPath;
import org.apache.maven.archiva.model.ArtifactReference;
import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
import org.apache.maven.archiva.repository.layout.LayoutException;
@ -33,6 +36,26 @@ import org.apache.maven.archiva.repository.layout.LayoutException;
public class LegacyPathParserTest
extends AbstractRepositoryLayerTestCase
{
private LegacyPathParser parser = new LegacyPathParser();
/**
* Configure the ArchivaConfiguration
* {@inheritDoc}
* @see org.codehaus.plexus.PlexusTestCase#setUp()
*/
protected void setUp()
throws Exception
{
super.setUp();
ArchivaConfiguration config = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
LegacyArtifactPath jaxen = new LegacyArtifactPath();
jaxen.setPath( "jaxen/jars/jaxen-1.0-FCS-full.jar" );
jaxen.setArtifact( "jaxen:jaxen:1.0-FCS:full:jar" );
config.getConfiguration().addLegacyArtifactPath( jaxen );
parser.configuration = config;
}
public void testBadPathArtifactIdMissingA()
{
assertBadPath( "groupId/jars/-1.0.jar", "artifactId is missing" );
@ -338,22 +361,37 @@ public class LegacyPathParserTest
assertLayout( path, groupId, artifactId, version, null, type );
}
/**
* [MRM-594] add some hook in LegacyPathParser to allow exceptions in artifact resolution
*/
public void testCustomExceptionsInArtifactResolution()
throws LayoutException
{
String groupId = "jaxen";
String artifactId = "jaxen";
String version = "1.0-FCS";
String type = "jar";
String classifier = "full";
String path = "jaxen/jars/jaxen-1.0-FCS-full.jar";
assertLayout( path, groupId, artifactId, version, classifier, type );
}
/**
* Perform a path to artifact reference lookup, and verify the results.
* @param classifier TODO
*/
private void assertLayout( String path, String groupId, String artifactId, String version, String classifier, String type )
throws LayoutException
{
// Path to Artifact Reference.
ArtifactReference testReference = LegacyPathParser.toArtifactReference( path );
ArtifactReference testReference = parser.toArtifactReference( path );
assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
}
private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
String version, String classifier, String type )
{
String expectedId = "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + type;
String expectedId = "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
assertNotNull( expectedId + " - Should not be null.", actualReference );
@ -368,7 +406,7 @@ public class LegacyPathParserTest
{
try
{
LegacyPathParser.toArtifactReference( path );
parser.toArtifactReference( path );
fail( "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
}
catch ( LayoutException e )