diff --git a/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferEvent.java b/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferEvent.java index 6b5b6080eb..a53e13f1f5 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferEvent.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferEvent.java @@ -76,12 +76,13 @@ public class ArtifactTransferEvent private File localFile; - private MavenArtifact artifact; - - public ArtifactTransferEvent( String wagon, final int eventType, final int requestType, MavenArtifact artifact ) + private ArtifactTransferResource artifact; + + public ArtifactTransferEvent( String wagon, final int eventType, final int requestType, + ArtifactTransferResource artifact ) { super( wagon ); - + setEventType( eventType ); setRequestType( requestType ); @@ -89,18 +90,19 @@ public ArtifactTransferEvent( String wagon, final int eventType, final int reque 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; } - + /** * @return Returns the exception. */ diff --git a/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferResource.java b/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferResource.java new file mode 100644 index 0000000000..dd91e7a1ca --- /dev/null +++ b/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferResource.java @@ -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(); + +} diff --git a/maven-compat/src/main/java/org/apache/maven/repository/MavenArtifact.java b/maven-compat/src/main/java/org/apache/maven/repository/legacy/MavenArtifact.java similarity index 59% rename from maven-compat/src/main/java/org/apache/maven/repository/MavenArtifact.java rename to maven-compat/src/main/java/org/apache/maven/repository/legacy/MavenArtifact.java index 9756b9cee3..29b9b82d11 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/MavenArtifact.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/legacy/MavenArtifact.java @@ -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 @@ * 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 @@ else if ( !repositoryUrl.endsWith( "/" ) && repositoryUrl.length() > 0 ) { 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.contentLength = contentLength; + this.transferStartTime = System.currentTimeMillis(); } - /** - * 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 diff --git a/maven-compat/src/main/java/org/apache/maven/repository/legacy/TransferListenerAdapter.java b/maven-compat/src/main/java/org/apache/maven/repository/legacy/TransferListenerAdapter.java index 2c2f0ad4f8..8bd1d64c02 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/legacy/TransferListenerAdapter.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/legacy/TransferListenerAdapter.java @@ -24,7 +24,7 @@ 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 artifacts; + private Map transfers; public static TransferListener newAdapter( ArtifactTransferListener listener ) @@ -53,6 +55,7 @@ public static TransferListener newAdapter( ArtifactTransferListener listener ) private TransferListenerAdapter( ArtifactTransferListener listener ) { this.listener = listener; + this.artifacts = new IdentityHashMap(); this.transfers = new IdentityHashMap(); } @@ -62,13 +65,16 @@ public void debug( String message ) 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 @@ private ArtifactTransferEvent wrap( TransferEvent event ) { 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 @@ private ArtifactTransferEvent wrap( TransferEvent event ) } } - private MavenArtifact wrap( Repository repository, Resource resource ) + private ArtifactTransferResource wrap( Repository repository, Resource resource ) { if ( resource == null ) { @@ -133,7 +139,15 @@ private MavenArtifact wrap( Repository repository, Resource resource ) } 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; } } diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/AbstractMavenTransferListener.java b/maven-embedder/src/main/java/org/apache/maven/cli/AbstractMavenTransferListener.java index cf0063f65b..d7200b36fa 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/AbstractMavenTransferListener.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/AbstractMavenTransferListener.java @@ -19,8 +19,13 @@ * 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 void transferCompleted( ArtifactTransferEvent transferEvent ) 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 ); } } diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/ConsoleMavenTransferListener.java b/maven-embedder/src/main/java/org/apache/maven/cli/ConsoleMavenTransferListener.java index ba6192a278..c6a289520b 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/ConsoleMavenTransferListener.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/ConsoleMavenTransferListener.java @@ -40,11 +40,11 @@ protected void doProgress( ArtifactTransferEvent transferEvent, long transferred // 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" ); } }