001 package 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
022 import java.io.PrintStream;
023 import java.text.DecimalFormat;
024 import java.text.DecimalFormatSymbols;
025 import java.util.Locale;
026
027 import org.eclipse.aether.transfer.AbstractTransferListener;
028 import org.eclipse.aether.transfer.TransferCancelledException;
029 import org.eclipse.aether.transfer.TransferEvent;
030 import org.eclipse.aether.transfer.TransferResource;
031
032 public abstract class AbstractMavenTransferListener
033 extends AbstractTransferListener
034 {
035
036 protected PrintStream out;
037
038 protected AbstractMavenTransferListener( PrintStream out )
039 {
040 this.out = out;
041 }
042
043 @Override
044 public void transferInitiated( TransferEvent event )
045 {
046 String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
047
048 out.println( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName() );
049 }
050
051 @Override
052 public void transferCorrupted( TransferEvent event )
053 throws TransferCancelledException
054 {
055 TransferResource resource = event.getResource();
056
057 out.println( "[WARNING] " + event.getException().getMessage() + " for " + resource.getRepositoryUrl()
058 + resource.getResourceName() );
059 }
060
061 @Override
062 public void transferSucceeded( TransferEvent event )
063 {
064 TransferResource resource = event.getResource();
065 long contentLength = event.getTransferredBytes();
066 if ( contentLength >= 0 )
067 {
068 String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" );
069 String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B";
070
071 String throughput = "";
072 long duration = System.currentTimeMillis() - resource.getTransferStartTime();
073 if ( duration > 0 )
074 {
075 DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) );
076 double kbPerSec = ( contentLength / 1024.0 ) / ( duration / 1000.0 );
077 throughput = " at " + format.format( kbPerSec ) + " KB/sec";
078 }
079
080 out.println( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len
081 + throughput + ")" );
082 }
083 }
084
085 protected long toKB( long bytes )
086 {
087 return ( bytes + 1023 ) / 1024;
088 }
089
090 }