move discovery to a new component

PR: MRM-9


git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@349577 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2005-11-29 01:34:22 +00:00
parent af0ddbcfc1
commit ea640e1803
6 changed files with 659 additions and 0 deletions

View File

@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.apache.maven.repository</groupId>
<artifactId>maven-repository-manager</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-repository-discovery</artifactId>
<name>Maven Repository Artifact Discovery</name>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,103 @@
package org.apache.maven.repository.discovery;
/*
* Copyright 2001-2005 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.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.DirectoryScanner;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Base class for artifact discoverers.
*
* @author John Casey
* @author Brett Porter
*/
public abstract class AbstractArtifactDiscoverer
extends AbstractLogEnabled
implements ArtifactDiscoverer
{
/**
* Standard patterns to exclude from discovery as they are not artifacts.
*/
private static final String[] STANDARD_DISCOVERY_EXCLUDES = {"bin/**", "reports/**", ".maven/**", "**/*.md5",
"**/*.MD5", "**/*.sha1", "**/*.SHA1", "**/*snapshot-version", "*/website/**", "*/licenses/**", "*/licences/**",
"**/.htaccess", "**/*.html", "**/*.asc", "**/*.txt", "**/*.xml", "**/README*", "**/CHANGELOG*", "**/KEYS*"};
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private List excludedPaths = new ArrayList();
private List kickedOutPaths = new ArrayList();
/**
* Scan the repository for artifact paths
*
* @todo operate on better parameters, more collections, less arrays
*/
protected String[] scanForArtifactPaths( File repositoryBase, String blacklistedPatterns )
{
String[] blacklisted;
if ( blacklistedPatterns != null && blacklistedPatterns.length() > 0 )
{
blacklisted = blacklistedPatterns.split( "," );
}
else
{
blacklisted = EMPTY_STRING_ARRAY;
}
String[] allExcludes = new String[STANDARD_DISCOVERY_EXCLUDES.length + blacklisted.length];
System.arraycopy( STANDARD_DISCOVERY_EXCLUDES, 0, allExcludes, 0, STANDARD_DISCOVERY_EXCLUDES.length );
System.arraycopy( blacklisted, 0, allExcludes, STANDARD_DISCOVERY_EXCLUDES.length, blacklisted.length );
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir( repositoryBase );
scanner.setExcludes( allExcludes );
scanner.scan();
excludedPaths.addAll( Arrays.asList( scanner.getExcludedFiles() ) );
return scanner.getIncludedFiles();
}
/**
* Add a path to the list of files that were kicked out due to being invalid.
*
* @param path the path to add
*/
protected void addKickedOutPath( String path )
{
kickedOutPaths.add( path );
}
public Iterator getExcludedPathsIterator()
{
return excludedPaths.iterator();
}
public Iterator getKickedOutPathsIterator()
{
return kickedOutPaths.iterator();
}
}

View File

@ -0,0 +1,60 @@
package org.apache.maven.repository.discovery;
/*
* Copyright 2001-2005 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 java.io.File;
import java.util.Iterator;
import java.util.List;
/**
* Interface for implementation that can discover artifacts within a repository.
*
* @author John Casey
* @author Brett Porter
*/
public interface ArtifactDiscoverer
{
String ROLE = ArtifactDiscoverer.class.getName();
/**
* Discover artifacts in the repository.
*
* @param repositoryBase the base directory of the repository on the local filesystem
* @param blacklistedPatterns pattern that lists any files to prevent from being included when scanning
* @param includeSnapshots whether to discover snapshots
* @return the list of artifacts discovered
* @todo replace repositoryBase with wagon repository
* @todo do we want blacklisted patterns in another form? Part of the object construction?
* @todo should includeSnapshots be configuration on the component?
* @todo instead of a returned list, should a listener be passed in?
*/
List discoverArtifacts( File repositoryBase, String blacklistedPatterns, boolean includeSnapshots );
/**
* Get the list of paths kicked out during the discovery process.
*
* @return the paths as Strings.
*/
Iterator getKickedOutPathsIterator();
/**
* Get the list of paths excluded during the discovery process.
*
* @return the paths as Strings.
*/
Iterator getExcludedPathsIterator();
}

View File

