Skip to content

Commit

Permalink
mojohaus#704: Remove WagonManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmoniuk committed Dec 4, 2022
1 parent e625c4c commit 189d8c7
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -66,8 +68,11 @@
import org.codehaus.mojo.versions.model.io.xpp3.RuleXpp3Reader;
import org.codehaus.mojo.versions.ordering.VersionComparator;
import org.codehaus.mojo.versions.ordering.VersionComparators;
import org.codehaus.mojo.versions.utils.ClassPathTransport;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.mojo.versions.utils.FileTransport;
import org.codehaus.mojo.versions.utils.HttpTransport;
import org.codehaus.mojo.versions.utils.PluginComparator;
import org.codehaus.mojo.versions.utils.RegexUtils;
import org.codehaus.mojo.versions.utils.VersionsExpressionEvaluator;
Expand All @@ -81,6 +86,7 @@
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResolutionException;

import static java.util.Arrays.asList;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.apache.maven.RepositoryUtils.toArtifact;
Expand All @@ -92,7 +98,7 @@
* @since 1.0-alpha-3
*/
public class DefaultVersionsHelper
implements VersionsHelper
implements VersionsHelper
{
private static final String TYPE_EXACT = "exact";

Expand Down Expand Up @@ -180,11 +186,11 @@ public ArtifactVersions lookupArtifactVersions( Artifact artifact, boolean usePl
return new ArtifactVersions( artifact,
aetherRepositorySystem.resolveVersionRange( mavenSession.getRepositorySession(),
new VersionRangeRequest(
toArtifact( artifact ).setVersion( "(,)" ),
usePluginRepositories
? mavenSession.getCurrentProject().getRemotePluginRepositories()
: mavenSession.getCurrentProject().getRemoteProjectRepositories(),
"lookupArtifactVersions" ) )
toArtifact( artifact ).setVersion( "(,)" ),
usePluginRepositories
? mavenSession.getCurrentProject().getRemotePluginRepositories()
: mavenSession.getCurrentProject().getRemoteProjectRepositories(),
"lookupArtifactVersions" ) )
.getVersions()
.parallelStream()
.filter( v -> ignoredVersions.stream()
Expand Down Expand Up @@ -243,9 +249,9 @@ private List<IgnoreVersion> getIgnoredVersions( Artifact artifact )
if ( !TYPE_EXACT.equals( ignoreVersion.getType() ) && !TYPE_REGEX.equals( ignoreVersion.getType() ) )
{
getLog().warn( "The type attribute '" + ignoreVersion.getType() + "' for global ignoreVersion["
+ ignoreVersion + "] is not valid." + " Please use either '" + TYPE_EXACT + "' or '"
+ TYPE_REGEX
+ "'." );
+ ignoreVersion + "] is not valid." + " Please use either '" + TYPE_EXACT + "' or '"
+ TYPE_REGEX
+ "'." );
}
else
{
Expand All @@ -262,7 +268,7 @@ private List<IgnoreVersion> getIgnoredVersions( Artifact artifact )
if ( !TYPE_EXACT.equals( ignoreVersion.getType() ) && !TYPE_REGEX.equals( ignoreVersion.getType() ) )
{
getLog().warn( "The type attribute '" + ignoreVersion.getType() + "' for " + rule + " is not valid."
+ " Please use either '" + TYPE_EXACT + "' or '" + TYPE_REGEX + "'." );
+ " Please use either '" + TYPE_EXACT + "' or '" + TYPE_REGEX + "'." );
}
else
{
Expand All @@ -276,7 +282,7 @@ private List<IgnoreVersion> getIgnoredVersions( Artifact artifact )

@Override
public void resolveArtifact( Artifact artifact, boolean usePluginRepositories )
throws ArtifactResolutionException
throws ArtifactResolutionException
{
try
{
Expand Down Expand Up @@ -445,7 +451,7 @@ public ArtifactVersion createArtifactVersion( String version )
* Returns a map of all possible updates per dependency. The lookup is done in parallel using
* {@code LOOKUP_PARALLEL_THREADS} threads.
*
* @param dependencies The set of {@link Dependency} instances to look up.
* @param dependencies The set of {@link Dependency} instances to look up.
* @param usePluginRepositories Search the plugin repositories.
* @return map containing the ArtifactVersions object per dependency
*/
Expand Down Expand Up @@ -536,7 +542,7 @@ public PluginUpdatesDetails lookupPluginUpdates( Plugin plugin, boolean allowSna
pluginDependencies.addAll( plugin.getDependencies() );
}
Map<Dependency, ArtifactVersions> pluginDependencyDetails =
lookupDependenciesUpdates( pluginDependencies, false );
lookupDependenciesUpdates( pluginDependencies, false );

ArtifactVersions allVersions =
lookupArtifactVersions( createPluginArtifact( plugin.getGroupId(), plugin.getArtifactId(), version ),
Expand All @@ -555,7 +561,7 @@ public ExpressionEvaluator getExpressionEvaluator( MavenProject project )

@Override
public Map<Property, PropertyVersions> getVersionPropertiesMap( VersionPropertiesMapRequest request )
throws MojoExecutionException
throws MojoExecutionException
{
Map<String, Property> properties = new HashMap<>();
if ( request.getPropertyDefinitions() != null )
Expand Down Expand Up @@ -584,18 +590,18 @@ public Map<Property, PropertyVersions> getVersionPropertiesMap( VersionPropertie
{
final Property value = new Property( name );
getLog().debug( "Property ${" + name + "}: Adding inferred version range of "
+ propertyVersionsBuilder.getVersionRange() );
+ propertyVersionsBuilder.getVersionRange() );
value.setVersion( propertyVersionsBuilder.getVersionRange() );
properties.put( name, value );
}
}
}

List<String> includePropertiesList = request.getIncludeProperties() != null
? Arrays.asList( request.getIncludeProperties().split( "\\s*,\\s*" ) )
: Collections.emptyList();
? asList( request.getIncludeProperties().split( "\\s*,\\s*" ) )
: Collections.emptyList();
List<String> excludePropertiesList = request.getExcludeProperties() != null
? Arrays.asList( request.getExcludeProperties().split( "\\s*,\\s*" ) )
? asList( request.getExcludeProperties().split( "\\s*,\\s*" ) )
: Collections.emptyList();

getLog().debug( "Searching for properties associated with builders" );
Expand Down Expand Up @@ -627,7 +633,7 @@ else if ( !excludePropertiesList.isEmpty() && excludePropertiesList.contains( pr
if ( builder == null || !builder.isAssociated() )
{
getLog().debug( "Property ${" + property.getName() + "}: Looks like this property is not "
+ "associated with any dependency..." );
+ "associated with any dependency..." );
builder = new PropertyVersionsBuilder( null, property.getName(), this );
}
if ( !property.isAutoLinkDependencies() )
Expand All @@ -648,10 +654,10 @@ else if ( !excludePropertiesList.isEmpty() && excludePropertiesList.contains( pr
{
final PropertyVersions versions = builder.newPropertyVersions();
if ( property.isAutoLinkDependencies() && StringUtils.isEmpty( property.getVersion() )
&& !StringUtils.isEmpty( builder.getVersionRange() ) )
&& !StringUtils.isEmpty( builder.getVersionRange() ) )
{
getLog().debug( "Property ${" + property.getName() + "}: Adding inferred version range of "
+ builder.getVersionRange() );
+ builder.getVersionRange() );
property.setVersion( builder.getVersionRange() );
}
versions.setCurrentVersion( request.getMavenProject().getProperties()
Expand All @@ -671,6 +677,8 @@ else if ( !excludePropertiesList.isEmpty() && excludePropertiesList.contains( pr
*/
public static class Builder
{
private static final List<Transport> TRANSPORTS = asList( new FileTransport(), new ClassPathTransport(),
new HttpTransport() );
private RepositorySystem repositorySystem;
private Collection<String> ignoredVersions;
private RuleSet ruleSet;
Expand All @@ -686,7 +694,7 @@ public Builder()
}

private static RuleSet getRulesFromClasspath( String uri, Log logger )
throws MojoExecutionException
throws MojoExecutionException
{
logger.debug( "Going to load rules from \"" + uri + "\"" );
String choppedUrl = uri.substring( "classpath".length() + 3 );
Expand Down Expand Up @@ -720,7 +728,7 @@ private static RuleSet getRulesFromClasspath( String uri, Log logger )
* @param ignoredVersions collection of ignored version to enrich the clone of the original rule set
* @param originalRuleSet original rule set
* @return new RuleSet object containing the (if passed) cloned version of the rule set, enriched with
* the given set of ignored versions
* the given set of ignored versions
*/
@SuppressWarnings( "checkstyle:AvoidNestedBlocks" )
private static RuleSet enrichRuleSet( Collection<String> ignoredVersions, RuleSet originalRuleSet )
Expand Down Expand Up @@ -754,7 +762,7 @@ private static RuleSet enrichRuleSet( Collection<String> ignoredVersions, RuleSe
return ruleSet;
}

private RuleSet getRulesUsingTransporter()
private RuleSet getRulesUsingTransporter() throws MojoExecutionException
{
URI uri;
try
Expand All @@ -767,45 +775,69 @@ private RuleSet getRulesUsingTransporter()
return null;
}

RemoteRepository repository = new RemoteRepository.Builder( serverId, uri.getScheme(), uri.toString() )
.build();
return null;
// return transporterFactoryMap
// .values()
// .stream()
// // highest priority first -> reversing the order of arguments:
// .sorted( ( f1, f2 ) -> Float.compare( f2.getPriority(), f1.getPriority() ) )
// .map( factory ->
// {
// try
// {
// return factory.newInstance( mavenSession.getRepositorySession(), repository );
// }
// catch ( NoTransporterException e )
// {
// log.warn( "No transporter possible for " + uri.baseUri + ": "
// + e.getMessage() );
// return null;
// }
// } )
// .filter( Objects::nonNull )
// .map( transporter ->
// {
// try
// {
// GetTask getTask = new GetTask( uri.fileUri );
// transporter.get( getTask );
// return new RuleXpp3Reader().read( new StringReader( getTask.getDataString() ) );
// }
// catch ( Exception e )
// {
// log.warn( "Error while reading the rules string: " + e.getMessage() );
// return null;
// }
// } )
// .filter( Objects::nonNull )
// .findFirst()
// .orElse( null );
try
{
return TRANSPORTS.stream()
.filter( transport -> transport.isApplicable( uri ) )
.findFirst()
.map( transport ->
{
try
{
try ( InputStream is = transport.download( uri, serverId, mavenSession ) )
{
return new RuleXpp3Reader().read( is );
}
}
catch ( IOException | XmlPullParserException e )
{
throw new IllegalStateException( e );
}
} )
.orElseThrow( () -> new MojoExecutionException( "No transport found for URI " + rulesUri ) );
}
catch ( IllegalStateException e )
{
throw new MojoExecutionException( "Error while downloading the rules file from " + rulesUri,
e.getCause() );
}

// return transporterFactoryMap
// .values()
// .stream()
// // highest priority first -> reversing the order of arguments:
// .sorted( ( f1, f2 ) -> Float.compare( f2.getPriority(), f1.getPriority() ) )
// .map( factory ->
// {
// try
// {
// return factory.newInstance( mavenSession.getRepositorySession(), repository );
// }
// catch ( NoTransporterException e )
// {
// log.warn( "No transporter possible for " + uri.baseUri + ": "
// + e.getMessage() );
// return null;
// }
// } )
// .filter( Objects::nonNull )
// .map( transporter ->
// {
// try
// {
// GetTask getTask = new GetTask( uri.fileUri );
// transporter.get( getTask );
// return new RuleXpp3Reader().read( new StringReader( getTask.getDataString() ) );
// }
// catch ( Exception e )
// {
// log.warn( "Error while reading the rules string: " + e.getMessage() );
// return null;
// }
// } )
// .filter( Objects::nonNull )
// .findFirst()
// .orElse( null );
}

public static Optional<String> protocol( final String url )
Expand Down Expand Up @@ -872,6 +904,7 @@ public Builder withAetherRepositorySystem( org.eclipse.aether.RepositorySystem a

/**
* Builds the constructed {@linkplain DefaultVersionsHelper} object
*
* @return constructed {@linkplain DefaultVersionsHelper}
* @throws MojoExecutionException should the constructor with the RuleSet retrieval doesn't succeed
*/
Expand All @@ -893,9 +926,7 @@ public DefaultVersionsHelper build() throws MojoExecutionException
{
instance.ruleSet = isBlank( rulesUri )
? new RuleSet()
: isClasspathUri( rulesUri )
? getRulesFromClasspath( rulesUri, log )
: getRulesUsingTransporter();
: getRulesUsingTransporter();
}
if ( ignoredVersions != null && !ignoredVersions.isEmpty() )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
*/
public interface Transport
{
/**
* Returns {@code true} if the instance is applicable for the given URI
* @param uri URI to download
* @return {@code true} if the instance can be used to download the URI
*/
boolean isApplicable( URI uri );

/**
* Retrieves the resource indicated by the given uri.
* @param uri uri pointing to the resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
*/
public class ClassPathTransport implements Transport
{
@Override
public boolean isApplicable( URI uri )
{
return "classpath".equals( uri.getScheme() );
}

/**
* Retrieves the resource indicated by the given uri.
* @param uri uri pointing to the resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@

public class FileTransport implements Transport
{
@Override
public boolean isApplicable( URI uri )
{
return "file".equals( uri.getScheme() );
}

/**
* Retrieves the resource indicated by the given uri.
* @param uri uri pointing to the resource
Expand All @@ -42,10 +48,6 @@ public class FileTransport implements Transport
@Override
public InputStream download( URI uri, String serverId, MavenSession mavenSession ) throws IOException
{
if ( "file".equals( uri.getScheme() ) )
{
return Files.newInputStream( Paths.get( uri ), StandardOpenOption.READ );
}
return null;
return Files.newInputStream( Paths.get( uri ), StandardOpenOption.READ );
}
}
Loading

0 comments on commit 189d8c7

Please sign in to comment.