mirror of https://github.com/apache/maven.git
[MNG-2985] DefaultWagonManager does not safely remove TransferListeners from the wagon
Submitted By: Abel Muiño git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@538041 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
44c006ff2a
commit
c8f8fef6a6
|
@ -69,6 +69,11 @@ public class DefaultWagonManager
|
||||||
{
|
{
|
||||||
private static final String WILDCARD = "*";
|
private static final String WILDCARD = "*";
|
||||||
|
|
||||||
|
private static final String[] CHECKSUM_IDS = { "md5", "sha1" };
|
||||||
|
|
||||||
|
/** have to match the CHECKSUM_IDS */
|
||||||
|
private static final String[] CHECKSUM_ALGORITHMS = { "MD5", "SHA-1" };
|
||||||
|
|
||||||
private PlexusContainer container;
|
private PlexusContainer container;
|
||||||
|
|
||||||
// TODO: proxies, authentication and mirrors are via settings, and should come in via an alternate method - perhaps
|
// TODO: proxies, authentication and mirrors are via settings, and should come in via an alternate method - perhaps
|
||||||
|
@ -190,20 +195,13 @@ private void putRemoteFile( ArtifactRepository repository, File source, String r
|
||||||
Map sums = new HashMap( 2 );
|
Map sums = new HashMap( 2 );
|
||||||
|
|
||||||
// TODO: configure these on the repository
|
// TODO: configure these on the repository
|
||||||
try
|
for ( int i = 0; i < CHECKSUM_IDS.length; i++ )
|
||||||
{
|
{
|
||||||
ChecksumObserver checksumObserver = new ChecksumObserver( "MD5" );
|
checksums.put( CHECKSUM_IDS[i], addChecksumObserver( wagon, CHECKSUM_ALGORITHMS[i] ) );
|
||||||
wagon.addTransferListener( checksumObserver );
|
|
||||||
checksums.put( "md5", checksumObserver );
|
|
||||||
checksumObserver = new ChecksumObserver( "SHA-1" );
|
|
||||||
wagon.addTransferListener( checksumObserver );
|
|
||||||
checksums.put( "sha1", checksumObserver );
|
|
||||||
}
|
|
||||||
catch ( NoSuchAlgorithmException e )
|
|
||||||
{
|
|
||||||
throw new TransferFailedException( "Unable to add checksum methods: " + e.getMessage(), e );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Repository artifactRepository = new Repository( repository.getId(), repository.getUrl() );
|
Repository artifactRepository = new Repository( repository.getId(), repository.getUrl() );
|
||||||
|
@ -225,8 +223,14 @@ private void putRemoteFile( ArtifactRepository repository, File source, String r
|
||||||
wagon.connect( artifactRepository, getAuthenticationInfo( repository.getId() ), getProxy( protocol ) );
|
wagon.connect( artifactRepository, getAuthenticationInfo( repository.getId() ), getProxy( protocol ) );
|
||||||
|
|
||||||
wagon.put( source, remotePath );
|
wagon.put( source, remotePath );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if ( downloadMonitor != null )
|
||||||
|
{
|
||||||
wagon.removeTransferListener( downloadMonitor );
|
wagon.removeTransferListener( downloadMonitor );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Pre-store the checksums as any future puts will overwrite them
|
// Pre-store the checksums as any future puts will overwrite them
|
||||||
for ( Iterator i = checksums.keySet().iterator(); i.hasNext(); )
|
for ( Iterator i = checksums.keySet().iterator(); i.hasNext(); )
|
||||||
|
@ -271,12 +275,37 @@ private void putRemoteFile( ArtifactRepository repository, File source, String r
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
// Remove every checksum listener
|
||||||
|
for ( int i = 0; i < CHECKSUM_IDS.length; i++ )
|
||||||
|
{
|
||||||
|
TransferListener checksumListener = (TransferListener) checksums.get( CHECKSUM_IDS[i] );
|
||||||
|
if ( checksumListener != null )
|
||||||
|
{
|
||||||
|
wagon.removeTransferListener( checksumListener );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
disconnectWagon( wagon );
|
disconnectWagon( wagon );
|
||||||
|
|
||||||
releaseWagon( protocol, wagon );
|
releaseWagon( protocol, wagon );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ChecksumObserver addChecksumObserver( Wagon wagon, String algorithm )
|
||||||
|
throws TransferFailedException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ChecksumObserver checksumObserver = new ChecksumObserver( algorithm );
|
||||||
|
wagon.addTransferListener( checksumObserver );
|
||||||
|
return checksumObserver;
|
||||||
|
}
|
||||||
|
catch ( NoSuchAlgorithmException e )
|
||||||
|
{
|
||||||
|
throw new TransferFailedException( "Unable to add checksum for unsupported algorithm " + algorithm, e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void getArtifact( Artifact artifact, List remoteRepositories )
|
public void getArtifact( Artifact artifact, List remoteRepositories )
|
||||||
throws TransferFailedException, ResourceDoesNotExistException
|
throws TransferFailedException, ResourceDoesNotExistException
|
||||||
{
|
{
|
||||||
|
@ -385,20 +414,9 @@ private void getRemoteFile( ArtifactRepository repository, File destination, Str
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: configure on repository
|
// TODO: configure on repository
|
||||||
ChecksumObserver md5ChecksumObserver;
|
int i = 0;
|
||||||
ChecksumObserver sha1ChecksumObserver;
|
ChecksumObserver md5ChecksumObserver = addChecksumObserver( wagon, CHECKSUM_ALGORITHMS[i++] );
|
||||||
try
|
ChecksumObserver sha1ChecksumObserver = addChecksumObserver( wagon, CHECKSUM_ALGORITHMS[i++] );
|
||||||
{
|
|
||||||
md5ChecksumObserver = new ChecksumObserver( "MD5" );
|
|
||||||
wagon.addTransferListener( md5ChecksumObserver );
|
|
||||||
|
|
||||||
sha1ChecksumObserver = new ChecksumObserver( "SHA-1" );
|
|
||||||
wagon.addTransferListener( sha1ChecksumObserver );
|
|
||||||
}
|
|
||||||
catch ( NoSuchAlgorithmException e )
|
|
||||||
{
|
|
||||||
throw new TransferFailedException( "Unable to add checksum methods: " + e.getMessage(), e );
|
|
||||||
}
|
|
||||||
|
|
||||||
File temp = new File( destination + ".tmp" );
|
File temp = new File( destination + ".tmp" );
|
||||||
temp.deleteOnExit();
|
temp.deleteOnExit();
|
||||||
|
@ -531,6 +549,14 @@ private void getRemoteFile( ArtifactRepository repository, File destination, Str
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
// Remove every TransferListener
|
||||||
|
wagon.removeTransferListener( md5ChecksumObserver );
|
||||||
|
wagon.removeTransferListener( sha1ChecksumObserver );
|
||||||
|
if ( downloadMonitor != null )
|
||||||
|
{
|
||||||
|
wagon.removeTransferListener( downloadMonitor );
|
||||||
|
}
|
||||||
|
|
||||||
disconnectWagon( wagon );
|
disconnectWagon( wagon );
|
||||||
|
|
||||||
releaseWagon( protocol, wagon );
|
releaseWagon( protocol, wagon );
|
||||||
|
|
|
@ -19,8 +19,19 @@
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import org.apache.maven.artifact.Artifact;
|
||||||
|
import org.apache.maven.artifact.DefaultArtifact;
|
||||||
|
import org.apache.maven.artifact.metadata.ArtifactMetadata;
|
||||||
|
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||||
|
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
|
||||||
|
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
|
||||||
|
import org.apache.maven.artifact.versioning.VersionRange;
|
||||||
import org.apache.maven.wagon.UnsupportedProtocolException;
|
import org.apache.maven.wagon.UnsupportedProtocolException;
|
||||||
import org.apache.maven.wagon.Wagon;
|
import org.apache.maven.wagon.Wagon;
|
||||||
|
import org.apache.maven.wagon.events.TransferListener;
|
||||||
|
import org.apache.maven.wagon.observers.Debug;
|
||||||
import org.apache.maven.wagon.repository.Repository;
|
import org.apache.maven.wagon.repository.Repository;
|
||||||
import org.codehaus.plexus.PlexusTestCase;
|
import org.codehaus.plexus.PlexusTestCase;
|
||||||
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
||||||
|
@ -35,6 +46,8 @@ public class DefaultWagonManagerTest
|
||||||
|
|
||||||
private WagonManager wagonManager;
|
private WagonManager wagonManager;
|
||||||
|
|
||||||
|
private TransferListener transferListener = new Debug();
|
||||||
|
|
||||||
protected void setUp()
|
protected void setUp()
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
|
@ -54,6 +67,8 @@ public void testDefaultWagonManager()
|
||||||
|
|
||||||
assertWagon( "c" );
|
assertWagon( "c" );
|
||||||
|
|
||||||
|
assertWagon( "noop" );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
assertWagon( "d" );
|
assertWagon( "d" );
|
||||||
|
@ -111,6 +126,46 @@ public void testGetWagonRepositoryNullProtocol()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check that transfer listeners are properly removed after getArtifact and putArtifact
|
||||||
|
*/
|
||||||
|
public void testWagonTransferListenerRemovedAfterGetArtifactAndPutArtifact()
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
File tmpFile = File.createTempFile( "mvn-test", ".temp" );
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
tmpFile.deleteOnExit();
|
||||||
|
Artifact artifact = new DefaultArtifact( "sample.group", "sample-art", VersionRange
|
||||||
|
.createFromVersion( "1.0" ), "scope", "type", "classifier", null );
|
||||||
|
artifact.setFile( tmpFile );
|
||||||
|
ArtifactRepository repo = new DefaultArtifactRepository( "id", "noop://url",
|
||||||
|
new ArtifactRepositoryLayoutStub() );
|
||||||
|
WagonNoOp wagon = (WagonNoOp) wagonManager.getWagon( "noop" );
|
||||||
|
|
||||||
|
/* getArtifact */
|
||||||
|
assertFalse( "Transfer listener is registered before test", wagon.getTransferEventSupport()
|
||||||
|
.hasTransferListener( transferListener ) );
|
||||||
|
wagonManager.setDownloadMonitor( transferListener );
|
||||||
|
wagonManager.getArtifact( artifact, repo );
|
||||||
|
assertFalse( "Transfer listener still registered after getArtifact", wagon.getTransferEventSupport()
|
||||||
|
.hasTransferListener( transferListener ) );
|
||||||
|
|
||||||
|
/* putArtifact */
|
||||||
|
assertFalse( "Transfer listener is registered before test", wagon.getTransferEventSupport()
|
||||||
|
.hasTransferListener( transferListener ) );
|
||||||
|
wagonManager.setDownloadMonitor( transferListener );
|
||||||
|
wagonManager.putArtifact( new File( "sample file" ), artifact, repo );
|
||||||
|
assertFalse( "Transfer listener still registered after putArtifact", wagon.getTransferEventSupport()
|
||||||
|
.hasTransferListener( transferListener ) );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
tmpFile.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void assertWagon( String protocol )
|
private void assertWagon( String protocol )
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
|
@ -147,4 +202,23 @@ private void assertWagonRepository( String protocol )
|
||||||
assertEquals( "Check configuration for wagon, protocol=" + protocol, s, wagon.getConfigurableField() );
|
assertEquals( "Check configuration for wagon, protocol=" + protocol, s, wagon.getConfigurableField() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final class ArtifactRepositoryLayoutStub
|
||||||
|
implements ArtifactRepositoryLayout
|
||||||
|
{
|
||||||
|
public String pathOfRemoteRepositoryMetadata( ArtifactMetadata metadata )
|
||||||
|
{
|
||||||
|
return "path";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
|
||||||
|
{
|
||||||
|
return "path";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String pathOf( Artifact artifact )
|
||||||
|
{
|
||||||
|
return "path";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
package org.apache.maven.artifact.manager;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you 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 org.apache.maven.wagon.AbstractWagon;
|
||||||
|
import org.apache.maven.wagon.ResourceDoesNotExistException;
|
||||||
|
import org.apache.maven.wagon.TransferFailedException;
|
||||||
|
import org.apache.maven.wagon.authorization.AuthorizationException;
|
||||||
|
import org.apache.maven.wagon.resource.Resource;
|
||||||
|
|
||||||
|
public class WagonNoOp
|
||||||
|
extends AbstractWagon
|
||||||
|
{
|
||||||
|
|
||||||
|
public void closeConnection()
|
||||||
|
{
|
||||||
|
// NO-OP
|
||||||
|
}
|
||||||
|
|
||||||
|
public void get( String resourceName, File destination )
|
||||||
|
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
|
||||||
|
{
|
||||||
|
Resource resource = new Resource( resourceName );
|
||||||
|
fireGetInitiated( resource, destination );
|
||||||
|
fireGetStarted( resource, destination );
|
||||||
|
fireGetCompleted( resource, destination );
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getIfNewer( String resourceName, File destination, long timestamp )
|
||||||
|
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
|
||||||
|
{
|
||||||
|
// NO-OP
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void openConnection()
|
||||||
|
{
|
||||||
|
// NO-OP
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put( File source, String destination )
|
||||||
|
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
|
||||||
|
{
|
||||||
|
Resource resource = new Resource( destination );
|
||||||
|
firePutInitiated( resource, source );
|
||||||
|
firePutStarted( resource, source );
|
||||||
|
firePutCompleted( resource, source );
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getSupportedProtocols()
|
||||||
|
{
|
||||||
|
return new String[] { "noop" };
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,6 +39,11 @@ under the License.
|
||||||
<role-hint>c</role-hint>
|
<role-hint>c</role-hint>
|
||||||
<implementation>org.apache.maven.artifact.manager.WagonC</implementation>
|
<implementation>org.apache.maven.artifact.manager.WagonC</implementation>
|
||||||
</component>
|
</component>
|
||||||
|
<component>
|
||||||
|
<role>org.apache.maven.wagon.Wagon</role>
|
||||||
|
<role-hint>noop</role-hint>
|
||||||
|
<implementation>org.apache.maven.artifact.manager.WagonNoOp</implementation>
|
||||||
|
</component>
|
||||||
<component>
|
<component>
|
||||||
<role>org.apache.maven.artifact.repository.authentication.AuthenticationInfoProvider</role>
|
<role>org.apache.maven.artifact.repository.authentication.AuthenticationInfoProvider</role>
|
||||||
<implementation>org.apache.maven.artifact.repository.authentication.DummyAuthenticationInfoProvider</implementation>
|
<implementation>org.apache.maven.artifact.repository.authentication.DummyAuthenticationInfoProvider</implementation>
|
||||||
|
|
Loading…
Reference in New Issue