From 4e3d791eefd8acc5f9c8c7a9ebed8965bffefbec Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Wed, 21 Oct 2009 14:17:38 +0000 Subject: [PATCH] o Revised transfer listener to provide better feedback git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@828026 13f79535-47bb-0310-9956-ffa450edef68 --- .../repository/ArtifactTransferListener.java | 4 +- .../maven/repository/MavenArtifact.java | 68 ++++++++++++++++++- .../legacy/TransferListenerAdapter.java | 31 +++++++-- .../cli/AbstractMavenTransferListener.java | 64 +++++++++++++++-- .../cli/BatchModeMavenTransferListener.java | 10 +-- .../cli/ConsoleMavenTransferListener.java | 37 ++-------- 6 files changed, 159 insertions(+), 55 deletions(-) diff --git a/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferListener.java b/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferListener.java index 45e063907e..59e373e110 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferListener.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/ArtifactTransferListener.java @@ -27,7 +27,9 @@ public interface ArtifactTransferListener void transferInitiated( ArtifactTransferEvent transferEvent ); - void transferProgress( ArtifactTransferEvent transferEvent, byte[] buffer, int length ); + void transferStarted( ArtifactTransferEvent transferEvent ); + + void transferProgress( ArtifactTransferEvent transferEvent, long transferred, byte[] buffer, int offset, int length ); void transferCompleted( ArtifactTransferEvent transferEvent ); diff --git a/maven-compat/src/main/java/org/apache/maven/repository/MavenArtifact.java b/maven-compat/src/main/java/org/apache/maven/repository/MavenArtifact.java index 0ded45b957..9756b9cee3 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/MavenArtifact.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/MavenArtifact.java @@ -22,24 +22,88 @@ package org.apache.maven.repository; public class MavenArtifact { + private String repositoryUrl; + private String name; private long contentLength; - public MavenArtifact( String name, long contentLength ) + public MavenArtifact( String repositoryUrl, String name, long contentLength ) { - this.name = name; + if ( repositoryUrl == null ) + { + this.repositoryUrl = ""; + } + else if ( !repositoryUrl.endsWith( "/" ) && repositoryUrl.length() > 0 ) + { + this.repositoryUrl = repositoryUrl + '/'; + } + else + { + this.repositoryUrl = repositoryUrl; + } + + if ( name == null ) + { + this.name = ""; + } + else if ( name.startsWith( "/" ) ) + { + this.name = name.substring( 1 ); + } + else + { + this.name = name; + } + 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() { 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; } + @Override + public String toString() + { + return getUrl(); + } + } 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 d6fb523fbe..2c2f0ad4f8 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 @@ -19,11 +19,15 @@ package org.apache.maven.repository.legacy; * under the License. */ +import java.util.IdentityHashMap; +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.wagon.events.TransferEvent; import org.apache.maven.wagon.events.TransferListener; +import org.apache.maven.wagon.repository.Repository; import org.apache.maven.wagon.resource.Resource; public class TransferListenerAdapter @@ -32,6 +36,8 @@ public class TransferListenerAdapter private ArtifactTransferListener listener; + private Map transfers; + public static TransferListener newAdapter( ArtifactTransferListener listener ) { if ( listener == null ) @@ -47,6 +53,7 @@ public class TransferListenerAdapter private TransferListenerAdapter( ArtifactTransferListener listener ) { this.listener = listener; + this.transfers = new IdentityHashMap(); } public void debug( String message ) @@ -55,6 +62,8 @@ public class TransferListenerAdapter public void transferCompleted( TransferEvent transferEvent ) { + transfers.remove( transferEvent.getResource() ); + listener.transferCompleted( wrap( transferEvent ) ); } @@ -69,11 +78,23 @@ public class TransferListenerAdapter public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length ) { - listener.transferProgress( wrap( transferEvent ), buffer, length ); + Long transferred = transfers.get( transferEvent.getResource() ); + if ( transferred == null ) + { + transferred = Long.valueOf( length ); + } + else + { + transferred = Long.valueOf( transferred.longValue() + length ); + } + transfers.put( transferEvent.getResource(), transferred ); + + listener.transferProgress( wrap( transferEvent ), transferred.longValue(), buffer, 0, length ); } public void transferStarted( TransferEvent transferEvent ) { + listener.transferStarted( wrap( transferEvent ) ); } private ArtifactTransferEvent wrap( TransferEvent event ) @@ -84,9 +105,9 @@ public class TransferListenerAdapter } else { - String wagon = event.getWagon().getRepository().getUrl(); + String wagon = event.getWagon().getClass().getName(); - MavenArtifact artifact = wrap( event.getResource() ); + MavenArtifact artifact = wrap( event.getWagon().getRepository(), event.getResource() ); ArtifactTransferEvent evt; if ( event.getException() != null ) @@ -104,7 +125,7 @@ public class TransferListenerAdapter } } - private MavenArtifact wrap( Resource resource ) + private MavenArtifact wrap( Repository repository, Resource resource ) { if ( resource == null ) { @@ -112,7 +133,7 @@ public class TransferListenerAdapter } else { - return new MavenArtifact( resource.getName(), resource.getContentLength() ); + return new MavenArtifact( repository.getUrl(), resource.getName(), resource.getContentLength() ); } } 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 93349cb577..cf0063f65b 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 @@ -25,8 +25,9 @@ import org.apache.maven.repository.ArtifactTransferListener; public abstract class AbstractMavenTransferListener implements ArtifactTransferListener { - private boolean showChecksumEvents = false; - + + private boolean showChecksumEvents; + protected boolean showEvent( ArtifactTransferEvent event ) { if ( event.getResource() == null ) @@ -54,20 +55,68 @@ public abstract class AbstractMavenTransferListener if ( !showEvent( transferEvent ) ) { return; - } + } + + doInitiated( transferEvent ); } - public void transferProgress( ArtifactTransferEvent transferEvent, byte[] buffer, int length ) + protected void doInitiated( ArtifactTransferEvent transferEvent ) { + String message = + transferEvent.getRequestType() == ArtifactTransferEvent.REQUEST_PUT ? "Uploading" : "Downloading"; + System.out.println( message + ": " + transferEvent.getResource().getUrl() ); + } + + public void transferStarted( ArtifactTransferEvent transferEvent ) + { + if ( !showEvent( transferEvent ) ) + { + return; + } + + doStarted( transferEvent ); + } + + protected void doStarted( ArtifactTransferEvent transferEvent ) + { + // to be overriden by sub classes + } + + public void transferProgress( ArtifactTransferEvent transferEvent, long transferred, byte[] buffer, int offset, + int length ) + { + if ( !showEvent( transferEvent ) ) + { + return; + } + + doProgress( transferEvent, transferred, buffer, offset, length ); + } + + protected void doProgress( ArtifactTransferEvent transferEvent, long transferred, byte[] buffer, int offset, + int length ) + { + // to be overriden by sub classes } public void transferCompleted( ArtifactTransferEvent transferEvent ) { - long contentLength = transferEvent.getResource().getContentLength(); - if ( contentLength != -1 ) + if ( !showEvent( transferEvent ) ) { - String type = ( transferEvent.getRequestType() == ArtifactTransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" ); + return; + } + + doCompleted( transferEvent ); + } + + protected void doCompleted( ArtifactTransferEvent transferEvent ) + { + long contentLength = transferEvent.getResource().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 ); } @@ -82,4 +131,5 @@ public abstract class AbstractMavenTransferListener { this.showChecksumEvents = showChecksumEvents; } + } diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/BatchModeMavenTransferListener.java b/maven-embedder/src/main/java/org/apache/maven/cli/BatchModeMavenTransferListener.java index 937f9ccb43..d746d1cada 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/BatchModeMavenTransferListener.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/BatchModeMavenTransferListener.java @@ -19,16 +19,8 @@ package org.apache.maven.cli; * under the License. */ -import org.apache.maven.repository.ArtifactTransferEvent; - public class BatchModeMavenTransferListener extends AbstractMavenTransferListener { - public void transferInitiated( ArtifactTransferEvent transferEvent ) - { - if ( !showEvent( transferEvent ) ) - { - return; - } - } + } 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 b2a1306041..ba6192a278 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 @@ -29,48 +29,23 @@ import org.apache.maven.repository.ArtifactTransferEvent; public class ConsoleMavenTransferListener extends AbstractMavenTransferListener { - private long complete; - public void transferInitiated( ArtifactTransferEvent transferEvent ) - { - super.transferInitiated( transferEvent ); - - complete = 0; - - if ( !showEvent( transferEvent ) ) - { - return; - } - - String message = - transferEvent.getRequestType() == ArtifactTransferEvent.REQUEST_PUT ? "Uploading" : "Downloading"; - - String url = transferEvent.getSource().toString(); - - System.out.println( message + ": " + url + "/" + transferEvent.getResource().getName() ); - } - - public void transferProgress( ArtifactTransferEvent transferEvent, byte[] buffer, int length ) + @Override + protected void doProgress( ArtifactTransferEvent transferEvent, long transferred, byte[] buffer, int offset, + int length ) { long total = transferEvent.getResource().getContentLength(); - complete += length; - - if ( !showEvent( transferEvent ) ) - { - return; - } + long complete = 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 ) + "K" ) + "\r" ); } else { System.out.print( complete + "/" + ( total == -1 ? "?" : total + "b" ) + "\r" ); } } -} +}