1 package org.apache.maven.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Iterator;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25
26 import org.apache.maven.wagon.WagonConstants;
27 import org.apache.maven.wagon.events.TransferEvent;
28 import org.apache.maven.wagon.resource.Resource;
29 import org.codehaus.plexus.logging.Logger;
30
31
32
33
34
35
36
37 public class ConsoleDownloadMonitor
38 extends AbstractConsoleDownloadMonitor
39 {
40 private Map
41 private int maxLength;
42
43 public ConsoleDownloadMonitor( Logger logger )
44 {
45 super( logger );
46
47 downloads = new LinkedHashMap();
48 }
49
50 public ConsoleDownloadMonitor()
51 {
52 downloads = new LinkedHashMap();
53 }
54
55 public void transferInitiated( TransferEvent transferEvent )
56 {
57 String message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";
58
59 String url = transferEvent.getWagon().getRepository().getUrl();
60
61 out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
62
63 }
64
65 public void transferStarted( TransferEvent transferEvent )
66 {
67
68 }
69
70 public synchronized void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
71 {
72 Resource resource = transferEvent.getResource();
73 if ( !downloads.containsKey( resource ) )
74 {
75 downloads.put( resource, new Long( length ) );
76 }
77 else
78 {
79 Long complete = (Long) downloads.get( resource );
80 complete = new Long( complete.longValue() + length );
81 downloads.put( resource, complete );
82 }
83
84 StringBuffer buf = new StringBuffer();
85 for ( Iterator i = downloads.entrySet().iterator(); i.hasNext(); )
86 {
87 Map.Entry entry = (Map.Entry) i.next();
88 Long complete = (Long) entry.getValue();
89 String status =
90 getDownloadStatusForResource( complete.longValue(), ( (Resource) entry.getKey() ).getContentLength() );
91 buf.append( status );
92 if ( i.hasNext() )
93 {
94 buf.append( " " );
95 }
96 }
97
98 if ( buf.length() > maxLength )
99 {
100 maxLength = buf.length();
101 }
102
103 out.print( buf.toString() + "\r" );
104 }
105
106 String getDownloadStatusForResource( long progress, long total )
107 {
108 if ( total >= 1024 )
109 {
110 return ( progress / 1024 ) + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : ( total / 1024 ) + "K" );
111 }
112 else
113 {
114 return progress + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : total + "b" );
115 }
116 }
117
118 public synchronized void transferCompleted( TransferEvent transferEvent )
119 {
120 StringBuffer line = new StringBuffer( createCompletionLine( transferEvent ) );
121
122 while ( line.length() < maxLength )
123 {
124 line.append( " " );
125 }
126 maxLength = 0;
127
128 out.println( line );
129 downloads.remove( transferEvent.getResource() );
130 }
131 }