From d69861820462742d0a88b777dfd68d40e39fbdc6 Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Sun, 11 Nov 2012 04:40:00 +0000 Subject: [PATCH] MNG-5374: Fix transfer listener after the JSR330 merge For the following cases I have done the following: 1) Logging to the console: I simply restored the class that was there such that when logging to the console it uses System.out as it did and the d ownload progress appears as it did. 2) Logging in batch mode: the batch mode transfer listener uses an SLF4J logger and the batch mode transfer listener doesn't report download progr ess so there is no issue. Download progress would just create a bunch of noise. The size and the speed at which it is downloaded are logged. 3) Logging to a file: same as 2) except it's all diverted to the specified file. 4) I created two protected methods in MavenCli so that integrators can supply their own console and batch transfer listeners if they wish. git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@1407913 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/maven/cli/MavenCli.java | 48 ++++++---- .../AbstractMavenTransferListener.java | 14 ++- .../BatchModeMavenTransferListener.java | 4 +- .../ConsoleMavenTransferListener.java | 8 +- .../transfer/Slf4jMavenTransferListener.java | 89 +++++++++++++++++++ 5 files changed, 131 insertions(+), 32 deletions(-) create mode 100644 maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 3990785507..175cfdab3a 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -21,6 +21,7 @@ package org.apache.maven.cli; import java.io.File; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Arrays; @@ -43,6 +44,7 @@ import org.apache.maven.cli.logging.Slf4jLoggerManager; import org.apache.maven.cli.transfer.BatchModeMavenTransferListener; import org.apache.maven.cli.transfer.ConsoleMavenTransferListener; import org.apache.maven.cli.transfer.QuietMavenTransferListener; +import org.apache.maven.cli.transfer.Slf4jMavenTransferListener; import org.apache.maven.eventspy.internal.EventSpyDispatcher; import org.apache.maven.exception.DefaultExceptionHandler; import org.apache.maven.exception.ExceptionHandler; @@ -320,6 +322,15 @@ public class MavenCli File logFile = new File( cliRequest.commandLine.getOptionValue( CLIManager.LOG_FILE ) ); logFile = resolveFile( logFile, cliRequest.workingDirectory ); System.setProperty("org.slf4j.simpleLogger.logFile", logFile.getAbsolutePath()); + try { + PrintStream ps = new PrintStream(new FileOutputStream(logFile)); + System.setOut(ps); + System.setErr(ps); + } catch (FileNotFoundException e) { + // + // Ignore + // + } } plexusLoggerManager = new Slf4jLoggerManager(); @@ -872,14 +883,18 @@ public class MavenCli if ( quiet ) { transferListener = new QuietMavenTransferListener(); - } - else if ( request.isInteractiveMode() ) + } + else if ( request.isInteractiveMode() && !cliRequest.commandLine.hasOption( CLIManager.LOG_FILE )) { - transferListener = new ConsoleMavenTransferListener( slf4jLogger ); + // + // If we're logging to a file then we don't want the console transfer listener as it will spew + // download progress all over the place + // + transferListener = getConsoleTransferListener(); } else { - transferListener = new BatchModeMavenTransferListener( slf4jLogger ); + transferListener = getBatchTransferListener(); } ExecutionListener executionListener = new ExecutionEventLogger( slf4jLogger ); @@ -1136,26 +1151,21 @@ public class MavenCli } } - - private PrintStreamLogger setupLogger( int loggingLevel ) - { - PrintStreamLogger logger = new PrintStreamLogger( new PrintStreamLogger.Provider() - { - public PrintStream getStream() - { - return System.out; - } - } ); - - logger.setThreshold( loggingLevel ); - - return logger; - } // // Customizations available via the CLI // + protected TransferListener getConsoleTransferListener() + { + return new ConsoleMavenTransferListener( System.out ); + } + + protected TransferListener getBatchTransferListener() + { + return new Slf4jMavenTransferListener( slf4jLogger ); + } + protected void customizeContainer( PlexusContainer container ) { } diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java index 438d70270a..d63cdfc070 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java @@ -19,11 +19,11 @@ package org.apache.maven.cli.transfer; * under the License. */ +import java.io.PrintStream; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; -import org.slf4j.Logger; import org.sonatype.aether.transfer.AbstractTransferListener; import org.sonatype.aether.transfer.TransferCancelledException; import org.sonatype.aether.transfer.TransferEvent; @@ -33,13 +33,11 @@ public abstract class AbstractMavenTransferListener extends AbstractTransferListener { - protected Logger out; - //protected PrintStream out; + protected PrintStream out; - protected AbstractMavenTransferListener( Logger out ) + protected AbstractMavenTransferListener( PrintStream out ) { this.out = out; - //this.out = ( out != null ) ? out : System.out; } @Override @@ -47,7 +45,7 @@ public abstract class AbstractMavenTransferListener { String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading"; - out.info( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName() ); + out.println( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName() ); } @Override @@ -56,7 +54,7 @@ public abstract class AbstractMavenTransferListener { TransferResource resource = event.getResource(); - out.warn( "[WARNING] " + event.getException().getMessage() + " for " + resource.getRepositoryUrl() + out.println( "[WARNING] " + event.getException().getMessage() + " for " + resource.getRepositoryUrl() + resource.getResourceName() ); } @@ -79,7 +77,7 @@ public abstract class AbstractMavenTransferListener throughput = " at " + format.format( kbPerSec ) + " KB/sec"; } - out.info( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len + out.println( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len + throughput + ")" ); } } diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/BatchModeMavenTransferListener.java b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/BatchModeMavenTransferListener.java index 3f026f81f0..ddecc62736 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/BatchModeMavenTransferListener.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/BatchModeMavenTransferListener.java @@ -1,5 +1,7 @@ package org.apache.maven.cli.transfer; +import java.io.PrintStream; + import org.slf4j.Logger; /* @@ -24,7 +26,7 @@ import org.slf4j.Logger; public class BatchModeMavenTransferListener extends AbstractMavenTransferListener { - public BatchModeMavenTransferListener( Logger out ) + public BatchModeMavenTransferListener( PrintStream out ) { super( out ); } diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java index 5aa39991be..2263c937a6 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java @@ -19,10 +19,10 @@ package org.apache.maven.cli.transfer; * under the License. */ +import java.io.PrintStream; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import org.slf4j.Logger; import org.sonatype.aether.transfer.TransferCancelledException; import org.sonatype.aether.transfer.TransferEvent; import org.sonatype.aether.transfer.TransferResource; @@ -40,7 +40,7 @@ public class ConsoleMavenTransferListener private int lastLength; - public ConsoleMavenTransferListener( Logger out ) + public ConsoleMavenTransferListener( PrintStream out ) { super( out ); } @@ -70,7 +70,7 @@ public class ConsoleMavenTransferListener pad( buffer, pad ); buffer.append( '\r' ); - out.info( buffer.toString() ); + out.print( buffer.toString() ); } private String getStatus( long complete, long total ) @@ -127,7 +127,7 @@ public class ConsoleMavenTransferListener StringBuilder buffer = new StringBuilder( 64 ); pad( buffer, lastLength ); buffer.append( '\r' ); - out.info( buffer.toString() ); + out.print( buffer.toString() ); } } diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java new file mode 100644 index 0000000000..27ff90d464 --- /dev/null +++ b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java @@ -0,0 +1,89 @@ +package org.apache.maven.cli.transfer; + +/* + * 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.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.Locale; + +import org.slf4j.Logger; +import org.sonatype.aether.transfer.AbstractTransferListener; +import org.sonatype.aether.transfer.TransferCancelledException; +import org.sonatype.aether.transfer.TransferEvent; +import org.sonatype.aether.transfer.TransferResource; + +public class Slf4jMavenTransferListener + extends AbstractTransferListener +{ + + protected Logger out; + + public Slf4jMavenTransferListener( Logger out ) + { + this.out = out; + } + + @Override + public void transferInitiated( TransferEvent event ) + { + String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading"; + + out.info( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName() ); + } + + @Override + public void transferCorrupted( TransferEvent event ) + throws TransferCancelledException + { + TransferResource resource = event.getResource(); + + out.warn( event.getException().getMessage() + " for " + resource.getRepositoryUrl() + resource.getResourceName() ); + } + + @Override + public void transferSucceeded( TransferEvent event ) + { + TransferResource resource = event.getResource(); + long contentLength = event.getTransferredBytes(); + if ( contentLength >= 0 ) + { + String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" ); + String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B"; + + String throughput = ""; + long duration = System.currentTimeMillis() - resource.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"; + } + + out.info( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len + + throughput + ")" ); + } + } + + protected long toKB( long bytes ) + { + return ( bytes + 1023 ) / 1024; + } + +}