1 package org.apache.maven.artifact.deployer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.maven.wagon.events.TransferEvent;
24 import org.apache.maven.wagon.events.TransferListener;
25
26 /**
27 * Bootstrap download progress meter.
28 *
29 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
30 * @version $Id: UploadMeter.java 532339 2007-04-25 12:28:56Z ltheussl $
31 */
32 public class UploadMeter
33 implements TransferListener
34 {
35 /** log for debug output */
36 private static final Log log = LogFactory.getLog( UploadMeter.class );
37
38 private int shownSoFar;
39
40 private final int numHashes;
41
42 private final char hashChar;
43
44 private long complete;
45
46 private long total;
47
48 private static final int KB = 1024;
49
50 public UploadMeter()
51 {
52 this( 20, '.' );
53 }
54
55 public UploadMeter( final int numHashes, final char hashChar )
56 {
57 this.numHashes = numHashes;
58 this.hashChar = hashChar;
59 }
60
61 public void transferInitiated( final TransferEvent transferEvent )
62 {
63 System.out.println( "Uploading to " + transferEvent.getResource().getName() + ": " );
64 }
65
66 public void transferStarted( final TransferEvent transferEvent )
67 {
68 this.shownSoFar = 0;
69 this.complete = 0;
70 this.total = transferEvent.getLocalFile().length();
71 }
72
73 public void transferProgress( final TransferEvent transferEvent, final byte[] buffer, final int length )
74 {
75 this.complete += length;
76
77 if ( this.total > 0 )
78 {
79 final int numToShow = (int) ( ( this.complete * this.numHashes ) / this.total );
80 for ( int i = this.shownSoFar + 1; i <= numToShow; i++ )
81 {
82 System.out.print( this.hashChar );
83 }
84 this.shownSoFar = numToShow;
85 }
86 }
87
88 public void transferCompleted( final TransferEvent transferEvent )
89 {
90 System.out.println( " (" + this.total / UploadMeter.KB + "K)" );
91 }
92
93 public void transferError( final TransferEvent transferEvent )
94 {
95 UploadMeter.log.error( transferEvent.getException().getMessage() );
96 }
97
98 public void debug( final String message )
99 {
100 UploadMeter.log.debug( message );
101 }
102 }