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
This commit is contained in:
Jason van Zyl 2012-11-11 04:40:00 +00:00
parent ed3893b188
commit d698618204
5 changed files with 131 additions and 32 deletions

View File

@ -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 )
{
}

View File

@ -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 + ")" );
}
}

View File

@ -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 );
}

View File

@ -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() );
}
}

View File

@ -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;
}
}