o Revised transfer listener to retain resource identity

o Allowed to monitor transfer duration

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@828109 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-10-21 17:07:39 +00:00
parent 658b110eee
commit bb889e5895
6 changed files with 144 additions and 59 deletions

View File

@ -76,9 +76,10 @@ public class ArtifactTransferEvent
private File localFile;
private MavenArtifact artifact;
private ArtifactTransferResource artifact;
public ArtifactTransferEvent( String wagon, final int eventType, final int requestType, MavenArtifact artifact )
public ArtifactTransferEvent( String wagon, final int eventType, final int requestType,
ArtifactTransferResource artifact )
{
super( wagon );
@ -89,14 +90,15 @@ public class ArtifactTransferEvent
this.artifact = artifact;
}
public ArtifactTransferEvent( String wagon, final Exception exception, final int requestType, MavenArtifact artifact )
public ArtifactTransferEvent( String wagon, final Exception exception, final int requestType,
ArtifactTransferResource artifact )
{
this( wagon, TRANSFER_ERROR, requestType, artifact );
this.exception = exception;
}
public MavenArtifact getResource()
public ArtifactTransferResource getResource()
{
return artifact;
}

View File

@ -0,0 +1,66 @@
package org.apache.maven.repository;
/*
* 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.
*/
/**
* Describes a resource being uploaded or downloaded by the repository system.
*
* @author Benjamin Bentmann
*/
public interface ArtifactTransferResource
{
/**
* The base URL of the repository, e.g. "http://repo1.maven.org/maven2/". Unless the URL is unknown, it will be
* terminated by a trailing slash.
*
* @return The base URL of the repository or an empty string if unknown, never {@code null}.
*/
String getRepositoryUrl();
/**
* The path of the artifact relative to the repository's base URL.
*
* @return The path of the artifact, never {@code null}.
*/
String getName();
/**
* Gets the full URL of the artifact.
*
* @return The full URL of the artifact, never {@code null}.
*/
String getUrl();
/**
* The size of the artifact in bytes.
*
* @return The of the artifact in bytes or a negative value if unknown.
*/
long getContentLength();
/**
* Gets the timestamp when the transfer of this artifact was started.
*
* @return The timestamp when the transfer of this artifact was started.
*/
long getTransferStartTime();
}

View File

