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.io.PrintStream; 023import java.util.Map; 024import java.util.concurrent.ConcurrentHashMap; 025 026import org.eclipse.aether.transfer.TransferCancelledException; 027import org.eclipse.aether.transfer.TransferEvent; 028import org.eclipse.aether.transfer.TransferResource; 029 030/** 031 * Console download progress meter. 032 * 033 * @author <a href="mailto:brett@apache.org">Brett Porter</a> 034 */ 035public class ConsoleMavenTransferListener 036 extends AbstractMavenTransferListener 037{ 038 039 private Map<TransferResource, Long> downloads = new ConcurrentHashMap<TransferResource, Long>(); 040 041 private int lastLength; 042 043 public ConsoleMavenTransferListener( PrintStream out ) 044 { 045 super( out ); 046 } 047 048 @Override 049 public void transferProgressed( TransferEvent event ) 050 throws TransferCancelledException 051 { 052 TransferResource resource = event.getResource(); 053 downloads.put( resource, event.getTransferredBytes() ); 054 055 StringBuilder buffer = new StringBuilder( 64 ); 056 057 for ( Map.Entry<TransferResource, Long> entry : downloads.entrySet() ) 058 { 059 long total = entry.getKey().getContentLength(); 060 Long complete = entry.getValue(); 061 // NOTE: This null check guards against http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6312056 062 if ( complete != null ) 063 { 064 buffer.append( getStatus( complete.longValue(), total ) ).append( " " ); 065 } 066 } 067 068 int pad = lastLength - buffer.length(); 069 lastLength = buffer.length(); 070 pad( buffer, pad ); 071 buffer.append( '\r' ); 072 073 out.print( buffer.toString() ); 074 } 075 076 private String getStatus( long complete, long total ) 077 { 078 if ( total >= 1024 ) 079 { 080 return toKB( complete ) + "/" + toKB( total ) + " KB "; 081 } 082 else if ( total >= 0 ) 083 { 084 return complete + "/" + total + " B "; 085 } 086 else if ( complete >= 1024 ) 087 { 088 return toKB( complete ) + " KB "; 089 } 090 else 091 { 092 return complete + " B "; 093 } 094 } 095 096 private void pad( StringBuilder buffer, int spaces ) 097 { 098 String block = " "; 099 while ( spaces > 0 ) 100 { 101 int n = Math.min( spaces, block.length() ); 102 buffer.append( block, 0, n ); 103 spaces -= n; 104 } 105 } 106 107 @Override 108 public void transferSucceeded( TransferEvent event ) 109 { 110 transferCompleted( event ); 111 112 super.transferSucceeded( event ); 113 } 114 115 @Override 116 public void transferFailed( TransferEvent event ) 117 { 118 transferCompleted( event ); 119 120 super.transferFailed( event ); 121 } 122 123 private void transferCompleted( TransferEvent event ) 124 { 125 downloads.remove( event.getResource() ); 126 127 StringBuilder buffer = new StringBuilder( 64 ); 128 pad( buffer, lastLength ); 129 buffer.append( '\r' ); 130 out.print( buffer.toString() ); 131 } 132 133}