Skip to content

Commit

Permalink
#704: Remove WagonManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmoniuk committed Nov 26, 2022
1 parent badd0c4 commit e0458fe
Show file tree
Hide file tree
Showing 48 changed files with 421 additions and 407 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
Expand All @@ -35,6 +38,8 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
Expand All @@ -50,7 +55,6 @@
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
Expand All @@ -65,13 +69,9 @@
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.apache.maven.wagon.TransferFailedException;
import org.apache.maven.wagon.UnsupportedProtocolException;
import org.apache.maven.wagon.Wagon;
import org.apache.maven.wagon.authentication.AuthenticationException;
import org.apache.maven.wagon.authorization.AuthorizationException;
import org.codehaus.mojo.versions.model.IgnoreVersion;
import org.codehaus.mojo.versions.model.Rule;
Expand All @@ -84,13 +84,19 @@
import org.codehaus.mojo.versions.utils.PluginComparator;
import org.codehaus.mojo.versions.utils.RegexUtils;
import org.codehaus.mojo.versions.utils.VersionsExpressionEvaluator;
import org.codehaus.mojo.versions.utils.WagonUtils;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.eclipse.aether.spi.connector.transport.GetTask;
import org.eclipse.aether.spi.connector.transport.TransporterFactory;
import org.eclipse.aether.transfer.NoTransporterException;

import static java.util.Optional.empty;
import static java.util.Optional.of;

/**
* Helper class that provides common functionality required by both the mojos and the reports.
Expand Down Expand Up @@ -157,7 +163,6 @@ public class DefaultVersionsHelper
* @since 2.12
*/
private final Map<String, Rule> artifactBestFitRule = new HashMap<>();

/**
* Private constructor used by the builder
*/
Expand Down Expand Up @@ -260,103 +265,6 @@ private static RuleSet enrichRuleSet( Collection<String> ignoredVersions, RuleSe
return ruleSet;
}

private static RuleSet getRulesFromClasspath( String uri, Log logger )
throws MojoExecutionException
{
logger.debug( "Going to load rules from \"" + uri + "\"" );

String choppedUrl = uri.substring( CLASSPATH_PROTOCOL.length() + 3 );

URL url = DefaultVersionsHelper.class.getResource( choppedUrl );

if ( null == url )
{
String message = "Resource \"" + uri + "\" not found in classpath.";

throw new MojoExecutionException( message );
}

try
{
RuleSet rules = readRulesFromStream( url.openStream() );
logger.debug( "Loaded rules from \"" + uri + "\" successfully" );
return rules;
}
catch ( IOException e )
{
throw new MojoExecutionException( "Could not load specified rules from " + uri, e );
}
}

private static RuleSet getRulesViaWagon( String rulesUri, Log logger, String serverId, String id,
WagonManager wagonManager, Settings settings )
throws MojoExecutionException
{
RuleSet loadedRules;

int split = rulesUri.lastIndexOf( '/' );
String baseUri = rulesUri;
String fileUri = "";

if ( split != -1 )
{
baseUri = rulesUri.substring( 0, split ) + '/';
fileUri = split + 1 < rulesUri.length() ? rulesUri.substring( split + 1 ) : "";
}

try
{
Wagon wagon = WagonUtils.createWagon( serverId, baseUri, wagonManager, settings, logger );
try
{
logger.debug( "Trying to load ruleset from file \"" + fileUri + "\" in " + baseUri );
loadedRules = getRuleSet( wagon, fileUri );
}
finally
{
logger.debug( "Rule set loaded" );

if ( wagon != null )
{
try
{
wagon.disconnect();
}
catch ( ConnectionException e )
{
logger.warn( "Could not disconnect wagon!", e );
}
}
}
}
catch ( TransferFailedException e )
{
throw new MojoExecutionException( "Could not transfer rules from " + rulesUri, e );
}
catch ( AuthorizationException e )
{
throw new MojoExecutionException( "Authorization failure trying to load rules from " + rulesUri, e );
}
catch ( ResourceDoesNotExistException | IOException e )
{
throw new MojoExecutionException( "Could not load specified rules from " + rulesUri, e );
}
catch ( AuthenticationException e )
{
throw new MojoExecutionException( "Authentication failure trying to load rules from " + rulesUri, e );
}
catch ( UnsupportedProtocolException e )
{
throw new MojoExecutionException( "Unsupported protocol for " + rulesUri, e );
}
catch ( ConnectionException e )
{
throw new MojoExecutionException( "Could not establish connection to " + rulesUri, e );
}

return loadedRules;
}