@ -0,0 +1,120 @@
package org.apache.maven.repository.discovery;
/*
* Copyright 2001-2005 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.factory.ArtifactFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
/**
* Artifact discoverer for the new repository layout (Maven 2.0+).
*
* @author John Casey
* @author Brett Porter
*/
public class DefaultArtifactDiscoverer
extends AbstractArtifactDiscoverer
{
private ArtifactFactory artifactFactory;
public List discoverArtifacts( File repositoryBase, String blacklistedPatterns, boolean convertSnapshots )
{
List artifacts = new ArrayList();
String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
for ( int i = 0; i < artifactPaths.length; i++ )
{
String path = artifactPaths[i];
Artifact artifact = buildArtifact( path );
if ( artifact != null )
{
if ( convertSnapshots || !artifact.isSnapshot() )
{
artifacts.add( artifact );
}
}
}
return artifacts;
}
private Artifact buildArtifact( String path )
{
Artifact result;
List pathParts = new ArrayList();
StringTokenizer st = new StringTokenizer( path, "/\\" );
while ( st.hasMoreTokens() )
{
pathParts.add( st.nextToken() );
}
Collections.reverse( pathParts );
if ( pathParts.size() < 4 )
{
addKickedOutPath( path );
return null;
}
//discard the actual artifact filename.
pathParts.remove( 0 );
// the next one is the version.
String version = (String) pathParts.get( 0 );
pathParts.remove( 0 );
// the next one is the artifactId.
String artifactId = (String) pathParts.get( 0 );
pathParts.remove( 0 );
// the remaining are the groupId.
StringBuffer groupBuffer = new StringBuffer();
boolean firstPart = true;
for ( Iterator it = pathParts.iterator(); it.hasNext(); )
{
String part = (String) it.next();
groupBuffer.append( part );
if ( firstPart )
{
firstPart = false;
}
else if ( it.hasNext() )
{
groupBuffer.append( "." );
}
}
result = artifactFactory.createArtifact( groupBuffer.toString(), artifactId, version, Artifact.SCOPE_RUNTIME,
"jar" );
return result;
}
}

View File

