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()
065            + resource.getResourceName() );
066    }
067
068    @Override
069    public void transferSucceeded( TransferEvent event )
070    {
071        TransferResource resource = event.getResource();
072        long contentLength = event.getTransferredBytes();
073        if ( contentLength >= 0 )
074        {
075            String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" );
076            String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B";
077
078            String throughput = "";
079            long duration = System.currentTimeMillis() - resource.getTransferStartTime();
080            if ( duration > 0 )
081            {
082                DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) );
083                double kbPerSec = ( contentLength / 1024.0 ) / ( duration / 1000.0 );
084                throughput = " at " + format.format( kbPerSec ) + " KB/sec";
085            }
086
087            out.info( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len
088                + throughput + ")" );
089        }
090    }
091
092    protected long toKB( long bytes )
093    {
094        return ( bytes + 1023 ) / 1024;
095    }
096
097}