@ -1,4 +1,4 @@
package org.apache.maven.repository;
package org.apache.maven.repository.legacy;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@ -19,16 +19,20 @@ package org.apache.maven.repository;
* under the License.
*/
public class MavenArtifact
import org.apache.maven.repository.ArtifactTransferResource;
import org.apache.maven.wagon.resource.Resource;
class MavenArtifact
implements ArtifactTransferResource
{
private String repositoryUrl;
private String name;
private Resource resource;
private long contentLength;
private long transferStartTime;
public MavenArtifact( String repositoryUrl, String name, long contentLength )
public MavenArtifact( String repositoryUrl, Resource resource )
{
if ( repositoryUrl == null )
{
@ -42,62 +46,45 @@ public class MavenArtifact
{
this.repositoryUrl = repositoryUrl;
}
this.resource = resource;
if ( name == null )
{
this.name = "";
}
else if ( name.startsWith( "/" ) )
{
this.name = name.substring( 1 );
}
else
{
this.name = name;
this.transferStartTime = System.currentTimeMillis();
}
this.contentLength = contentLength;
}
/**
* The base URL of the repository, e.g. "http://repo1.maven.org/maven2/". Unless the URL is unknown, it will be
* terminated by a trailing slash.
*
* @return The base URL of the repository or an empty string if unknown, never {@code null}.
*/
public String getRepositoryUrl()
{
return repositoryUrl;
}
/**
* The path of the artifact relative to the repository's base URL.
*
* @return The path of the artifact, never {@code null}.
*/
public String getName()
{
String name = resource.getName();
if ( name == null )
{
name = "";
}
else if ( name.startsWith( "/" ) )
{
name = name.substring( 1 );
}
return name;
}
/**
* Gets the full URL of the artifact.
*
* @return The full URL of the artifact, never {@code null}.
*/
public String getUrl()
{
return getRepositoryUrl() + getName();
}
/**
* The size of the artifact in bytes.
*
* @return The of the artifact in bytes or a negative value if unknown.
*/
public long getContentLength()
{
return contentLength;
return resource.getContentLength();
}
public long getTransferStartTime()
{
return transferStartTime;
}
@Override

View File

@ -24,7 +24,7 @@ import java.util.Map;
import org.apache.maven.repository.ArtifactTransferEvent;
import org.apache.maven.repository.ArtifactTransferListener;
import org.apache.maven.repository.MavenArtifact;
import org.apache.maven.repository.ArtifactTransferResource;
import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.events.TransferListener;
import org.apache.maven.wagon.repository.Repository;
@ -36,6 +36,8 @@ public class TransferListenerAdapter
private ArtifactTransferListener listener;
private Map<Resource, ArtifactTransferResource> artifacts;
private Map<Resource, Long> transfers;
public static TransferListener newAdapter( ArtifactTransferListener listener )
@ -53,6 +55,7 @@ public class TransferListenerAdapter
private TransferListenerAdapter( ArtifactTransferListener listener )
{
this.listener = listener;
this.artifacts = new IdentityHashMap<Resource, ArtifactTransferResource>();
this.transfers = new IdentityHashMap<Resource, Long>();
}
@ -62,13 +65,16 @@ public class TransferListenerAdapter
public void transferCompleted( TransferEvent transferEvent )
{
transfers.remove( transferEvent.getResource() );
listener.transferCompleted( wrap( transferEvent ) );
artifacts.remove( transferEvent.getResource() );
transfers.remove( transferEvent.getResource() );
}
public void transferError( TransferEvent transferEvent )
{
artifacts.remove( transferEvent.getResource() );
transfers.remove( transferEvent.getResource() );
}
public void transferInitiated( TransferEvent transferEvent )
@ -107,7 +113,7 @@ public class TransferListenerAdapter
{
String wagon = event.getWagon().getClass().getName();
MavenArtifact artifact = wrap( event.getWagon().getRepository(), event.getResource() );
ArtifactTransferResource artifact = wrap( event.getWagon().getRepository(), event.getResource() );
ArtifactTransferEvent evt;
if ( event.getException() != null )
@ -125,7 +131,7 @@ public class TransferListenerAdapter
}
}
private MavenArtifact wrap( Repository repository, Resource resource )
private ArtifactTransferResource wrap( Repository repository, Resource resource )
{
if ( resource == null )
{
@ -133,7 +139,15 @@ public class TransferListenerAdapter
}
else
{
return new MavenArtifact( repository.getUrl(), resource.getName(), resource.getContentLength() );
ArtifactTransferResource artifact = artifacts.get( resource );
if ( artifact == null )
{
artifact = new MavenArtifact( repository.getUrl(), resource );
artifacts.put( resource, artifact );
}
return artifact;
}
}

View File

@ -19,8 +19,13 @@ package org.apache.maven.cli;
* under the License.
*/
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import org.apache.maven.repository.ArtifactTransferEvent;
import org.apache.maven.repository.ArtifactTransferListener;
import org.apache.maven.repository.ArtifactTransferResource;
public abstract class AbstractMavenTransferListener
implements ArtifactTransferListener
@ -112,13 +117,24 @@ public abstract class AbstractMavenTransferListener
protected void doCompleted( ArtifactTransferEvent transferEvent )
{
long contentLength = transferEvent.getResource().getContentLength();
ArtifactTransferResource artifact = transferEvent.getResource();
long contentLength = artifact.getContentLength();
if ( contentLength >= 0 )
{
String type =
( transferEvent.getRequestType() == ArtifactTransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" );
String l = contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b";
System.out.println( l + " " + type );
String l = contentLength >= 1024 ? ( ( contentLength + 1023 ) / 1024 ) + " KB" : contentLength + " B";
String throughput = "";
long duration = System.currentTimeMillis() - artifact.getTransferStartTime();
if ( duration > 0 )
{
DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) );
double kbPerSec = ( contentLength / 1024.0 ) / ( duration / 1000.0 );
throughput = " at " + format.format( kbPerSec ) + " KB/sec";
}
System.out.println( l + " " + type + throughput );
}
}

View File

@ -40,11 +40,11 @@ public class ConsoleMavenTransferListener
// TODO [BP]: Sys.out may no longer be appropriate, but will \r work with getLogger()?
if ( total >= 1024 )
{
System.out.print( ( complete / 1024 ) + "/" + ( total == -1 ? "?" : ( total / 1024 ) + "K" ) + "\r" );
System.out.print( ( complete / 1024 ) + "/" + ( total == -1 ? "?" : ( total / 1024 ) + " KB" ) + "\r" );
}
else
{
System.out.print( complete + "/" + ( total == -1 ? "?" : total + "b" ) + "\r" );
System.out.print( complete + "/" + ( total == -1 ? "?" : total + " B" ) + "\r" );
}
}