@ -0,0 +1,324 @@
package org.apache.maven.repository.discovery;
/*
* Copyright 2001-2005 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.factory.ArtifactFactory;
import org.codehaus.plexus.util.StringUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
/**
* Artifact discoverer for the legacy repository layout (Maven 1.x).
*
* @author John Casey
* @author Brett Porter
*/
public class LegacyArtifactDiscoverer
extends AbstractArtifactDiscoverer
{
private ArtifactFactory artifactFactory;
public List discoverArtifacts( File repositoryBase, String blacklistedPatterns, boolean includeSnapshots )
{
List artifacts = new ArrayList();
String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
for ( int i = 0; i < artifactPaths.length; i++ )
{
String path = artifactPaths[i];
Artifact artifact = buildArtifact( path );
if ( artifact != null )
{
if ( includeSnapshots || !artifact.isSnapshot() )
{
artifacts.add( artifact );
}
}
}
return artifacts;
}
private Artifact buildArtifact( String path )
{
try
{
StringTokenizer tokens = new StringTokenizer( path, "/\\" );
int numberOfTokens = tokens.countTokens();
if ( numberOfTokens != 3 )
{
addKickedOutPath( path );
return null;
}
String groupId = tokens.nextToken();
String type = tokens.nextToken();
if ( type.endsWith( "s" ) )
{
type = type.substring( 0, type.length() - 1 );
}
// contains artifactId, version, classifier, and extension.
String avceGlob = tokens.nextToken();
LinkedList avceTokenList = new LinkedList();
StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" );
while ( avceTokenizer.hasMoreTokens() )
{
avceTokenList.addLast( avceTokenizer.nextToken() );
}
String lastAvceToken = (String) avceTokenList.removeLast();
if ( lastAvceToken.endsWith( ".tar.gz" ) )
{
type = "distribution-tgz";
lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() );
avceTokenList.addLast( lastAvceToken );
}
else if ( lastAvceToken.endsWith( "sources.jar" ) )
{
type = "java-source";
lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() );
avceTokenList.addLast( lastAvceToken );
}
else if ( lastAvceToken.endsWith( ".zip" ) )
{
type = "distribution-zip";
lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() );
avceTokenList.addLast( lastAvceToken );
}
else
{
int extPos = lastAvceToken.lastIndexOf( '.' );
if ( extPos > 0 )
{
String ext = lastAvceToken.substring( extPos + 1 );
if ( type.equals( ext ) )
{
lastAvceToken = lastAvceToken.substring( 0, extPos );
avceTokenList.addLast( lastAvceToken );
}
else
{
addKickedOutPath( path );
return null;
}
}
}
// TODO: this is obscene - surely a better way?
String validVersionParts = "([Dd][Ee][Vv][_.0-9]*)|" + "([Ss][Nn][Aa][Pp][Ss][Hh][Oo][Tt])|" +
"([0-9][_.0-9a-zA-Z]*)|" + "([Gg]?[_.0-9ab]*([Pp][Rr][Ee]|[Rr][Cc]|[Gg]|[Mm])[_.0-9]*)|" +
"([Aa][Ll][Pp][Hh][Aa][_.0-9]*)|" + "([Bb][Ee][Tt][Aa][_.0-9]*)|" + "([Rr][Cc][_.0-9]*)|" +
"([Tt][Ee][Ss][Tt][_.0-9]*)|" + "([Dd][Ee][Bb][Uu][Gg][_.0-9]*)|" +
"([Uu][Nn][Oo][Ff][Ff][Ii][Cc][Ii][Aa][Ll][_.0-9]*)|" + "([Cc][Uu][Rr][Rr][Ee][Nn][Tt])|" +
"([Ll][Aa][Tt][Ee][Ss][Tt])|" + "([Ff][Cc][Ss])|" + "([Rr][Ee][Ll][Ee][Aa][Ss][Ee][_.0-9]*)|" +
"([Nn][Ii][Gg][Hh][Tt][Ll][Yy])|" + "([AaBb][_.0-9]*)";
// let's discover the version, and whatever's leftover will be either
// a classifier, or part of the artifactId, depending on position.
// Since version is at the end, we have to move in from the back.
Collections.reverse( avceTokenList );
StringBuffer classifierBuffer = new StringBuffer();
StringBuffer versionBuffer = new StringBuffer();
boolean firstVersionTokenEncountered = false;
boolean firstToken = true;
int tokensIterated = 0;
for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
{
String token = (String) it.next();
boolean tokenIsVersionPart = token.matches( validVersionParts );
StringBuffer bufferToUpdate;
// NOTE: logic in code is reversed, since we're peeling off the back
// Any token after the last versionPart will be in the classifier.
// Any token UP TO first non-versionPart is part of the version.
if ( !tokenIsVersionPart )
{
if ( firstVersionTokenEncountered )
{
break;
}
else
{
bufferToUpdate = classifierBuffer;
}
}
else
{
firstVersionTokenEncountered = true;
bufferToUpdate = versionBuffer;
}
if ( firstToken )
{
firstToken = false;
}
else
{
bufferToUpdate.insert( 0, '-' );
}
bufferToUpdate.insert( 0, token );
tokensIterated++;
}
getLogger().debug( "After parsing loop, state of buffers:\no Version Buffer: \'" + versionBuffer +
"\'\no Classifier Buffer: \'" + classifierBuffer + "\'\no Number of Tokens Iterated: " +
tokensIterated );
// Now, restore the proper ordering so we can build the artifactId.
Collections.reverse( avceTokenList );
getLogger().debug(
"Before repairing bad version and/or cleaning up used tokens, avce token list is:\n" + avceTokenList );
// if we didn't find a version, then punt. Use the last token
// as the version, and set the classifier empty.
if ( versionBuffer.length() < 1 )
{
if ( avceTokenList.size() > 1 )
{
int lastIdx = avceTokenList.size() - 1;
versionBuffer.append( avceTokenList.get( lastIdx ) );
avceTokenList.remove( lastIdx );
}
else
{
getLogger().debug( "Cannot parse version from artifact path: \'" + path + "\'." );
getLogger().debug(
"artifact-version-classifier-extension remaining tokens is: \'" + avceTokenList + "\'" );
}
classifierBuffer.setLength( 0 );
}
else
{
getLogger().debug( "Removing " + tokensIterated + " tokens from avce token list." );
// if everything is kosher, then pop off all the classifier and
// version tokens, leaving the naked artifact id in the list.
avceTokenList = new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) );
}
getLogger().debug( "Now, remainder of avce token list is:\n" + avceTokenList );
StringBuffer artifactIdBuffer = new StringBuffer();
firstToken = true;
for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
{
String token = (String) it.next();
if ( firstToken )
{
firstToken = false;
}
else
{
artifactIdBuffer.append( '-' );
}
artifactIdBuffer.append( token );
}
String artifactId = artifactIdBuffer.toString();
int lastVersionCharIdx = versionBuffer.length() - 1;
if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' )
{
versionBuffer.setLength( lastVersionCharIdx );
}
String version = versionBuffer.toString();
if ( version.length() < 1 )
{
version = null;
}
getLogger().debug( "Extracted artifact information from path:\n" + "groupId: \'" + groupId + "\'\n" +
"artifactId: \'" + artifactId + "\'\n" + "type: \'" + type + "\'\n" + "version: \'" + version + "\'\n" +
"classifier: \'" + classifierBuffer + "\'" );
Artifact result = null;
if ( classifierBuffer.length() > 0 )
{
getLogger().debug( "Creating artifact with classifier." );
result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
classifierBuffer.toString() );
}
else
{
if ( StringUtils.isNotEmpty( groupId ) && StringUtils.isNotEmpty( artifactId ) &&
StringUtils.isNotEmpty( version ) && StringUtils.isNotEmpty( type ) )
{
result =
artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type );
}
}
// getLogger().debug(
// "Resulting artifact is: " + result + " and has classifier of: "
// + result.getClassifier() + "\n\n" );
return result;
}
catch ( RuntimeException e )
{
getLogger().error( "While parsing artifact path: \'" + path + "\'...", e );
throw e;
}
}
}

View File

@ -0,0 +1,27 @@
<component-set>
<components>
<component>
<role>org.apache.maven.repository.discovery.ArtifactDiscoverer</role>
<role-hint>legacy</role-hint>
<implementation>org.apache.maven.repository.discovery.LegacyArtifactDiscoverer</implementation>
<instantiation-strategy>per-lookup</instantiation-strategy>
<requirements>
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
</requirement>
</requirements>
</component>
<component>
<role>org.apache.maven.repository.discovery.ArtifactDiscoverer</role>
<role-hint>default</role-hint>
<implementation>org.apache.maven.repository.discovery.DefaultArtifactDiscoverer</implementation>
<instantiation-strategy>per-lookup</instantiation-strategy>
<requirements>
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
</requirement>
</requirements>
</component>
</components>
</component-set>