static boolean isClasspathUri( String uri )
{
return ( uri != null && uri.startsWith( CLASSPATH_PROTOCOL + ":" ) );
Expand Down Expand Up @@ -868,19 +776,131 @@ public static class Builder
private ArtifactRepository localRepository;
private Collection<String> ignoredVersions;
private RuleSet ruleSet;
private WagonManager wagonManager;
private Settings settings;
private String serverId;
private String rulesUri;
private Log log;
private MavenSession mavenSession;
private MojoExecution mojoExecution;
private org.eclipse.aether.RepositorySystem aetherRepositorySystem;
private Map<String, TransporterFactory> transporterFactoryMap;

public Builder()
{
}

private static RuleSet getRulesFromClasspath( String uri, Log logger )
throws MojoExecutionException
{
logger.debug( "Going to load rules from \"" + uri + "\"" );

String choppedUrl = uri.substring( CLASSPATH_PROTOCOL.length() + 3 );

URL url = DefaultVersionsHelper.class.getResource( choppedUrl );

if ( null == url )
{
String message = "Resource \"" + uri + "\" not found in classpath.";

throw new MojoExecutionException( message );
}

try
{
RuleSet rules = readRulesFromStream( url.openStream() );
logger.debug( "Loaded rules from \"" + uri + "\" successfully" );
return rules;
}
catch ( IOException e )
{
throw new MojoExecutionException( "Could not load specified rules from " + uri, e );
}
}

private static class RulesUri
{
String baseUri;
URI fileUri;

private RulesUri( String baseUri, URI fileUri )
{
this.baseUri = baseUri;
this.fileUri = fileUri;
}

static RulesUri build( String rulesUri ) throws URISyntaxException
{
int split = rulesUri.lastIndexOf( '/' );
return split == -1
? new RulesUri( rulesUri, new URI( "" ) )
: new RulesUri( rulesUri.substring( 0, split ) + '/',
new URI( split + 1 < rulesUri.length()
? rulesUri.substring( split + 1 )
: "" ) );
}
}


private RuleSet getRulesUsingTransporter()
{
RulesUri uri;
try
{
uri = RulesUri.build( rulesUri );
}
catch ( URISyntaxException e )
{
log.warn( "Invalid rulesUri protocol: " + e.getMessage() );
return null;
}

RemoteRepository repository = new RemoteRepository.Builder( null, null, uri.baseUri )
.build();
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 )
{
int pos = url.indexOf( ":" );
return pos == -1
? empty()
: of( url.substring( 0, pos ).trim() );
}

public Builder withRepositorySystem( RepositorySystem repositorySystem )
{
this.repositorySystem = repositorySystem;
Expand Down Expand Up @@ -910,19 +930,7 @@ public Builder withRuleSet( RuleSet ruleSet )
this.ruleSet = ruleSet;
return this;
}

public Builder withWagonManager( WagonManager wagonManager )
{
this.wagonManager = wagonManager;
return this;
}

public Builder withSettings( Settings settings )
{
this.settings = settings;
return this;
}


public Builder withServerId( String serverId )
{
this.serverId = serverId;
Expand Down Expand Up @@ -959,6 +967,12 @@ public Builder withAetherRepositorySystem( org.eclipse.aether.RepositorySystem a
return this;
}

public Builder withTransporterFactoryMap( Map<String, TransporterFactory> transporterFactoryMap )
{
this.transporterFactoryMap = transporterFactoryMap;
return this;
}

/**
* Builds the constructed {@linkplain DefaultVersionsHelper} object
* @return constructed {@linkplain DefaultVersionsHelper}
Expand All @@ -984,9 +998,8 @@ public DefaultVersionsHelper build() throws MojoExecutionException
instance.ruleSet = isBlank( rulesUri )
? new RuleSet()
: isClasspathUri( rulesUri )
? getRulesFromClasspath( rulesUri, log )
: getRulesViaWagon( rulesUri, log, serverId, serverId, wagonManager,
settings );
? getRulesFromClasspath( rulesUri, log )
: getRulesUsingTransporter();
}
if ( ignoredVersions != null && !ignoredVersions.isEmpty() )
{
Expand Down
Loading

0 comments on commit e0458fe

Please sign in to comment.