001package org.apache.maven.resolver.examples.util; 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.io.PrintStream; 023import java.text.DecimalFormat; 024import java.text.DecimalFormatSymbols; 025import java.util.Locale; 026import java.util.Map; 027import java.util.concurrent.ConcurrentHashMap; 028 029import org.eclipse.aether.transfer.AbstractTransferListener; 030import org.eclipse.aether.transfer.MetadataNotFoundException; 031import org.eclipse.aether.transfer.TransferEvent; 032import org.eclipse.aether.transfer.TransferResource; 033 034/** 035 * A simplistic transfer listener that logs uploads/downloads to the console. 036 */ 037public class ConsoleTransferListener 038 extends AbstractTransferListener 039{ 040 041 private PrintStream out; 042 043 private Map<TransferResource, Long> downloads = new ConcurrentHashMap<TransferResource, Long>(); 044 045 private int lastLength; 046 047 public ConsoleTransferListener() 048 { 049 this( null ); 050 } 051 052 public ConsoleTransferListener( PrintStream out ) 053 { 054 this.out = ( out != null ) ? out : System.out; 055 } 056 057 @Override 058 public void transferInitiated( TransferEvent event ) 059 { 060 String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading"; 061 062 out.println( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName() ); 063 } 064 065 @Override 066 public void transferProgressed( TransferEvent event ) 067 { 068 TransferResource resource = event.getResource(); 069 downloads.put( resource, Long.valueOf( event.getTransferredBytes() ) ); 070 071 StringBuilder buffer = new StringBuilder( 64 ); 072 073 for ( Map.Entry<TransferResource, Long> entry : downloads.entrySet() ) 074 { 075 long total = entry.getKey().getContentLength(); 076 long complete = entry.getValue().longValue(); 077 078 buffer.append( getStatus( complete, total ) ).append( " " ); 079 } 080 081 int pad = lastLength - buffer.length(); 082 lastLength = buffer.length(); 083 pad( buffer, pad ); 084 buffer.append( '\r' ); 085 086 out.print( buffer ); 087 } 088 089 private String getStatus( long complete, long total ) 090 { 091 if ( total >= 1024 ) 092 { 093 return toKB( complete ) + "/" + toKB( total ) + " KB "; 094 } 095 else if ( total >= 0 ) 096 { 097 return complete + "/" + total + " B "; 098 } 099 else if ( complete >= 1024 ) 100 { 101 return toKB( complete ) + " KB "; 102 } 103 else 104 { 105 return complete + " B "; 106 } 107 } 108 109 private void pad( StringBuilder buffer, int spaces ) 110 { 111 String block = " "; 112 while ( spaces > 0 ) 113 { 114 int n = Math.min( spaces, block.length() ); 115 buffer.append( block, 0, n ); 116 spaces -= n; 117 } 118 } 119 120 @Override 121 public void transferSucceeded( TransferEvent event ) 122 { 123 transferCompleted( event ); 124 125 TransferResource resource = event.getResource(); 126 long contentLength = event.getTransferredBytes(); 127 if ( contentLength >= 0 ) 128 { 129 String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" ); 130 String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B"; 131 132 String throughput = ""; 133 long duration = System.currentTimeMillis() - resource.getTransferStartTime(); 134 if ( duration > 0 ) 135 { 136 long bytes = contentLength - resource.getResumeOffset(); 137 DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) ); 138 double kbPerSec = ( bytes / 1024.0 ) / ( duration / 1000.0 ); 139 throughput = " at " + format.format( kbPerSec ) + " KB/sec"; 140 } 141 142 out.println( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len 143 + throughput + ")" ); 144 } 145 } 146 147 @Override 148 public void transferFailed( TransferEvent event ) 149 { 150 transferCompleted( event ); 151 152 if ( !( event.getException() instanceof MetadataNotFoundException ) ) 153 { 154 event.getException().printStackTrace( out ); 155 } 156 } 157 158 private void transferCompleted( TransferEvent event ) 159 { 160 downloads.remove( event.getResource() ); 161 162 StringBuilder buffer = new StringBuilder( 64 ); 163 pad( buffer, lastLength ); 164 buffer.append( '\r' ); 165 out.print( buffer ); 166 } 167 168 public void transferCorrupted( TransferEvent event ) 169 { 170 event.getException().printStackTrace( out ); 171 } 172 173 protected long toKB( long bytes ) 174 { 175 return ( bytes + 1023 ) / 1024; 176 } 177 178}