001package org.apache.maven.cli.transfer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.text.DecimalFormat;
023import java.text.DecimalFormatSymbols;
024import java.util.Locale;
025
026import org.eclipse.aether.transfer.AbstractTransferListener;
027import org.eclipse.aether.transfer.TransferCancelledException;
028import org.eclipse.aether.transfer.TransferEvent;
029import org.eclipse.aether.transfer.TransferResource;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033public class Slf4jMavenTransferListener
034    extends AbstractTransferListener
035{
036
037    protected final Logger out;
038
039    public Slf4jMavenTransferListener()
040    {
041        this.out = LoggerFactory.getLogger( Slf4jMavenTransferListener.class );
042    }
043
044    // TODO should we deprecate?
045    public Slf4jMavenTransferListener( Logger out )
046    {
047        this.out = out;
048    }
049
050    @Override
051    public void transferInitiated( TransferEvent event )
052    {
053        String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
054
055        out.info( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName() );
056    }
057
058    @Override
059    public void transferCorrupted( TransferEvent event )
060        throws TransferCancelledException
061    {
062        TransferResource resource = event.getResource();
063
064        out.warn( event.getException().getMessage() + " for " + resource.getRepositoryUrl() + resource.getResourceName() );
065    }
066
067    @Override
068    public void transferSucceeded( TransferEvent event )
069    {
070        TransferResource resource = event.getResource();
071        long contentLength = event.getTransferredBytes();
072        if ( contentLength >= 0 )
073        {
074            String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" );
075            String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B";
076
077            String throughput = "";
078            long duration = System.currentTimeMillis() - resource.getTransferStartTime();
079            if ( duration > 0 )
080            {
081                DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) );
082                double kbPerSec = ( contentLength / 1024.0 ) / ( duration / 1000.0 );
083                throughput = " at " + format.format( kbPerSec ) + " KB/sec";
084            }
085
086            out.info( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len
087                + throughput + ")" );
088        }
089    }
090
091    protected long toKB( long bytes )
092    {
093        return ( bytes + 1023 ) / 1024;
094    }
095
096}